diff --git a/Makefile b/Makefile index 49770ba..3875009 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=bsb_io -PKG_VERSION:=0.0.2 +PKG_VERSION:=0.0.3 PKG_RELEASE:=1 PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME) @@ -15,7 +15,8 @@ define Package/bsb_io DEFAULT:=n TITLE:=BlackSwift GPIO python module URL:=http://black-swift.com - DEPENDS:=+python +libstdcpp +cython + DEPENDS:=+python +libstdcpp + PKG_BUILD_DEPENDS:=+cython endef define Package/bsb_io/description @@ -48,3 +49,4 @@ define Package/bsb_io/install endef $(eval $(call BuildPackage,bsb_io)) + diff --git a/README.md b/README.md index 29d7f0b..fa3af4e 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ This module uses _fast_ "direct gpio register access" method. It means that this There is only one class `Pin`. It's constructor accepts GPIO number. You can change GPIO direction (input or output) by writing `direction` property. You can read and write GPIO using `value` property. +You can modify pin state i.e. both value and direction using `state` propery. This code toggles GPIO1 every 2 seconds: ``` diff --git a/src/Makefile b/src/Makefile index 3401f45..d708c5c 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,10 +1,9 @@ bsb_io: bsb_fastio.o ex.o $(CC) $(LDFLAGS) ex.o bsb_fastio.o -o bsb_io bsb_fastio.o: bsb_fastio.c - $(CC) $(CFLAGS) -std=gnu99 -c bsb_fastio.c + $(CC) $(CFLAGS) -std=gnu99 -fPIC -c bsb_fastio.c ex.o: ex.c $(CC) $(CFLAGS) -std=gnu99 -c ex.c - .PHONY: clean clean: rm -f *.o bsb_io diff --git a/src/bsb_fastio.c b/src/bsb_fastio.c index 50f0465..51cc1ca 100644 --- a/src/bsb_fastio.c +++ b/src/bsb_fastio.c @@ -27,6 +27,10 @@ int bsb_setup() return 0; } +int bsb_getdirection(int gpio){ + return (*(gpioAddress + 0) & (1 << gpio))? 1 : 0; +} + unsigned long bsb_direction(int gpio, int direction) { unsigned long value = *(gpioAddress + 0); // obtain current settings @@ -58,5 +62,5 @@ void bsb_set(int gpio, int value) int bsb_read(int gpio) { unsigned long value = *(gpioAddress + 1); - return (value & (1 < gpio)); + return (value & (1 << gpio))? 1 : 0; } diff --git a/src/bsb_fastio.h b/src/bsb_fastio.h index 8b9717b..901b7a4 100644 --- a/src/bsb_fastio.h +++ b/src/bsb_fastio.h @@ -2,6 +2,7 @@ #define BSB_IOH int bsb_setup(); unsigned long bsb_direction(int gpio, int direction); +int bsb_getdirection(int gpio); void bsb_set(int gpio, int value); int bsb_read(int gpio); #endif diff --git a/src/bsb_io.pyx b/src/bsb_io.pyx index d718ee5..51f8fb4 100644 --- a/src/bsb_io.pyx +++ b/src/bsb_io.pyx @@ -1,6 +1,7 @@ cdef extern from "bsb_fastio.h": int bsb_setup() unsigned long bsb_direction(int gpio, int direction) + int bsb_getdirection(int gpio) void bsb_set(int gpio, int value) int bsb_read(int gpio) @@ -19,7 +20,17 @@ cdef class Pin: raise RuntimeError("Something went wrong with GPIO setup:", res) self.gpio = gpio + + property state: + def __set__(self, values): + self.direction = values[1] + self.value = values[0] + def __get__(self): + return self.value, self.direction + + property direction: + def __get__(self): return bsb_getdirection(self.gpio) def __set__(self, int direction): if direction in [0,1]: bsb_direction(self.gpio, direction) @@ -27,6 +38,7 @@ cdef class Pin: raise RuntimeError( "Direction can only be 1 or 0, specified:"+str(direction)) + property value: def __get__(self): return bsb_read(self.gpio) def __set__(self, val):