Skip to content

Commit

Permalink
gpio: updated documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Garrett Berg committed Mar 10, 2015
1 parent 40a739f commit 552b66d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
13 changes: 4 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@

This library provides gpio access via the standard linux [sysfs interface](https://www.kernel.org/doc/Documentation/gpio/sysfs.txt)

It is intended to mimick [RPIO](http://pythonhosted.org/RPIO/) as much as possible for all features,
while also supporting additional (and better named) functionality to the same methods.
It is intended to mimick [RPIO](http://pythonhosted.org/RPIO/) as much as possible
for all features, while also supporting additional (and better named) functionality
to the same methods.

## Supported Features

**sysfs** is still under development, and so only supports minimal features

## Supported Features
- get pin values with `read(pin)` or `input(pin)`
- set pin values with `set(pin, value)` or `output(pin)`
- get the pin mode with `mode(pin)`
- set the pin mode with `setup(pin, mode)`
- `mode` can currently equal `sysfs.IN` or `sysfs.OUT`

# Future Features
- setting pullup / pulldown values for pins
- setting pins as pwm
40 changes: 24 additions & 16 deletions gpio.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
__version__ = '0.0.4'
__version__ = '0.1.0'

import functools
import threading
Expand Down Expand Up @@ -33,6 +33,7 @@ def except_hook(exctype, value, tb):
FMODE = 'w+'

IN, OUT = 'in', 'out'
LOW, HIGH = 'low', 'high'


def _write(f, v):
Expand Down Expand Up @@ -68,17 +69,35 @@ def wrapped(pin, *args, **kwargs):


@_verify
def setup(pin, mode, pullup=None):
if mode not in {IN, OUT}:
def setup(pin, mode, value=False):
'''Setup pin with mode IN or OUT.
Args:
pin (int):
mode (str): use either gpio.OUT or gpio.IN
value (bool, optional): Initial pin value
'''


if mode not in {IN, OUT, LOW, HIGH}:
raise ValueError(mode)
log.debug("Setup {}: {}".format(pin, mode))
f = _open[pin]['direction']
if mode == 'OUT':
if value:
mode = LOW
else:
mode = HIGH
_write(f, mode)


@_verify
def mode(pin):
'''get the pin mode'''
'''get the pin mode
Returns:
str: "in" or "out"
'''
f = _open[pin]['direction']
return _read(f)

Expand All @@ -99,7 +118,7 @@ def read(pin):
@_verify
def set(pin, value):
'''set the pin value to 0 or 1'''
value = int(value)
value = int(bool(value))
log.debug("Write {}: {}".format(pin, value))
f = _open[pin]['value']
_write(f, value)
Expand All @@ -115,14 +134,3 @@ def input(pin):
def output(pin, value):
'''set the pin. Same as set'''
return set(pin)


if __name__ == '__main__':
print("starting")
skip = {8, 10, 16, 17}
values = []
for n in xrange(24, 29):
if n in skip: continue
# print("pin {:3d} = {}".format(n, read(n)))
values.append(read(n))
print(values)

0 comments on commit 552b66d

Please sign in to comment.