esp8266-ws2801d/src/ws2801_udp.cpp

96 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
};
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();
}
}