Skip to content

Commit 4b5c3a3

Browse files
committed
applied suggestions from review
1 parent 550b4d9 commit 4b5c3a3

File tree

2 files changed

+71
-88
lines changed

2 files changed

+71
-88
lines changed

usermods/udp_name_sync/udp_name_sync.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,8 @@ class UdpNameSync : public Usermod {
4646
return;
4747
}
4848

49-
char checksumSegName = 0;
50-
char checksumCurName = 0;
51-
for(int i=0; i++; mainseg.name[i]==0 || segmentName[i]==0) {
52-
checksumSegName+=segmentName[i];
53-
checksumCurName+=mainseg.name[i];
54-
}
55-
if (checksumCurName == checksumSegName) return; // same name, do nothing
49+
const char* curName = mainseg.name ? mainseg.name : "";
50+
if (strncmp(curName, segmentName, sizeof(segmentName)) == 0) return; // same name, do nothing
5651

5752
notifierUdp.beginPacket(broadcastIp, udpPort);
5853
DEBUG_PRINT(F("UdpNameSync: saving segment name "));

wled00/udp.cpp

Lines changed: 69 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -557,9 +557,6 @@ void handleNotifications()
557557
return;
558558
}
559559

560-
// usermods hook can override processing
561-
if (UsermodManager::onUdpPacket(udpIn, packetSize)) return;
562-
563560
//wled notifier, ignore if realtime packets active
564561
if (udpIn[0] == 0 && !realtimeMode && receiveGroups)
565562
{
@@ -568,93 +565,82 @@ void handleNotifications()
568565
return;
569566
}
570567

