-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
86 lines (66 loc) · 1.99 KB
/
main.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
from MicroWebSrv2 import *
from time import sleep
from _thread import allocate_lock
from lights import *
@WebRoute(GET, '/test')
def RequestTestGet(microWebSrv2, request):
request.Response.ReturnOkJSON({
'ClientAddr' : request.UserAddress,
'Accept' : request.Accept,
'UserAgent' : request.UserAgent
})
@WebRoute(POST, '/lights', name='Lights')
def RequestTestPost(microWebSrv2, request) :
data = request.GetPostedJSONObject()
print(data)
try :
state = data['state']
if 'interval' in data:
interval = float(data['interval'])
else:
interval = None
if 'colors' in data:
colors = list(map(lambda c: (c['r'], c['g'], c['b']), data['colors']))
else:
colors = None
except :
print("Bad Request")
request.Response.ReturnBadRequest()
return
if lights:
if state == 'static':
lights.state = STATIC
elif state == 'marquee':
lights.state = MARQUEE
elif state == 'fade':
lights.state = FADE
elif state == 'rainbow':
lights.state = RAINBOW
else:
lights.state = OFF
if colors:
lights.colors = colors
if interval:
lights.interval = interval
request.Response.ReturnOk()
mws2 = MicroWebSrv2()
# For embedded MicroPython, use a very light configuration,
mws2.SetEmbeddedConfig()
mws2._slotsCount = 4
# All pages not found will be redirected to the home '/',
mws2.NotFoundURL = '/'
mws2.StartManaged()
lights = Lights(pin=5, led_count=150)
try :
while mws2.IsRunning :
lights.run()
except KeyboardInterrupt :
pass
# End,
print()
mws2.Stop()
print('Bye')
print()
# ============================================================================
# ============================================================================
# ============================================================================