-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·87 lines (62 loc) · 1.73 KB
/
app.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
#!/usr/bin/python3
import board
import neopixel
from subprocess import call
import threading
import time
from flask import Flask
from flask import render_template
# Flask setup
app = Flask(__name__)
# Hardware setup
pixels = neopixel.NeoPixel(board.D18, 42)
rooms = [0] * 42
lights = [0] * 42
# Setup the home page
@app.route('/')
def index():
return render_template('index.html')
# Setup the room control
@app.route('/room<int:room_number>')
def handle_request(room_number):
response = 'Room ' + str(room_number) + ' toggled'
rooms[room_number - 1] = (( rooms[room_number - 1] + 1) % 2)
# Turn the LEDs on and off
for i in range(len(rooms)):
pixels[i] = (50*rooms[i], 50*rooms[i], 50*rooms[i])
return response, 200
# Draw the Path
def path():
# Reset the rooms and LEDs when the path animation starts
for i in range(len(rooms)):
rooms[i] = 0
pixels.fill((0, 0, 0))
# Run the animation while no rooms are lit
i = 3
while not any(rooms):
if ( i < 23 ):
pixels[i] = (50, 0, 0)
else:
pixels[i] = (0, 0, 50)
pixels[i-1] = (0, 0, 0)
time.sleep(0.5)
i += 1
if ( i > 41 ):
pixels[41] = (0, 0, 0)
i = 3
return
# Handle a path request
@app.route('/path')
def handle_path():
# Start a thread to run the path
pixels.fill((0, 0, 0))
path_thr = threading.Thread(target=path)
path_thr.start()
return 'User flow animation', 200
# Shutdown the Pi
@app.route('/shutdown')
def handle_shutdown():
call("sudo nohup shutdown -h now", shell=True)
return 'Shutting down Pi', 200
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')