From b155ab1eb13e7b31ccbbf6891aa17da3ca2fce84 Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Sun, 17 Nov 2019 17:57:21 +0100 Subject: [PATCH] Implemented UDP protocol This protocol is used for high performance updates of the LEDs. --- include/UDPProto.h | 25 +++++++++++++ src/UDPProto.cpp | 89 ++++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 8 ++++- 3 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 include/UDPProto.h create mode 100644 src/UDPProto.cpp diff --git a/include/UDPProto.h b/include/UDPProto.h new file mode 100644 index 0000000..ff12b0e --- /dev/null +++ b/include/UDPProto.h @@ -0,0 +1,25 @@ +#pragma once + +#include + +class Fader; + +class UDPProto +{ +public: + enum { + SET_COLOUR = 0, + FADE_COLOUR = 1, + ADD_COLOUR = 2, + SET_FADESTEP = 3 + }; + + UDPProto(Fader *fader); + + bool start(uint16_t port); + bool loop(void); + +private: + WiFiUDP m_udpServer; + Fader *m_fader; +}; \ No newline at end of file diff --git a/src/UDPProto.cpp b/src/UDPProto.cpp new file mode 100644 index 0000000..0ef9c45 --- /dev/null +++ b/src/UDPProto.cpp @@ -0,0 +1,89 @@ +#include "Fader.h" + +#include "UDPProto.h" + +#define WS2801_CMD_LEN 7 + +UDPProto::UDPProto(Fader *fader) + : m_fader(fader) +{ +} + +bool UDPProto::start(uint16_t port) +{ + Serial.print("UDPProto: Starting UDP server on port "); + Serial.println(port); + + if(!m_udpServer.begin(port)) { + Serial.println("UDPProto: UDP server start FAILED!"); + return false; + } + + Serial.println("UDPProto: setup done"); + return true; +} + +bool UDPProto::loop(void) +{ + byte cmd[WS2801_CMD_LEN]; + byte action, strip, module, r, g, b, w; + int len; + + int packetSize = m_udpServer.parsePacket(); + if(packetSize) + { + // read the packet into packetBufffer + while((len = m_udpServer.read(cmd, WS2801_CMD_LEN)) == WS2801_CMD_LEN) { + action = cmd[0]; + strip = cmd[1]; + module = cmd[2]; + r = cmd[3]; + g = cmd[4]; + b = cmd[5]; + w = cmd[6]; + + Fader::Color color{r,g,b,w}; + + if(strip >= m_fader->strips()) { + // strip index out of range + continue; + } + + if(module >= m_fader->modules_per_strip()) { + // module index out of range + continue; + } + + switch(action) { + case SET_COLOUR: + m_fader->set_color(strip, module, color); + break; + + case FADE_COLOUR: + m_fader->fade_color(strip, module, color); + break; + + case ADD_COLOUR: + m_fader->add_color(strip, module, color); + break; + + case SET_FADESTEP: + m_fader->set_fadestep(r); // red channel contains the fadestep in this case + break; + + default: + break; + } + } + // send a reply, to the IP address and port that sent us the packet we received + /* + ws2801UDP.beginPacket(ws2801UDP.remoteIP(), ws2801UDP.remotePort()); + ws2801UDP.write("OK\n"); + */ + m_udpServer.endPacket(); + + return true; // processed a packet + } else { + return false; + } +} diff --git a/src/main.cpp b/src/main.cpp index bc2a97e..76c690f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,6 +7,7 @@ #include "WLAN_Logins.h" #include "HTTPServer.h" #include "Fader.h" +#include "UDPProto.h" #include #include @@ -26,7 +27,8 @@ size_t frame; WiFiMulti WiFiMulti; -Fader ledFader(NUM_LEDS); +Fader ledFader(NUM_STRIPS, NUM_LEDS); +UDPProto udpProto(&ledFader); bool initLEDs() { @@ -84,6 +86,8 @@ static void ledTask( void * parameter ) uint32_t start_time = micros(); + udpProto.loop(); + led_idx++; if(led_idx >= STRANDS[0].numPixels) { led_idx = 0; @@ -174,6 +178,8 @@ void setup() HTTPServer::instance().setFader(&ledFader); + udpProto.start(2703); + ledFader.set_color(Fader::Color{64,0,0,0}); ledFader.fade_color(Fader::Color{0,64,0,0});