gpio  1.4.2
General Purpose I/O Package
gpioread

Macros

#define APP_EC_OK   0
 success exit code
 
#define APP_EC_ARGS   2
 command-line options/arguments error exit code
 
#define APP_EC_EXEC   4
 execution exit code
 

Functions

static int strToInt (const string &str, int &val)
 
static void printValue (int val)
 
static void mainInit (int argc, char *argv[])
 Main initialization. More...
 
static int sysfsRead ()
 Read GPIO value(s). More...
 
int main (int argc, char *argv[])
 Main. More...
 

Variables

static char * Argv0
 the command
 
static char * OptsMethod = (char *)"sysfs"
 system file system
 
static int OptsCount = 1
 number of reads
 
static double OptsInterval = 1.0
 read interval in seconds
 
static bool_t OptsVerbose = false
 permissions
 
static int ArgsGpio
 gpio number
 
static OptsPgmInfo_T PgmInfo
 Program information. More...
 
static OptsInfo_T OptsInfo []
 Command line options information.
 

Detailed Description

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Main.

Parameters
argcCommand-line argument count.
argvCommand-line argument list.
Returns
Returns 0 on succes, non-zero on failure.

Definition at line 324 of file gpioread.cxx.

References APP_EC_ARGS, mainInit(), OptsMethod, and sysfsRead().

325 {
326  int ec;
327 
328  mainInit(argc, argv);
329 
330  if( !strcmp(OptsMethod, "sysfs") )
331  {
332  ec = sysfsRead();
333  }
334 
335 #ifdef MMAP_GPIO
336  else if( !strcmp(OptsMethod, "mmap") )
337  {
338  ec = mmapRead();
339  }
340 #endif // MMAP_GPIO
341 
342  else
343  {
344  fprintf(stderr,"%s: Unknown GPIO access method.", OptsMethod);
345  ec = APP_EC_ARGS;
346  }
347 
348  return ec;
349 }
static char * OptsMethod
system file system
Definition: gpioread.cxx:60
static void mainInit(int argc, char *argv[])
Main initialization.
Definition: gpioread.cxx:188
#define APP_EC_ARGS
command-line options/arguments error exit code
Definition: gpioread.cxx:56
static int sysfsRead()
Read GPIO value(s).
Definition: gpioread.cxx:228
static void mainInit ( int  argc,
char *  argv[] 
)
static

Main initialization.

Parameters
argcCommand-line argument count.
argvCommand-line argument list.
Exits:
Program terminates on conversion error.

Definition at line 188 of file gpioread.cxx.

References APP_EC_ARGS, ArgsGpio, Argv0, OptsCount, OptsInfo, OptsInterval, PgmInfo, and PkgInfo.

Referenced by main().

189 {
190  // name of this process
191  Argv0 = basename(argv[0]);
192 
193  // parse input options
194  argv = OptsGet(Argv0, &PkgInfo, &PgmInfo, OptsInfo, true, &argc, argv);
195 
196  if( OptsCount < 0 )
197  {
198  OptsCount = -OptsCount;
199  }
200 
201  if( OptsInterval < 0.0 )
202  {
204  }
205 
206  if( argc == 0 )
207  {
208  fprintf(stderr, "%s: No GPIO pin number <gpio> specified.\n", Argv0);
209  fprintf(stderr, "Try '%s --help' for more information.\n", Argv0);
210  exit(APP_EC_ARGS);
211  }
212 
213  else if( strToInt(argv[0], ArgsGpio) < 0 )
214  {
215  fprintf(stderr, "%s: '%s': Bad GPIO number.\n", Argv0, argv[0]);
216  fprintf(stderr, "Try '%s --help' for more information.\n", Argv0);
217  exit(APP_EC_ARGS);
218  }
219 }
static const PkgInfo_T PkgInfo
Definition: version.h:45
#define APP_EC_ARGS
command-line options/arguments error exit code
Definition: gpioread.cxx:56
static OptsPgmInfo_T PgmInfo
Program information.
Definition: gpioread.cxx:70
static int ArgsGpio
gpio number
Definition: gpioread.cxx:65
static OptsInfo_T OptsInfo[]
Command line options information.
Definition: gpioread.cxx:89
static double OptsInterval
read interval in seconds
Definition: gpioread.cxx:62
static int OptsCount
number of reads
Definition: gpioread.cxx:61
static char * Argv0
the command
Definition: gpioread.cxx:59
static int sysfsRead ( )
static

Read GPIO value(s).

Method: sysfs

Returns
Application exit code.

Definition at line 228 of file gpioread.cxx.

References APP_EC_EXEC, APP_EC_OK, ArgsGpio, gpioClose(), gpioOpen(), gpioQuickRead(), OptsCount, and OptsInterval.

Referenced by main().

229 {
230  int fd;
231  bool bForever;
232  int nReads;
233  int val;
234  uint_t usec;
235 
236  if( (fd = gpioOpen(ArgsGpio)) < 0 )
237  {
238  return APP_EC_EXEC;
239  }
240 
241  bForever = OptsCount == 0? true: false;
242  nReads = 0;
243  usec = (uint_t)(OptsInterval * 1000000);
244 
245  while( bForever || (nReads < OptsCount) )
246  {
247  if( (nReads > 0) && (usec > 0) )
248  {
249  usleep(usec);
250  }
251 
252  if( (val = gpioQuickRead(fd)) < 0 )
253  {
254  gpioClose(fd);
255  return APP_EC_EXEC;
256  }
257 
258  printValue(val);
259 
260  ++nReads;
261  }
262 
263  gpioClose(fd);
264 
265  return APP_EC_OK;
266 }
#define APP_EC_OK
success exit code
Definition: gpioread.cxx:55
int gpioOpen(int gpio)
Open GPIO pin.
Definition: gpio.c:492
int gpioQuickRead(int fd)
Quick read GPIO pin&#39;s current value.
Definition: gpio.c:520
int gpioClose(int fd)
Close GPIO pin.
Definition: gpio.c:510
#define APP_EC_EXEC
execution exit code
Definition: gpioread.cxx:57
static int ArgsGpio
gpio number
Definition: gpioread.cxx:65
static double OptsInterval
read interval in seconds
Definition: gpioread.cxx:62
static int OptsCount
number of reads
Definition: gpioread.cxx:61

Variable Documentation

OptsPgmInfo_T PgmInfo
static
Initial value:
=
{
"<gpio>",
"Read GPIO pin value.",
"The %P command reads the current GPIO pin value (0 or 1) for the specified "
"GPIO exported number.",
NULL
}

Program information.

Definition at line 70 of file gpioread.cxx.

Referenced by mainInit().