From 552b66dbc93ca7fcceb4f3127b6b4a6290bf102f Mon Sep 17 00:00:00 2001 From: Garrett Berg Date: Tue, 10 Mar 2015 17:19:23 -0600 Subject: [PATCH] gpio: updated documentation --- README.md | 13 ++++--------- gpio.py | 40 ++++++++++++++++++++++++---------------- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 7485572..a0d3b10 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/gpio.py b/gpio.py index 896d3e7..f9a9b52 100644 --- a/gpio.py +++ b/gpio.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -__version__ = '0.0.4' +__version__ = '0.1.0' import functools import threading @@ -33,6 +33,7 @@ def except_hook(exctype, value, tb): FMODE = 'w+' IN, OUT = 'in', 'out' +LOW, HIGH = 'low', 'high' def _write(f, v): @@ -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) @@ -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) @@ -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)