Skip to content

Commit

Permalink
Added refined API and fixed typos
Browse files Browse the repository at this point in the history
  • Loading branch information
asmfreak committed Aug 3, 2015
1 parent b1cd0c4 commit a75cb5f
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 5 deletions.
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -48,3 +49,4 @@ define Package/bsb_io/install
endef

$(eval $(call BuildPackage,bsb_io))

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
```
Expand Down
3 changes: 1 addition & 2 deletions src/Makefile
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion src/bsb_fastio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions src/bsb_fastio.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions src/bsb_io.pyx
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -19,14 +20,25 @@ 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)
else:
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):
Expand Down

0 comments on commit a75cb5f

Please sign in to comment.