From ea212325cc4962742ec8d9ddff39ddbf815fd85e Mon Sep 17 00:00:00 2001 From: Gerardo Marx Date: Wed, 30 Sep 2026 19:36:31 -0600 Subject: [PATCH] gpio_unexport function --- main.c | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/main.c b/main.c index aebda6f..f525e3c 100644 --- a/main.c +++ b/main.c @@ -15,6 +15,7 @@ int gpio_export(const char *gpio); int gpio_direction(const char *gpio, const char *direction); // this function int gpio_write(const char *gpio, const char *value); // write function int gpio_read(const char *gpio); //read function +int gpio_unexport(const char *gpio); int main(int argc, char *argv[]){ int fd; @@ -47,16 +48,8 @@ int main(int argc, char *argv[]){ gpio_export(pin); gpio_direction(pin, dir); gpio_write(pin, val); + gpio_unexport(pin); - // unexport - fd = open("/sys/class/gpio/chip0/unexport", O_WRONLY); - if(fd<0) - { - perror("Error opening the GPIO path"); - return -1; - } - write(fd,"17",2); - close(fd); } // comple functions @@ -66,7 +59,7 @@ int gpio_export(const char *gpio){ int fd; fd = open("/sys/class/gpio/chip0/export", O_WRONLY); if(fd<0){ - perror("Error opening the GPIO path"); + perror("Error opening the GPIO path during export"); return -1; } write(fd,gpio,strlen(gpio)); @@ -101,7 +94,7 @@ int gpio_write(const char *gpio, const char *value){ "/sys/class/gpio/chip0/gpio%s/value", gpio); fd = open(path, O_WRONLY); if(fd<0){ - perror("Error opening the GPIO path"); + perror("Error opening the GPIO path during write"); return -1; } if(write(fd,value, strlen(value)) < 0) { @@ -113,3 +106,27 @@ int gpio_write(const char *gpio, const char *value){ close(fd); return 0; } + +// gpio read +int gpio_read(const char *gpio); //read function + + + + +// unexport +int gpio_unexport(const char *gpio){ + int fd; + fd = open("/sys/class/gpio/chip0/unexport", O_WRONLY); + if(fd<0) + { + perror("Error opening the GPIO path during unexport"); + return -1; + } + if(write(fd,gpio, strlen(gpio)) < 0) { + perror("unexport"); + close(fd); + return -1; + } + close(fd); + return 0; +}