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+
49class 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 () {
0 commit comments