From 51685388948b86027844f4e1eb9d252ede52e220 Mon Sep 17 00:00:00 2001 From: Thomas Ingleby Date: Fri, 11 Apr 2014 09:05:04 +0100 Subject: [PATCH 1/4] gpio: modified api to include close. Add file descriptor to gpio_t Signed-off-by: Thomas Ingleby --- api/gpio.h | 4 ++++ src/gpio/gpio.c | 1 - 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/api/gpio.h b/api/gpio.h index b58735e..ab9598f 100644 --- a/api/gpio.h +++ b/api/gpio.h @@ -23,6 +23,8 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#include + #ifdef __cplusplus extern "C" { #endif @@ -32,6 +34,7 @@ typedef struct gpio_struct int pin; int pinMap; char path[64]; + FILE *value_fp; } gpio_t; typedef char gpio_mode_t[16]; @@ -42,6 +45,7 @@ int gpio_set(int pin); void gpio_mode(gpio_t *gpio, gpio_mode_t mode); void gpio_dir(gpio_t *gpio, gpio_dir_t dir); +void gpio_close(gpio_t *gpio); int gpio_read(gpio_t *gpio); void gpio_write(gpio_t *gpio, int value); diff --git a/src/gpio/gpio.c b/src/gpio/gpio.c index e2b5110..177adce 100644 --- a/src/gpio/gpio.c +++ b/src/gpio/gpio.c @@ -24,7 +24,6 @@ */ #include -#include #include "gpio.h" From aa2ed728be597f765a60533da538b8b43a8ee18c Mon Sep 17 00:00:00 2001 From: Thomas Ingleby Date: Fri, 11 Apr 2014 11:41:48 +0100 Subject: [PATCH 2/4] gpio: work done to export,read,write to gpio Signed-off-by: Thomas Ingleby --- src/gpio/gpio.c | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/gpio/gpio.c b/src/gpio/gpio.c index 177adce..bee93b4 100644 --- a/src/gpio/gpio.c +++ b/src/gpio/gpio.c @@ -24,6 +24,9 @@ */ #include +#include +#include +#include #include "gpio.h" @@ -33,6 +36,18 @@ extern "C" { void gpio_init(gpio_t *gpio, int pin) { + FILE *export_f; + + if((export_f = fopen("/sys/class/gpio/export", "w")) == NULL) { + fprintf(stderr, "Failed to open export for writing!\n"); + } else { + fprintf(export_f, "%d", pin); + fclose(export_f); + + char bu[64]; + sprintf(bu, "/sys/class/gpio/gpio%d/value", pin); + gpio->value_fp = fopen(bu, "r+b"); + } gpio->pin = pin; } @@ -49,16 +64,36 @@ gpio_mode(gpio_t *gpio, gpio_mode_t mode) { void gpio_dir(gpio_t *gpio, gpio_dir_t dir) { + char filepath[64]; + snprintf(filepath, 64, "/sys/class/gpio/gpio%d/direction", gpio->pin); + int fd; + fd = open(filepath, O_WRONLY); + if(fd == -1) { + fprintf(stderr, "Failed to open direction for writing!\n"); + } else if(write(fd, dir, 5) == -1) { + fprintf(stderr, "Failed to set direction\n"); + } else { + close(fd); + } } int gpio_read(gpio_t *gpio) { - return 0; + fseek(gpio->value_fp, SEEK_SET, 0); + char buffer[2]; + fread(buffer, 2, 1, gpio->value_fp); + return atoi(buffer); } void gpio_write(gpio_t *gpio, int value){ + fseek(gpio->value_fp, SEEK_SET, 0); + fprintf(gpio->value_fp, "%d", value); +} + +void +gpio_close(gpio_t *gpio) { } From 6d13a41c967cafe22f70a275c47cf8482304f466 Mon Sep 17 00:00:00 2001 From: Thomas Ingleby Date: Fri, 11 Apr 2014 13:33:46 +0100 Subject: [PATCH 3/4] gpio: get file handle if none exists Signed-off-by: Thomas Ingleby --- src/gpio/gpio.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/gpio/gpio.c b/src/gpio/gpio.c index bee93b4..78ba514 100644 --- a/src/gpio/gpio.c +++ b/src/gpio/gpio.c @@ -34,6 +34,9 @@ extern "C" { #endif +int +gpio_get_valfp(gpio_t *gpio); + void gpio_init(gpio_t *gpio, int pin) { FILE *export_f; @@ -43,10 +46,6 @@ gpio_init(gpio_t *gpio, int pin) { } else { fprintf(export_f, "%d", pin); fclose(export_f); - - char bu[64]; - sprintf(bu, "/sys/class/gpio/gpio%d/value", pin); - gpio->value_fp = fopen(bu, "r+b"); } gpio->pin = pin; } @@ -80,6 +79,9 @@ gpio_dir(gpio_t *gpio, gpio_dir_t dir) { int gpio_read(gpio_t *gpio) { + if(gpio->value_fp == NULL) { + gpio_get_valfp(gpio); + } fseek(gpio->value_fp, SEEK_SET, 0); char buffer[2]; fread(buffer, 2, 1, gpio->value_fp); @@ -87,7 +89,10 @@ gpio_read(gpio_t *gpio) { } void -gpio_write(gpio_t *gpio, int value){ +gpio_write(gpio_t *gpio, int value) { + if(gpio->value_fp == NULL) { + gpio_get_valfp(gpio); + } fseek(gpio->value_fp, SEEK_SET, 0); fprintf(gpio->value_fp, "%d", value); } @@ -97,6 +102,19 @@ gpio_close(gpio_t *gpio) { } +int +gpio_get_valfp(gpio_t *gpio) { + char bu[64]; + sprintf(bu, "/sys/class/gpio/gpio%d/value", gpio->pin); + + if((gpio->value_fp = fopen(bu, "r+b")) == NULL) { + return 1; + } else { + return 0; + } + return 1; +} + #ifdef __cplusplus } #endif From e64ffcf77dd2bdccc06679dedfb5e5dd5d325c41 Mon Sep 17 00:00:00 2001 From: Thomas Ingleby Date: Fri, 11 Apr 2014 15:55:01 +0100 Subject: [PATCH 4/4] gpio: gpio in/out both work * Python example included. Signed-off-by: Thomas Ingleby --- examples/python/blink-io8.py | 14 ++++++++++++++ src/gpio/gpio.c | 5 +++++ 2 files changed, 19 insertions(+) create mode 100644 examples/python/blink-io8.py diff --git a/examples/python/blink-io8.py b/examples/python/blink-io8.py new file mode 100644 index 0000000..3641283 --- /dev/null +++ b/examples/python/blink-io8.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python3 + +import pymaa as maa +import time + +x = maa.gpio_t() +maa.gpio_init(x, 26) +maa.gpio_dir(x, "out") + +while True: + maa.gpio_write(x,1) + time.sleep(0.2) + maa.gpio_write(x,0) + time.sleep(0.2) diff --git a/src/gpio/gpio.c b/src/gpio/gpio.c index 78ba514..4726ee7 100644 --- a/src/gpio/gpio.c +++ b/src/gpio/gpio.c @@ -63,6 +63,8 @@ gpio_mode(gpio_t *gpio, gpio_mode_t mode) { void gpio_dir(gpio_t *gpio, gpio_dir_t dir) { + fclose(gpio->value_fp); + gpio->value_fp = NULL; char filepath[64]; snprintf(filepath, 64, "/sys/class/gpio/gpio%d/direction", gpio->pin); int fd; @@ -85,6 +87,7 @@ gpio_read(gpio_t *gpio) { fseek(gpio->value_fp, SEEK_SET, 0); char buffer[2]; fread(buffer, 2, 1, gpio->value_fp); + fseek(gpio->value_fp, SEEK_SET, 0); return atoi(buffer); } @@ -95,6 +98,8 @@ gpio_write(gpio_t *gpio, int value) { } fseek(gpio->value_fp, SEEK_SET, 0); fprintf(gpio->value_fp, "%d", value); + fseek(gpio->value_fp, SEEK_SET, 0); + } void