gpio  1.4.2
General Purpose I/O Package
gpiowrite

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 mainInit (int argc, char *argv[])
 Main initialization. More...
 
static int sysfsWrite ()
 Write value to GPIO. 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 ArgsGpio
 gpio number
 
static int ArgsValue
 value to write
 
static OptsPgmInfo_T PgmInfo
 Program information. More...
 
static OptsInfo_T OptsInfo []
 Command line options information. More...
 

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 231 of file gpiowrite.cxx.

References APP_EC_ARGS, APP_EC_OK, mainInit(), OptsMethod, and sysfsWrite().

232 {
233  int pin;
234  int ec = APP_EC_OK;
235 
236  mainInit(argc, argv);
237 
238  if( !strcmp(OptsMethod, "sysfs") )
239  {
240  ec = sysfsWrite();
241  }
242 
243 #ifdef MMAP_GPIO
244  else if( !strcmp(OptsMethod, "mmap") )
245  {
246  ec = mmapWrite();
247  }
248 #endif // MMAP_GPIO
249 
250  else
251  {
252  fprintf(stderr,"%s: Unknown GPIO access method.", OptsMethod);
253  ec = APP_EC_ARGS;
254  }
255 
256  return ec;
257 }
#define APP_EC_OK
success exit code
Definition: gpiowrite.cxx:53
static char * OptsMethod
system file system
Definition: gpiowrite.cxx:58
static int sysfsWrite()
Write value to GPIO.
Definition: gpiowrite.cxx:175
#define APP_EC_ARGS
command-line options/arguments error exit code
Definition: gpiowrite.cxx:54
static void mainInit(int argc, char *argv[])
Main initialization.
Definition: gpiowrite.cxx:130
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 130 of file gpiowrite.cxx.

References APP_EC_ARGS, ArgsGpio, ArgsValue, Argv0, OptsInfo, PgmInfo, and PkgInfo.

Referenced by main().

131 {
132  // name of this process
133  Argv0 = basename(argv[0]);
134 
135  // parse input options
136  argv = OptsGet(Argv0, &PkgInfo, &PgmInfo, OptsInfo, true, &argc, argv);
137 
138  if( argc == 0 )
139  {
140  fprintf(stderr, "%s: No GPIO pin number <gpio> specified.\n", Argv0);
141  fprintf(stderr, "Try '%s --help' for more information.\n", Argv0);
142  exit(APP_EC_ARGS);
143  }
144 
145  else if( strToInt(argv[0], ArgsGpio) < 0 )
146  {
147  fprintf(stderr, "%s: '%s': Bad GPIO number.\n", Argv0, argv[0]);
148  fprintf(stderr, "Try '%s --help' for more information.\n", Argv0);
149  exit(APP_EC_ARGS);
150  }
151 
152  if( argc == 1 )
153  {
154  fprintf(stderr, "%s: No value specified.\n", Argv0);
155  fprintf(stderr, "Try '%s --help' for more information.\n", Argv0);
156  exit(APP_EC_ARGS);
157  }
158 
159  else if( (strToInt(argv[1], ArgsValue) < 0) ||
160  ((ArgsValue != 0) && (ArgsValue != 1)) )
161  {
162  fprintf(stderr, "%s: '%s': Bad value.\n", Argv0, argv[1]);
163  fprintf(stderr, "Try '%s --help' for more information.\n", Argv0);
164  exit(APP_EC_ARGS);
165  }
166 }
static int ArgsGpio
gpio number
Definition: gpiowrite.cxx:59
static OptsPgmInfo_T PgmInfo
Program information.
Definition: gpiowrite.cxx:65
static char * Argv0
the command
Definition: gpiowrite.cxx:57
static int ArgsValue
value to write
Definition: gpiowrite.cxx:60
static const PkgInfo_T PkgInfo
Definition: version.h:45
#define APP_EC_ARGS
command-line options/arguments error exit code
Definition: gpiowrite.cxx:54
static OptsInfo_T OptsInfo[]
Command line options information.
Definition: gpiowrite.cxx:86
static int sysfsWrite ( )
static

Write value to GPIO.

Method: sysfs

Returns
Application exit code.

Definition at line 175 of file gpiowrite.cxx.

References APP_EC_EXEC, APP_EC_OK, ArgsGpio, ArgsValue, and gpioWrite().

Referenced by main().

176 {
177  int ec;
178 
179  if( gpioWrite(ArgsGpio, ArgsValue) < 0 )
180  {
181  ec = APP_EC_EXEC;
182  }
183  else
184  {
185  ec = APP_EC_OK;
186  }
187 
188  return ec;
189 }
#define APP_EC_OK
success exit code
Definition: gpiowrite.cxx:53
static int ArgsGpio
gpio number
Definition: gpiowrite.cxx:59
int gpioWrite(int gpio, int value)
Write GPIO value.
Definition: gpio.c:414
static int ArgsValue
value to write
Definition: gpiowrite.cxx:60
#define APP_EC_EXEC
execution exit code
Definition: gpiowrite.cxx:55

Variable Documentation

OptsInfo_T OptsInfo[]
static
Initial value:
=
{
{NULL, }
}

Command line options information.

Definition at line 86 of file gpiowrite.cxx.

Referenced by mainInit().

OptsPgmInfo_T PgmInfo
static
Initial value:
=
{
"<gpio> {0 | 1}",
"Write a value to a GPIO pin.",
"The %P command writes the specified value to the GPIO at the given "
"<gpio> exported number."
"A value of 0 sets the pin low. "
"A value of 1 set the pin high.",
NULL
}

Program information.

Definition at line 65 of file gpiowrite.cxx.

Referenced by mainInit().