From a1ae5deafe3d69310dcc5bad39acd89edbb28156 Mon Sep 17 00:00:00 2001 From: Thomas Klaehn Date: Thu, 25 Jul 2019 14:04:28 +0200 Subject: [PATCH] Add to api --- inc/gpio_sys.h | 5 +++-- src/gpio.c | 30 +++++++++++++++++++++--------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/inc/gpio_sys.h b/inc/gpio_sys.h index c19817d..6bebd45 100644 --- a/inc/gpio_sys.h +++ b/inc/gpio_sys.h @@ -12,12 +12,13 @@ enum pin_state { }; struct gpio_sys { - int pin; - int direction; + enum pin_state pin; + enum direction direction; }; int gpio_open(const struct gpio_sys *gpio); int gpio_close(const struct gpio_sys *gpio); +int gpio_direction(struct gpio_sys *gpio, enum direction direction); int gpio_read(const struct gpio_sys *gpio); int gpio_write(const struct gpio_sys *gpio, int value); diff --git a/src/gpio.c b/src/gpio.c index d1fab61..4795c5e 100644 --- a/src/gpio.c +++ b/src/gpio.c @@ -20,7 +20,7 @@ static int gpio_export(int pin); static int gpio_unexport(int pin); -static int gpio_direction(int pin, int dir); +static int gpio_set_direction(int pin, int dir); int gpio_open(const struct gpio_sys *gpio) { @@ -35,13 +35,7 @@ int gpio_open(const struct gpio_sys *gpio) return res; } - usleep(100000); - - res = gpio_direction(gpio->pin, gpio->direction); - if(res < 0) { - syslog(LOG_ERR, "Unable to set direction for pin %d.\n", gpio->pin); - return res; - } + usleep(100000); /* Wait until sysfs is exported. */ syslog(LOG_DEBUG, "Gpio pin %d successfully opened.\n", gpio->pin); return 0; @@ -64,6 +58,24 @@ int gpio_close(const struct gpio_sys *gpio) return 0; } +int gpio_direction(struct gpio_sys *gpio, enum direction direction) +{ + int res; + + assert(NULL != gpio); + syslog(LOG_DEBUG, "Start setting gpio direction for pin %d\n.", gpio->pin); + + res = gpio_set_direction(gpio->pin, direction); + if(res < 0) { + syslog(LOG_ERR, "Unable to set direction for pin %d.\n", gpio->pin); + return res; + } + gpio->direction = direction; + + syslog(LOG_DEBUG, "Successfully set gpio direction for pin %d\n.", gpio->pin); + return 0; +} + int gpio_read(const struct gpio_sys *gpio) { char path[VALUE_MAX]; @@ -149,7 +161,7 @@ static int gpio_unexport(int pin) return 0; } -static int gpio_direction(int pin, int dir) +static int gpio_set_direction(int pin, int dir) { static const char directions_str[] = "in\0out"; char path[DIRECTION_MAX];