forked from roger-selzler/SYSC3010Lab4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice.py
More file actions
65 lines (54 loc) · 1.89 KB
/
Copy pathdevice.py
File metadata and controls
65 lines (54 loc) · 1.89 KB
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
from sense_hat import SenseHat
from backend import Backend, logger
from mydbconfig import *
sense = SenseHat()
def init_screen(backend):
if backend.is_device():
colors=backend.get_led_status(backend._device_info['serial'])
sense.set_pixels(colors)
else:
raise Exception("This is not a Raspberry pi. This file should be run from a RPi.")
def led_stream_handler(message):
if message['event'] =='put':
if message ['path'][1:].isnumeric():
ledn = int(message ['path'][1:])
color = message['data']
sense.set_pixel(
(ledn-1)%8,
int((ledn-1)/8),
message['data'][0],
message['data'][1],
message['data'][2],
)
logger.debug(f"Received update for led: {message}")
def main():
#initialize the db with configuration and user data
backend = Backend(config, email, firstname, lastname)
#initialize the LED values from database
init_screen(backend)
#register a stream: whenever there is a change, execute the function led_stream_handler()
led_stream = backend._db \
.child('devices') \
.child(backend._device_info['serial']) \
.child('leds') \
.stream(led_stream_handler)
while True:
for event in sense.stick.get_events():
if(event.action == "pressed"):
if(event.direction == "middle"):
clear_leds()
# to be implemented by students
def clear_leds():
B = (0,0,0)
all_pixels = [
B, B, B, B, B, B, B, B,
B, B, B, B, B, B, B, B,
B, B, B, B, B, B, B, B,
B, B, B, B, B, B, B, B,
B, B, B, B, B, B, B, B,
B, B, B, B, B, B, B, B,
B, B, B, B, B, B, B, B,
B, B, B, B, B, B, B, B]
sense.set_pixels(all_pixels)
if __name__ == '__main__':
main()