-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.py
More file actions
137 lines (101 loc) · 3.21 KB
/
controller.py
File metadata and controls
137 lines (101 loc) · 3.21 KB
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
from multiprocessing import Process, Pipe
import SimpleHTTPServer
import SocketServer
import sys
import time
# Server parameters
PORT = 8000
# Default controller settings
DEFAULT_SETTINGS = {
'mode': 'mood',
'color': 0.5,
'brightness': 1,
'speed': 1,
'type': 'single',
'flash': 'none',
'threshold': 1.5,
'moving average': 20,
'pulsing': True,
'smoothness': 0.5,
'sensitivity': 1
}
# Each process has own copy of settings
settings = DEFAULT_SETTINGS.copy()
# Custom handler built on top of SimpleHTTPRequestHandler class
class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
# Service GET Request
def do_GET(self):
if 'init' in self.path:
self.send_response(200)
self.end_headers()
self.wfile.write(json.dumps(settings))
else:
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
# Service POST Request
def do_POST(self):
try:
# Load message
body = json.loads(self.rfile.read(int(self.headers['Content-Length'])))
except:
print 'WARNING: Invalid POST message received:', body
return
# Send response
self.send_response(200)
self.end_headers()
# Update server settings
update_settings(body, settings)
# Send new settings to controller
server_pipe.send(body)
# Log each request
# def log_message(self, format, *args):
# return
# Controller process
def controller(ctrl_pipe):
# Control loop
while True:
changed_setting = None
# Read messages from pipe
if ctrl_pipe.poll():
msg = ctrl_pipe.recv()
update_settings(msg, settings)
changed_setting = msg.keys()[0] # Only one setting changed at a time
# Mood mode
if settings['mode'] == 'mood':
# Single type
if settings['type'] == 'single':
if changed_setting == 'color' or changed_setting == 'brightness':
# Set all LEDS to settings['color'], settings['brightness']
print('MODE: MOOD, TYPE: SINGLE; color: ' + str(settings['color']) + ', brightness: ' + str(settings['brightness']))
# Fade type
elif settings['type'] == 'fade':
# Gradient type
elif settings['type'] == 'gradient':
# Beat mode
elif settings['mode'] == 'beat':
# Frequency mode
elif settings['mode'] == 'freq':
# print 'controller running...'
time.sleep(1)
def update_settings(src, dest):
for key in src:
if key in dest:
dest[key] = src[key]
else:
print "WARNING: Invalid setting:", key
# Main method
if __name__ == '__main__':
# Create Pipe
server_pipe, ctrl_pipe = Pipe()
# Spawn child controller process
p = Process(target=controller, args=(ctrl_pipe,))
p.start()
# Run server
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "serving at port", PORT
try:
httpd.serve_forever()
except KeyboardInterrupt:
# Exit cleanly on CTRL-C
httpd.shutdown()
p.terminate()
sys.exit()