|
#!/usr/bin/env python3 |
|
|
|
import math |
|
import asyncio |
|
import websockets |
|
import neopixel |
|
|
|
# Neo configuration: |
|
LED_COUNT = 60 # Number of LED pixels. |
|
LED_PIN = 18 # GPIO pin connected to the pixels (must support PWM!). |
|
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz) |
|
LED_DMA = 5 # DMA channel to use for generating signal (try 5) |
|
LED_BRIGHTNESS = 100 # Set to 0 for darkest and 255 for brightest |
|
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift) |
|
|
|
neo = neopixel.Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS) |
|
neo.begin() |
|
|
|
def spread(strip, length): |
|
return list(map(lambda x: strip[math.floor(x / (length / len(strip)))], range(length))); |
|
|
|
async def router(websocket, path): |
|
while True: |
|
payload = await websocket.recv() |
|
decoded = payload.split('#') |
|
command = None |
|
args = None |
|
|
|
if (len(decoded) == 1): |
|
command = decoded[0] |
|
args = '' |
|
elif (len(decoded) == 2): |
|
command = decoded[0] |
|
args = decoded[1] |
|
|
|
if (command == None or command == ''): |
|
print('Invalid Payload: ', payload) |
|
continue |
|
|
|
print(command, args) |
|
|
|
# Python WHY U HAZ NO SWITCH |
|
if command == 'color': |
|
for led, color in enumerate(spread(args.split(','), LED_COUNT)): |
|
neo.setPixelColor(led, int(color)) |
|
neo.show() |
|
elif command == 'brightness': |
|
neo.setBrightness(int(args)) |
|
elif command == 'clear': |
|
for led, color in enumerate(spread([0], LED_COUNT)): |
|
neo.setPixelColor(led, int(color)) |
|
neo.show() |
|
else: |
|
print('Command Not Recognised') |
|
|
|
start_server = websockets.serve(router, '0.0.0.0', 8765) |
|
|
|
asyncio.get_event_loop().run_until_complete(start_server) |
|
asyncio.get_event_loop().run_forever() |