From a862d0e9ffd52b8a162b4cd8cdf0a5cbd6c84d7b Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Sat, 4 Mar 2017 21:22:48 +0100 Subject: [PATCH] Implemented ws2801 UDP protocol --- src/main.cpp | 58 ++++++---------------------- src/ws2801_udp.cpp | 95 ++++++++++++++++++++++++++++++++++++++++++++++ src/ws2801_udp.h | 7 ++++ 3 files changed, 113 insertions(+), 47 deletions(-) create mode 100644 src/ws2801_udp.cpp create mode 100644 src/ws2801_udp.h diff --git a/src/main.cpp b/src/main.cpp index 3a41384..3ffbbc8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,24 +4,23 @@ * the most obvious difference being the different file you need to include: */ #include -#include #include "ESP8266WiFi.h" #include "relais.h" #include "wlan.h" #include "fader.h" #include "ws2801.h" +#include "ws2801_udp.h" -WiFiUDP udpServer; - -#define UDP_PORT 2703 - -#define NWLANFAV 2 +#define NWLANFAV 5 const char *favorite_wlans[2*NWLANFAV] = { // SSID, password (NULL if open) + "Error-404", "XKZbEF0AiMNPAGf4EbHAAZ", "Error-403", "2b4EWWpqAyCpgOQK1oeD", - "franken.freifunk.net", NULL + "bytewerk", "bluemchenwiese", + "franken.freifunk.net", NULL, + "Freifunk", NULL }; uint32_t fader_next_update = 0; @@ -63,18 +62,13 @@ void setup() ws2801_init(); fader_init(); + ws2801_udp_setup(); + for(int i = 0; i < NUM_MODULES; i++) { fader_set_colour(i, 0, 0, 0); fader_fade_colour(i, 0, 255, 0); } - Serial.print("starting UDP server on port "); - Serial.println(UDP_PORT); - - if(!udpServer.begin(UDP_PORT)) { - Serial.println("UDP server start FAILED!"); - } - Serial.println("Setup done"); } @@ -95,6 +89,7 @@ void loop() fader_next_update += 10; // -> 100 FPS + /* if((fader_loop % 200) == 0) { int round = (fader_loop / 200) % 3; if(round == 0) { @@ -111,43 +106,12 @@ void loop() } } } + */ fader_loop++; } - /*int n = udpServer.read(buf, 512); - - if(n > 0) { - Serial.print("Received UDP packet with "); - Serial.print(n); - Serial.println(" bytes."); - }*/ - - // if there's data available, read a packet - int packetSize = udpServer.parsePacket(); - if(packetSize) - { - Serial.print("Received packet of size "); - Serial.println(packetSize); - Serial.print("From "); - IPAddress remoteIp = udpServer.remoteIP(); - Serial.print(remoteIp); - Serial.print(", port "); - Serial.println(udpServer.remotePort()); - - // read the packet into packetBufffer - int len = udpServer.read(buf,512); - if (len >0) buf[len]=0; - /* - Serial.println("Contents:"); - Serial.println(buf); - */ - - // send a reply, to the IP address and port that sent us the packet we received - udpServer.beginPacket(udpServer.remoteIP(), udpServer.remotePort()); - udpServer.write("OK\n"); - udpServer.endPacket(); - } + ws2801_udp_loop(); relais_loop(); } diff --git a/src/ws2801_udp.cpp b/src/ws2801_udp.cpp new file mode 100644 index 0000000..5ecad4d --- /dev/null +++ b/src/ws2801_udp.cpp @@ -0,0 +1,95 @@ +#include +#include + +#include "ws2801.h" +#include "fader.h" + +#include "ws2801_udp.h" + +WiFiUDP ws2801UDP; + +#define WS2801_UDP_PORT 2703 + +#define WS2801_CMD_LEN 5 + +enum { + SET_COLOUR = 0, + FADE_COLOUR = 1, + ADD_COLOUR = 2, + SET_FADESTEP = 3 +}; + +void ws2801_udp_setup(void) +{ + Serial.print("WS2801UDP: Starting UDP server on port "); + Serial.println(WS2801_UDP_PORT); + + if(!ws2801UDP.begin(WS2801_UDP_PORT)) { + Serial.println("WS2801UDP: UDP server start FAILED!"); + } + + Serial.println("WS2801UDP setup done"); +} + +void ws2801_udp_loop(void) +{ + byte cmd[WS2801_CMD_LEN]; + byte action, module, r, g, b; + int len; + + int packetSize = ws2801UDP.parsePacket(); + if(packetSize) + { + /* + Serial.print("Received packet of size "); + Serial.println(packetSize); + Serial.print("From "); + IPAddress remoteIp = ws2801UDP.remoteIP(); + Serial.print(remoteIp); + Serial.print(", port "); + Serial.println(ws2801UDP.remotePort()); + */ + + // read the packet into packetBufffer + while((len = ws2801UDP.read(cmd, WS2801_CMD_LEN)) == WS2801_CMD_LEN) { + action = cmd[0]; + module = cmd[1]; + r = cmd[2]; + g = cmd[3]; + b = cmd[4]; + + if(module >= NUM_MODULES) { + // module index out of range + continue; + } + + switch(action) { + case SET_COLOUR: + fader_set_colour(module, r, g, b); + break; + + case FADE_COLOUR: + fader_fade_colour(module, r, g, b); + break; + + case ADD_COLOUR: + fader_add_colour(module, r, g, b); + break; + + case SET_FADESTEP: + fader_set_fadestep(r); // red channel contains the fadestep in this case + break; + + default: + //Serial.println("WS2801UDP: ERROR: invalid action received!"); + 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"); + */ + ws2801UDP.endPacket(); + } +} diff --git a/src/ws2801_udp.h b/src/ws2801_udp.h new file mode 100644 index 0000000..6c009ae --- /dev/null +++ b/src/ws2801_udp.h @@ -0,0 +1,7 @@ +#ifndef WS2801_UDP_H +#define WS2801_UDP_H + +void ws2801_udp_setup(void); +void ws2801_udp_loop(void); + +#endif // WS2801_UDP_H