Commit 904672c6 authored by Fjen Undso's avatar Fjen Undso
Browse files

use pystribs library

parent c01ad1b3
[submodule "pystrips"]
path = pystrips
url = https://git.finf.uni-hannover.de/honeypot/pystrips.git
#!/usr/bin/env python3
from collections import defaultdict
import socket
import time
UDP_IP = "10.0.0.255"
UDP_PORT = 1234
LED_NUM = 42
SEND_RATE = 1 / 120
VERSION = 2
SEQUENCES = defaultdict(int)
SOCKET = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
SOCKET.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
SOCKET.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
def send(data, send_rate=SEND_RATE, host_id=0):
"""Send data to the ESP listening at host:UDP_PORT.
Args:
data (bytes): the data to send.
host (string): the host name or IP to send the data to. Default 0 (all)
send_rate (float): the time in seconds to wait after sending.
"""
global SEQUENCES
header = bytes([VERSION, SEQUENCES[host_id], host_id, 0])
SOCKET.sendto(header + data, (UDP_IP, UDP_PORT))
SEQUENCES[host_id] += 128
if SEQUENCES[host_id] > 255:
SEQUENCES[host_id] = 0
time.sleep(send_rate)
def send_to_many(data, host_ids):
"""Send data to a list of hosts.
Args:
data (bytes): the data to send.
host_ids: the host IDs to send the data to.
"""
for h in host_ids:
send(data, host_id=h)
def pixel2bytes(pixel):
"""Convert rgbw color values to bytes. Limit values to range(0,255)
Args:
pixel: list of color values.
"""
out = []
for color in pixel:
if color < 0:
color = 0
if color > 255:
color = 255
out.append(color)
return bytes(out)
def make_stripe_data(pixellist):
"""Convert a list of rgbw color values to bytes.
make sure it matches the led strip length.
Args:
pixellist: list of color values
"""
assert(len(pixellist) % 4 == 0)
if len(pixellist) > LED_NUM * 4:
pixellist = pixellist[:LED_NUM * 4]
if len(pixellist) < LED_NUM * 4:
print("!!! Filling missing pixels")
missing_pixels = (LED_NUM - int(len(pixellist) / 4)) * 4
pixellist += missing_pixels * [0]
return pixel2bytes(pixellist)
def test(colors):
"""Test colors by bruteforcing sequence numbers."""
for i in range(2):
send(colors)
if __name__ == '__main__':
test(make_stripe_data([255, 0, 0, 0] * LED_NUM))
time.sleep(0.5)
test(make_stripe_data([0, 255, 0, 0] * LED_NUM))
time.sleep(0.5)
test(make_stripe_data([0, 0, 255, 0] * LED_NUM))
time.sleep(0.5)
test(make_stripe_data([0, 0, 0, 255] * LED_NUM))
......@@ -2,7 +2,7 @@
import logging
import curses
from curses import wrapper
import lights
from pystrips import LEDStrip
from gui.curseshandler import CursesHandler
from gui.numberrange import NumberRange
from gui.checkbox import CheckBox
......@@ -17,6 +17,7 @@ def main(stdscr):
scr_center_x = max_x // 2
# hide cursor
curses.curs_set(0)
curses.mousemask(1)
# init colors
curses.start_color()
curses.use_default_colors()
......@@ -111,6 +112,9 @@ def main(stdscr):
fs_main.toggle_focus()
stdscr.refresh()
strips = [LEDStrip(host_id=i) for i in range(1, 9)]
strips_all = LEDStrip()
while True:
k = stdscr.getkey()
logger.info(k)
......@@ -119,25 +123,23 @@ def main(stdscr):
# quit
if k == 'q':
break
# all off:
if k == 'o':
colors = lights.make_stripe_data([0, 0, 0, 0] * 42)
lights.send(colors)
strips_all.set_all([0] * 4)
strips_all.send()
continue
# all white
if k == 'w':
colors = lights.make_stripe_data([0, 255, 100, 255] * 42)
lights.send(colors)
strips_all.set_all([0, 255, 100, 255])
strips_all.send()
continue
# send values to ESPs
if k == 's' or continous.active:
colors = lights.make_stripe_data(
[numbers[1].value, numbers[0].value, numbers[2].value, numbers[3].value] * 42)
esps = [e.name for e in checkboxes_top + checkboxes_bottom if e.active]
lights.send_to_many(colors, esps)
color = [numbers[i].value for i in range(1, 4)]
for e in checkboxes_top + checkboxes_bottom:
if e.active:
strips[e.name].set_all(color)
strips[e.name].send()
if __name__ == '__main__':
......
Subproject commit 3ef088e61b55930428223f2c1440b062c5972a1a
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment