53 lines
1.1 KiB
Python
Executable File
53 lines
1.1 KiB
Python
Executable File
#!/usr/bin/python3
|
|
import socket
|
|
import re
|
|
import Adafruit_WS2801
|
|
import Adafruit_GPIO.SPI as SPI
|
|
import RPi.GPIO as GPIO
|
|
|
|
|
|
PIXEL_COUNT = 32
|
|
|
|
SPI_PORT = 0
|
|
SPI_DEVICE = 0
|
|
pixels = Adafruit_WS2801.WS2801Pixels(PIXEL_COUNT, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE), gpio=GPIO)
|
|
|
|
def hex_to_rgb(hex):
|
|
r = int(hex[0:2],16)
|
|
g = int(hex[2:4],16)
|
|
b = int(hex[4:6],16)
|
|
return r,g,b
|
|
|
|
|
|
reg = re.compile("[0-9a-f]{6}$")
|
|
|
|
host = "0.0.0.0"
|
|
port = 5000
|
|
|
|
s = socket.socket()
|
|
s.bind((host, port))
|
|
s.listen(1)
|
|
|
|
while True:
|
|
conn, address = s.accept()
|
|
print("Connection from: ", str(address))
|
|
|
|
while True:
|
|
data = conn.recv(1024).decode()
|
|
if not data:
|
|
print("Disconneted: ", str(address))
|
|
break
|
|
|
|
if reg.match(data):
|
|
print("set color", data)
|
|
for i in range(PIXEL_COUNT):
|
|
pixels.set_pixel_rgb(i, *hex_to_rgb(data))
|
|
pixels.show()
|
|
|
|
elif data == 'PING':
|
|
conn.send('PONG'.encode())
|
|
else:
|
|
print(data, "incorrect format. use hex color code.")
|
|
break
|
|
conn.close()
|