forked from vitiral/gpio
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gpio.py
128 lines (97 loc) · 2.74 KB
/
gpio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# -*- coding: utf-8 -*-
__version__ = '0.0.4'
import functools
import threading
import os
import sys
import traceback
import pdb
import logging
logging.basicConfig(level=logging.ERROR)
log = logging.getLogger(__name__)
def except_hook(exctype, value, tb):
traceback.print_tb(tb)
print(repr(value))
pdb.post_mortem(tb)
sys.excepthook = except_hook
path = os.path
pjoin = os.path.join
gpio_root = '/sys/class/gpio'
gpiopath = lambda pin: os.path.join(gpio_root, 'gpio{}'.format(pin))
_export_lock = threading.Lock()
_pyset = set
_open = dict()
FMODE = 'w+'
IN, OUT = 'in', 'out'
def _write(f, v):
log.debug("writing: {}: {}".format(f, v))
f.write(str(v))
def _read(f):
log.debug("Reading: {}".format(f))
f.seek(0)
return f.read()
def _verify(function):
"""decorator to ensure pin is properly set up"""
# @functools.wraps
def wrapped(pin, *args, **kwargs):
pin = int(pin)
if pin not in _open:
ppath = gpiopath(pin)
if not os.path.exists(ppath):
log.debug("Creating Pin {}".format(pin))
with _export_lock:
with open(pjoin(gpio_root, 'export'), 'w') as f:
_write(f, pin)
_open[pin] = {
'value': open(pjoin(ppath, 'value'), FMODE),
'direction': open(pjoin(ppath, 'direction'), FMODE),
'drive': open(pjoin(ppath, 'drive'), FMODE),
}
return function(pin, *args, **kwargs)
return wrapped
@_verify
def setup(pin, mode, pullup=None):
if mode not in {IN, OUT}:
raise ValueError(mode)
log.debug("Setup {}: {}".format(pin, mode))
f = _open[pin]['direction']
_write(f, mode)
@_verify
def mode(pin):
'''get the pin mode'''
f = _open[pin]['direction']
return _read(f)
@_verify
def read(pin):
'''read the pin value
Returns:
bool: 0 or 1
'''
f = _open[pin]['value']
out = int(_read(f))
log.debug("Read {}: {}".format(pin, out))
return out
@_verify
def set(pin, value):
'''set the pin value to 0 or 1'''
value = int(value)
log.debug("Write {}: {}".format(pin, value))
f = _open[pin]['value']
_write(f, value)
@_verify
def input(pin):
'''read the pin. Same as read'''
return read(pin)
@_verify
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)