forked from SamTheGeek/OscarLight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
223 lines (190 loc) · 5.98 KB
/
server.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
import multiprocessing
import os
import random
import time
from flask import Flask, jsonify, request, abort
# set to true to print light values instead of setting
DEV_MODE = False
DEFAULT_INCR = 0.01
LIGHT_BRIGHTS = [0.0, 0.0, 0.0, 0.0, 0.0]
LIGHT_IDS = [ '23', '24', '22', '25', '17']
MAX_BRIGHTNESS = 0.8
MIN_BRIGHTNESS = 0.0
NUM_LIGHTS = 5
app = Flask(__name__)
command_queue = multiprocessing.Queue()
def set_light(light_id, brightness):
LIGHT_BRIGHTS[light_id] = brightness
if DEV_MODE:
print LIGHT_IDS[light_id] + ": " + str(brightness)
else:
os.system("echo " + LIGHT_IDS[light_id] + "=" + str(brightness)
+ " > /dev/pi-blaster")
def all_lights_up(incr):
while True:
done = True
for i in range(5):
if LIGHT_BRIGHTS[i] < MAX_BRIGHTNESS:
done = False
set_light(i, LIGHT_BRIGHTS[i] + incr)
if done:
return
def one_light_up(incr, light):
while True:
done = True
if LIGHT_BRIGHTS[light] < MAX_BRIGHTNESS:
done = False
set_light(light, LIGHT_BRIGHTS[light] + incr)
if done:
return
def all_lights_down(incr):
while True:
done = True
for i in range(5):
if LIGHT_BRIGHTS[i] > MIN_BRIGHTNESS:
done = False
set_light(i, LIGHT_BRIGHTS[i] - incr)
if done:
for i in range(5):
set_light(i, 0)
return
def one_light_down(incr, light):
while True:
done = True
if LIGHT_BRIGHTS[light] > MIN_BRIGHTNESS:
done = False
set_light(light, LIGHT_BRIGHTS[light] - incr)
if done:
set_light(light, 0)
return
def blink_light(light_id):
brightness = LIGHT_BRIGHTS[light_id]
while brightness >= MIN_BRIGHTNESS:
set_light(light_id, brightness)
brightness -= DEFAULT_INCR / 2
while brightness <= MAX_BRIGHTNESS:
set_light(light_id, brightness)
brightness += DEFAULT_INCR / 2
def blink_all():
all_lights_down(DEFAULT_INCR)
all_lights_up(DEFAULT_INCR)
def wave_lights():
count_downs = [0, 10, 20, 30, 40]
turning_up = [False, False, False, False, False]
all_lights_up(DEFAULT_INCR)
while True:
for i in range(5):
if count_downs[i] == 0:
if not turning_up[i]:
set_light(i, LIGHT_BRIGHTS[i] - .005)
if (LIGHT_BRIGHTS[i] <= .1):
turning_up[i] = True
else:
if (LIGHT_BRIGHTS[i] < MAX_BRIGHTNESS):
set_light(i, LIGHT_BRIGHTS[i] + .005)
else:
count_downs[i] -= 1
# this is the worst code I've written all year
done = True
for i in range(5):
if LIGHT_BRIGHTS[i] < MAX_BRIGHTNESS:
done = False
if done:
return
def randomize():
incr = DEFAULT_INCR / 2
target_values = []
for i in range(5):
target_values.append(random.random())
while True:
done = True
for i in range(5):
if abs(LIGHT_BRIGHTS[i] - target_values[i]) < DEFAULT_INCR:
continue
elif LIGHT_BRIGHTS[i] < target_values[i]:
done = False
set_light(i, LIGHT_BRIGHTS[i] + incr)
elif LIGHT_BRIGHTS[i] > target_values[i]:
done = False
set_light(i, LIGHT_BRIGHTS[i] - incr)
if done:
return
@app.route('/wave')
def wave_endpoint():
command_queue.put("WAVE")
return '{}'
@app.route('/up')
def up_endpoint():
light_id_arg = request.args.get('light')
if not light_id_arg:
command_queue.put("UP")
return '{}'
light_id = int(light_id_arg)
if light_id < 0 or light_id >= 5:
abort(400)
command_queue.put("UP " + light_id_arg)
return '{}'
@app.route('/down')
def down_endpoint():
light_id_arg = request.args.get('light')
if not light_id_arg:
command_queue.put("DOWN")
return '{}'
light_id = int(light_id_arg)
if light_id < 0 or light_id >= 5:
abort(400)
command_queue.put("DOWN " + light_id_arg)
return '{}'
@app.route('/blink')
def blink_endpoint():
light_id_arg = request.args.get('light')
if not light_id_arg:
command_queue.put("BLINK")
return '{}'
light_id = int(light_id_arg)
if light_id < 0 or light_id >= 5:
abort(400)
command_queue.put("BLINK " + light_id_arg)
return '{}'
@app.route('/sparkle')
def sparkle_endpoint():
command_queue.put("SPARKLE")
return '{}'
def test_loop():
command = "STEADY"
while True:
if not command_queue.empty():
command = command_queue.get()
if command.startswith("BLINK"):
command_parts = command.split()
if len(command_parts) != 2:
blink_all()
else:
# heaven forgive me for this
blink_light(int(command_parts[1]))
elif command.startswith("UP"):
command_parts = command.split()
if len(command_parts) != 2:
all_lights_up(DEFAULT_INCR)
else:
# also for this
one_light_up(DEFAULT_INCR, int(command_parts[1]))
command = "STEADY"
elif command.startswith("DOWN"):
command_parts = command.split()
if len(command_parts) != 2:
all_lights_down(DEFAULT_INCR)
else:
# after three times this may no longer be forgivable, admittedly
one_light_down(DEFAULT_INCR, int(command_parts[1]))
command = "STEADY"
elif command == "WAVE":
wave_lights()
elif command == "SPARKLE":
randomize()
if __name__ == '__main__':
all_lights_up(DEFAULT_INCR / 5)
p = multiprocessing.Process(target=test_loop)
p.start()
app.run(host='0.0.0.0', debug=True)
p.join()