/
gpio_device.py
89 lines (73 loc) · 2.82 KB
/
gpio_device.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
from threading import Lock
try:
import RPi.GPIO as GPIO
except:
# Let the block code load anyway so that som unit tests can run.
pass
class GPIODevice():
"""Communicate with a device over GPIO."""
def __init__(self, logger):
self.logger = logger
GPIO.setmode(GPIO.BCM)
self._gpio_lock = Lock()
def read(self, pin, pull_up_down=None):
"""Read bool value from a pin.
Args:
pin (int): the pin to read from
Return:
bool: value of digital pin reading
"""
with self._gpio_lock:
# TODO: don't call this every time
if pull_up_down is not None:
if pull_up_down:
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
else:
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
else:
GPIO.setup(pin, GPIO.IN)
value = GPIO.input(pin)
self.logger.debug(
"Read value from GPIO pin {}: {}".format(pin, value))
return bool(value)
def write(self, pin, value):
"""Write bool value to a pin.
Args:
pin (int): the pin to write to
value (bool): boolean value to write to pin
"""
with self._gpio_lock:
# TODO: don't call this every time
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, value)
self.logger.debug(
"Wrote value to GPIO pin {}: {}".format(pin, value))
def interrupt(self, callback, pin, pull_up_down=None, bouncetime=200):
"""Init interrupt callback function for pin.
Args:
callback (function): function to call on interrupt
pin (int): the pin to monitor for interrupts
"""
with self._gpio_lock:
if pull_up_down is not None:
if pull_up_down:
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Use falling detection since we are pulled up
GPIO.add_event_detect(
pin, GPIO.FALLING, bouncetime=bouncetime)
else:
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# Use rising detection since we are pulled down
GPIO.add_event_detect(
pin, GPIO.RISING, bouncetime=bouncetime)
else:
GPIO.setup(pin, GPIO.IN)
GPIO.add_event_detect(pin, GPIO.BOTH, bouncetime=bouncetime)
GPIO.add_event_callback(pin, callback)
self.logger.debug(
"Set interrupt callback of GPIO pin {}".format(pin))
def close(self):
try:
GPIO.cleanup()
except:
self.logger.warning("Failed to close GPIO", exc_info=True)