gpio  1.4.2
General Purpose I/O Package
gpiobitbang

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 int strToBits (const string &str, byte_t *p)
 
static void mainInit (int argc, char *argv[])
 Main initialization. More...
 
static int sysfsBitBang ()
 Bit bang bit pattern. 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 OptsIbd = 1000
 inter-bit delay
 
static int ArgsGpio
 gpio number
 
static byte_t * ArgsPattern
 bit pattern to write
 
static size_t PatByteCount
 pattern byte count
 
static size_t PatBitCount
 pattern bit count
 
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 308 of file gpiobitbang.cxx.

References APP_EC_ARGS, ArgsGpio, ArgsPattern, mainInit(), OptsIbd, OptsMethod, PatByteCount, and sysfsBitBang().

Referenced by mainInit().

309 {
310  int pin;
311  int ec;
312 
313  mainInit(argc, argv);
314 
315 
316  printf("Bit banging pattern to GPIO %d.\n", ArgsGpio);
317  printf(" Pattern: ");
318  for(size_t i=0; i<PatByteCount; ++i)
319  {
320  printf("%02x ", ArgsPattern[i]);
321  }
322  printf("\n");
323  printf(" Ibd: %u usec\n", OptsIbd);
324 
325  if( !strcmp(OptsMethod, "sysfs") )
326  {
327  ec = sysfsBitBang();
328  }
329 #ifdef MMAP_GPIO
330  else if( !strcmp(OptsMethod, "mmap") )
331  {
332  ec = mmapBitBang();
333  }
334 #endif // MMAP_GPIO
335  else
336  {
337  fprintf(stderr,"%s: Unknown GPIO access method.", OptsMethod);
338  ec = APP_EC_ARGS;
339  }
340 
341  return ec;
342 }
static void mainInit(int argc, char *argv[])
Main initialization.
#define APP_EC_ARGS
command-line options/arguments error exit code
Definition: gpiobitbang.cxx:54
static int sysfsBitBang()
Bit bang bit pattern.
static char * OptsMethod
system file system
Definition: gpiobitbang.cxx:58
static byte_t * ArgsPattern
bit pattern to write
Definition: gpiobitbang.cxx:61
static size_t PatByteCount
pattern byte count
Definition: gpiobitbang.cxx:62
static int ArgsGpio
gpio number
Definition: gpiobitbang.cxx:60
static int OptsIbd
inter-bit delay
Definition: gpiobitbang.cxx:59
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 190 of file gpiobitbang.cxx.

References APP_EC_ARGS, ArgsGpio, ArgsPattern, Argv0, OptsInfo, PatBitCount, PatByteCount, PgmInfo, and PkgInfo.

Referenced by main().

