-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleds.py
87 lines (66 loc) · 2.64 KB
/
leds.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
import threading, time, logging
from effects.off import Off
from effects.unicolor import *
from effects.multicolor import *
logger = logging.getLogger(__name__)
class LEDThread(threading.Thread):
def __init__(self, pixelcount=183):
threading.Thread.__init__(self)
self._shutdown = False
self._mode = 0
self._effects = [Off(pixelcount),
KnightRider(pixelcount),
Arthur3(pixelcount),
MovingRGBCycle(pixelcount),
Arthur2(pixelcount),
Arthur(pixelcount),
# White(pixelcount),
SingleColor(pixelcount),
RainbowColors(pixelcount),
RainbowCycle(pixelcount),
RainbowCycleSuccessive(pixelcount),
AppearFromBack(pixelcount)
]
def _mode_name(self, mode):
return self._effects[mode].get_name()
def current_effect(self):
return self._effects[self._mode]
def shutdown(self):
self._shutdown = True
self._effects[self._mode].hide()
def is_running(self):
return not self._shutdown
def on_power_on(self):
self.set_mode(1)
def before_power_off(self):
self.set_mode(0)
time.sleep(0.3)
def next_color(self, step=0.01):
self.current_effect().next_color(step)
def prev_color(self, step=0.01):
self.current_effect().prev_color(step)
def light_up(self, step=0.01):
self.current_effect().light_up(step)
def light_down(self, step=0.01):
self.current_effect().light_down(step)
def next_mode(self):
if self._mode == 0:
logger.error ('Ignoring switch to next mode (leds are off)')
return
new_mode = self._mode + 1
if new_mode >= len(self._effects):
new_mode = 1
self.set_mode(new_mode)
def set_mode(self, mode):
if(not isinstance(mode, int) or mode >= len(self._effects) or mode < 0):
logger.error ('Mode ' + str(mode) + ' is not known!')
elif(mode != self._mode):
old_mode = self._mode
logger.info ("Switching from '" + self._mode_name(old_mode) + "' to '" + self._mode_name(mode) + "'")
self._mode = mode
self._effects[old_mode].hide()
def run(self):
logger.info("Starting LED thread ...")
while not self._shutdown:
self._effects[self._mode].show() # blocks until hide() or _wake()
logger.info("Shutdown LED thread...")