esp8266-ws2801d/src/ws2801_udp.cpp

95 lines
1.9 KiB
C++

#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,
SET_NUM_MODULES = 4
};
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");
}
bool ws2801_udp_loop(void)
{
byte cmd[WS2801_CMD_LEN];
byte action, module, r, g, b;
int len;
int packetSize = ws2801UDP.parsePacket();
if(packetSize)
{
// 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 >= ws2801_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;
case SET_NUM_MODULES:
// red and green channels contain the number of modules, big endian
ws2801_set_num_modules(((uint32_t)r << 16) + ((uint32_t)g << 8) + b);
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();
return true; // processed a packet
} else {
return false;
}
}