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
|
|
|
|
2019-10-18 12:19:52 +02:00
|
|
|
#define WLEDPACKETSIZE 29
|
2018-11-01 16:16:38 +01:00
|
|
|
#define UDP_IN_MAXSIZE 1472
|
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;
|
2016-11-19 19:39:17 +01:00
|
|
|
switch (callMode)
|
|
|
|
{
|
2020-02-22 16:17:32 +01:00
|
|
|
case NOTIFIER_CALL_MODE_INIT: return;
|
|
|
|
case NOTIFIER_CALL_MODE_DIRECT_CHANGE: if (!notifyDirect) return; break;
|
|
|
|
case NOTIFIER_CALL_MODE_BUTTON: if (!notifyButton) return; break;
|
|
|
|
case NOTIFIER_CALL_MODE_NIGHTLIGHT: if (!notifyDirect) return; break;
|
|
|
|
case NOTIFIER_CALL_MODE_HUE: if (!notifyHue) return; break;
|
|
|
|
case NOTIFIER_CALL_MODE_PRESET_CYCLE: if (!notifyDirect) return; break;
|
|
|
|
case NOTIFIER_CALL_MODE_BLYNK: if (!notifyDirect) return; break;
|
|
|
|
case NOTIFIER_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];
|
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;
|
|
|
|
udpOut[3] = col[0];
|
|
|
|
udpOut[4] = col[1];
|
|
|
|
udpOut[5] = col[2];
|
|
|
|
udpOut[6] = nightlightActive;
|
2016-12-14 21:40:09 +01:00
|
|
|
udpOut[7] = nightlightDelayMins;
|
|
|
|
udpOut[8] = effectCurrent;
|
|
|
|
udpOut[9] = effectSpeed;
|
2019-02-05 21:53:39 +01:00
|
|
|
udpOut[10] = col[3];
|
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
|
2019-10-18 12:19:52 +02:00
|
|
|
//6: supports timebase syncing, 29 byte packet 7: supports tertiary color
|
2019-12-13 14:14:36 +01:00
|
|
|
udpOut[11] = 7;
|
2018-03-14 13:16:28 +01:00
|
|
|
udpOut[12] = colSec[0];
|
|
|
|
udpOut[13] = colSec[1];
|
|
|
|
udpOut[14] = colSec[2];
|
2019-02-05 21:53:39 +01:00
|
|
|
udpOut[15] = colSec[3];
|
2018-01-27 23:28:20 +01:00
|
|
|
udpOut[16] = effectIntensity;
|
2018-03-15 12:04:14 +01:00
|
|
|
udpOut[17] = (transitionDelay >> 0) & 0xFF;
|
|
|
|
udpOut[18] = (transitionDelay >> 8) & 0xFF;
|
2018-09-06 02:05:56 +02:00
|
|
|
udpOut[19] = effectPalette;
|
2019-12-13 14:14:36 +01:00
|
|
|
uint32_t colTer = strip.getSegment(strip.getMainSegmentId()).colors[2];
|
2019-12-13 14:16:42 +01:00
|
|
|
udpOut[20] = (colTer >> 16) & 0xFF;
|
|
|
|
udpOut[21] = (colTer >> 8) & 0xFF;
|
|
|
|
udpOut[22] = (colTer >> 0) & 0xFF;
|
|
|
|
udpOut[23] = (colTer >> 24) & 0xFF;
|
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;
|
2016-11-19 19:39:17 +01:00
|
|
|
|
2016-11-27 22:37:51 +01:00
|
|
|
IPAddress broadcastIp;
|
2019-02-12 11:03:54 +01:00
|
|
|
broadcastIp = ~uint32_t(WiFi.subnetMask()) | uint32_t(WiFi.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();
|
|
|
|
notificationTwoRequired = (followUp)? false:notifyTwice;
|
2016-11-27 22:37:51 +01:00
|
|
|
}
|
|
|
|
|
2018-11-24 11:52:23 +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
|
|
|
{
|
2020-04-30 01:52:36 +02:00
|
|
|
if (!realtimeMode && !realtimeOverride){
|
2019-03-11 00:20:17 +01:00
|
|
|
for (uint16_t i = 0; i < ledCount; i++)
|
|
|
|
{
|
|
|
|
strip.setPixelColor(i,0,0,0,0);
|
|
|
|
}
|
2018-05-18 23:24:47 +02:00
|
|
|
}
|
2020-04-30 01:52:36 +02:00
|
|
|
|
2018-09-15 17:29:01 +02:00
|
|
|
realtimeTimeout = millis() + timeoutMs;
|
2019-04-14 19:31:25 +02:00
|
|
|
if (timeoutMs == 255001 || timeoutMs == 65000) realtimeTimeout = UINT32_MAX;
|
2020-04-30 01:52:36 +02:00
|
|
|
realtimeMode = md;
|
|
|
|
|
|
|
|
if (arlsForceMaxBri && !realtimeOverride) strip.setBrightness(255);
|
2018-08-11 22:57:13 +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()
|
|
|
|
{
|
2018-05-18 23:24:47 +02:00
|
|
|
//send second notification if enabled
|
2018-03-06 23:47:08 +01:00
|
|
|
if(udpConnected && notificationTwoRequired && millis()-notificationSentTime > 250){
|
|
|
|
notify(notificationSentCallMode,true);
|
|
|
|
}
|
2018-05-18 23:24:47 +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
|
2020-02-09 19:10:29 +01:00
|
|
|
if (realtimeMode && millis() > realtimeTimeout)
|
2018-05-18 23:24:47 +02:00
|
|
|
{
|
2020-04-30 01:52:36 +02:00
|
|
|
if (realtimeOverride == REALTIME_OVERRIDE_ONCE) realtimeOverride = REALTIME_OVERRIDE_NONE;
|
2018-08-11 22:57:13 +02:00
|
|
|
strip.setBrightness(bri);
|
2020-02-09 19:10:29 +01:00
|
|
|
realtimeMode = REALTIME_MODE_INACTIVE;
|
2020-04-30 01:52:36 +02:00
|
|
|
realtimeIP[0] = 0;
|
2018-05-18 23:24:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//receive UDP notifications
|
2018-11-09 17:00:36 +01:00
|
|
|
if (!udpConnected || !(receiveNotifications || receiveDirect)) return;
|
|
|
|
|
|
|
|
uint16_t packetSize = notifierUdp.parsePacket();
|
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();
|
|
|
|
if (!receiveDirect) return;
|
|
|
|
if (packetSize > UDP_IN_MAXSIZE || packetSize < 3) return;
|
|
|
|
realtimeIP = rgbUdp.remoteIP();
|
|
|
|
DEBUG_PRINTLN(rgbUdp.remoteIP());
|
2019-03-11 19:30:49 +01:00
|
|
|
uint8_t lbuf[packetSize];
|
|
|
|
rgbUdp.read(lbuf, packetSize);
|
2020-04-13 00:42:27 +02:00
|
|
|
realtimeLock(realtimeTimeoutMs, REALTIME_MODE_HYPERION);
|
2020-04-30 01:52:36 +02:00
|
|
|
if (realtimeOverride) return;
|
2018-11-09 17:00:36 +01:00
|
|
|
uint16_t id = 0;
|
|
|
|
for (uint16_t i = 0; i < packetSize -2; i += 3)
|
|
|
|
{
|
2019-03-11 19:30:49 +01:00
|
|
|
setRealtimePixel(id, lbuf[i], lbuf[i+1], lbuf[i+2], 0);
|
2018-11-09 17:00:36 +01:00
|
|
|
|
|
|
|
id++; if (id >= ledCount) break;
|
2018-05-18 23:24:47 +02:00
|
|
|
}
|
2018-11-09 17:00:36 +01:00
|
|
|
strip.show();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//notifier and UDP realtime
|
2020-05-18 16:36:31 +02:00
|
|
|
if (!packetSize || packetSize > UDP_IN_MAXSIZE) return;
|
|
|
|
if (notifierUdp.remoteIP() == WiFi.localIP()) return; //don't process broadcasts we send ourselves
|
2018-11-21 23:28:20 +01:00
|
|
|
|
2020-05-18 16:36:31 +02:00
|
|
|
uint8_t udpIn[packetSize];
|
|
|
|
notifierUdp.read(udpIn, packetSize);
|
|
|
|
|
|
|
|
//wled notifier, block if realtime packets active
|
|
|
|
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
|
|
|
|
|
|
|
|
bool someSel = (receiveNotificationBrightness || receiveNotificationColor || receiveNotificationEffects);
|
|
|
|
//apply colors from notification
|
|
|
|
if (receiveNotificationColor || !someSel)
|
2016-11-20 00:07:04 +01:00
|
|
|
{
|
2020-05-18 16:36:31 +02:00
|
|
|
col[0] = udpIn[3];
|
|
|
|
col[1] = udpIn[4];
|
|
|
|
col[2] = udpIn[5];
|
|
|
|
if (udpIn[11] > 0) //sending module's white val is intended
|
2016-12-31 00:38:51 +01:00
|
|
|
{
|
2020-05-18 16:36:31 +02:00
|
|
|
col[3] = udpIn[10];
|
|
|
|
if (udpIn[11] > 1)
|
2018-03-15 13:03:50 +01:00
|
|
|
{
|
2020-05-18 16:36:31 +02:00
|
|
|
colSec[0] = udpIn[12];
|
|
|
|
colSec[1] = udpIn[13];
|
|
|
|
colSec[2] = udpIn[14];
|
|
|
|
colSec[3] = udpIn[15];
|
|
|
|
}
|
|
|
|
if (udpIn[11] > 5)
|
|
|
|
{
|
|
|
|
uint32_t t = (udpIn[25] << 24) | (udpIn[26] << 16) | (udpIn[27] << 8) | (udpIn[28]);
|
|
|
|
t += 2;
|
|
|
|
t -= millis();
|
|
|
|
strip.timebase = t;
|
|
|
|
}
|
|
|
|
if (udpIn[11] > 6)
|
|
|
|
{
|
|
|
|
strip.setColor(2, udpIn[20], udpIn[21], udpIn[22], udpIn[23]); //tertiary color
|
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
|
|
|
|
2020-05-18 16:36:31 +02:00
|
|
|
//apply effects from notification
|
|
|
|
if (udpIn[11] < 200 && (receiveNotificationEffects || !someSel))
|
2018-11-09 17:00:36 +01:00
|
|
|
{
|
2020-05-18 16:36:31 +02:00
|
|
|
if (udpIn[8] < strip.getModeCount()) effectCurrent = udpIn[8];
|
|
|
|
effectSpeed = udpIn[9];
|
|
|
|
if (udpIn[11] > 2) effectIntensity = udpIn[16];
|
|
|
|
if (udpIn[11] > 4 && udpIn[19] < strip.getPaletteCount()) effectPalette = udpIn[19];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (udpIn[11] > 3)
|
|
|
|
{
|
|
|
|
transitionDelayTemp = ((udpIn[17] << 0) & 0xFF) + ((udpIn[18] << 8) & 0xFF00);
|
|
|
|
}
|
|
|
|
|
|
|
|
nightlightActive = udpIn[6];
|
|
|
|
if (nightlightActive) nightlightDelayMins = udpIn[7];
|
|
|
|
|
|
|
|
if (receiveNotificationBrightness || !someSel) bri = udpIn[2];
|
|
|
|
colorUpdated(NOTIFIER_CALL_MODE_NOTIFICATION);
|
|
|
|
|
|
|
|
}
|
|
|
|
if (!receiveDirect) return;
|
|
|
|
|
|
|
|
//TPM2.NET
|
|
|
|
if (udpIn[0] == 0x9c)
|
|
|
|
{
|
|
|
|
byte tpmType = udpIn[1];
|
|
|
|
if (tpmType == 0xaa) { //TPM2.NET polling, expect answer
|
|
|
|
sendTPM2Ack(); return;
|
|
|
|
}
|
|
|
|
if (tpmType == 0xda) { //TPM2.NET data
|
2018-11-09 17:00:36 +01:00
|
|
|
realtimeIP = notifierUdp.remoteIP();
|
2020-05-18 16:36:31 +02:00
|
|
|
realtimeLock(realtimeTimeoutMs, REALTIME_MODE_TPM2NET);
|
|
|
|
if (realtimeOverride) return;
|
2020-04-30 01:52:36 +02:00
|
|
|
|
2020-05-18 16:36:31 +02:00
|
|
|
uint16_t frameSize = (udpIn[2] << 8) + udpIn[3];
|
|
|
|
byte packetNum = udpIn[4]; //starts with 1!
|
|
|
|
byte numPackets = udpIn[5];
|
|
|
|
|
|
|
|
uint16_t id = tpmDataReceived / 3; //start LED
|
|
|
|
for (uint16_t i = 6; i < frameSize + 4; 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);
|
|
|
|
id++; if (id >= ledCount) break;
|
2020-04-30 01:52:36 +02:00
|
|
|
}
|
2020-05-18 16:36:31 +02:00
|
|
|
tpmDataReceived += frameSize;
|
|
|
|
if (packetNum == numPackets) {strip.show(); tpmDataReceived = 0;} //show if last packet
|
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
realtimeIP = notifierUdp.remoteIP();
|
|
|
|
DEBUG_PRINTLN(notifierUdp.remoteIP());
|
|
|
|
if (packetSize < 2) return;
|
|
|
|
|
|
|
|
if (udpIn[1] == 0)
|
|
|
|
{
|
|
|
|
realtimeTimeout = 0;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
realtimeLock(udpIn[1]*1000 +1, REALTIME_MODE_UDP);
|
|
|
|
}
|
|
|
|
if (realtimeOverride) return;
|
|
|
|
|
|
|
|
if (udpIn[0] == 1) //warls
|
|
|
|
{
|
|
|
|
for (uint16_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;
|
|
|
|
for (uint16_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
|
|
|
|
2020-05-18 16:36:31 +02:00
|
|
|
id++; if (id >= ledCount) break;
|
|
|
|
}
|
|
|
|
} else if (udpIn[0] == 3) //drgbw
|
|
|
|
{
|
|
|
|
uint16_t id = 0;
|
|
|
|
for (uint16_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]);
|
|
|
|
|
|
|
|
id++; if (id >= ledCount) break;
|
|
|
|
}
|
|
|
|
} else if (udpIn[0] == 4) //dnrgb
|
|
|
|
{
|
|
|
|
uint16_t id = ((udpIn[3] << 0) & 0xFF) + ((udpIn[2] << 8) & 0xFF00);
|
|
|
|
for (uint16_t i = 4; i < packetSize -2; i += 3)
|
2020-04-30 01:52:36 +02:00
|
|
|
{
|
2020-05-18 16:36:31 +02:00
|
|
|
if (id >= ledCount) break;
|
|
|
|
setRealtimePixel(id, udpIn[i], udpIn[i+1], udpIn[i+2], 0);
|
|
|
|
id++;
|
2016-11-27 22:37:51 +01:00
|
|
|
}
|
2016-11-20 00:07:04 +01:00
|
|
|
}
|
2020-05-18 16:36:31 +02:00
|
|
|
strip.show();
|
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;
|
|
|
|
if (pix < ledCount)
|
2018-08-11 22:57:13 +02:00
|
|
|
{
|
2019-05-22 00:23:09 +02:00
|
|
|
if (!arlsDisableGammaCorrection && strip.gammaCorrectCol)
|
2018-08-11 22:57:13 +02:00
|
|
|
{
|
2019-05-22 00:23:09 +02:00
|
|
|
strip.setPixelColor(pix, strip.gamma8(r), strip.gamma8(g), strip.gamma8(b), strip.gamma8(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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|