Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions usermods/udp_name_sync/library.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "udp_name_sync",
"build": { "libArchive": false },
"dependencies": {}
}
79 changes: 79 additions & 0 deletions usermods/udp_name_sync/udp_name_sync.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include "wled.h"

class UdpNameSync : public Usermod {

private:

bool enabled = false;
bool initDone = false;
unsigned long lastTime = 0;
char segmentName[WLED_MAX_SEGNAME_LEN] = {0};
static const char _name[];
static const char _enabled[];

public:
/**
* Enable/Disable the usermod
*/
inline void enable(bool enable) { enabled = enable; }

/**
* Get usermod enabled/disabled state
*/
inline bool isEnabled() { return enabled; }

void setup() override {
initDone = true;
}

void loop() override {
if (!WLED_CONNECTED) return;
if (!udpConnected) return;
Segment& mainseg = strip.getMainSegment();
if (!strlen(segmentName) && !mainseg.name) return; //name was never set, do nothing

Comment thread
coderabbitai[bot] marked this conversation as resolved.
IPAddress broadcastIp = ~uint32_t(Network.subnetMask()) | uint32_t(Network.gatewayIP());
byte udpOut[WLED_MAX_SEGNAME_LEN + 2];
udpOut[0] = 2; // 0: wled notifier protocol, 1: warls protocol, 2 is free

Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
if (strlen(segmentName) && !mainseg.name) { //name is back to null
notifierUdp.beginPacket(broadcastIp, udpPort);
strcpy(segmentName,"");
DEBUG_PRINTLN(F("UdpNameSync: sending Null name"));
notifierUdp.write( udpOut , 2);
notifierUdp.endPacket();
return;
}
Comment thread
Liliputech marked this conversation as resolved.
Outdated

if (0 == strcmp(mainseg.name, segmentName)) return; //same name, do nothing
Comment thread
netmindz marked this conversation as resolved.
Outdated
Comment thread
Liliputech marked this conversation as resolved.
Outdated

notifierUdp.beginPacket(broadcastIp, udpPort);
DEBUG_PRINT(F("UdpNameSync: saving segment name "));
DEBUG_PRINTLN(mainseg.name);
byte length = strlen(mainseg.name);
strlcpy(segmentName, mainseg.name, length+1);
strlcpy((char *)&udpOut[1], segmentName, length+1);
notifierUdp.write(udpOut, length + 2);
notifierUdp.endPacket();
Comment thread
netmindz marked this conversation as resolved.
Outdated
DEBUG_PRINT(F("UdpNameSync: Sent segment name : "));
DEBUG_PRINTLN(segmentName);
}

bool onUdpPacket(uint8_t * payload, uint8_t len) override {
DEBUG_PRINT(F("UdpNameSync: Received packet"));
if (payload[0] != 2) return false;
//else
Segment& mainseg = strip.getMainSegment();
mainseg.setName((char *)&payload[1]);
DEBUG_PRINT(F("UdpNameSync: set segment name"));
return true;
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
};


// add more strings here to reduce flash memory usage
const char UdpNameSync::_name[] PROGMEM = "UdpNameSync";
const char UdpNameSync::_enabled[] PROGMEM = "enabled";

static UdpNameSync udp_name_sync;
REGISTER_USERMOD(udp_name_sync);
2 changes: 2 additions & 0 deletions wled00/fcn_declare.h
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ class Usermod {
virtual void onMqttConnect(bool sessionPresent) {} // fired when MQTT connection is established (so usermod can subscribe)
virtual bool onMqttMessage(char* topic, char* payload) { return false; } // fired upon MQTT message received (wled topic)
virtual bool onEspNowMessage(uint8_t* sender, uint8_t* payload, uint8_t len) { return false; } // fired upon ESP-NOW message received
virtual bool onUdpPacket(uint8_t* payload, uint8_t len) { return false; } //fired upon UDP packet received
virtual void onUpdateBegin(bool) {} // fired prior to and after unsuccessful firmware update
Comment thread
Liliputech marked this conversation as resolved.
Outdated
virtual void onStateChange(uint8_t mode) {} // fired upon WLED state change
virtual uint16_t getId() {return USERMOD_ID_UNSPECIFIED;}
Expand Down Expand Up @@ -481,6 +482,7 @@ namespace UsermodManager {
#ifndef WLED_DISABLE_ESPNOW
bool onEspNowMessage(uint8_t* sender, uint8_t* payload, uint8_t len);
#endif
bool onUdpPacket(uint8_t* payload, uint8_t len);
void onUpdateBegin(bool);
void onStateChange(uint8_t);
Usermod* lookup(uint16_t mod_id);
Expand Down
3 changes: 3 additions & 0 deletions wled00/udp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,9 @@ void handleNotifications()
return;
}

// usermods hook can override processing
if (UsermodManager::onUdpPacket(udpIn, packetSize)) return;
Comment thread
Liliputech marked this conversation as resolved.
Outdated

Comment thread
Liliputech marked this conversation as resolved.
Outdated
//wled notifier, ignore if realtime packets active
if (udpIn[0] == 0 && !realtimeMode && receiveGroups)
{
Expand Down
4 changes: 4 additions & 0 deletions wled00/um_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ bool UsermodManager::onEspNowMessage(uint8_t* sender, uint8_t* payload, uint8_t
return false;
}
#endif
bool UsermodManager::onUdpPacket(uint8_t* payload, uint8_t len) {
for (auto mod = _usermod_table_begin; mod < _usermod_table_end; ++mod) if ((*mod)->onUdpPacket(payload, len)) return true;
return false;
}
Comment thread
netmindz marked this conversation as resolved.
Outdated
void UsermodManager::onUpdateBegin(bool init) { for (auto mod = _usermod_table_begin; mod < _usermod_table_end; ++mod) (*mod)->onUpdateBegin(init); } // notify usermods that update is to begin
void UsermodManager::onStateChange(uint8_t mode) { for (auto mod = _usermod_table_begin; mod < _usermod_table_end; ++mod) (*mod)->onStateChange(mode); } // notify usermods that WLED state changed

Expand Down