|
|
|
@ -13,38 +13,41 @@
|
|
|
|
// prototype functions
|
|
|
|
// prototype functions
|
|
|
|
int gpio_export(const char *gpio);
|
|
|
|
int gpio_export(const char *gpio);
|
|
|
|
int gpio_direction(const char *gpio, const char *direction); // this function
|
|
|
|
int gpio_direction(const char *gpio, const char *direction); // this function
|
|
|
|
int gpio_write(int gpio, int value);
|
|
|
|
int gpio_write(const char *gpio, const char *value); // write function
|
|
|
|
|
|
|
|
int gpio_read(const char *gpio); //read function
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]){
|
|
|
|
int main(int argc, char *argv[]){
|
|
|
|
|
|
|
|
|
|
|
|
int fd;
|
|
|
|
int fd;
|
|
|
|
const char *pin = argv[1]; // get gpio number from argument
|
|
|
|
const char *pin;
|
|
|
|
const char *dir = argv[2]; // get gpio direction from arguments
|
|
|
|
const char *dir;
|
|
|
|
//
|
|
|
|
const char *val = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// checking that at least gpio and direction is passed
|
|
|
|
|
|
|
|
if(argc<3){
|
|
|
|
|
|
|
|
printf("Usage:\n");
|
|
|
|
|
|
|
|
printf(" %s <gpio> in\n", argv[0]);
|
|
|
|
|
|
|
|
printf(" %s <gpio> out <value>\n", argv[0]);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pin = argv[1]; // get gpio number from argument
|
|
|
|
|
|
|
|
dir = argv[2]; // get gpio direction from arguments
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//const char *val = argv[3];
|
|
|
|
|
|
|
|
// checking the gpio direction
|
|
|
|
|
|
|
|
if(strcmp(dir, "out") == 0) {
|
|
|
|
|
|
|
|
if(argc<4){
|
|
|
|
|
|
|
|
printf("Error: GPIO out option requires a value.\n");
|
|
|
|
|
|
|
|
printf("Usage: %s <gpio> out <0|1>\n", argv[0]);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
val = argv[3];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gpio_export(pin);
|
|
|
|
gpio_export(pin);
|
|
|
|
gpio_direction(pin, dir);
|
|
|
|
gpio_direction(pin, dir);
|
|
|
|
|
|
|
|
gpio_write(pin, val);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// pin value to 1
|
|
|
|
|
|
|
|
fd = open("/sys/class/gpio/chip0/gpio17/value", O_WRONLY);
|
|
|
|
|
|
|
|
if(fd<0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
perror("Error opening the GPIO path");
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
write(fd,"1",1);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
sleep(1);
|
|
|
|
|
|
|
|
// pin valu to 0
|
|
|
|
|
|
|
|
fd = open("/sys/class/gpio/chip0/gpio17/value", O_WRONLY);
|
|
|
|
|
|
|
|
if(fd<0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
perror("Error opening the GPIO path");
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
write(fd,"0",1);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
sleep(1);
|
|
|
|
|
|
|
|
// unexport
|
|
|
|
// unexport
|
|
|
|
fd = open("/sys/class/gpio/chip0/unexport", O_WRONLY);
|
|
|
|
fd = open("/sys/class/gpio/chip0/unexport", O_WRONLY);
|
|
|
|
if(fd<0)
|
|
|
|
if(fd<0)
|
|
|
|
@ -54,15 +57,6 @@ int main(int argc, char *argv[]){
|
|
|
|
}
|
|
|
|
}
|
|
|
|
write(fd,"17",2);
|
|
|
|
write(fd,"17",2);
|
|
|
|
close(fd);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
printf("Hello from Raspberry Pi!\n");
|
|
|
|
|
|
|
|
printf("Number of arguments: %d\n", argc);
|
|
|
|
|
|
|
|
printf("Program is reciving: %s\n", argv[0]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(argc > 1 )
|
|
|
|
|
|
|
|
printf("Argument: %s\n", argv[1]);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// comple functions
|
|
|
|
// comple functions
|
|
|
|
@ -100,11 +94,22 @@ int gpio_direction(const char *gpio, const char *direction){
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int gpio_write(const char *gpio, const char *value){
|
|
|
|
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
char path[64];
|
|
|
|
|
|
|
|
snprintf(path, sizeof(path),
|
|
|
|
|
|
|
|
"/sys/class/gpio/chip0/gpio%s/value", gpio);
|
|
|
|
|
|
|
|
fd = open(path, O_WRONLY);
|
|
|
|
|
|
|
|
if(fd<0){
|
|
|
|
|
|
|
|
perror("Error opening the GPIO path");
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(write(fd,value, strlen(value)) < 0) {
|
|
|
|
|
|
|
|
perror("write");
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//write(fd, value, strlen(value));
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|