Commit 64d07cf8 authored by Fjen Undso's avatar Fjen Undso
Browse files

lights: send packet with hostids via broadcast

parent eb553edc
......@@ -11,6 +11,7 @@ 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_broadcast(data):
......@@ -22,7 +23,7 @@ def send_broadcast(data):
send(data, UDP_IP)
def send(data, host, send_rate=SEND_RATE):
def send(data, send_rate=SEND_RATE, host_id=0):
"""Send data to the ESP listening at host:UDP_PORT.
Args:
......@@ -31,28 +32,23 @@ def send(data, host, send_rate=SEND_RATE):
send_rate (float): the time in seconds to wait after sending.
"""
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
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, hosts):
def send_to_many(data, host_ids):
"""Send data to a list of hosts.
Args:
data (bytes): the data to send.
hosts: the host names or IPs to send the data to.
host_ids: the host IDs to send the data to.
"""
for h in hosts:
send(data, h, 0)
time.sleep(SEND_RATE)
for h in host_ids:
send(data, host_id=h)
def pixel2bytes(pixel):
......
......@@ -81,7 +81,7 @@ def main(stdscr):
toggle_key=' ',
toggle_key_unfocused=str(i),
text='ESP({})'.format(i),
name='ESP{}'.format(i)) for i in [1, 3, 5, 7]]
name=i) for i in [1, 3, 5, 7]]
checkboxes_bottom = [
CheckBox(
window=stdscr,
......@@ -90,7 +90,7 @@ def main(stdscr):
toggle_key=' ',
toggle_key_unfocused=str(i),
text='ESP({})'.format(i),
name='ESP{}'.format(i)) for i in [2, 4, 6, 8]]
name=i) for i in [2, 4, 6, 8]]
fs_cb = FocusSwitcher(
elements=checkboxes_top + checkboxes_bottom,
prev_key='KEY_LEFT',
......
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