Add to api

This commit is contained in:
Thomas Klaehn 2019-07-25 14:04:28 +02:00
parent 72d25c5f59
commit a1ae5deafe
2 changed files with 24 additions and 11 deletions

View File

@ -12,12 +12,13 @@ enum pin_state {
}; };
struct gpio_sys { struct gpio_sys {
int pin; enum pin_state pin;
int direction; enum direction direction;
}; };
int gpio_open(const struct gpio_sys *gpio); int gpio_open(const struct gpio_sys *gpio);
int gpio_close(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_read(const struct gpio_sys *gpio);
int gpio_write(const struct gpio_sys *gpio, int value); int gpio_write(const struct gpio_sys *gpio, int value);

View File

@ -20,7 +20,7 @@
static int gpio_export(int pin); static int gpio_export(int pin);
static int gpio_unexport(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) int gpio_open(const struct gpio_sys *gpio)
{ {
@ -35,13 +35,7 @@ int gpio_open(const struct gpio_sys *gpio)
return res; return res;
} }
usleep(100000); usleep(100000); /* Wait until sysfs is exported. */
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;
}
syslog(LOG_DEBUG, "Gpio pin %d successfully opened.\n", gpio->pin); syslog(LOG_DEBUG, "Gpio pin %d successfully opened.\n", gpio->pin);
return 0; return 0;
@ -64,6 +58,24 @@ int gpio_close(const struct gpio_sys *gpio)
return 0; 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) int gpio_read(const struct gpio_sys *gpio)
{ {
char path[VALUE_MAX]; char path[VALUE_MAX];
@ -149,7 +161,7 @@ static int gpio_unexport(int pin)
return 0; 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"; static const char directions_str[] = "in\0out";
char path[DIRECTION_MAX]; char path[DIRECTION_MAX];