-
-
Notifications
You must be signed in to change notification settings - Fork 4k
PoC: Generic Sensor Handling #5344
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
Draft
JoaDick
wants to merge
11
commits into
wled:main
Choose a base branch
from
JoaDick:Sensor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,130
−8
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
79163dd
Sensor: initial commit
JoaDick 8117c7a
Sensor: added cursors
JoaDick 3171c65
Sensor: first examples
JoaDick 04b7bf6
Sensor: concept of "channel quantity" (measurand)
JoaDick bfea4dc
Sensor: helper class EasySensorArray
JoaDick fe43dae
Sensor: more examples
JoaDick ce4cf8a
Sensor: some cleanup
JoaDick ef342d2
Sensor: new EasySensorBase
JoaDick 0109780
Sensor: simplified cursors
JoaDick bdb060b
Added comments, incorporated rabbit findings
JoaDick 20dc817
Merge branch 'main' into Sensor
JoaDick 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
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
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 @@ | ||
| # Dummy usermod to simulate random sensor readings. | ||
|
|
||
| Use `UM_SensorInfo` and `UM_SensorDummy` together as an example for how sensors are implemented, | ||
| and how its data can be retrieved. The generated sensor data can be processed by effects and other | ||
| usermods - without directly knowing the sensor and its specific type of data. |
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,105 @@ | ||
| /** | ||
| * (c) 2026 Joachim Dick | ||
| * Licensed under the EUPL v. 1.2 or later | ||
| */ | ||
|
|
||
| #include "wled.h" | ||
|
|
||
| //-------------------------------------------------------------------------------------------------- | ||
|
|
||
| /** Dummy usermod implementation that simulates random sensor readings. | ||
| */ | ||
| class UM_SensorDummy : public Usermod, public Sensor | ||
| { | ||
| public: | ||
| UM_SensorDummy() : Sensor{"SEF", 4} {} | ||
|
|
||
| // ----- usermod functions ----- | ||
|
|
||
| void setup() override {} | ||
|
|
||
| void loop() override | ||
| { | ||
| const auto now = millis(); | ||
| if (now < _nextUpdateTime) | ||
| return; | ||
| readWeatherStation(); | ||
| _nextUpdateTime = now + 20; // 50 sensor updates per second | ||
| } | ||
|
|
||
| uint8_t getSensorCount() override { return 2; } | ||
|
|
||
| Sensor *getSensor(uint8_t index) override | ||
| { | ||
| if (index == 0) | ||
| return &_sensorArray; | ||
| return this; | ||
| } | ||
|
|
||
| bool do_isSensorReady() override { return true; } | ||
| SensorValue do_getSensorChannelValue(uint8_t channelIndex) override { return readSEF(channelIndex); } | ||
| const SensorChannelProps &do_getSensorChannelProperties(uint8_t channelIndex) override { return _localSensorProps[channelIndex]; } | ||
|
|
||
| // ----- internal processing functions ----- | ||
|
|
||
| void readWeatherStation() | ||
| { | ||
| _sensorArray.set(0, 1012.34f); | ||
| _sensorArray.set(1, readTemperature()); | ||
| _sensorArray.set(2, readHumidity()); | ||
| #if (0) // Battery is empty :-( | ||
| _sensorArray.set(3, readTemperature() - 3.0f); | ||
| _sensorArray.set(4, readHumidity() - 5.0f); | ||
| #endif | ||
| } | ||
|
|
||
| /// The dummy implementation to simulate temperature values (based on perlin noise). | ||
| float readTemperature() | ||
| { | ||
| const int32_t raw = perlin16(strip.now * 8) - 0x8000; | ||
| // simulate some random temperature around 20°C | ||
| return 20.0f + raw / 65535.0f * 30.0f; | ||
| } | ||
|
|
||
| /// The dummy implementation to simulate humidity values (a sine wave). | ||
| float readHumidity() | ||
| { | ||
| const int32_t raw = beatsin16_t(1); | ||
| // simulate some random humidity between 10% and 90% | ||
| return 10.0f + raw / 65535.0f * 80.0f; | ||
| } | ||
|
|
||
| float readSEF(uint8_t index) | ||
| { | ||
| if (index >= 3) | ||
| { | ||
| const int32_t raw = abs(beat16(20) - 0x8000); | ||
| return raw / 32767.0f * 100.0f; | ||
| } | ||
| const int32_t raw = beatsin16_t(40, 0, 0xFFFF, 0, (index * 0xFFFF) / 3); | ||
| return 90.0f + raw / 65535.0f * 90.0f; | ||
| } | ||
|
|
||
| // ----- member variables ----- | ||
|
|
||
| uint32_t _nextUpdateTime = 0; | ||
|
|
||
| const SensorChannelPropsArray<4> _localSensorProps = | ||
| {{makeChannelProps_Float("deltaX", {"offset", "°rad"}, 0.0f, 360.0f), | ||
| makeChannelProps_Float("deltaY", {"offset", "°rad"}, 0.0f, 360.0f), | ||
| makeChannelProps_Float("deltaZ", {"offset", "°rad"}, 0.0f, 360.0f), | ||
| makeChannelProps_Float("deltaT", {"jitter", "µs"}, -1000.0f, 1000.0f)}}; | ||
|
|
||
| EasySensorArray<5> _sensorArray{"Weather Station", | ||
| {{SensorChannelProps{"Barometer", | ||
| SensorQuantity::AirPressure(), 950.0f, 1050.0f}, | ||
| makeChannelProps_Temperature("Indoor Temp."), | ||
| makeChannelProps_Humidity("Indoor Hum."), | ||
| makeChannelProps_Temperature("Outdoor Temp."), | ||
| makeChannelProps_Humidity("Outdoor Hum.")}}}; | ||
| }; | ||
|
|
||
| //-------------------------------------------------------------------------------------------------- | ||
|
|
||
| static UM_SensorDummy um_SensorDummy; | ||
| REGISTER_USERMOD(um_SensorDummy); | ||
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,4 @@ | ||
| { | ||
| "name": "UM_SensorDummy", | ||
| "build": { "libArchive": false } | ||
| } |
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 @@ | ||
| # Examples for working with sensors. | ||
|
|
||
| Use `UM_SensorInfo` and `UM_SensorDummy` together as an example for how sensors are implemented, | ||
| and how its data can be retrieved. The generated sensor data can be processed by effects and other | ||
| usermods - without directly knowing the sensor and its specific type of data. |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
getSensorindex handling is inverted and potentially buggy.The current logic returns:
_sensorArray(Weather Station) whenindex == 0this(SEF sensor) whenindex != 0(including 1, 2, 3, etc.)This means any index ≥ 2 also returns the SEF sensor, which is inconsistent with
getSensorCount()returning 2. Consider explicit bounds checking:🐛 Proposed fix
Sensor *getSensor(uint8_t index) override { - if (index != 0) - return this; - return &_sensorArray; + if (index == 0) + return &_sensorArray; + if (index == 1) + return this; + return nullptr; }🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@coderabbitai Added comments that invalid indices result in undefined behaviour (just like out of bound array access) -- thus anything can happen. In those cases, I prefer "the UB" to be a valid (although unexpected) sensor over a nullptr to avoid crashes.
So unexperienced developers can recognize that something is not working as expected, which is more helpful and less frustrating than crashes. And it avoids range checks that are not really beneficial.
Aside from that, do we have adequate assert macros in WLED? Those would be appropriate here, assuming that debug builds are a relevant thing at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Length of output: 2844
🏁 Script executed:
Length of output: 6503
@JoaDick, I completely understand your rationale—returning a valid sensor provides more graceful degradation than nullptr crashes, especially helpful for learning developers who can see "wrong behavior" rather than hitting a segfault.Based on my search of the codebase, here's what's available for assertions:
Standard C assert(): Available and already used in WLED. For example, in
wled00/um_manager.cpp:96:assert(!oappend_shim);Debug builds: WLED supports debug builds via the
WLED_DEBUGflag, which enables:DEBUG_PRINT(x),DEBUG_PRINTLN(x),DEBUG_PRINTF(x...)macros (defined inwled00/wled.h)WLED_DEBUGis not definedWLED_DEBUG_MATHfor specific subsystemsNo WLED-specific assert macro currently exists. If you want consistent assertion behavior across the codebase, you could consider adding something like:
However, given your preference for graceful degradation and the minimal use of assertions in the current codebase, documenting the UB behavior in comments (as you've done) seems like a pragmatic approach that aligns well with WLED's design philosophy.
✏️ Learnings added
🧠 Learnings used