forked from mjmccaffrey/marquee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signs.py
72 lines (62 loc) · 1.82 KB
/
signs.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
"""Marquee Lighted Sign Project - signs"""
import logging
import buttons
from buttons import ( # pylint: disable=unused-import
ButtonPressed,
PhysicalButtonPressed,
VirtualButtonPressed,
)
import relayboards
LIGHTS_BY_ROW = [
[ 0, 1, 2, ],
[ 9, 3, ],
[ 8, 4, ],
[ 7, 6, 5, ],
]
TOP_LIGHTS_LEFT_TO_RIGHT = [9, 0, 1, 2, 3]
BOTTOM_LIGHTS_LEFT_TO_RIGHT = [8, 7, 6, 5, 4]
LIGHTS_CLOCKWISE = [9, 0, 1, 2, 3, 4, 5, 6, 7, 8]
_LIGHT_TO_RELAY = {
0: 9,
1: 13,
2: 14,
3: 15,
4: 2,
5: 1,
6: 0,
7: 6,
8: 7,
9: 8,
}
LIGHT_COUNT = len(_LIGHT_TO_RELAY)
class Sign:
"""Supports the physical devices."""
def __init__(self):
"""Prepare devices and initial state."""
self._relayboard = relayboards.RelayBoard(_LIGHT_TO_RELAY)
self._button = buttons.Button()
def close(self):
"""Clean up."""
try:
self._button.close()
except Exception as e:
logging.exception(e)
try:
self._relayboard.close()
except Exception as e:
logging.exception(e)
def set_lights(self, lights):
"""Set all lights per the supplied pattern."""
self._relayboard.set_relays_from_pattern(lights)
def wait_for_interrupt(self, seconds):
"""Pause the thread until either the seconds have elapsed
or the button has been pressed."""
if self._button.pressed_event.wait(seconds):
raise PhysicalButtonPressed
def interrupt_reset(self):
"""Prepare for a button press."""
self._button.reset()
@staticmethod
def is_valid_light_pattern(arg):
""" Return True if arg is a valid light pattern, otherwise False. """
return len(arg) == LIGHT_COUNT and all(e in {"0", "1"} for e in arg)