gpio: gpio in/out both work
authorThomas Ingleby <thomas.c.ingleby@intel.com>
Fri, 11 Apr 2014 14:55:01 +0000 (15:55 +0100)
committerThomas Ingleby <thomas.c.ingleby@intel.com>
Fri, 11 Apr 2014 14:55:01 +0000 (15:55 +0100)
 * Python example included.

Signed-off-by: Thomas Ingleby <thomas.c.ingleby@intel.com>
examples/python/blink-io8.py [new file with mode: 0644]
src/gpio/gpio.c

diff --git a/examples/python/blink-io8.py b/examples/python/blink-io8.py
new file mode 100644 (file)
index 0000000..3641283
--- /dev/null
@@ -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)
index 78ba514..4726ee7 100644 (file)
@@ -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