29 #include <sys/types.h> 38 #include "rnr/rnrconfig.h" 76 "Read GPIO pin value.",
79 "The %P command reads the current GPIO pin value (0 or 1) for the specified " 80 "GPIO exported number.",
102 "Number of times to read the GPIO pin. A zero value means forever." 116 "Wait interval seconds between reads specified as a floating-point number." 131 "GPIO access method. One of: sysfs mmap." 146 "Print read value(s) in verbose mode." 152 static int strToInt(
const string &str,
int &val)
156 if( sscanf(str.c_str(),
"%lli", &val1) != 1 )
166 static void printValue(
int val)
168 struct timespec tsNow;
172 clock_gettime(CLOCK_REALTIME, &tsNow);
173 printf(
"[%ld.%09ld] gpio %d = ", tsNow.tv_sec, tsNow.tv_nsec,
ArgsGpio);
191 Argv0 = basename(argv[0]);
208 fprintf(stderr,
"%s: No GPIO pin number <gpio> specified.\n",
Argv0);
209 fprintf(stderr,
"Try '%s --help' for more information.\n",
Argv0);
213 else if( strToInt(argv[0],
ArgsGpio) < 0 )
215 fprintf(stderr,
"%s: '%s': Bad GPIO number.\n",
Argv0, argv[0]);
216 fprintf(stderr,
"Try '%s --help' for more information.\n",
Argv0);
245 while( bForever || (nReads <
OptsCount) )
247 if( (nReads > 0) && (usec > 0) )
276 static int mmapRead()
283 if( mmapGpioMap() < 0 )
292 while( bForever || (nReads <
OptsCount) )
294 if( (nReads > 0) && (usec > 0) )
299 if( (val = mmapGpioRead(
ArgsGpio)) < 0 )
324 int main(
int argc,
char* argv[])
344 fprintf(stderr,
"%s: Unknown GPIO access method.",
OptsMethod);
#define APP_EC_OK
success exit code
int gpioOpen(int gpio)
Open GPIO pin.
static bool_t OptsVerbose
permissions
static char * OptsMethod
system file system
GPIO interface declarations and defines.
int gpioQuickRead(int fd)
Quick read GPIO pin's current value.
static void mainInit(int argc, char *argv[])
Main initialization.
int gpioClose(int fd)
Close GPIO pin.
static const PkgInfo_T PkgInfo
#define APP_EC_ARGS
command-line options/arguments error exit code
#define APP_EC_EXEC
execution exit code
Package version information.
static OptsPgmInfo_T PgmInfo
Program information.
static int ArgsGpio
gpio number
static int sysfsRead()
Read GPIO value(s).
static OptsInfo_T OptsInfo[]
Command line options information.
static double OptsInterval
read interval in seconds
static int OptsCount
number of reads
static char * Argv0
the command
int main(int argc, char *argv[])
Main.