Skip to content

Commit

Permalink
Add constants, trow additional exceptions, fix typos
Browse files Browse the repository at this point in the history
  • Loading branch information
asmfreak committed May 13, 2015
1 parent 262abb4 commit ce82d97
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,38 @@ Simple python module to interact with BlackSwift GPIO. Based on articles by Blac
This module uses _fast_ "direct gpio register access" method. It means that this library is working with AR9331's registers directly. Use it with care.

##Installing
1. Download .ipk from releases tab or build it for yourself (see section below)
1. Download .ipk from releases tab or build it yourself (see section below)
2. Copy it to your BlackSwift
3. Run `opkg install bsb_io`

##Building.
1. Download and install OpenWrt SDK from the second article
2. Clone this repo to `packages` directory of OpenWrt SDK.
3. In `make menuconfig` section `Languages->` in subsetion `Python->` select `bsb_io`
4. Run `make` if you want to build whole distribution or `make package/bsb_io/install` to build only bsb_io
3. In `make menuconfig` section `Languages->` in subsection `Python->` select `bsb_io`
4. Run `make` if you want to build whole distribution or `make package/bsb_io/install` to build only bsb_io with dependencies.
5. Locate .ipk in `bin/ar71xx/packages/base/`

**N.B.** This module can be built with Cython 0.11 (Used by current OpenWrt BarrierBreaker Buildroot). If version of Cython in your Buildroot is higher, you would probably need to change `setup.py`.

##Usage
There is only one class `Pin`. It's constructor accepts GPIO number.
You can change GPIO direction (input or output) by calling `.setDirection(dir)` methon. `dir` is one of 1, 0.
You can change GPIO direction (input or output) by writing `direction` property.
You can read and write GPIO using `value` property.

This code toggles GPIO1 every 2 seconds:
```
import time
from bsb_io import Pin
from bsb_io import *
p1 = Pin(1)
p1.direction = OUTPUT
v = 1
while True:
if v==1:
v = 0
v = OFF
print "[OFF]\r"
else:
v = 1
v = ON
print "[ON ]\r"
p1.value = v
time.sleep(2)
Expand Down
27 changes: 19 additions & 8 deletions src/bsb_io.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,33 @@ cdef extern from "bsb_fastio.h":
void bsb_set(int gpio, int value)
int bsb_read(int gpio)

INPUT = 0
OUTPUT = 1
HIGH = 1
LOW = 0
ON = 1
OFF = 0

cdef class Pin:
cdef int gpio
def __cinit__(self, int gpio):
int res = bsb_setup()
cdef int res = bsb_setup()
if res!=0:
raise RuntimeError("Something went wrong with GPIO setup:", res)
self.gpio = gpio

def setDirection(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 direction:
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): bsb_set(self.gpio, val)
def __set__(self, val):
if val in [0,1]:
bsb_set(self.gpio, val)
else:
raise RuntimeError("Can't assign value "+str(val))

0 comments on commit ce82d97

Please sign in to comment.