forked from vitiral/gpio
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gpio.py
138 lines (105 loc) · 2.93 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
129
130
131
132
133
134
135
136
137
138
# -*- coding: utf-8 -*-
__version__ = '0.1.0'
import functools
import threading
import os
import sys
import traceback
import pdb
import logging
# logging.basicConfig(level=logging.ERROR)
logging.basicConfig(level=logging.DEBUG)
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'
LOW, HIGH = 'low', 'high'
def _write(f, v):
log.debug("writing: {}: {}".format(f, v))
f.write(str(v))
f.flush()
def _read(f):
log.debug("Reading: {}".format(f))
f.seek(0)
return f.read().strip()
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, 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
Returns:
str: "in" or "out"
'''
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(bool(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)