-
Notifications
You must be signed in to change notification settings - Fork 0
/
led_driver.py
120 lines (105 loc) · 3.41 KB
/
led_driver.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
"""Module containing Led Driver"""
from typing import Iterable
from time import sleep
from GPIOSimulator_v5 import GPIOSimulator
class LedDriver():
"""Led driver class"""
def __init__(self) -> None:
self.gpio_sim = GPIOSimulator()
self.led_lighting_states = (
((0, 1, 1), (1, 1, 0)),
((1, 1, 1), (0, 1, 0)),
((1, 1, 1), (2, 1, 0)),
((2, 1, 1), (1, 1, 0)),
((0, 1, 1), (2, 1, 0)),
((2, 1, 1), (0, 1, 0)),
)
self._all_to_input()
def _all_to_input(self) -> None:
for i in range(0, 6):
self.gpio_sim.setup(i, 0)
def _light_diode(self, led: int) -> None:
states = self.led_lighting_states[led]
self.gpio_sim.setup(states[0][0], 1)
self.gpio_sim.setup(states[1][0], 1)
self.gpio_sim.output(states[0][0], states[0][2])
self.gpio_sim.output(states[1][0], states[1][2])
self.gpio_sim.show_leds_states()
self._all_to_input()
def _light_sequence(self, sequence: Iterable) -> None:
for led, state in enumerate(sequence):
if state:
self._light_diode(led)
sleep(0.001)
def _light_simult(self, sequence: Iterable, frame_count: int = 60) -> None:
i = 0
while i < frame_count:
self._light_sequence(sequence)
i += 1
def _light_anim(self, sequence: Iterable, frame_count: int = 60) ->None:
for frame in sequence:
self._light_simult(frame, frame_count)
def power_up(self) -> None:
"""Light power up sequence"""
self._light_anim((
(1, 0, 0, 0, 0, 0),
(1, 1, 0, 0, 0, 0),
(1, 1, 1, 0, 0, 0),
(1, 1, 1, 1, 0, 0),
(1, 1, 1, 1, 1, 0),
(1, 1, 1, 1, 1, 1)
))
def power_down(self) -> None:
"""Light power down sequence"""
self._light_anim((
(1, 1, 1, 1, 1, 1),
(1, 1, 1, 1, 1, 0),
(1, 1, 1, 1, 0, 0),
(1, 1, 1, 0, 0, 0),
(1, 1, 0, 0, 0, 0),
(1, 0, 0, 0, 0, 0),
))
def twinkle(self) -> None:
"""Light twinkle sequence"""
self._light_anim((
(0, 0, 0, 1, 1, 0),
(0, 0, 1, 0, 0, 1),
(1, 0, 1, 1, 0, 0),
(0, 1, 0, 0, 1, 0),
(1, 0, 1, 0, 0, 1),
(0, 1, 0, 0, 1, 0),
(0, 1, 0, 0, 1, 0),
(0, 0, 1, 0, 0, 1),
(1, 0, 1, 1, 0, 0),
(0, 1, 0, 0, 1, 0),
(1, 0, 1, 0, 0, 1),
(0, 1, 0, 0, 1, 0),
), 10)
def flash(self) -> None:
"""Light flash sequence"""
self._light_anim((
(1, 1, 1, 1, 1, 1),
(0, 0, 0, 0, 0, 0),
(1, 1, 1, 1, 1, 1),
(0, 0, 0, 0, 0, 0),
(1, 1, 1, 1, 1, 1),
(0, 0, 0, 0, 0, 0),
(1, 1, 1, 1, 1, 1),
(0, 0, 0, 0, 0, 0),
(1, 1, 1, 1, 1, 1),
(0, 0, 0, 0, 0, 0),
(1, 1, 1, 1, 1, 1),
(0, 0, 0, 0, 0, 0),
), 10)
def light_single(self, led: int, dur: float) -> None:
"""Light single LED"""
self._light_diode(led)
sleep(dur)
self._all_to_input()
self.gpio_sim.show_leds_states()
def main():
"""main func"""
ld_driver = LedDriver()
ld_driver.light_single(2, 1)
if __name__ == "__main__":
main()