2020-03-25 09:00:55 +01:00
|
|
|
#include "wled.h"
|
2020-03-31 02:38:08 +02:00
|
|
|
|
|
|
|
/*
|
2020-05-18 16:36:31 +02:00
|
|
|
* UDP sync notifier / Realtime / Hyperion / TPM2.NET
|
2020-03-31 02:38:08 +02:00
|
|
|
*/
|
2016-12-31 00:38:51 +01:00
|
|
|
|
2022-10-31 07:13:12 +01:00
|
|
|
#define UDP_SEG_SIZE 36
|
2022-01-10 09:03:36 +01:00
|
|
|
#define SEG_OFFSET (41+(MAX_NUM_SEGMENTS*UDP_SEG_SIZE))
|
|
|
|
#define WLEDPACKETSIZE (41+(MAX_NUM_SEGMENTS*UDP_SEG_SIZE)+0)
|
2018-11-01 16:16:38 +01:00
|
|
|
#define UDP_IN_MAXSIZE 1472
|
2021-05-27 02:02:02 +02:00
|
|
|
#define PRESUMED_NETWORK_DELAY 3 //how many ms could it take on avg to reach the receiver? This will be added to transmitted times
|
2018-01-27 23:28:20 +01:00
|
|
|
|
2020-03-25 10:14:23 +01:00
|
|
|
void notify(byte callMode, bool followUp)
|
2016-11-19 19:39:17 +01:00
|
|
|
{
|
2017-05-08 21:46:04 +02:00
|
|
|
if (!udpConnected) return;
|
2021-08-25 16:39:12 +02:00
|
|
|
if (!syncGroups) return;
|
2016-11-19 19:39:17 +01:00
|
|
|
switch (callMode)
|
|
|
|
{
|
2021-07-09 18:54:28 +02:00
|
|
|
case CALL_MODE_INIT: return;
|
|
|
|
case CALL_MODE_DIRECT_CHANGE: if (!notifyDirect) return; break;
|
|
|
|
case CALL_MODE_BUTTON: if (!notifyButton) return; break;
|
2022-01-25 12:47:14 +01:00
|
|
|
case CALL_MODE_BUTTON_PRESET: if (!notifyButton) return; break;
|
2021-07-09 18:54:28 +02:00
|
|
|
case CALL_MODE_NIGHTLIGHT: if (!notifyDirect) return; break;
|
|
|
|
case CALL_MODE_HUE: if (!notifyHue) return; break;
|
|
|
|
case CALL_MODE_PRESET_CYCLE: if (!notifyDirect) return; break;
|
|
|
|
case CALL_MODE_BLYNK: if (!notifyDirect) return; break;
|
|
|
|
case CALL_MODE_ALEXA: if (!notifyAlexa) return; break;
|
2016-11-19 19:39:17 +01:00
|
|
|
default: return;
|
|
|
|
}
|
2018-01-27 23:28:20 +01:00
|
|
|
byte udpOut[WLEDPACKETSIZE];
|
2022-07-06 13:13:54 +02:00
|
|
|
Segment& mainseg = strip.getMainSegment();
|
2017-02-04 23:37:28 +01:00
|
|
|
udpOut[0] = 0; //0: wled notifier protocol 1: WARLS protocol
|
2016-11-27 22:37:51 +01:00
|
|
|
udpOut[1] = callMode;
|
|
|
|
udpOut[2] = bri;
|
2022-02-20 22:24:11 +01:00
|
|
|
uint32_t col = mainseg.colors[0];
|
|
|
|
udpOut[3] = R(col);
|
|
|
|
udpOut[4] = G(col);
|
|
|
|
udpOut[5] = B(col);
|
2016-11-27 22:37:51 +01:00
|
|
|
udpOut[6] = nightlightActive;
|
2016-12-14 21:40:09 +01:00
|
|
|
udpOut[7] = nightlightDelayMins;
|
2022-02-20 22:24:11 +01:00
|
|
|
udpOut[8] = mainseg.mode;
|
|
|
|
udpOut[9] = mainseg.speed;
|
|
|
|
udpOut[10] = W(col);
|
2019-06-20 14:40:12 +02:00
|
|
|
//compatibilityVersionByte:
|
|
|
|
//0: old 1: supports white 2: supports secondary color
|
|
|
|
//3: supports FX intensity, 24 byte packet 4: supports transitionDelay 5: sup palette
|
2021-05-27 02:02:02 +02:00
|
|
|
//6: supports timebase syncing, 29 byte packet 7: supports tertiary color 8: supports sys time sync, 36 byte packet
|
2021-12-17 11:22:20 +01:00
|
|
|
//9: supports sync groups, 37 byte packet 10: supports CCT, 39 byte packet 11: per segment options, variable packet length (40+MAX_NUM_SEGMENTS*3)
|
2022-10-31 07:13:12 +01:00
|
|
|
//12: enhanced effct sliders, 2D & mapping options
|
|
|
|
udpOut[11] = 12;
|
2022-02-20 22:24:11 +01:00
|
|
|
col = mainseg.colors[1];
|
|
|
|
udpOut[12] = R(col);
|
|
|
|
udpOut[13] = G(col);
|
|
|
|
udpOut[14] = B(col);
|
|
|
|
udpOut[15] = W(col);
|
|
|
|
udpOut[16] = mainseg.intensity;
|
2018-03-15 12:04:14 +01:00
|
|
|
udpOut[17] = (transitionDelay >> 0) & 0xFF;
|
|
|
|
udpOut[18] = (transitionDelay >> 8) & 0xFF;
|
2022-02-20 22:24:11 +01:00
|
|
|
udpOut[19] = mainseg.palette;
|
|
|
|
col = mainseg.colors[2];
|
|
|
|
udpOut[20] = R(col);
|
|
|
|
udpOut[21] = G(col);
|
|
|
|
udpOut[22] = B(col);
|
|
|
|
udpOut[23] = W(col);
|
2019-12-13 14:14:36 +01:00
|
|
|
|
2019-10-18 12:19:52 +02:00
|
|
|
udpOut[24] = followUp;
|
|
|
|
uint32_t t = millis() + strip.timebase;
|
|
|
|
udpOut[25] = (t >> 24) & 0xFF;
|
|
|
|
udpOut[26] = (t >> 16) & 0xFF;
|
|
|
|
udpOut[27] = (t >> 8) & 0xFF;
|
|
|
|
udpOut[28] = (t >> 0) & 0xFF;
|
2021-05-27 02:02:02 +02:00
|
|
|
|
|
|
|
//sync system time
|
|
|
|
udpOut[29] = toki.getTimeSource();
|
|
|
|
Toki::Time tm = toki.getTime();
|
|
|
|
uint32_t unix = tm.sec;
|
|
|
|
udpOut[30] = (unix >> 24) & 0xFF;
|
|
|
|
udpOut[31] = (unix >> 16) & 0xFF;
|
|
|
|
udpOut[32] = (unix >> 8) & 0xFF;
|
|
|
|
udpOut[33] = (unix >> 0) & 0xFF;
|
|
|
|
uint16_t ms = tm.ms;
|
|
|
|
udpOut[34] = (ms >> 8) & 0xFF;
|
|
|
|
udpOut[35] = (ms >> 0) & 0xFF;
|
2021-08-25 16:39:12 +02:00
|
|
|
|
|
|
|
//sync groups
|
|
|
|
udpOut[36] = syncGroups;
|
2021-12-17 11:22:20 +01:00
|
|
|
|
2022-01-25 12:47:14 +01:00
|
|
|
//Might be changed to Kelvin in the future, receiver code should handle that case
|
|
|
|
//0: byte 38 contains 0-255 value, 255: no valid CCT, 1-254: Kelvin value MSB
|
|
|
|
udpOut[37] = strip.hasCCTBus() ? 0 : 255; //check this is 0 for the next value to be significant
|
|
|
|
udpOut[38] = mainseg.cct;
|
|
|
|
|
2022-07-17 15:58:41 +02:00
|
|
|
udpOut[39] = strip.getActiveSegmentsNum();
|
2022-01-10 09:03:36 +01:00
|
|
|
udpOut[40] = UDP_SEG_SIZE; //size of each loop iteration (one segment)
|
2022-07-19 22:14:46 +02:00
|
|
|
size_t s = 0, nsegs = strip.getSegmentsNum();
|
|
|
|
for (size_t i = 0; i < nsegs; i++) {
|
2022-07-06 13:13:54 +02:00
|
|
|
Segment &selseg = strip.getSegment(i);
|
2022-07-19 16:16:43 +02:00
|
|
|
if (!selseg.isActive()) continue;
|
|
|
|
uint16_t ofs = 41 + s*UDP_SEG_SIZE; //start of segment offset byte
|
|
|
|
udpOut[0 +ofs] = s;
|
2022-01-25 12:47:14 +01:00
|
|
|
udpOut[1 +ofs] = selseg.start >> 8;
|
|
|
|
udpOut[2 +ofs] = selseg.start & 0xFF;
|
|
|
|
udpOut[3 +ofs] = selseg.stop >> 8;
|
|
|
|
udpOut[4 +ofs] = selseg.stop & 0xFF;
|
2022-01-24 18:31:05 +01:00
|
|
|
udpOut[5 +ofs] = selseg.grouping;
|
|
|
|
udpOut[6 +ofs] = selseg.spacing;
|
|
|
|
udpOut[7 +ofs] = selseg.offset >> 8;
|
|
|
|
udpOut[8 +ofs] = selseg.offset & 0xFF;
|
2022-10-31 07:13:12 +01:00
|
|
|
udpOut[9 +ofs] = selseg.options & 0x8F; //only take into account selected, mirrored, on, reversed, reverse_y (for 2D); ignore freeze, reset, transitional
|
2022-01-24 18:31:05 +01:00
|
|
|
udpOut[10+ofs] = selseg.opacity;
|
|
|
|
udpOut[11+ofs] = selseg.mode;
|
|
|
|
udpOut[12+ofs] = selseg.speed;
|
|
|
|
udpOut[13+ofs] = selseg.intensity;
|
|
|
|
udpOut[14+ofs] = selseg.palette;
|
|
|
|
udpOut[15+ofs] = R(selseg.colors[0]);
|
|
|
|
udpOut[16+ofs] = G(selseg.colors[0]);
|
|
|
|
udpOut[17+ofs] = B(selseg.colors[0]);
|
|
|
|
udpOut[18+ofs] = W(selseg.colors[0]);
|
|
|
|
udpOut[19+ofs] = R(selseg.colors[1]);
|
|
|
|
udpOut[20+ofs] = G(selseg.colors[1]);
|
|
|
|
udpOut[21+ofs] = B(selseg.colors[1]);
|
|
|
|
udpOut[22+ofs] = W(selseg.colors[1]);
|
|
|
|
udpOut[23+ofs] = R(selseg.colors[2]);
|
|
|
|
udpOut[24+ofs] = G(selseg.colors[2]);
|
|
|
|
udpOut[25+ofs] = B(selseg.colors[2]);
|
|
|
|
udpOut[26+ofs] = W(selseg.colors[2]);
|
2022-01-25 12:47:14 +01:00
|
|
|
udpOut[27+ofs] = selseg.cct;
|
2022-10-31 07:13:12 +01:00
|
|
|
udpOut[28+ofs] = (selseg.options>>8) & 0xFF; //mirror_y, transpose, 2D mapping & sound
|
|
|
|
udpOut[29+ofs] = selseg.custom1;
|
|
|
|
udpOut[30+ofs] = selseg.custom2;
|
|
|
|
udpOut[31+ofs] = selseg.custom3 | (selseg.check1<<5) | (selseg.check2<<6) | (selseg.check3<<7);
|
|
|
|
udpOut[32+ofs] = selseg.startY >> 8;
|
|
|
|
udpOut[33+ofs] = selseg.startY & 0xFF;
|
|
|
|
udpOut[34+ofs] = selseg.stopY >> 8;
|
|
|
|
udpOut[35+ofs] = selseg.stopY & 0xFF;
|
2022-07-19 16:16:43 +02:00
|
|
|
++s;
|
2021-12-17 11:22:20 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 12:47:14 +01:00
|
|
|
//uint16_t offs = SEG_OFFSET;
|
|
|
|
//next value to be added has index: udpOut[offs + 0]
|
2022-01-10 09:03:36 +01:00
|
|
|
|
2016-11-27 22:37:51 +01:00
|
|
|
IPAddress broadcastIp;
|
2020-11-13 18:25:13 +01:00
|
|
|
broadcastIp = ~uint32_t(Network.subnetMask()) | uint32_t(Network.gatewayIP());
|
2016-11-19 19:39:17 +01:00
|
|
|
|
2016-11-27 22:37:51 +01:00
|
|
|
notifierUdp.beginPacket(broadcastIp, udpPort);
|
2018-01-27 23:28:20 +01:00
|
|
|
notifierUdp.write(udpOut, WLEDPACKETSIZE);
|
2016-11-27 22:37:51 +01:00
|
|
|
notifierUdp.endPacket();
|
2018-03-06 23:47:08 +01:00
|
|
|
notificationSentCallMode = callMode;
|
|
|
|
notificationSentTime = millis();
|
2022-10-19 01:31:23 +02:00
|
|
|
notificationCount = followUp ? notificationCount + 1 : 0;
|
2016-11-27 22:37:51 +01:00
|
|
|
}
|
|
|
|
|
2020-04-13 00:42:27 +02:00
|
|
|
void realtimeLock(uint32_t timeoutMs, byte md)
|
2018-05-18 23:24:47 +02:00
|
|
|
{
|
2022-04-01 00:59:19 +02:00
|
|
|
if (!realtimeMode && !realtimeOverride) {
|
2022-03-25 16:36:05 +01:00
|
|
|
uint16_t stop, start;
|
|
|
|
if (useMainSegmentOnly) {
|
2022-07-06 13:13:54 +02:00
|
|
|
Segment& mainseg = strip.getMainSegment();
|
2022-03-25 16:36:05 +01:00
|
|
|
start = mainseg.start;
|
|
|
|
stop = mainseg.stop;
|
2022-08-19 21:37:26 +02:00
|
|
|
mainseg.freeze = true;
|
2022-03-25 16:36:05 +01:00
|
|
|
} else {
|
|
|
|
start = 0;
|
|
|
|
stop = strip.getLengthTotal();
|
|
|
|
}
|
|
|
|
// clear strip/segment
|
2022-08-03 21:36:47 +02:00
|
|
|
for (size_t i = start; i < stop; i++) strip.setPixelColor(i,BLACK);
|
2022-04-01 00:59:19 +02:00
|
|
|
// if WLED was off and using main segment only, freeze non-main segments so they stay off
|
2022-03-25 16:36:05 +01:00
|
|
|
if (useMainSegmentOnly && bri == 0) {
|
2022-07-19 22:14:46 +02:00
|
|
|
for (size_t s=0; s < strip.getSegmentsNum(); s++) {
|
2022-08-19 21:37:26 +02:00
|
|
|
strip.getSegment(s).freeze = true;
|
2022-03-25 16:36:05 +01:00
|
|
|
}
|
2019-03-11 00:20:17 +01:00
|
|
|
}
|
2018-05-18 23:24:47 +02:00
|
|
|
}
|
2021-10-02 10:48:48 +02:00
|
|
|
// if strip is off (bri==0) and not already in RTM
|
2022-04-01 00:59:19 +02:00
|
|
|
if (briT == 0 && !realtimeMode && !realtimeOverride) {
|
2022-03-10 20:40:48 +01:00
|
|
|
strip.setBrightness(scaledBri(briLast), true);
|
2021-10-02 10:48:48 +02:00
|
|
|
}
|
2022-04-01 00:59:19 +02:00
|
|
|
|
|
|
|
if (realtimeTimeout != UINT32_MAX) {
|
|
|
|
realtimeTimeout = (timeoutMs == 255001 || timeoutMs == 65000) ? UINT32_MAX : millis() + timeoutMs;
|
|
|
|
}
|
2020-04-30 01:52:36 +02:00
|
|
|
realtimeMode = md;
|
|
|
|
|
2022-04-01 00:59:19 +02:00
|
|
|
if (realtimeOverride) return;
|
|
|
|
if (arlsForceMaxBri) strip.setBrightness(scaledBri(255), true);
|
2022-03-10 20:40:48 +01:00
|
|
|
if (briT > 0 && md == REALTIME_MODE_GENERIC) strip.show();
|
2018-08-11 22:57:13 +02:00
|
|
|
}
|
|
|
|
|
2022-04-01 00:59:19 +02:00
|
|
|
void exitRealtime() {
|
|
|
|
if (!realtimeMode) return;
|
|
|
|
if (realtimeOverride == REALTIME_OVERRIDE_ONCE) realtimeOverride = REALTIME_OVERRIDE_NONE;
|
|
|
|
strip.setBrightness(scaledBri(bri));
|
|
|
|
realtimeTimeout = 0; // cancel realtime mode immediately
|
|
|
|
realtimeMode = REALTIME_MODE_INACTIVE; // inform UI immediately
|
|
|
|
realtimeIP[0] = 0;
|
|
|
|
if (useMainSegmentOnly) { // unfreeze live segment again
|
2022-08-19 21:37:26 +02:00
|
|
|
strip.getMainSegment().freeze = false;
|
2022-04-01 00:59:19 +02:00
|
|
|
}
|
2022-04-08 20:11:34 +02:00
|
|
|
updateInterfaces(CALL_MODE_WS_SEND);
|
2022-04-01 00:59:19 +02:00
|
|
|
}
|
|
|
|
|
2018-11-24 11:52:23 +01:00
|
|
|
|
2020-05-18 16:36:31 +02:00
|
|
|
#define TMP2NET_OUT_PORT 65442
|
|
|
|
|
|
|
|
void sendTPM2Ack() {
|
|
|
|
notifierUdp.beginPacket(notifierUdp.remoteIP(), TMP2NET_OUT_PORT);
|
|
|
|
uint8_t response_ack = 0xac;
|
|
|
|
notifierUdp.write(&response_ack, 1);
|
|
|
|
notifierUdp.endPacket();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-27 22:37:51 +01:00
|
|
|
void handleNotifications()
|
|
|
|
{
|
2021-09-19 19:51:54 +02:00
|
|
|
IPAddress localIP;
|
|
|
|
|
2018-05-18 23:24:47 +02:00
|
|
|
//send second notification if enabled
|
2022-10-19 01:31:23 +02:00
|
|
|
if(udpConnected && (notificationCount < udpNumRetries) && ((millis()-notificationSentTime) > 250)){
|
2018-03-06 23:47:08 +01:00
|
|
|
notify(notificationSentCallMode,true);
|
|
|
|
}
|
2020-09-27 11:37:16 +02:00
|
|
|
|
2019-11-18 20:43:27 +01:00
|
|
|
if (e131NewData && millis() - strip.getLastShow() > 15)
|
|
|
|
{
|
|
|
|
e131NewData = false;
|
|
|
|
strip.show();
|
|
|
|
}
|
2018-08-11 22:57:13 +02:00
|
|
|
|
2018-05-18 23:24:47 +02:00
|
|
|
//unlock strip when realtime UDP times out
|
2022-04-01 00:59:19 +02:00
|
|
|
if (realtimeMode && millis() > realtimeTimeout) exitRealtime();
|
2018-05-18 23:24:47 +02:00
|
|
|
|
|
|
|
//receive UDP notifications
|
2020-12-02 22:47:31 +01:00
|
|
|
if (!udpConnected) return;
|
2018-11-09 17:00:36 +01:00
|
|
|
|
2020-09-27 11:43:28 +02:00
|
|
|
bool isSupp = false;
|
2022-07-19 22:14:46 +02:00
|
|
|
size_t packetSize = notifierUdp.parsePacket();
|
2020-09-27 11:43:28 +02:00
|
|
|
if (!packetSize && udp2Connected) {
|
|
|
|
packetSize = notifier2Udp.parsePacket();
|
|
|
|
isSupp = true;
|
|
|
|
}
|
2018-05-18 23:24:47 +02:00
|
|
|
|
2018-11-09 17:00:36 +01:00
|
|
|
//hyperion / raw RGB
|
|
|
|
if (!packetSize && udpRgbConnected) {
|
|
|
|
packetSize = rgbUdp.parsePacket();
|
2020-09-27 11:37:16 +02:00
|
|
|
if (packetSize) {
|
|
|
|
if (!receiveDirect) return;
|
|
|
|
if (packetSize > UDP_IN_MAXSIZE || packetSize < 3) return;
|
|
|
|
realtimeIP = rgbUdp.remoteIP();
|
|
|
|
DEBUG_PRINTLN(rgbUdp.remoteIP());
|
|
|
|
uint8_t lbuf[packetSize];
|
|
|
|
rgbUdp.read(lbuf, packetSize);
|
|
|
|
realtimeLock(realtimeTimeoutMs, REALTIME_MODE_HYPERION);
|
2022-03-25 16:36:05 +01:00
|
|
|
if (realtimeOverride && !(realtimeMode && useMainSegmentOnly)) return;
|
2020-09-27 11:37:16 +02:00
|
|
|
uint16_t id = 0;
|
2021-10-31 11:57:41 +01:00
|
|
|
uint16_t totalLen = strip.getLengthTotal();
|
2022-07-19 22:14:46 +02:00
|
|
|
for (size_t i = 0; i < packetSize -2; i += 3)
|
2020-09-27 11:37:16 +02:00
|
|
|
{
|
|
|
|
setRealtimePixel(id, lbuf[i], lbuf[i+1], lbuf[i+2], 0);
|
2021-10-31 11:57:41 +01:00
|
|
|
id++; if (id >= totalLen) break;
|
2020-09-27 11:37:16 +02:00
|
|
|
}
|
2022-03-25 16:36:05 +01:00
|
|
|
if (!(realtimeMode && useMainSegmentOnly)) strip.show();
|
2020-09-27 11:37:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-12-02 22:47:31 +01:00
|
|
|
|
|
|
|
if (!(receiveNotifications || receiveDirect)) return;
|
2020-09-27 11:37:16 +02:00
|
|
|
|
2021-09-19 19:51:54 +02:00
|
|
|
localIP = Network.localIP();
|
2018-11-09 17:00:36 +01:00
|
|
|
//notifier and UDP realtime
|
2020-05-18 16:36:31 +02:00
|
|
|
if (!packetSize || packetSize > UDP_IN_MAXSIZE) return;
|
2021-09-19 19:51:54 +02:00
|
|
|
if (!isSupp && notifierUdp.remoteIP() == localIP) return; //don't process broadcasts we send ourselves
|
2018-11-21 23:28:20 +01:00
|
|
|
|
2020-09-27 11:43:28 +02:00
|
|
|
uint8_t udpIn[packetSize +1];
|
2021-01-22 16:17:18 +01:00
|
|
|
uint16_t len;
|
|
|
|
if (isSupp) len = notifier2Udp.read(udpIn, packetSize);
|
|
|
|
else len = notifierUdp.read(udpIn, packetSize);
|
|
|
|
|
|
|
|
// WLED nodes info notifications
|
|
|
|
if (isSupp && udpIn[0] == 255 && udpIn[1] == 1 && len >= 40) {
|
2021-09-19 19:51:54 +02:00
|
|
|
if (!nodeListEnabled || notifier2Udp.remoteIP() == localIP) return;
|
2021-01-22 16:17:18 +01:00
|
|
|
|
|
|
|
uint8_t unit = udpIn[39];
|
|
|
|
NodesMap::iterator it = Nodes.find(unit);
|
2021-03-09 16:23:19 +01:00
|
|
|
if (it == Nodes.end() && Nodes.size() < WLED_MAX_NODES) { // Create a new element when not present
|
|
|
|
Nodes[unit].age = 0;
|
|
|
|
it = Nodes.find(unit);
|
|
|
|
}
|
2021-01-22 16:17:18 +01:00
|
|
|
|
|
|
|
if (it != Nodes.end()) {
|
2022-07-19 22:14:46 +02:00
|
|
|
for (size_t x = 0; x < 4; x++) {
|
2021-01-22 16:17:18 +01:00
|
|
|
it->second.ip[x] = udpIn[x + 2];
|
|
|
|
}
|
|
|
|
it->second.age = 0; // reset 'age counter'
|
|
|
|
char tmpNodeName[33] = { 0 };
|
|
|
|
memcpy(&tmpNodeName[0], reinterpret_cast<byte *>(&udpIn[6]), 32);
|
|
|
|
tmpNodeName[32] = 0;
|
|
|
|
it->second.nodeName = tmpNodeName;
|
|
|
|
it->second.nodeName.trim();
|
|
|
|
it->second.nodeType = udpIn[38];
|
2021-03-04 14:34:36 +01:00
|
|
|
uint32_t build = 0;
|
|
|
|
if (len >= 44)
|
2022-07-19 22:14:46 +02:00
|
|
|
for (size_t i=0; i<sizeof(uint32_t); i++)
|
2021-03-04 14:34:36 +01:00
|
|
|
build |= udpIn[40+i]<<(8*i);
|
2021-03-09 16:23:19 +01:00
|
|
|
it->second.build = build;
|
2021-01-22 16:17:18 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2020-05-18 16:36:31 +02:00
|
|
|
|
2020-12-02 22:47:31 +01:00
|
|
|
//wled notifier, ignore if realtime packets active
|
2020-05-18 16:36:31 +02:00
|
|
|
if (udpIn[0] == 0 && !realtimeMode && receiveNotifications)
|
|
|
|
{
|
|
|
|
//ignore notification if received within a second after sending a notification ourselves
|
|
|
|
if (millis() - notificationSentTime < 1000) return;
|
|
|
|
if (udpIn[1] > 199) return; //do not receive custom versions
|
2021-05-27 02:02:02 +02:00
|
|
|
|
|
|
|
//compatibilityVersionByte:
|
|
|
|
byte version = udpIn[11];
|
2021-08-25 16:39:12 +02:00
|
|
|
|
|
|
|
// if we are not part of any sync group ignore message
|
|
|
|
if (version < 9 || version > 199) {
|
|
|
|
// legacy senders are treated as if sending in sync group 1 only
|
|
|
|
if (!(receiveGroups & 0x01)) return;
|
|
|
|
} else if (!(receiveGroups & udpIn[36])) return;
|
2020-05-18 16:36:31 +02:00
|
|
|
|
|
|
|
bool someSel = (receiveNotificationBrightness || receiveNotificationColor || receiveNotificationEffects);
|
2022-01-24 18:31:05 +01:00
|
|
|
|
|
|
|
//apply colors from notification to main segment, only if not syncing full segments
|
|
|
|
if ((receiveNotificationColor || !someSel) && (version < 11 || !receiveSegmentOptions)) {
|
2022-02-23 19:20:07 +01:00
|
|
|
// primary color, only apply white if intented (version > 0)
|
|
|
|
strip.setColor(0, RGBW32(udpIn[3], udpIn[4], udpIn[5], (version > 0) ? udpIn[10] : 0));
|
|
|
|
if (version > 1) {
|
|
|
|
strip.setColor(1, RGBW32(udpIn[12], udpIn[13], udpIn[14], udpIn[15])); // secondary color
|
|
|
|
}
|
|
|
|
if (version > 6) {
|
|
|
|
strip.setColor(2, RGBW32(udpIn[20], udpIn[21], udpIn[22], udpIn[23])); // tertiary color
|
|
|
|
if (version > 9 && version < 200 && udpIn[37] < 255) { // valid CCT/Kelvin value
|
2022-11-02 14:56:50 +01:00
|
|
|
uint16_t cct = udpIn[38];
|
2022-02-23 19:20:07 +01:00
|
|
|
if (udpIn[37] > 0) { //Kelvin
|
2022-11-02 14:56:50 +01:00
|
|
|
cct |= (udpIn[37] << 8);
|
2022-01-25 12:47:14 +01:00
|
|
|
}
|
2022-02-23 19:20:07 +01:00
|
|
|
strip.setCCT(cct);
|
2018-03-15 13:03:50 +01:00
|
|
|
}
|
2018-11-09 17:00:36 +01:00
|
|
|
}
|
2020-05-18 16:36:31 +02:00
|
|
|
}
|
2018-11-21 23:28:20 +01:00
|
|
|
|
2021-05-30 00:08:24 +02:00
|
|
|
bool timebaseUpdated = false;
|
2020-05-18 16:36:31 +02:00
|
|
|
//apply effects from notification
|
2022-01-25 12:47:14 +01:00
|
|
|
bool applyEffects = (receiveNotificationEffects || !someSel);
|
2022-01-24 18:31:05 +01:00
|
|
|
if (version < 200)
|
2018-11-09 17:00:36 +01:00
|
|
|
{
|
2022-01-24 18:31:05 +01:00
|
|
|
if (applyEffects && currentPlaylist >= 0) unloadPlaylist();
|
2022-02-20 22:24:11 +01:00
|
|
|
if (version > 10 && (receiveSegmentOptions || receiveSegmentBounds)) {
|
|
|
|
uint8_t numSrcSegs = udpIn[39];
|
2022-07-19 22:14:46 +02:00
|
|
|
for (size_t i = 0; i < numSrcSegs; i++) {
|
2022-01-25 12:47:14 +01:00
|
|
|
uint16_t ofs = 41 + i*udpIn[40]; //start of segment offset byte
|
|
|
|
uint8_t id = udpIn[0 +ofs];
|
2022-07-19 16:16:43 +02:00
|
|
|
if (id > strip.getSegmentsNum()) break;
|
2022-11-02 14:56:50 +01:00
|
|
|
|
2022-07-06 13:13:54 +02:00
|
|
|
Segment& selseg = strip.getSegment(id);
|
2022-10-31 07:13:12 +01:00
|
|
|
if (!selseg.isActive() || !selseg.isSelected()) continue; //do not apply to non selected segments
|
2022-11-02 14:56:50 +01:00
|
|
|
|
2022-10-31 07:13:12 +01:00
|
|
|
uint16_t startY = 0, start = (udpIn[1+ofs] << 8 | udpIn[2+ofs]);
|
|
|
|
uint16_t stopY = 1, stop = (udpIn[3+ofs] << 8 | udpIn[4+ofs]);
|
2022-02-20 22:24:11 +01:00
|
|
|
uint16_t offset = (udpIn[7+ofs] << 8 | udpIn[8+ofs]);
|
|
|
|
if (!receiveSegmentOptions) {
|
2022-10-31 07:13:12 +01:00
|
|
|
strip.setSegment(id, start, stop, selseg.grouping, selseg.spacing, offset, startY, stopY);
|
2022-02-20 22:24:11 +01:00
|
|
|
continue;
|
|
|
|
}
|
2022-10-31 07:13:12 +01:00
|
|
|
//for (size_t j = 1; j<4; j++) selseg.setOption(j, (udpIn[9 +ofs] >> j) & 0x01); //only take into account mirrored, on, reversed; ignore selected
|
2022-11-02 14:56:50 +01:00
|
|
|
selseg.options = (selseg.options & 0x0071U) | (udpIn[9 +ofs] & 0x0E); // ignore selected, freeze, reset & transitional
|
2022-07-10 22:23:25 +02:00
|
|
|
selseg.setOpacity(udpIn[10+ofs]);
|
2022-01-25 12:47:14 +01:00
|
|
|
if (applyEffects) {
|
|
|
|
strip.setMode(id, udpIn[11+ofs]);
|
|
|
|
selseg.speed = udpIn[12+ofs];
|
|
|
|
selseg.intensity = udpIn[13+ofs];
|
|
|
|
selseg.palette = udpIn[14+ofs];
|
|
|
|
}
|
|
|
|
if (receiveNotificationColor || !someSel) {
|
2022-07-10 22:23:25 +02:00
|
|
|
selseg.setColor(0, RGBW32(udpIn[15+ofs],udpIn[16+ofs],udpIn[17+ofs],udpIn[18+ofs]));
|
|
|
|
selseg.setColor(1, RGBW32(udpIn[19+ofs],udpIn[20+ofs],udpIn[21+ofs],udpIn[22+ofs]));
|
|
|
|
selseg.setColor(2, RGBW32(udpIn[23+ofs],udpIn[24+ofs],udpIn[25+ofs],udpIn[26+ofs]));
|
|
|
|
selseg.setCCT(udpIn[27+ofs]);
|
2022-01-25 12:47:14 +01:00
|
|
|
}
|
2022-10-31 07:13:12 +01:00
|
|
|
if (version > 11) {
|
|
|
|
// when applying synced options ignore selected as it may be used as indicator of which segments to sync
|
|
|
|
// freeze, reset & transitional should never be synced
|
2022-11-02 14:56:50 +01:00
|
|
|
selseg.options = (selseg.options & 0x0071U) | (udpIn[28+ofs]<<8) | (udpIn[9 +ofs] & 0x8E); // ignore selected, freeze, reset & transitional
|
2022-10-31 07:13:12 +01:00
|
|
|
if (applyEffects) {
|
|
|
|
selseg.custom1 = udpIn[29+ofs];
|
|
|
|
selseg.custom2 = udpIn[30+ofs];
|
|
|
|
selseg.custom3 = udpIn[31+ofs] & 0x1F;
|
|
|
|
selseg.check1 = (udpIn[31+ofs]>>5) & 0x1;
|
|
|
|
selseg.check1 = (udpIn[31+ofs]>>6) & 0x1;
|
|
|
|
selseg.check1 = (udpIn[31+ofs]>>7) & 0x1;
|
|
|
|
}
|
|
|
|
startY = (udpIn[32+ofs] << 8 | udpIn[33+ofs]);
|
|
|
|
stopY = (udpIn[34+ofs] << 8 | udpIn[35+ofs]);
|
|
|
|
}
|
2022-02-20 22:24:11 +01:00
|
|
|
//setSegment() also properly resets segments
|
|
|
|
if (receiveSegmentBounds) {
|
2022-10-31 07:13:12 +01:00
|
|
|
strip.setSegment(id, start, stop, udpIn[5+ofs], udpIn[6+ofs], offset, startY, stopY);
|
2022-02-20 22:24:11 +01:00
|
|
|
} else {
|
2022-10-31 07:13:12 +01:00
|
|
|
strip.setSegment(id, selseg.start, selseg.stop, udpIn[5+ofs], udpIn[6+ofs], selseg.offset, selseg.startY, selseg.stopY);
|
2022-02-20 22:24:11 +01:00
|
|
|
}
|
2022-01-25 12:47:14 +01:00
|
|
|
}
|
2022-02-21 22:12:13 +01:00
|
|
|
stateChanged = true;
|
2022-02-20 22:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-02-23 19:20:07 +01:00
|
|
|
// simple effect sync, applies to all selected segments
|
|
|
|
if (applyEffects && (version < 11 || !receiveSegmentOptions)) {
|
2022-07-19 22:14:46 +02:00
|
|
|
for (size_t i = 0; i < strip.getSegmentsNum(); i++) {
|
2022-07-06 13:13:54 +02:00
|
|
|
Segment& seg = strip.getSegment(i);
|
2022-02-23 19:20:07 +01:00
|
|
|
if (!seg.isActive() || !seg.isSelected()) continue;
|
|
|
|
if (udpIn[8] < strip.getModeCount()) strip.setMode(i, udpIn[8]);
|
|
|
|
seg.speed = udpIn[9];
|
|
|
|
if (version > 2) seg.intensity = udpIn[16];
|
|
|
|
if (version > 4 && udpIn[19] < strip.getPaletteCount()) seg.palette = udpIn[19];
|
|
|
|
}
|
|
|
|
stateChanged = true;
|
2022-01-25 12:47:14 +01:00
|
|
|
}
|
2022-01-09 14:16:07 +01:00
|
|
|
|
2022-01-24 18:31:05 +01:00
|
|
|
if (applyEffects && version > 5) {
|
2021-05-27 02:02:02 +02:00
|
|
|
uint32_t t = (udpIn[25] << 24) | (udpIn[26] << 16) | (udpIn[27] << 8) | (udpIn[28]);
|
|
|
|
t += PRESUMED_NETWORK_DELAY; //adjust trivially for network delay
|
|
|
|
t -= millis();
|
|
|
|
strip.timebase = t;
|
2021-05-30 00:08:24 +02:00
|
|
|
timebaseUpdated = true;
|
2021-05-27 02:02:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//adjust system time, but only if sender is more accurate than self
|
2021-08-25 16:39:12 +02:00
|
|
|
if (version > 7 && version < 200)
|
2021-05-27 02:02:02 +02:00
|
|
|
{
|
|
|
|
Toki::Time tm;
|
|
|
|
tm.sec = (udpIn[30] << 24) | (udpIn[31] << 16) | (udpIn[32] << 8) | (udpIn[33]);
|
|
|
|
tm.ms = (udpIn[34] << 8) | (udpIn[35]);
|
2021-05-30 00:08:24 +02:00
|
|
|
if (udpIn[29] > toki.getTimeSource()) { //if sender's time source is more accurate
|
|
|
|
toki.adjust(tm, PRESUMED_NETWORK_DELAY); //adjust trivially for network delay
|
|
|
|
uint8_t ts = TOKI_TS_UDP;
|
|
|
|
if (udpIn[29] > 99) ts = TOKI_TS_UDP_NTP;
|
|
|
|
else if (udpIn[29] >= TOKI_TS_SEC) ts = TOKI_TS_UDP_SEC;
|
|
|
|
toki.setTime(tm, ts);
|
|
|
|
} else if (timebaseUpdated && toki.getTimeSource() > 99) { //if we both have good times, get a more accurate timebase
|
|
|
|
Toki::Time myTime = toki.getTime();
|
|
|
|
uint32_t diff = toki.msDifference(tm, myTime);
|
|
|
|
strip.timebase -= PRESUMED_NETWORK_DELAY; //no need to presume, use difference between NTP times at send and receive points
|
|
|
|
if (toki.isLater(tm, myTime)) {
|
|
|
|
strip.timebase += diff;
|
|
|
|
} else {
|
|
|
|
strip.timebase -= diff;
|
|
|
|
}
|
|
|
|
}
|
2020-05-18 16:36:31 +02:00
|
|
|
}
|
|
|
|
|
2021-05-27 02:02:02 +02:00
|
|
|
if (version > 3)
|
2020-05-18 16:36:31 +02:00
|
|
|
{
|
|
|
|
transitionDelayTemp = ((udpIn[17] << 0) & 0xFF) + ((udpIn[18] << 8) & 0xFF00);
|
|
|
|
}
|
|
|
|
|
|
|
|
nightlightActive = udpIn[6];
|
|
|
|
if (nightlightActive) nightlightDelayMins = udpIn[7];
|
|
|
|
|
|
|
|
if (receiveNotificationBrightness || !someSel) bri = udpIn[2];
|
2022-02-23 19:20:07 +01:00
|
|
|
stateUpdated(CALL_MODE_NOTIFICATION);
|
2020-09-27 11:43:28 +02:00
|
|
|
return;
|
2020-05-18 16:36:31 +02:00
|
|
|
}
|
2020-12-02 22:47:31 +01:00
|
|
|
|
2020-05-18 16:36:31 +02:00
|
|
|
if (!receiveDirect) return;
|
|
|
|
|
|
|
|
//TPM2.NET
|
|
|
|
if (udpIn[0] == 0x9c)
|
|
|
|
{
|
2020-08-19 10:40:16 +02:00
|
|
|
//WARNING: this code assumes that the final TMP2.NET payload is evenly distributed if using multiple packets (ie. frame size is constant)
|
|
|
|
//if the number of LEDs in your installation doesn't allow that, please include padding bytes at the end of the last packet
|
2020-05-18 16:36:31 +02:00
|
|
|
byte tpmType = udpIn[1];
|
|
|
|
if (tpmType == 0xaa) { //TPM2.NET polling, expect answer
|
|
|
|
sendTPM2Ack(); return;
|
|
|
|
}
|
2020-05-22 23:30:55 +02:00
|
|
|
if (tpmType != 0xda) return; //return if notTPM2.NET data
|
2020-04-30 01:52:36 +02:00
|
|
|
|
2020-09-27 11:43:28 +02:00
|
|
|
realtimeIP = (isSupp) ? notifier2Udp.remoteIP() : notifierUdp.remoteIP();
|
2020-05-22 23:30:55 +02:00
|
|
|
realtimeLock(realtimeTimeoutMs, REALTIME_MODE_TPM2NET);
|
2022-03-25 16:36:05 +01:00
|
|
|
if (realtimeOverride && !(realtimeMode && useMainSegmentOnly)) return;
|
2020-05-18 16:36:31 +02:00
|
|
|
|
2020-08-19 10:40:16 +02:00
|
|
|
tpmPacketCount++; //increment the packet count
|
|
|
|
if (tpmPacketCount == 1) tpmPayloadFrameSize = (udpIn[2] << 8) + udpIn[3]; //save frame size for the whole payload if this is the first packet
|
2020-05-22 23:30:55 +02:00
|
|
|
byte packetNum = udpIn[4]; //starts with 1!
|
|
|
|
byte numPackets = udpIn[5];
|
|
|
|
|
2020-08-19 10:40:16 +02:00
|
|
|
uint16_t id = (tpmPayloadFrameSize/3)*(packetNum-1); //start LED
|
2021-10-31 11:57:41 +01:00
|
|
|
uint16_t totalLen = strip.getLengthTotal();
|
2022-07-19 22:14:46 +02:00
|
|
|
for (size_t i = 6; i < tpmPayloadFrameSize + 4U; i += 3)
|
2020-05-22 23:30:55 +02:00
|
|
|
{
|
2021-10-31 11:57:41 +01:00
|
|
|
if (id < totalLen)
|
2020-08-19 10:40:16 +02:00
|
|
|
{
|
|
|
|
setRealtimePixel(id, udpIn[i], udpIn[i+1], udpIn[i+2], 0);
|
|
|
|
id++;
|
|
|
|
}
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
if (tpmPacketCount == numPackets) //reset packet count and show if all packets were received
|
|
|
|
{
|
|
|
|
tpmPacketCount = 0;
|
|
|
|
strip.show();
|
2020-05-18 16:36:31 +02:00
|
|
|
}
|
2020-09-27 11:43:28 +02:00
|
|
|
return;
|
2020-05-18 16:36:31 +02:00
|
|
|
}
|
2020-04-30 01:52:36 +02:00
|
|
|
|
2020-05-18 16:36:31 +02:00
|
|
|
//UDP realtime: 1 warls 2 drgb 3 drgbw
|
|
|
|
if (udpIn[0] > 0 && udpIn[0] < 5)
|
|
|
|
{
|
2020-09-27 11:43:28 +02:00
|
|
|
realtimeIP = (isSupp) ? notifier2Udp.remoteIP() : notifierUdp.remoteIP();
|
|
|
|
DEBUG_PRINTLN(realtimeIP);
|
2020-05-18 16:36:31 +02:00
|
|
|
if (packetSize < 2) return;
|
|
|
|
|
|
|
|
if (udpIn[1] == 0)
|
|
|
|
{
|
|
|
|
realtimeTimeout = 0;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
realtimeLock(udpIn[1]*1000 +1, REALTIME_MODE_UDP);
|
|
|
|
}
|
2022-03-25 16:36:05 +01:00
|
|
|
if (realtimeOverride && !(realtimeMode && useMainSegmentOnly)) return;
|
2020-05-18 16:36:31 +02:00
|
|
|
|
2021-10-31 11:57:41 +01:00
|
|
|
uint16_t totalLen = strip.getLengthTotal();
|
2020-05-18 16:36:31 +02:00
|
|
|
if (udpIn[0] == 1) //warls
|
|
|
|
{
|
2022-07-19 22:14:46 +02:00
|
|
|
for (size_t i = 2; i < packetSize -3; i += 4)
|
2020-04-30 01:52:36 +02:00
|
|
|
{
|
2020-05-18 16:36:31 +02:00
|
|
|
setRealtimePixel(udpIn[i], udpIn[i+1], udpIn[i+2], udpIn[i+3], 0);
|
|
|
|
}
|
|
|
|
} else if (udpIn[0] == 2) //drgb
|
|
|
|
{
|
|
|
|
uint16_t id = 0;
|
2022-07-19 22:14:46 +02:00
|
|
|
for (size_t i = 2; i < packetSize -2; i += 3)
|
2020-04-30 01:52:36 +02:00
|
|
|
{
|
2020-05-18 16:36:31 +02:00
|
|
|
setRealtimePixel(id, udpIn[i], udpIn[i+1], udpIn[i+2], 0);
|
2018-08-11 22:57:13 +02:00
|
|
|
|
2021-10-31 11:57:41 +01:00
|
|
|
id++; if (id >= totalLen) break;
|
2020-05-18 16:36:31 +02:00
|
|
|
}
|
|
|
|
} else if (udpIn[0] == 3) //drgbw
|
|
|
|
{
|
|
|
|
uint16_t id = 0;
|
2022-07-19 22:14:46 +02:00
|
|
|
for (size_t i = 2; i < packetSize -3; i += 4)
|
2020-04-30 01:52:36 +02:00
|
|
|
{
|
2020-05-18 16:36:31 +02:00
|
|
|
setRealtimePixel(id, udpIn[i], udpIn[i+1], udpIn[i+2], udpIn[i+3]);
|
|
|
|
|
2021-10-31 11:57:41 +01:00
|
|
|
id++; if (id >= totalLen) break;
|
2020-05-18 16:36:31 +02:00
|
|
|
}
|
|
|
|
} else if (udpIn[0] == 4) //dnrgb
|
|
|
|
{
|
|
|
|
uint16_t id = ((udpIn[3] << 0) & 0xFF) + ((udpIn[2] << 8) & 0xFF00);
|
2022-07-19 22:14:46 +02:00
|
|
|
for (size_t i = 4; i < packetSize -2; i += 3)
|
2020-04-30 01:52:36 +02:00
|
|
|
{
|
2021-10-31 11:57:41 +01:00
|
|
|
if (id >= totalLen) break;
|
2020-05-18 16:36:31 +02:00
|
|
|
setRealtimePixel(id, udpIn[i], udpIn[i+1], udpIn[i+2], 0);
|
|
|
|
id++;
|
2016-11-27 22:37:51 +01:00
|
|
|
}
|
2021-01-30 07:11:37 +01:00
|
|
|
} else if (udpIn[0] == 5) //dnrgbw
|
|
|
|
{
|
|
|
|
uint16_t id = ((udpIn[3] << 0) & 0xFF) + ((udpIn[2] << 8) & 0xFF00);
|
2022-07-19 22:14:46 +02:00
|
|
|
for (size_t i = 4; i < packetSize -2; i += 4)
|
2021-01-30 07:11:37 +01:00
|
|
|
{
|
2021-10-31 11:57:41 +01:00
|
|
|
if (id >= totalLen) break;
|
2021-01-30 07:11:37 +01:00
|
|
|
setRealtimePixel(id, udpIn[i], udpIn[i+1], udpIn[i+2], udpIn[i+3]);
|
|
|
|
id++;
|
|
|
|
}
|
2016-11-20 00:07:04 +01:00
|
|
|
}
|
2020-05-18 16:36:31 +02:00
|
|
|
strip.show();
|
2020-09-27 11:43:28 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// API over UDP
|
|
|
|
udpIn[packetSize] = '\0';
|
|
|
|
|
2022-01-07 19:18:16 +01:00
|
|
|
if (requestJSONBufferLock(18)) {
|
|
|
|
if (udpIn[0] >= 'A' && udpIn[0] <= 'Z') { //HTTP API
|
2022-02-19 20:34:37 +01:00
|
|
|
String apireq = "win"; apireq += '&'; // reduce flash string usage
|
2022-01-07 19:18:16 +01:00
|
|
|
apireq += (char*)udpIn;
|
|
|
|
handleSet(nullptr, apireq);
|
|
|
|
} else if (udpIn[0] == '{') { //JSON API
|
|
|
|
DeserializationError error = deserializeJson(doc, udpIn);
|
|
|
|
JsonObject root = doc.as<JsonObject>();
|
|
|
|
if (!error && !root.isNull()) deserializeState(root);
|
|
|
|
}
|
|
|
|
releaseJSONBufferLock();
|
2018-04-11 23:50:35 +02:00
|
|
|
}
|
2016-11-19 19:39:17 +01:00
|
|
|
}
|
2016-11-27 22:37:51 +01:00
|
|
|
|
2018-11-24 11:52:23 +01:00
|
|
|
|
2018-10-01 21:31:31 +02:00
|
|
|
void setRealtimePixel(uint16_t i, byte r, byte g, byte b, byte w)
|
2018-08-11 22:57:13 +02:00
|
|
|
{
|
2018-10-01 21:31:31 +02:00
|
|
|
uint16_t pix = i + arlsOffset;
|
2022-08-03 21:36:47 +02:00
|
|
|
if (pix < strip.getLengthTotal()) {
|
2022-09-04 20:17:05 +02:00
|
|
|
if (!arlsDisableGammaCorrection && gammaCorrectCol) {
|
2022-08-03 21:36:47 +02:00
|
|
|
r = gamma8(r);
|
|
|
|
g = gamma8(g);
|
|
|
|
b = gamma8(b);
|
|
|
|
w = gamma8(w);
|
|
|
|
}
|
|
|
|
if (useMainSegmentOnly) {
|
|
|
|
Segment &seg = strip.getMainSegment();
|
|
|
|
if (pix<seg.length()) seg.setPixelColor(pix, r, g, b, w);
|
2018-08-11 22:57:13 +02:00
|
|
|
} else {
|
2018-10-01 21:31:31 +02:00
|
|
|
strip.setPixelColor(pix, r, g, b, w);
|
2018-08-11 22:57:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-22 16:17:18 +01:00
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
Refresh aging for remote units, drop if too old...
|
|
|
|
\*********************************************************************************************/
|
|
|
|
void refreshNodeList()
|
|
|
|
{
|
|
|
|
for (NodesMap::iterator it = Nodes.begin(); it != Nodes.end();) {
|
|
|
|
bool mustRemove = true;
|
|
|
|
|
|
|
|
if (it->second.ip[0] != 0) {
|
|
|
|
if (it->second.age < 10) {
|
|
|
|
it->second.age++;
|
|
|
|
mustRemove = false;
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mustRemove) {
|
|
|
|
it = Nodes.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
Broadcast system info to other nodes. (to update node lists)
|
|
|
|
\*********************************************************************************************/
|
2021-03-09 17:21:19 +01:00
|
|
|
void sendSysInfoUDP()
|
2021-01-22 16:17:18 +01:00
|
|
|
{
|
2021-03-09 17:21:19 +01:00
|
|
|
if (!udp2Connected) return;
|
2021-01-22 16:17:18 +01:00
|
|
|
|
2021-03-09 16:23:19 +01:00
|
|
|
IPAddress ip = Network.localIP();
|
2021-10-30 14:42:17 +02:00
|
|
|
if (!ip || ip == IPAddress(255,255,255,255)) ip = IPAddress(4,3,2,1);
|
2021-01-22 16:17:18 +01:00
|
|
|
|
|
|
|
// TODO: make a nice struct of it and clean up
|
|
|
|
// 0: 1 byte 'binary token 255'
|
|
|
|
// 1: 1 byte id '1'
|
|
|
|
// 2: 4 byte ip
|
|
|
|
// 6: 32 char name
|
|
|
|
// 38: 1 byte node type id
|
|
|
|
// 39: 1 byte node id
|
2021-03-04 14:34:36 +01:00
|
|
|
// 40: 4 byte version ID
|
|
|
|
// 44 bytes total
|
2021-01-22 16:17:18 +01:00
|
|
|
|
|
|
|
// send my info to the world...
|
2021-03-09 16:23:19 +01:00
|
|
|
uint8_t data[44] = {0};
|
|
|
|
data[0] = 255;
|
|
|
|
data[1] = 1;
|
|
|
|
|
2022-07-19 22:14:46 +02:00
|
|
|
for (size_t x = 0; x < 4; x++) {
|
2021-03-09 16:23:19 +01:00
|
|
|
data[x + 2] = ip[x];
|
2021-01-22 16:17:18 +01:00
|
|
|
}
|
2021-03-09 16:23:19 +01:00
|
|
|
memcpy((byte *)data + 6, serverDescription, 32);
|
|
|
|
#ifdef ESP8266
|
|
|
|
data[38] = NODE_TYPE_ID_ESP8266;
|
2022-11-02 14:56:50 +01:00
|
|
|
#elif defined(CONFIG_IDF_TARGET_ESP32C3)
|
|
|
|
data[38] = NODE_TYPE_ID_ESP32C3;
|
|
|
|
#elif defined(CONFIG_IDF_TARGET_ESP32S3)
|
|
|
|
data[38] = NODE_TYPE_ID_ESP32S3;
|
|
|
|
#elif defined(CONFIG_IDF_TARGET_ESP32S2)
|
|
|
|
data[38] = NODE_TYPE_ID_ESP32S2;
|
2021-03-09 16:23:19 +01:00
|
|
|
#elif defined(ARDUINO_ARCH_ESP32)
|
|
|
|
data[38] = NODE_TYPE_ID_ESP32;
|
|
|
|
#else
|
|
|
|
data[38] = NODE_TYPE_ID_UNDEFINED;
|
|
|
|
#endif
|
|
|
|
data[39] = ip[3]; // unit ID == last IP number
|
|
|
|
|
|
|
|
uint32_t build = VERSION;
|
2022-07-19 22:14:46 +02:00
|
|
|
for (size_t i=0; i<sizeof(uint32_t); i++)
|
2021-03-09 16:23:19 +01:00
|
|
|
data[40+i] = (build>>(8*i)) & 0xFF;
|
|
|
|
|
|
|
|
IPAddress broadcastIP(255, 255, 255, 255);
|
|
|
|
notifier2Udp.beginPacket(broadcastIP, udpPort2);
|
|
|
|
notifier2Udp.write(data, sizeof(data));
|
|
|
|
notifier2Udp.endPacket();
|
2021-01-22 16:17:18 +01:00
|
|
|
}
|
2021-10-02 10:48:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Art-Net, DDP, E131 output - work in progress
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#define DDP_HEADER_LEN 10
|
|
|
|
#define DDP_SYNCPACKET_LEN 10
|
|
|
|
|
|
|
|
#define DDP_FLAGS1_VER 0xc0 // version mask
|
|
|
|
#define DDP_FLAGS1_VER1 0x40 // version=1
|
|
|
|
#define DDP_FLAGS1_PUSH 0x01
|
|
|
|
#define DDP_FLAGS1_QUERY 0x02
|
|
|
|
#define DDP_FLAGS1_REPLY 0x04
|
|
|
|
#define DDP_FLAGS1_STORAGE 0x08
|
|
|
|
#define DDP_FLAGS1_TIME 0x10
|
|
|
|
|
|
|
|
#define DDP_ID_DISPLAY 1
|
|
|
|
#define DDP_ID_CONFIG 250
|
|
|
|
#define DDP_ID_STATUS 251
|
|
|
|
|
|
|
|
// 1440 channels per packet
|
|
|
|
#define DDP_CHANNELS_PER_PACKET 1440 // 480 leds
|
|
|
|
|
|
|
|
//
|
2021-10-02 15:07:02 +02:00
|
|
|
// Send real time UDP updates to the specified client
|
2021-10-02 10:48:48 +02:00
|
|
|
//
|
2021-10-02 15:07:02 +02:00
|
|
|
// type - protocol type (0=DDP, 1=E1.31, 2=ArtNet)
|
2021-10-02 10:48:48 +02:00
|
|
|
// client - the IP address to send to
|
|
|
|
// length - the number of pixels
|
|
|
|
// buffer - a buffer of at least length*4 bytes long
|
|
|
|
// isRGBW - true if the buffer contains 4 components per pixel
|
|
|
|
|
|
|
|
uint8_t sequenceNumber = 0; // this needs to be shared across all outputs
|
|
|
|
|
2021-10-04 19:41:20 +02:00
|
|
|
uint8_t realtimeBroadcast(uint8_t type, IPAddress client, uint16_t length, uint8_t *buffer, uint8_t bri, bool isRGBW) {
|
2022-03-16 17:00:29 +01:00
|
|
|
if (!(apActive || interfacesInited) || !client[0] || !length) return 1; // network not initialised or dummy/unset IP address 031522 ajn added check for ap
|
2021-10-02 10:48:48 +02:00
|
|
|
|
2021-10-02 15:07:02 +02:00
|
|
|
WiFiUDP ddpUdp;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 0: // DDP
|
|
|
|
{
|
2022-03-06 22:24:24 +01:00
|
|
|
// calculate the number of UDP packets we need to send
|
2022-09-02 03:12:03 +02:00
|
|
|
size_t channelCount = length * (isRGBW? 4:3); // 1 channel for every R,G,B value
|
2022-07-19 22:14:46 +02:00
|
|
|
size_t packetCount = ((channelCount-1) / DDP_CHANNELS_PER_PACKET) +1;
|
2021-10-02 10:48:48 +02:00
|
|
|
|
2021-10-02 15:07:02 +02:00
|
|
|
// there are 3 channels per RGB pixel
|
|
|
|
uint32_t channel = 0; // TODO: allow specifying the start channel
|
|
|
|
// the current position in the buffer
|
2022-07-19 22:14:46 +02:00
|
|
|
size_t bufferOffset = 0;
|
2021-10-02 10:48:48 +02:00
|
|
|
|
2022-07-19 22:14:46 +02:00
|
|
|
for (size_t currentPacket = 0; currentPacket < packetCount; currentPacket++) {
|
2021-10-02 10:48:48 +02:00
|
|
|
if (sequenceNumber > 15) sequenceNumber = 0;
|
|
|
|
|
2021-10-02 15:07:02 +02:00
|
|
|
if (!ddpUdp.beginPacket(client, DDP_DEFAULT_PORT)) { // port defined in ESPAsyncE131.h
|
|
|
|
DEBUG_PRINTLN(F("WiFiUDP.beginPacket returned an error"));
|
2021-10-02 10:48:48 +02:00
|
|
|
return 1; // problem
|
|
|
|
}
|
|
|
|
|
|
|
|
// the amount of data is AFTER the header in the current packet
|
2022-07-19 22:14:46 +02:00
|
|
|
size_t packetSize = DDP_CHANNELS_PER_PACKET;
|
2021-10-02 10:48:48 +02:00
|
|
|
|
|
|
|
uint8_t flags = DDP_FLAGS1_VER1;
|
2022-07-19 22:14:46 +02:00
|
|
|
if (currentPacket == (packetCount - 1U)) {
|
2021-10-02 15:07:02 +02:00
|
|
|
// last packet, set the push flag
|
|
|
|
// TODO: determine if we want to send an empty push packet to each destination after sending the pixel data
|
|
|
|
flags = DDP_FLAGS1_VER1 | DDP_FLAGS1_PUSH;
|
|
|
|
if (channelCount % DDP_CHANNELS_PER_PACKET) {
|
|
|
|
packetSize = channelCount % DDP_CHANNELS_PER_PACKET;
|
|
|
|
}
|
2021-10-02 10:48:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// write the header
|
|
|
|
/*0*/ddpUdp.write(flags);
|
2021-10-02 15:07:02 +02:00
|
|
|
/*1*/ddpUdp.write(sequenceNumber++ & 0x0F); // sequence may be unnecessary unless we are sending twice (as requested in Sync settings)
|
2022-09-02 03:12:03 +02:00
|
|
|
/*2*/ddpUdp.write(isRGBW ? DDP_TYPE_RGBW32 : DDP_TYPE_RGB24);
|
2021-10-02 10:48:48 +02:00
|
|
|
/*3*/ddpUdp.write(DDP_ID_DISPLAY);
|
|
|
|
// data offset in bytes, 32-bit number, MSB first
|
2021-10-02 15:07:02 +02:00
|
|
|
/*4*/ddpUdp.write(0xFF & (channel >> 24));
|
|
|
|
/*5*/ddpUdp.write(0xFF & (channel >> 16));
|
|
|
|
/*6*/ddpUdp.write(0xFF & (channel >> 8));
|
|
|
|
/*7*/ddpUdp.write(0xFF & (channel ));
|
2021-10-02 10:48:48 +02:00
|
|
|
// data length in bytes, 16-bit number, MSB first
|
2021-10-02 15:07:02 +02:00
|
|
|
/*8*/ddpUdp.write(0xFF & (packetSize >> 8));
|
|
|
|
/*9*/ddpUdp.write(0xFF & (packetSize ));
|
2021-10-02 10:48:48 +02:00
|
|
|
|
|
|
|
// write the colors, the write write(const uint8_t *buffer, size_t size)
|
|
|
|
// function is just a loop internally too
|
2022-07-19 22:14:46 +02:00
|
|
|
for (size_t i = 0; i < packetSize; i += 3) {
|
2021-10-04 19:41:20 +02:00
|
|
|
ddpUdp.write(scale8(buffer[bufferOffset++], bri)); // R
|
|
|
|
ddpUdp.write(scale8(buffer[bufferOffset++], bri)); // G
|
|
|
|
ddpUdp.write(scale8(buffer[bufferOffset++], bri)); // B
|
2022-09-02 03:12:03 +02:00
|
|
|
if (isRGBW) ddpUdp.write(scale8(buffer[bufferOffset++], bri)); // W
|
2021-10-02 10:48:48 +02:00
|
|
|
}
|
|
|
|
|
2021-10-02 15:07:02 +02:00
|
|
|
if (!ddpUdp.endPacket()) {
|
2021-10-06 14:30:41 +02:00
|
|
|
DEBUG_PRINTLN(F("WiFiUDP.endPacket returned an error"));
|
2021-10-02 10:48:48 +02:00
|
|
|
return 1; // problem
|
|
|
|
}
|
|
|
|
|
|
|
|
channel += packetSize;
|
2021-10-02 15:07:02 +02:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case 1: //E1.31
|
|
|
|
{
|
|
|
|
} break;
|
2021-10-02 10:48:48 +02:00
|
|
|
|
2021-10-02 15:07:02 +02:00
|
|
|
case 2: //ArtNet
|
|
|
|
{
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
return 0;
|
2021-10-02 10:48:48 +02:00
|
|
|
}
|