From 9940e1c35bb2b41bcfb9f4b78b31e641038c0444 Mon Sep 17 00:00:00 2001 From: Gerardo Marx Date: Wed, 30 Sep 2026 10:07:41 -0600 Subject: [PATCH] gpio_direction function ok --- main.c | 51 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/main.c b/main.c index 7da73fb..cc478a4 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,10 @@ // Gerardo Marx 24/09/2026 // gpio command tool -// +// eg. ./gpio 17 out 1 +// ./gpio 17 in +// ./gpio 17 blink 500(ms) + + #include #include #include @@ -8,24 +12,18 @@ // prototype functions 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 main(int argc, char *argv[]){ 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_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 fd = open("/sys/class/gpio/chip0/gpio17/value", O_WRONLY); @@ -81,3 +79,32 @@ int gpio_export(const char *gpio){ close(fd); 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; +} + + + + + + + + + +