From 4d10491e6e2caea47c2e93c127f2fbeedd7291b3 Mon Sep 17 00:00:00 2001 From: Fjen Undso Date: Tue, 23 Jan 2018 22:23:57 +0100 Subject: [PATCH] esp: add hostid in data header --- README.md | 9 ++++++--- esp/main.py | 23 +++++++++++++++++++++-- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 9a2877a..fde558c 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ The node listens on udp port 1234 and expects packets of following layout: - [VER:8][SEQ:8][ OFFSET:16 ] + [VER:8][SEQ:8][ID:8 ][RES:8] [ PIXEL:32*n ] [ ... ] [ ... ] @@ -19,8 +19,11 @@ The node listens on udp port 1234 and expects packets of following layout: - value: 2 - SEQ: sequence number - this should be incremented for each datagram - - OFFSET: (ignored/WIP) defines where the first pixel should be drawn + - ID: receiver address id + - ID of the node is the number in its hostname. can be 0 to address all + - RES: reserved for future use. + - (WIP/idea) defines where the first pixel should be drawn - default: 0 - - PIXEL: colors in grbw format + - PIXEL: colors in GRBW format - 1 <= n <= pixel count diff --git a/esp/main.py b/esp/main.py index 321544d..1160b97 100644 --- a/esp/main.py +++ b/esp/main.py @@ -2,13 +2,17 @@ import socket import machine import neopixel import utime +import wifi_config + +# config PORT = 1234 SIGNAL_LED_PIN = 2 LED_PIN = 0 LED_NUM = 42 -LED_PIXEL = 4 # pixel per led (3 or 4) +LED_PIXEL = 4 # pixel per led (4 for RGBW) +ESP_ID = int(''.join(c for c in wifi_config.HOSTNAME if c.isdigit())) machine.freq(160000000) gamma8 = [ @@ -102,9 +106,24 @@ class UDPled: @micropython.native def run_fast_seq(self): - """Receives SEQ|GRBW|... as a UDP packet. Must contain all LEDs""" + """Receives a UDP packet with header. Must contain all LEDs. + + Packet format: + [VER:8][SEQ:8][ID:8 ][RES:8] + [ PIXEL:4*8 ] + [ ... ] + + VER: protocol version number (currently 2) + SEQ: sequence number + ID: the host id of this ESP. Ignored if 0 + RES: reserved for future use + PIXEL: 8bit GRBW values for a pixel + """ while True: self.sock.readinto(self.udp_buffer) + if self.udp_buffer[2] not in [0, ESP_ID]: + # ignore packet if it's not for us or everyone + continue if (self.udp_buffer[0] == 2 and (self.udp_buffer[1] - self.udp_seq)%256 <= 128): self.udp_seq = self.udp_buffer[1] self.np.buf = self.udp_buffer[4:] -- GitLab