forked from SamTheGeek/OscarLight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
166 lines (130 loc) · 3.89 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
import os
import random
from flask import Flask, jsonify, request, abort
LIGHT_IDS = [ '23', '24', '22', '25', '17']
LIGHT_BRIGHTS = [0.0, 0.0, 0.0, 0.0, 0.0]
MAX_BRIGHTNESS = 0.8
MIN_BRIGHTNESS = 0.0
DEFAULT_INCR = 0.015
NUM_LIGHTS = 5
app = Flask(__name__)
def set_light(light_id, brightness):
LIGHT_BRIGHTS[light_id] = brightness
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 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 blink_light(light_id):
brightness = LIGHT_BRIGHTS[light_id]
while brightness >= MIN_BRIGHTNESS:
set_light(light_id, brightness)
brightness -= .01
while brightness <= MAX_BRIGHTNESS:
set_light(light_id, brightness)
brightness += .01
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():
for i in range(5):
set_light(i, random.random())
@app.route('/wave')
def wave_endpoint():
wave_lights()
return '{}'
@app.route('/up')
def up_endpoint():
all_lights_up(DEFAULT_INCR)
return '{}'
@app.route('/down')
def down_endpoint():
all_lights_down(DEFAULT_INCR)
return '{}'
@app.route('/blink')
def blink_endpoint():
light_id_arg = request.args.get('light')
if not light_id_arg:
all_lights_down(DEFAULT_INCR)
all_lights_up(DEFAULT_INCR)
return '{}'
light_id = int(light_id_arg)
if light_id < 0 or light_id >= 5:
abort(400)
blink_light(light_id)
return '{}'
@app.route('/set')
def set_light_endpoint():
light_id_arg = request.args.get('light')
brightness_arg = request.args.get('bright')
if not light_id_arg or not brightness_arg:
abort(400)
light_id = int(light_id_arg)
brightness = float(brightness_arg)
if light_id < 0 or light_id >= 5:
abort(400)
if brightness < MIN_BRIGHTNESS or brightness > MAX_BRIGHTNESS:
abort(400)
set_light(light_id, brightness)
return '{}'
@app.route('/set_all')
def set_all_endpoint():
brightness_arg = request.args.get('bright')
if not brightness_arg:
abort(400)
brightness = float(brightness_arg)
if brightness < MIN_BRIGHTNESS or brightness > MAX_BRIGHTNESS:
abort(400)
for i in range(5):
set_light(i, brightness)
return '{}'
@app.route('/rand')
def random_endpoint():
randomize()
return '{}'
@app.route('/sparkle')
def sparkle_endpoint():
for i in range(200):
randomize()
all_lights_up(DEFAULT_INCR)
return '{}'
if __name__ == '__main__':
all_lights_up(DEFAULT_INCR / 5)
app.run(debug=True)