571-
if (!receiveDirect) return;
572-
573-
//TPM2.NET
574-
if (udpIn[0] == 0x9c)
575-
{
576-
//WARNING: this code assumes that the final TMP2.NET payload is evenly distributed if using multiple packets (ie. frame size is constant)
577-
//if the number of LEDs in your installation doesn't allow that, please include padding bytes at the end of the last packet
578-
byte tpmType = udpIn[1];
579-
if (tpmType == 0xaa) { //TPM2.NET polling, expect answer
580-
sendTPM2Ack(); return;
581-
}
582-
if (tpmType != 0xda) return; //return if notTPM2.NET data
583-
584-
realtimeIP = (isSupp) ? notifier2Udp.remoteIP() : notifierUdp.remoteIP();
585-
realtimeLock(realtimeTimeoutMs, REALTIME_MODE_TPM2NET);
586-
if (realtimeOverride) return;
587-
588-
tpmPacketCount++; //increment the packet count
589-
if (tpmPacketCount == 1) tpmPayloadFrameSize = (udpIn[2] << 8) + udpIn[3]; //save frame size for the whole payload if this is the first packet
590-
byte packetNum = udpIn[4]; //starts with 1!
591-
byte numPackets = udpIn[5];
568+
if (receiveDirect) {
569+
//TPM2.NET
570+
if (udpIn[0] == 0x9c) {
571+
//WARNING: this code assumes that the final TMP2.NET payload is evenly distributed if using multiple packets (ie. frame size is constant)
572+
//if the number of LEDs in your installation doesn't allow that, please include padding bytes at the end of the last packet
573+
byte tpmType = udpIn[1];
574+
if (tpmType == 0xaa) { //TPM2.NET polling, expect answer
575+
sendTPM2Ack(); return;
576+
}
577+
if (tpmType != 0xda) return; //return if notTPM2.NET data
592578

593-
unsigned id = (tpmPayloadFrameSize/3)*(packetNum-1); //start LED
594-
unsigned totalLen = strip.getLengthTotal();
595-
for (size_t i = 6; i < tpmPayloadFrameSize + 4U && id < totalLen; i += 3, id++) {
596-
setRealtimePixel(id, udpIn[i], udpIn[i+1], udpIn[i+2], 0);
597-
}
598-
if (tpmPacketCount == numPackets) { //reset packet count and show if all packets were received
599-
tpmPacketCount = 0;
600-
if (useMainSegmentOnly) strip.trigger();
601-
else strip.show();
602-
}
603-
return;
604-
}
579+
realtimeIP = (isSupp) ? notifier2Udp.remoteIP() : notifierUdp.remoteIP();
580+
realtimeLock(realtimeTimeoutMs, REALTIME_MODE_TPM2NET);
581+
if (realtimeOverride) return;
605582

606-
//UDP realtime: 1 warls 2 drgb 3 drgbw 4 dnrgb 5 dnrgbw
607-
if (udpIn[0] > 0 && udpIn[0] < 6)
608-
{
609-
realtimeIP = (isSupp) ? notifier2Udp.remoteIP() : notifierUdp.remoteIP();
610-
DEBUG_PRINTLN(realtimeIP);
611-
if (packetSize < 2) return;
583+
tpmPacketCount++; //increment the packet count
584+
if (tpmPacketCount == 1) tpmPayloadFrameSize = (udpIn[2] << 8) + udpIn[3]; //save frame size for the whole payload if this is the first packet
585+
byte packetNum = udpIn[4]; //starts with 1!
586+
byte numPackets = udpIn[5];
612587

613-
if (udpIn[1] == 0) {
614-
realtimeTimeout = 0; // cancel realtime mode immediately
588+
unsigned id = (tpmPayloadFrameSize/3)*(packetNum-1); //start LED
589+
unsigned totalLen = strip.getLengthTotal();
590+
for (size_t i = 6; i < tpmPayloadFrameSize + 4U && id < totalLen; i += 3, id++) {
591+
setRealtimePixel(id, udpIn[i], udpIn[i+1], udpIn[i+2], 0);
592+
}
593+
if (tpmPacketCount == numPackets) { //reset packet count and show if all packets were received
594+
tpmPacketCount = 0;
595+
if (useMainSegmentOnly) strip.trigger();
596+
else strip.show();
597+
}
615598
return;
616-
} else {
617-
realtimeLock(udpIn[1]*1000 +1, REALTIME_MODE_UDP);
618599
}
619-
if (realtimeOverride) return;
620600

621-
unsigned totalLen = strip.getLengthTotal();
622-
if (udpIn[0] == 1 && packetSize > 5) //warls
623-
{
624-
for (size_t i = 2; i < packetSize -3; i += 4)
625-
{
626-
setRealtimePixel(udpIn[i], udpIn[i+1], udpIn[i+2], udpIn[i+3], 0);
627-
}
628-
} else if (udpIn[0] == 2 && packetSize > 4) //drgb
629-
{
630-
for (size_t i = 2, id = 0; i < packetSize -2 && id < totalLen; i += 3, id++)
631-
{
632-
setRealtimePixel(id, udpIn[i], udpIn[i+1], udpIn[i+2], 0);
633-
}
634-
} else if (udpIn[0] == 3 && packetSize > 6) //drgbw
635-
{
636-
for (size_t i = 2, id = 0; i < packetSize -3 && id < totalLen; i += 4, id++)
637-
{
638-
setRealtimePixel(id, udpIn[i], udpIn[i+1], udpIn[i+2], udpIn[i+3]);
639-
}
640-
} else if (udpIn[0] == 4 && packetSize > 7) //dnrgb
641-
{
642-
unsigned id = ((udpIn[3] << 0) & 0xFF) + ((udpIn[2] << 8) & 0xFF00);
643-
for (size_t i = 4; i < packetSize -2 && id < totalLen; i += 3, id++)
644-
{
645-
setRealtimePixel(id, udpIn[i], udpIn[i+1], udpIn[i+2], 0);
601+
//UDP realtime: 1 warls 2 drgb 3 drgbw 4 dnrgb 5 dnrgbw
602+
if (udpIn[0] > 0 && udpIn[0] < 6) {
603+
realtimeIP = (isSupp) ? notifier2Udp.remoteIP() : notifierUdp.remoteIP();
604+
DEBUG_PRINTLN(realtimeIP);
605+
if (packetSize < 2) return;
606+
607+
if (udpIn[1] == 0) {
608+
realtimeTimeout = 0; // cancel realtime mode immediately
609+
return;
610+
} else {
611+
realtimeLock(udpIn[1]*1000 +1, REALTIME_MODE_UDP);
646612
}
647-
} else if (udpIn[0] == 5 && packetSize > 8) //dnrgbw
648-
{
649-
unsigned id = ((udpIn[3] << 0) & 0xFF) + ((udpIn[2] << 8) & 0xFF00);
650-
for (size_t i = 4; i < packetSize -2 && id < totalLen; i += 4, id++)
651-
{
652-
setRealtimePixel(id, udpIn[i], udpIn[i+1], udpIn[i+2], udpIn[i+3]);
613+
if (realtimeOverride) return;
614+
615+
unsigned totalLen = strip.getLengthTotal();
616+
if (udpIn[0] == 1 && packetSize > 5) { //warls
617+
for (size_t i = 2; i < packetSize -3; i += 4) {
618+
setRealtimePixel(udpIn[i], udpIn[i+1], udpIn[i+2], udpIn[i+3], 0);
619+
}
620+
} else if (udpIn[0] == 2 && packetSize > 4) { //drgb
621+
for (size_t i = 2, id = 0; i < packetSize -2 && id < totalLen; i += 3, id++)
622+
{
623+
setRealtimePixel(id, udpIn[i], udpIn[i+1], udpIn[i+2], 0);
624+
}
625+
} else if (udpIn[0] == 3 && packetSize > 6) { //drgbw
626+
for (size_t i = 2, id = 0; i < packetSize -3 && id < totalLen; i += 4, id++) {
627+
setRealtimePixel(id, udpIn[i], udpIn[i+1], udpIn[i+2], udpIn[i+3]);
628+
}
629+
} else if (udpIn[0] == 4 && packetSize > 7) { //dnrgb
630+
unsigned id = ((udpIn[3] << 0) & 0xFF) + ((udpIn[2] << 8) & 0xFF00);
631+
for (size_t i = 4; i < packetSize -2 && id < totalLen; i += 3, id++) {
632+
setRealtimePixel(id, udpIn[i], udpIn[i+1], udpIn[i+2], 0);
633+
}
634+
} else if (udpIn[0] == 5 && packetSize > 8) { //dnrgbw
635+
unsigned id = ((udpIn[3] << 0) & 0xFF) + ((udpIn[2] << 8) & 0xFF00);
636+
for (size_t i = 4; i < packetSize -2 && id < totalLen; i += 4, id++) {
637+
setRealtimePixel(id, udpIn[i], udpIn[i+1], udpIn[i+2], udpIn[i+3]);
638+
}
653639
}
640+
if (useMainSegmentOnly) strip.trigger();
641+
else strip.show();
642+
return;
654643
}
655-
if (useMainSegmentOnly) strip.trigger();
656-
else strip.show();
657-
return;
658644
}
659645

660646
// API over UDP
@@ -672,6 +658,8 @@ void handleNotifications()
672658
}
673659
releaseJSONBufferLock();
674660
}
661+
662+
UsermodManager::onUdpPacket(udpIn, packetSize);
675663
}
676664

677665

0 commit comments

Comments
 (0)