Commit a8531cd8 authored by Fjen Undso's avatar Fjen Undso
Browse files

lights: support sending to one or many hosts

parent 419c8ddf
#!/usr/bin/env python3
from collections import defaultdict
import socket
import time
UDP_IP = "10.0.0.255"
UDP_PORT = 1234
LED_NUM = 42
TIMEOUT = 1 / 120
SEND_RATE = 1 / 120
VERSION = 2
SEQUENCE = 0
SEQUENCES = defaultdict(int)
SOCKET = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
SOCKET.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
SOCKET.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
def send(data):
"""Send data to the esp listening at UDP_IP:UD_PORT"""
global SEQUENCE
header = bytes([VERSION, SEQUENCE, 0, 0])
SOCKET.sendto(header + data, (UDP_IP, UDP_PORT))
SEQUENCE += 1
if SEQUENCE == 255:
SEQUENCE = 0
time.sleep(TIMEOUT)
def send_broadcast(data):
"""Send data via broadcast"""
send(data, UDP_IP)
def send(data, host, send_rate=SEND_RATE):
"""Send data to the ESP listening at host:UDP_PORT."""
global SEQUENCES
header = bytes([VERSION, SEQUENCES[host], 0, 0])
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
if host.endswith('255'):
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.sendto(header + data, (host, UDP_PORT))
s.close()
SEQUENCES[host] += 1
if SEQUENCES[host] > 255:
SEQUENCES[host] = 0
time.sleep(send_rate)
def send_to_many(data, hosts):
"""Send data to a list of ESPs"""
for h in hosts:
send(data, h, 0)
time.sleep(SEND_RATE)
def pixel2bytes(pixel):
......@@ -50,7 +66,7 @@ def make_stripe_data(pixellist):
if __name__ == '__main__':
colors = make_stripe_data([0, 255, 255, 0] * 24)
SEQUENCE=0
SEQUENCE = 0
send(colors)
SEQUENCE=128
SEQUENCE = 128
send(colors)
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