-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
391 lines (339 loc) · 13 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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
from flask import Flask, render_template, request, jsonify
import time
import RPi.GPIO as GPIO
import pigpio
import os
import subprocess
import re
import psutil
pi = pigpio.pi()
app = Flask(__name__)
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
pins = [23, 24, 25]
frequency = 70
need_to_kill = False
PID = ""
last_command = ""
current_color = ""
current_feature = ""
current_times = [0, 0]
def convert(color):
color['green'] *= 200/255
color['blue'] *= 150/255
return color
colors = {
# White (255. 200, 150)
'white': {'red': 255.0, 'green': 200.0, 'blue': 150.0},
# Red (255, 0, 0)
'red' : convert({'red': 255.0, 'green': 0.0, 'blue': 0.0}),
# Yellow (255, 255, 0)
'yellow' : convert({'red': 255.0, 'green': 255.0, 'blue': 0.0}),
# Orange (255. 87, 51)
'orange' : convert({'red': 255.0, 'green': 87.0, 'blue': 51.0}),
# Green (0, 255, 0)
'green' : convert({'red': 0.0, 'green': 255.0, 'blue': 0.0}),
# Aqua (0, 255, 255)
'aqua' : convert({'red': 0.0, 'green': 255.0, 'blue': 255.0}),
# Blue (0, 0, 255)
'blue' : convert({'red': 0.0, 'green': 0.0, 'blue': 255.0}),
# Pink (255, 0, 255)
'pink' : convert({'red': 255.0, 'green': 0.0, 'blue': 255.0}),
# Purple/Violet (128, 0, 128)
'purple' : convert({'red': 128.0, 'green': 0.0, 'blue': 128.0}),
# Navy (0, 0, 128)
'navy' : convert({'red': 0.0, 'green': 0.0, 'blue': 128.0}),
# Light Blue
'lightblue': {'red': 0.0, 'green': 0.0, 'blue': 255.0},
}
def return_colors():
return colors
# no specification for multi!
# restructure so that there are current states saved as objects
# slow speed adjustment for breathe and flash
@app.route("/", methods=['GET', 'POST'])
def main():
global current_color, current_times, last_command, current_feature
if request.method == "POST":
info = request.values.get('info')
times = request.values.get('times')
array = request.values.get('array')
print("recieved POST.", "info=", info, "times=", times, "array=", array)
if info == 'off':
last_command = 'off'
clear_lights()
elif info == 'on':
# need to make this handle multi to change
execute_prev()
elif info == 'flash':
# toggle
current_times = times.split(",")
print(current_times)
if current_feature == 'flash':
last_command = 'on'
current_feature = 'on'
get_lit_safe(current_color)
else:
if current_color == "":
current_color = 'white'
flash_fx(current_color, current_times[0], current_times[1])
elif info == 'breathe':
# toggle
current_times = times.split(",")
if current_feature == 'breathe':
last_command = 'on'
current_feature = 'on'
get_lit_safe(current_color)
else:
breathe_fx(current_color, current_times[0], current_times[1])
elif info == 'multi breathe':
current_times = times.split(",")
multi_fx("breathe", comma_separate(array), current_times[0], current_times[1])
elif info == 'multi flash':
current_times = times.split(",")
multi_fx("flash", comma_separate(array), current_times[0], current_times[1])
elif info == 'multi on':
current_times = times.split(",")
multi_fx("on", comma_separate(array), current_times[0], current_times[1])
elif info in colors:
if last_command == 'off' or last_command == '':
last_command = 'on'
current_feature = 'on'
print("Turning the lights back on: color= " + info)
current_color = info
get_lit_safe(info)
else:
current_color = info
execute_prev()
return render_template('main.html')
@app.route('/status', methods = ['POST'])
def ajax_request():
return jsonify(currentState= 'off' if last_command == 'off' else current_feature, currentColor= current_color, currentTimes= current_times)
def comma_separate(times):
temp = repr(times).split(',')
for i in range(len(temp)):
temp[i] = temp[i].replace('\'', '')
temp[i] = temp[i].replace('[', '')
temp[i] = temp[i].replace(']', '')
toreturn = ""
for item in temp:
toreturn = toreturn + item + ","
return toreturn[:-1]
@app.route('/on/<color>', methods=['GET', 'POST'])
def on(color):
color = color.lower()
if color in colors:
get_lit_safe(color)
global current_color, current_feature, last_command
current_color = color
current_feature = "on"
last_command = "on"
return render_template('main.html')
@app.route('/off/', methods=['GET', 'POST'])
def off():
global last_command
last_command = "off"
clear_lights()
return render_template('main.html')
def get_process():
y = subprocess.check_output(['pidof', 'python3'])
# print(y)
global need_to_kill, PID
y = str(y)[2:-3]
# print(y)
y = y.split(' ')
# print(y)
need_to_kill = True
for pid in y:
# print(get_pname(pid))
if get_par(pid) == 1:
PID = pid
# PID = y[0]
print('Process to kill:', PID)
def print_status():
print("Light color:", current_color)
print("Current feature:", current_feature)
print("Current times:", current_times)
@app.route('/flash/<color>/<hi_time>/<lo_time>', methods=['GET', 'POST'])
def flash(color, hi_time, lo_time):
color = color.lower()
if not color in colors:
return render_template('main.html')
flash_fx(color, hi_time, lo_time)
return render_template('main.html')
def flash_fx(color, hi_time, lo_time):
print("Flashing lights " + color + ", times: " + hi_time, lo_time)
color = color.lower()
if not color in colors:
return
global current_color, current_times, current_feature, last_command, need_to_kill, PID
last_command = "flash"
current_color = color
current_times = [str(float(hi_time)), str(float(lo_time))]
current_feature = "flash"
clear_lights()
os.system("python3 features.py "+ "flash " + color + " " + hi_time + " " + lo_time + " &")
get_process()
print_status()
@app.route('/breathe/<color>/<hi_time>/<lo_time>', methods=['GET', 'POST'])
def breathe(color, hi_time, lo_time):
color = color.lower()
if not color in colors:
return render_template('main.html')
breathe_fx(color, hi_time, lo_time)
return render_template('main.html')
def breathe_fx(color, hi_time, lo_time):
print("Breathing lights " + color + ", times: " + hi_time, lo_time)
color = color.lower()
print("Color: " + color)
if not color in colors:
return
global current_color, current_times, current_feature, last_command, need_to_kill, PID
last_command = "breathe"
current_color = color
current_times = [str(float(hi_time)), str(float(lo_time))]
current_feature = "breathe"
clear_lights()
os.system("python3 features.py "+ "breathe " + color + " " + hi_time + " " + lo_time + " &")
get_process()
print_status()
@app.route('/multi/<feature>/<colorlist>/<hi_time>')
@app.route('/multi/<feature>/<colorlist>/<hi_time>/<lo_time>')
def multi(feature, colorlist, hi_time, lo_time = ""):
colorlist2 = parse_multi_colors(colorlist)
for color in colorlist2:
if not color in colors:
return render_template('main.html')
multi_fx(feature, colorlist, hi_time, lo_time)
return render_template('main.html')
def multi_fx(feature, colorlist, hi_time, lo_time = ""):
print("Multi-" + feature + "ing" + colorlist + ", times: " + hi_time, lo_time)
colorlist2 = parse_multi_colors(colorlist)
for color in colorlist2:
if not color in colors:
return
global current_color, current_times, current_feature, last_command
current_color = colorlist
try:
current_times = [str(float(hi_time)), str(float(lo_time))]
except:
current_times = [str(float(hi_time)), "0"]
if feature == 'on':
lo_time = ""
current_feature = "multi " + feature
clear_lights()
colorlist = colorlist.replace(' ', ',')
last_command = "multi " + feature
os.system("python3 features.py "+ "multi " + feature + " " + colorlist + " " + hi_time + " " + lo_time + " &")
get_process()
print_status()
def parse_multi_colors(colors):
if "," in colors:
temp = colors.split(',')
for i in range(len(temp)):
temp[i] = temp[i].lower()
return temp
else:
temp = colors.split(' ')
for i in range(len(temp)):
temp[i] = temp[i].lower()
return temp
def multi_space_to_comma(colors):
return colors.replace(' ', ',')
def execute_prev():
print("Executing previous: " + current_feature)
if current_feature == 'on':
get_lit_safe(current_color)
elif current_feature == 'flash':
flash_fx(current_color, current_times[0], current_times[1])
elif current_feature == 'breathe':
breathe_fx(current_color, current_times[0], current_times[1])
elif current_feature == 'multi on':
multi_fx('on', current_color, current_times[0], current_times[1])
elif current_feature == 'multi flash':
multi_fx('flash', current_color, current_times[0], current_times[1])
elif current_feature == 'multi breathe':
multi_fx('breathe', current_color, current_times[0], current_times[1])
@app.route('/speedup', methods=['GET', 'POST'])
def speedup():
if current_feature == "flash":
print("Speeding up flash.")
if float(current_times[0])-30 > 0 and float(current_times[1])-30 > 0:
flash_fx(current_color, str((float(current_times[0])-30)), str((float(current_times[1])-30)))
else:
print("Cannot speed up anymore.")
elif current_feature == "breathe":
print("Speeding up breathe.")
if float(current_times[0])-30 > 0 and float(current_times[1])-30 > 0:
breathe_fx(current_color, str((float(current_times[0])-30)), str((float(current_times[1])-30)))
else:
print("Cannot speed up anymore.")
elif "multi" in current_feature:
if "on" in current_feature:
if float(current_times[0])-30 > 0:
multi_fx("on", current_color, str((float(current_times[0])-30)))
elif "flash" in current_feature:
if float(current_times[0])-30 > 0 and float(current_times[1])-30 > 0:
multi_fx("flash", current_color, str((float(current_times[0])-30)), str((float(current_times[1])-30)))
else:
if float(current_times[0])-30 > 0 and float(current_times[1])-30 > 0:
multi_fx("breathe", current_color, str((float(current_times[0])-30)), str((float(current_times[1])-30)))
return render_template('main.html')
@app.route('/slowdown', methods=['GET', 'POST'])
def slowdown():
if current_feature == "flash":
print("Slowing down flash.")
flash_fx(current_color, str(float(current_times[0])+30), str(float(current_times[1])+30))
elif current_feature == "breathe":
print("Slowing down breathe.")
breathe_fx(current_color, str(float(current_times[0])+30), str(float(current_times[1])+30))
elif "multi" in current_feature:
if "on" in current_feature:
multi_fx("on", current_color, str(float(current_times[0])+30))
elif "flash" in current_feature:
multi_fx("flash", current_color, str(float(current_times[0])+30), str(float(current_times[1])+30))
else:
multi_fx("breathe", current_color, str(float(current_times[0])+30), str(float(current_times[1])+30))
return render_template('main.html')
@app.route('/sound', methods=['GET', 'POST'])
def sound():
sound_fx()
return render_template('main.html')
def sound_fx():
global current_feature, last_command, need_to_kill, PID
current_feature = "sound"
last_command = "sound"
clear_lights()
os.system("python3 features.py "+ "sound " + "&")
get_process()
print(current_feature)
def get_lit(color):
color_dict = colors[color]
pi.set_PWM_dutycycle(23, color_dict['red'])
pi.set_PWM_dutycycle(24, color_dict['green'])
pi.set_PWM_dutycycle(25, color_dict['blue'])
def get_lit_safe(color):
clear_lights()
color_dict = colors[color]
pi.set_PWM_dutycycle(23, color_dict['red'])
pi.set_PWM_dutycycle(24, color_dict['green'])
pi.set_PWM_dutycycle(25, color_dict['blue'])
def get_pname(id):
process = psutil.Process(int(id))
return process.name()
def get_par(id):
process = psutil.Process(int(id))
return int(process.ppid())
def clear_lights():
global need_to_kill
if need_to_kill:
os.system("kill " + PID)
need_to_kill = False
pi.wave_clear()
for pin in pins:
pi.set_mode(pin, pigpio.OUTPUT)
pi.set_PWM_frequency(pin, frequency)
pi.set_PWM_dutycycle(pin, 0)
if __name__ == "__main__":
clear_lights()
app.run(host='0.0.0.0', debug=True)