-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
new usermod hooks "onUdpPacket" #4859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4de6656
new usermod hooks "onUdpPacket"
Liliputech 2082b01
Apply suggestions from code review
netmindz f3e3f58
usermod udp_name_sync : properly initialize packet if segment name is…
Liliputech 550b4d9
fix comments for usermod hooks "onUdpPacket"
Liliputech 4b5c3a3
applied suggestions from review
Liliputech f8ce598
removed tabs and replace by space
Liliputech a60be25
fix nitpicks from coderabbit
Liliputech 62fad4d
applied coderabbit suggestions
Liliputech c8757d4
fix more nitpicks comments
Liliputech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "name": "udp_name_sync", | ||
| "build": { "libArchive": false }, | ||
| "dependencies": {} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
|
||
| 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 | ||
|
|
||
|
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; | ||
| } | ||
|
Liliputech marked this conversation as resolved.
Outdated
|
||
|
|
||
| if (0 == strcmp(mainseg.name, segmentName)) return; //same name, do nothing | ||
|
netmindz marked this conversation as resolved.
Outdated
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(); | ||
|
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; | ||
| } | ||
|
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); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.