Implemented ws2801 UDP protocol

This commit is contained in:
Thomas Kolb 2017-03-04 21:22:48 +01:00
parent 91fac56f88
commit a862d0e9ff
3 changed files with 113 additions and 47 deletions

View File

@ -4,24 +4,23 @@
* the most obvious difference being the different file you need to include:
*/
#include <Arduino.h>
#include <WiFiUdp.h>
#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();
}

95
src/ws2801_udp.cpp Normal file
View File

@ -0,0 +1,95 @@
#include <WiFiUdp.h>
#include <Arduino.h>
#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();
}
}

7
src/ws2801_udp.h Normal file
View File

@ -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