-
Notifications
You must be signed in to change notification settings - Fork 9
/
ws281x.py
336 lines (264 loc) · 10.1 KB
/
ws281x.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
#!/usr/bin/env python3
import json
import multiprocessing
import os
import paho.mqtt.client as paho
import time
from rpi_ws281x import Color
from rpi_ws281x import Adafruit_NeoPixel, ws
from effects.utils.utils import *
from effects.theater_chase_rainbow import effect_theater_chase_rainbow
from effects.rainbow_cycle import effect_rainbow_cycle
from effects.solid import effect_solid
from effects.knight_rider import effect_knight_rider
LED_GPIO = os.getenv('LED_GPIO', 18)
LED_COUNT = os.getenv('LED_COUNT', 10)
LED_CHANNEL = os.getenv('LED_CHANNEL', 0)
LED_FREQ_HZ = os.getenv('LED_FREQ_HZ', 800000)
LED_DMA_NUM = os.getenv('LED_DMA_NUM', 10)
LED_BRIGHTNESS = os.getenv('LED_BRIGHTNESS', 255)
LED_INVERT = os.getenv('LED_INVERT', 0)
LED_STRIP_TYPE = os.getenv('LED_STRIP_TYPE', 'GRB').upper()
MQTT_BROKER = os.getenv('MQTT_BROKER', 'localhost')
MQTT_USER = os.getenv('MQTT_USER', None)
MQTT_PASSWORD = os.getenv('MQTT_PASSWORD', None)
MQTT_PORT = os.getenv('MQTT_PORT', 1883)
MQTT_QOS = os.getenv('MQTT_QOS', 1)
MQTT_ID = os.getenv('MQTT_ID', 'rpi-ws281x')
MQTT_PREFIX = os.getenv('MQTT_PREFIX', 'rpi-ws281x')
MQTT_DISCOVERY_PREFIX = os.getenv('MQTT_DISCOVERY_PREFIX',
'homeassistant')
MQTT_STATUS_TOPIC = '%s/alive' % MQTT_PREFIX
MQTT_STATE_TOPIC = '%s/state' % MQTT_PREFIX
MQTT_COMMAND_TOPIC = '%s/command' % MQTT_PREFIX
MQTT_CONFIG_TOPIC = '%s/light/%s/config' % (MQTT_DISCOVERY_PREFIX,
MQTT_PREFIX)
MQTT_PAYLOAD_ONLINE = '1'
MQTT_PAYLOAD_OFFLINE = '0'
# global states
current = {
'state': 'OFF',
'color': {'r': 255, 'g': 255, 'b': 255},
'brightness': 255,
'effect': 'effect_solid'
}
# worker process that maintains running effects
effect_process = None
effect_active = False
# key is actually a function name
effects_list = {
'effects': {
'effect_theater_chase_rainbow': 'Theater Rainbow',
'effect_rainbow_cycle': 'Rainbow'
},
'color_effects': {
'effect_solid': 'Solid',
'effect_knight_rider': 'Knight Rider'
}
}
strip_type_by_name = {
"BGR": ws.WS2811_STRIP_BGR,
"BRG": ws.WS2811_STRIP_BRG,
"GBR": ws.WS2811_STRIP_GBR,
"GRB": ws.WS2811_STRIP_GRB,
"RBG": ws.WS2811_STRIP_RBG,
"RGB": ws.WS2811_STRIP_RGB,
}
# error checking
LED_CHANNEL = int(LED_CHANNEL)
LED_COUNT = int(LED_COUNT)
LED_FREQ_HZ = int(LED_FREQ_HZ)
LED_DMA_NUM = int(LED_DMA_NUM)
LED_GPIO = int(LED_GPIO)
LED_BRIGHTNESS = int(LED_BRIGHTNESS)
LED_INVERT = int(LED_INVERT)
LED_STRIP_TYPE = strip_type_by_name.get(LED_STRIP_TYPE)
if LED_COUNT is None:
raise ValueError('LED_COUNT is required env parameter')
if LED_GPIO is None:
raise ValueError('LED_GPIO is required env parameter')
if not 1 <= LED_BRIGHTNESS <= 255:
raise ValueError('LED_BRIGHTNESS must be between 1-255')
if LED_FREQ_HZ != 800000 and LED_FREQ_HZ != 400000:
raise ValueError('LED_FREQ_HZ must be 800khz or 400khz')
if LED_DMA_NUM > 14 or LED_DMA_NUM < 0:
raise ValueError('LED_DMA_NUM must be between 0-14')
if LED_STRIP_TYPE is None:
raise ValueError('LED_STRIP_TYPE must be one of %s', ', '.join(strip_type_by_name.keys()))
def effect_list_string():
global effects_list
ret = []
for effect_name in effects_list['effects'].values():
ret.append(effect_name)
for effect_name in effects_list['color_effects'].values():
ret.append(effect_name)
return ret
def get_fn(name):
for (effect_fn, effect_name) in effects_list['effects'].items():
if effect_name == name:
return effect_fn
for (effect_fn, effect_name) in effects_list['color_effects'].items():
if effect_name == name:
return effect_fn
return None
def get_fn_pretty(fn):
if effects_list['effects'].get(fn) is not None:
return effects_list['effects'].get(fn)
if effects_list['color_effects'].get(fn) is not None:
return effects_list['color_effects'].get(fn)
return None
def on_mqtt_message(mqtt, data, message):
payload = json.loads(str(message.payload.decode('utf-8')))
print('Message received ', payload)
global current, effect_active, effect_process
response = {}
if payload['state'] == 'ON' or payload['state'] == 'OFF':
if current['state'] != payload['state']:
print("Turning %s" % payload['state'])
# set global state
current['state'] = payload['state']
# terminate active effect
if effect_active:
effect_process.terminate()
effect_active = False
# power on led strip
if current['state'] == 'ON':
# extract fields from payload
if 'effect' in payload:
fn = get_fn(payload['effect'])
if fn is None:
response['error'] = "Unsupported effect '%s'" % payload['effect']
else:
# set global efect
current['effect'] = fn
if 'brightness' in payload:
if 0 <= payload['brightness'] <= 255:
# set global brightness
current['brightness'] = payload['brightness']
else:
response['error'] = "Invalid brightness '%u'" % payload['brightness']
if 'color' in payload:
if ('r' in payload['color'] and 0 <= payload['color']['r'] <= 255) \
and ('g' in payload['color'] and 0 <= payload['color']['g'] <= 255) \
and ('b' in payload['color'] and 0 <= payload['color']['b'] <= 255):
# set global color
current['color'] = payload['color']
else:
response['error'] = "Invalid color payload"
response['effect'] = get_fn_pretty(current['effect'])
response['brightness'] = current['brightness']
response['color'] = current['color']
# efects with color
if current['effect'] in effects_list['color_effects']:
print('Setting new color effect: "%s"' %
get_fn_pretty(current['effect']))
effect_process = \
multiprocessing.Process(target=loop_function_call, args=(
current['effect'], strip, current['color'], current['brightness']))
effect_process.start()
effect_active = True
# efects not dependant on the color
elif current['effect'] in effects_list['effects']:
print('Setting new effect: "%s"' %
get_fn_pretty(current['effect']))
effect_process = \
multiprocessing.Process(target=loop_function_call,
args=(current['effect'], strip, 30))
effect_process.start()
effect_active = True
else:
response['error'] = \
'Invalid request: A color or a valid effect has to be provided'
else:
set_all_leds_color(strip, 0x000000)
response['state'] = current['state']
else:
response['state'] = 'none'
response['error'] = "Invalid request: Missing/invalid 'state' field"
if 'error' in response:
print(response['error'])
current['state'] = 'OFF'
response['state'] = current['state']
response = json.dumps(response)
mqtt.publish(MQTT_STATE_TOPIC, payload=response, qos=MQTT_QOS,
retain=True)
def on_mqtt_connect(mqtt, userdata, flags, rc):
if rc == 0:
print('MQTT connected')
discovery_data = json.dumps({
'name': MQTT_ID,
'schema': 'json',
'command_topic': MQTT_COMMAND_TOPIC,
'state_topic': MQTT_STATE_TOPIC,
'availability_topic': MQTT_STATUS_TOPIC,
'payload_available': MQTT_PAYLOAD_ONLINE,
'payload_not_available': MQTT_PAYLOAD_OFFLINE,
'qos': MQTT_QOS,
'brightness': True,
'rgb': True,
'color_temp': False,
'effect': True,
'effect_list': effect_list_string(),
'optimistic': False,
'unique_id': MQTT_ID,
})
mqtt.subscribe(MQTT_COMMAND_TOPIC)
mqtt.publish(MQTT_STATUS_TOPIC, payload=MQTT_PAYLOAD_ONLINE,
qos=MQTT_QOS, retain=True)
mqtt.publish(MQTT_CONFIG_TOPIC, payload=discovery_data,
qos=MQTT_QOS, retain=True)
if current['state'] == 'ON':
response = {
'state': current['state'],
'color': current['color'],
'effect': get_fn_pretty(current['effect']),
'brightness': current['brightness']
}
else:
response = {'state': current['state']}
response = json.dumps(response)
mqtt.publish(MQTT_STATE_TOPIC, payload=response, qos=MQTT_QOS,
retain=True)
else:
print('MQTT connect failed:', rc)
print('Setting up %d LEDS on pin %d' % (LED_COUNT, LED_GPIO))
# Create NeoPixel object with appropriate configuration.
strip = Adafruit_NeoPixel(
LED_COUNT,
LED_GPIO,
LED_FREQ_HZ,
LED_DMA_NUM,
LED_INVERT,
LED_BRIGHTNESS,
LED_CHANNEL,
LED_STRIP_TYPE
)
# Intialize the library (must be called once before other functions).
strip.begin()
set_all_leds_color(strip, 0x000000)
mqtt = paho.Client(MQTT_ID)
mqtt.on_message = on_mqtt_message
mqtt.on_connect = on_mqtt_connect
mqtt.will_set(MQTT_STATUS_TOPIC, payload=MQTT_PAYLOAD_OFFLINE,
qos=MQTT_QOS, retain=True)
mqtt.username_pw_set(MQTT_USER, MQTT_PASSWORD)
mqtt.connect(MQTT_BROKER, MQTT_PORT)
mqtt.loop_start()
def loop_function_call(function, *args):
while True:
if isinstance(function, str):
globals()[function](*args)
else:
function(*args)
try:
loop_function_call(time.sleep, 0.1)
except KeyboardInterrupt:
pass
finally:
set_all_leds_color(strip, 0x000000)
mqtt.disconnect()
mqtt.loop_stop()
try:
effect_process.terminate()
except AttributeError:
pass