191 {
192  int i;
193  byte_t *p;
194 
195  // name of this process
196  Argv0 = basename(argv[0]);
197 
198  // parse input options
199  argv = OptsGet(Argv0, &PkgInfo, &PgmInfo, OptsInfo, true, &argc, argv);
200 
201  if( argc == 0 )
202  {
203  fprintf(stderr, "%s: No GPIO pin number <gpio> specified.\n", Argv0);
204  fprintf(stderr, "Try '%s --help' for more information.\n", Argv0);
205  exit(APP_EC_ARGS);
206  }
207 
208  else if( strToInt(argv[0], ArgsGpio) < 0 )
209  {
210  fprintf(stderr, "%s: '%s': Bad GPIO number.\n", Argv0, argv[0]);
211  fprintf(stderr, "Try '%s --help' for more information.\n", Argv0);
212  exit(APP_EC_ARGS);
213  }
214 
215  if( argc == 1 )
216  {
217  fprintf(stderr, "%s: No bit pattern specified.\n", Argv0);
218  fprintf(stderr, "Try '%s --help' for more information.\n", Argv0);
219  exit(APP_EC_ARGS);
220  }
221 
222  PatByteCount = argc - 1;
224  ArgsPattern = (byte_t *)malloc(sizeof(byte_t) * PatByteCount);
225 
226  for(i=1, p=ArgsPattern; i<argc; ++i, ++p)
227  {
228  if( strToBits(argv[i], p) < 0 )
229  {
230  fprintf(stderr, "%s: '%s': Bad bit pattern %d.\n", Argv0, argv[i], i);
231  fprintf(stderr, "Try '%s --help' for more information.\n", Argv0);
232  exit(APP_EC_ARGS);
233  }
234  }
235 }
static OptsInfo_T OptsInfo[]
Command line options information.
Definition: gpiobitbang.cxx:95
#define APP_EC_ARGS
command-line options/arguments error exit code
Definition: gpiobitbang.cxx:54
static byte_t * ArgsPattern
bit pattern to write
Definition: gpiobitbang.cxx:61
static size_t PatByteCount
pattern byte count
Definition: gpiobitbang.cxx:62
static int ArgsGpio
gpio number
Definition: gpiobitbang.cxx:60
static size_t PatBitCount
pattern bit count
Definition: gpiobitbang.cxx:63
static const PkgInfo_T PkgInfo
Definition: version.h:45
static char * Argv0
the command
Definition: gpiobitbang.cxx:57
static OptsPgmInfo_T PgmInfo
Program information.
Definition: gpiobitbang.cxx:69
static int sysfsBitBang ( )
static

Bit bang bit pattern.

Method: sysfs

Returns
Application exit code.

Definition at line 244 of file gpiobitbang.cxx.

References APP_EC_EXEC, APP_EC_OK, ArgsGpio, ArgsPattern, gpioBitBang(), gpioClose(), gpioOpen(), OptsIbd, and PatBitCount.

Referenced by main().

245 {
246  int fd;
247  int ec;
248 
249  if( (fd = gpioOpen(ArgsGpio)) < 0 )
250  {
251  return APP_EC_EXEC;
252  }
253 
254  if( gpioBitBang(fd, ArgsPattern, PatBitCount, OptsIbd) < 0 )
255  {
256  ec = APP_EC_EXEC;
257  }
258  else
259  {
260  ec = APP_EC_OK;
261  }
262 
263  gpioClose(fd);
264 
265  return ec;
266 }
#define APP_EC_EXEC
execution exit code
Definition: gpiobitbang.cxx:55
int gpioOpen(int gpio)
Open GPIO pin.
Definition: gpio.c:492
#define APP_EC_OK
success exit code
Definition: gpiobitbang.cxx:53
int gpioBitBang(int fd, byte_t pattern[], size_t bitCount, unsigned int usecIbd)
Bit-bang bits out a GPIO pin.
Definition: gpio.c:558
static byte_t * ArgsPattern
bit pattern to write
Definition: gpiobitbang.cxx:61
int gpioClose(int fd)
Close GPIO pin.
Definition: gpio.c:510
static int ArgsGpio
gpio number
Definition: gpiobitbang.cxx:60
static size_t PatBitCount
pattern bit count
Definition: gpiobitbang.cxx:63
static int OptsIbd
inter-bit delay
Definition: gpiobitbang.cxx:59

Variable Documentation

OptsPgmInfo_T PgmInfo
static
Initial value:
=
{
"<gpio> byte [byte...]",
"Bit-bang bit pattern out GPIO pin.",
"The %P command writes the specified bit pattern to the GPIO at the given "
"exported number. "
"A bit value of 0 sets the pin low. "
"A bit value of 1 set the pin high."
"The byte pattern is specified as a series of byte arguments of any of the "
"format:\n"
" 0xH[H] hexidecimal examples: 0x5 0x3C 0xff\n"
" 0bB[B...] binary examples: 0b00000101 0b001111 0b11111111\n"
" D[D...] unsigned integer examples: 5 60 255",
NULL
}

Program information.

Definition at line 69 of file gpiobitbang.cxx.

Referenced by mainInit().