Reputation: 848
Following code basiaclly lets access to GPIO port with LED.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
int main(int argc, char *argv[])
{
extern char *optarg;
char *cptr;
int gpio_value = 0;
int nchannel = 0;
int c;
int i;
opterr = 0;
while ((c = getopt(argc, argv, "g:io:ck")) != -1) {
switch (c) {
case 'g':
gl_gpio_base = (int)strtoul(optarg, &cptr, 0);
if (cptr == optarg)
usage(argv[0]);
break;
case 'i':
gpio_opt = IN;
break;
case 'o':
gpio_opt = OUT;
gpio_value = (int)strtoul(optarg, &cptr, 0);
if (cptr == optarg)
usage(argv[0]);
break;
case 'c':
gpio_opt = CYLON;
break;
case 'k':
gpio_opt = KIT;
break;
case '?':
usage(argv[0]);
default:
usage(argv[0]);
}
}
if (gl_gpio_base == 0) {
usage(argv[0]);
}
nchannel = open_gpio_channel(gl_gpio_base);
signal(SIGTERM, signal_handler); /* catch kill signal */
signal(SIGHUP, signal_handler); /* catch hang up signal */
signal(SIGQUIT, signal_handler); /* catch quit signal */
signal(SIGINT, signal_handler); /* catch a CTRL-c signal */
switch (gpio_opt) {
case IN:
set_gpio_direction(gl_gpio_base, nchannel, "in");
gpio_value=get_gpio_value(gl_gpio_base, nchannel);
fprintf(stdout,"0x%08X\n", gpio_value);
break;
case OUT:
set_gpio_direction(gl_gpio_base, nchannel, "out");
set_gpio_value(gl_gpio_base, nchannel, gpio_value);
break;
case CYLON:
#define CYLON_DELAY_USECS (10000)
set_gpio_direction(gl_gpio_base, nchannel, "out");
for (;;) {
for(i=0; i < ARRAY_SIZE(cylon); i++) {
gpio_value=(int)cylon[i];
set_gpio_value(gl_gpio_base, nchannel, gpio_value);
}
usleep(CYLON_DELAY_USECS);
}
case KIT:
#define KIT_DELAY_USECS (10000)
set_gpio_direction(gl_gpio_base, nchannel, "out");
for (;;) {
for (i=0; i<ARRAY_SIZE(kit); i++) {
gpio_value=(int)kit[i];
set_gpio_value(gl_gpio_base, nchannel, gpio_value);
}
usleep(KIT_DELAY_USECS);
}
default:
break;
}
close_gpio_channel(gl_gpio_base);
return 0;
}
Genarlly I would give commands like this;
gpio-demo -g 255 -o 0
255 above tells me chip number, 000 tells me what data will go to GPIO port.
This would turn off all the LEDs.
However for some debugging purpose I have to modify this code such that it behaves a litlle differently:
When running teh program, tt should simply switch off the LED i.e run the following command.
gpio-demo -g 255 -o 0
This is the only command that will work- hard coded. An ON LED will be now OFF.
What I tried is I inserted the command as the first line in main:
int main(int argc, char *argv[])
{
argv = "gpio-demo -g 255 -o 0" //irrespective of what user what user type, run this command. gpio-demo is the binary of this program
.
.
.
.
}
is this the correct way to hard code command line arguments ?
Upvotes: 1
Views: 2415
Reputation: 23031
If you call you program like this gpio-demo -g 255 -o 0
, than argc
would be equal to 5
and argv
will be a list of five pointers to sequences of characters where the first is gpio-demo
, the second -g
, the third 255
, ...
So if you really want to hardcode, you would have to write:
argc = 5;
argv = (char**)malloc(sizeof(char*)*argc);
argv[0] = "gpio-demo";
argv[1] = "-g";
argv[2] = "255";
argv[3] = "-o";
argv[4] = "0";
However, I would not recommend this! Rather use a bash script with the desired command line.
Upvotes: 2