-
Notifications
You must be signed in to change notification settings - Fork 1
/
led_client.py
249 lines (228 loc) · 7.02 KB
/
led_client.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import os
import logging
import logging.config
import socket
import time
LM_SOCKET_PATH = "/home/pi/ubo/ledmanagersocket.sock"
LOG_CONFIG = "/home/pi/ubo/log/logging-debug.ini"
logging.config.fileConfig(LOG_CONFIG,
disable_existing_loggers=False)
logger = logging.getLogger("led_client")
class LEDClient:
''' The methods of this class send commands to the LED manager,
which runs with root previlages due to hardware DMA security constraints.
The commands are sent through a socket connection to the LED manager.
The LED manager is a Python script that runs as a daemon. The LED manager
is a daemon because it uses DMA to control the LEDs. This is a hardware security constraint.
The LED client is a Python script that runs as a non-root user. The LED client function
is to serialize LED commands and send them to the LED manager in a secure manner.
'''
def __init__(self):
self.client = None
if os.path.exists(LM_SOCKET_PATH):
self.client = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
self.client.connect(LM_SOCKET_PATH)
def __del__(self):
if self.client is not None:
self.client.close()
def set_enabled(self, enabled=True):
if self.client is None:
return
self.send("set_enabled " + str(int(enabled)))
def send(self, cmd):
self.client.send(cmd.encode('utf-8'))
def set_all(self, color = (255,255,255)):
'''
sets all LEDs to the specified color
'''
if self.client is None:
return
(r,g,b) = color
self.send("set_all " +
str(r) + " " +
str(g) + " " +
str(b))
def set_brightness(self, brightness = 0.5):
'''
sets all LEDs to the specified color
'''
if self.client is None:
return
if brightness < 0 or brightness > 1:
print("brightness must be between 0 and 1")
return
self.send("set_brightness " +
str(brightness))
def blank(self):
if self.client is None:
return
self.send("blank")
def rainbow(self, rounds, wait):
'''
glows the LEDs in a rainbow pattern
percentage: is a float between 0 and 1
wait: is in milliseconds
'''
if self.client is None:
return
self.send("rainbow " +
str(rounds) + " " +
str(wait))
def progress_wheel_step(self, color = (255,255,255)):
'''
increments the position of bright strip by one step.
'''
if self.client is None:
return
(r,g,b) = color
self.send("progress_wheel_step " +
str(r) + " " +
str(g) + " " +
str(b))
def pulse(self, color = (255,255,255),
wait = 100,
repetitions = 5):
if self.client is None:
return
(r,g,b) = color
self.send("pulse " +
str(r) + " " +
str(g) + " " +
str(b) + " " +
str(wait) + " " +
str(repetitions))
def blink(self, color = (255,255,255),
wait = 100,
repetitions = 5):
if self.client is None:
return
(r,g,b) = color
self.send("blink " +
str(r) + " " +
str(g) + " " +
str(b) + " " +
str(wait) + " " +
str(repetitions))
def spinning_wheel(self,
color = (255,255,255),
wait = 100,
length = 5,
repetitions = 5):
if self.client is None:
return
(r,g,b) = color
self.send("spinning_wheel " +
str(r) + " " +
str(g) + " " +
str(b) + " " +
str(wait) + " " +
str(length) + " " +
str(repetitions))
def progress_wheel(self,
color = (255,255,255),
percentage = 0.5):
if self.client is None:
return
(r,g,b) = color
self.send("progress_wheel " +
str(r) + " " +
str(g) + " " +
str(b) + " " +
str(percentage))
def fill_upto(self,
color = (255,255,255),
percentage = 0.5,
wait = 100):
if self.client is None:
return
(r,g,b) = color
self.send("fill_upto " +
str(r) + " " +
str(g) + " " +
str(b) + " " +
str(percentage) + " " +
str(wait))
def fill_downfrom(self,
color = (255,255,255),
percentage = 0.5,
wait = 100):
if self.client is None:
return
(r,g,b) = color
self.send("fill_downfrom " +
str(r) + " " +
str(g) + " " +
str(b) + " " +
str(percentage) + " " +
str(wait))
if __name__ == '__main__':
# add test cased for (1) enable and disable (2) set brightness
lc = LEDClient()
lc.set_enabled(True)
lc.set_all((255, 255, 255))
time.sleep(1)
print("white ring must glow for 1 second")
lc.set_enabled(False)
lc.set_all((255, 0, 0))
print("red ring must NOT glow for 1 second")
time.sleep(1)
lc.set_enabled(True)
lc.blank()
# fill_upto
lc.fill_upto((0,0,255), 1, 20)
time.sleep(1)
# fill_downfrom
lc.fill_downfrom((0,0,255), 1, 20)
time.sleep(1)
# magenta
lc.set_all((255, 0, 255))
time.sleep(1)
# yellow
lc.set_all((255, 255, 0))
time.sleep(1)
# cyan
lc.set_all((0, 255, 255))
time.sleep(1)
# white
lc.set_all((255, 255, 255))
time.sleep(1)
# red
lc.set_all((255, 0, 0))
time.sleep(1)
# green
lc.set_all((0, 255, 0))
time.sleep(1)
# blue
lc.set_all((0, 0, 255))
time.sleep(1)
# turn off all
lc.blank()
# activate rainbow
lc.rainbow(10, 5)
time.sleep(1)
for i in range(0, 11, 1):
lc.set_brightness(i/10)
time.sleep(0.2)
# show progress wheel step
for i in range(15):
lc.progress_wheel_step((0, 255, 0))
time.sleep(0.1)
# pulse
lc.pulse((255, 0, 255), 50, 5)
time.sleep(2)
# blink
lc.blink((255, 0, 0), 200, 5)
time.sleep(1)
# spinning wheel
lc.spinning_wheel((255, 255, 255), 20, 10, 3)
time.sleep(2)
lc.blank()
# set progress wheel to 60%
for i in range(25):
lc.progress_wheel((0, 0, 255), i/25)
time.sleep(0.1)
lc.blank()
time.sleep(1)
lc.rainbow(3, 5)
time.sleep(2)
lc.blank()