gpio_direction function ok

arguments
Gerardo Marx 2 weeks ago
parent 45749112f7
commit 9940e1c35b

@ -1,6 +1,10 @@
// Gerardo Marx 24/09/2026 // Gerardo Marx 24/09/2026
// gpio command tool // gpio command tool
// // eg. ./gpio 17 out 1
// ./gpio 17 in
// ./gpio 17 blink 500(ms)
#include <stdio.h> #include <stdio.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
@ -8,24 +12,18 @@
// prototype functions // prototype functions
int gpio_export(const char *gpio); int gpio_export(const char *gpio);
int gpio_direction(int 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(int gpio, int value);
int main(int argc, char *argv[]){ int main(int argc, char *argv[]){
int fd; int fd;
char pin[] = "17"; // get gpio number from argument const char *pin = argv[1]; // get gpio number from argument
const char *dir = argv[2]; // get gpio direction from arguments
//
gpio_export(pin); gpio_export(pin);
gpio_direction(pin, dir);
// pin direction - convert into functions
fd = open("/sys/class/gpio/chip0/gpio17/direction", O_WRONLY);
if(fd<0)
{
perror("Error opening the GPIO path");
return -1;
}
write(fd,"out",3);
close(fd);
// pin value to 1 // pin value to 1
fd = open("/sys/class/gpio/chip0/gpio17/value", O_WRONLY); fd = open("/sys/class/gpio/chip0/gpio17/value", O_WRONLY);
@ -81,3 +79,32 @@ int gpio_export(const char *gpio){
close(fd); close(fd);
return 0; return 0;
} }
// gpio direction
int gpio_direction(const char *gpio, const char *direction){
// pin direction - convert into functions
int fd;
char path[64];
snprintf(path, sizeof(path),
"/sys/class/gpio/chip0/gpio%s/direction", gpio);
fd = open(path, O_WRONLY); //gpio number
if(fd<0)
{
perror("Error opening the GPIO path");
return -1;
}
write(fd,direction,strlen(direction));
close(fd);
return 0;
}

Loading…
Cancel
Save