Skip to content

Commit 52ceabb

Browse files
committed
LDR_Dusk_Dawn: use pinManager, check ldrPin before use (quick-fix for wled#3490)
1 parent 075cc2d commit 52ceabb

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

usermods/LDR_Dusk_Dawn_v2/usermod_LDR_Dusk_Dawn_v2.h

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#pragma once
22
#include "wled.h"
33

4+
#ifndef ARDUINO_ARCH_ESP32
5+
// 8266 does not support analogRead on user selectable pins
6+
#error only ESP32 is supported by usermod LDR_DUSK_DAWN
7+
#endif
8+
49
class LDR_Dusk_Dawn_v2 : public Usermod {
510
private:
611
// Defaults
@@ -12,22 +17,30 @@ class LDR_Dusk_Dawn_v2 : public Usermod {
1217
int ldrOffPreset = 2; // Default "Off" Preset
1318

1419
// Variables
20+
bool initDone = false;
1521
bool ldrEnabledPreviously = false; // Was LDR enabled for the previous check? First check is always no.
1622
int ldrOffCount; // Number of readings above the threshold
1723
int ldrOnCount; // Number of readings below the threshold
18-
int ldrReading; // Last LDR reading
24+
int ldrReading = 0; // Last LDR reading
1925
int ldrLEDState; // Current LED on/off state
2026
unsigned long lastMillis = 0;
2127
static const char _name[];
2228

2329
public:
2430
void setup() {
31+
// register ldrPin
32+
if ((ldrPin >= 0) && (digitalPinToAnalogChannel(ldrPin) >= 0)) {
33+
if(!pinManager.allocatePin(ldrPin, false, PinOwner::UM_LDR_DUSK_DAWN)) ldrEnabled = false; // pin already in use -> disable usermod
34+
else pinMode(ldrPin, INPUT); // alloc success -> configure pin for input
35+
} else ldrEnabled = false; // invalid pin -> disable usermod
36+
initDone = true;
2537
}
2638

2739
void loop() {
2840
// Only update every 10 seconds
2941
if (millis() - lastMillis > 10000) {
30-
if (ldrEnabled == true) {
42+
if ( (ldrEnabled == true)
43+
&& (ldrPin >= 0) && (digitalPinToAnalogChannel(ldrPin) >= 0) ) { // make sure that pin is valid for analogread()
3144
// Default state is off
3245
if (ldrEnabledPreviously == false) {
3346
applyPreset(ldrOffPreset);
@@ -85,6 +98,7 @@ class LDR_Dusk_Dawn_v2 : public Usermod {
8598
}
8699

87100
bool readFromConfig(JsonObject& root) {
101+
int8_t oldLdrPin = ldrPin;
88102
JsonObject top = root[FPSTR(_name)];
89103
bool configComplete = !top.isNull();
90104
configComplete &= getJsonValue(top["Enabled"], ldrEnabled);
@@ -93,6 +107,12 @@ class LDR_Dusk_Dawn_v2 : public Usermod {
93107
configComplete &= getJsonValue(top["Threshold"], ldrThreshold);
94108
configComplete &= getJsonValue(top["On Preset"], ldrOnPreset);
95109
configComplete &= getJsonValue(top["Off Preset"], ldrOffPreset);
110+
111+
if (initDone && (ldrPin != oldLdrPin)) {
112+
// pin changed - un-register previous pin, register new pin
113+
if (oldLdrPin >= 0) pinManager.deallocatePin(oldLdrPin, PinOwner::UM_LDR_DUSK_DAWN);
114+
setup(); // setup new pin
115+
}
96116
return configComplete;
97117
}
98118

@@ -102,7 +122,8 @@ class LDR_Dusk_Dawn_v2 : public Usermod {
102122
if (user.isNull()) user = root.createNestedObject("u");
103123

104124
JsonArray LDR_Enabled = user.createNestedArray("LDR dusk/dawn enabled");
105-
LDR_Enabled.add(ldrEnabled);
125+
LDR_Enabled.add(ldrEnabled);
126+
if (!ldrEnabled) return; // do not add more if usermod is disabled
106127

107128
JsonArray LDR_Reading = user.createNestedArray("LDR reading");
108129
LDR_Reading.add(ldrReading);
@@ -116,6 +137,12 @@ class LDR_Dusk_Dawn_v2 : public Usermod {
116137

117138
//JsonArray LDR_Off_Count = user.createNestedArray("LDR off count");
118139
//LDR_Off_Count.add(ldrOffCount);
140+
141+
//bool pinValid = ((ldrPin >= 0) && (digitalPinToAnalogChannel(ldrPin) >= 0));
142+
//if (pinManager.getPinOwner(ldrPin) != PinOwner::UM_LDR_DUSK_DAWN) pinValid = false;
143+
//JsonArray LDR_valid = user.createNestedArray(F("LDR pin"));
144+
//LDR_valid.add(ldrPin);
145+
//LDR_valid.add(pinValid ? F(" OK"): F(" invalid"));
119146
}
120147

121148
uint16_t getId() {

wled00/pin_manager.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ enum struct PinOwner : uint8_t {
6060
UM_BME280 = USERMOD_ID_BME280, // 0x1E // Usermod "usermod_bme280.h -- Uses "standard" HW_I2C pins
6161
UM_Audioreactive = USERMOD_ID_AUDIOREACTIVE, // 0x20 // Usermod "audio_reactive.h"
6262
UM_SdCard = USERMOD_ID_SD_CARD, // 0x25 // Usermod "usermod_sd_card.h"
63-
UM_PWM_OUTPUTS = USERMOD_ID_PWM_OUTPUTS // 0x26 // Usermod "usermod_pwm_outputs.h"
63+
UM_PWM_OUTPUTS = USERMOD_ID_PWM_OUTPUTS, // 0x26 // Usermod "usermod_pwm_outputs.h"
64+
UM_LDR_DUSK_DAWN = USERMOD_ID_LDR_DUSK_DAWN // 0x2B // Usermod "usermod_LDR_Dusk_Dawn_v2.h"
6465
};
6566
static_assert(0u == static_cast<uint8_t>(PinOwner::None), "PinOwner::None must be zero, so default array initialization works as expected");
6667

0 commit comments

Comments
 (0)