mirror of
https://github.com/wled/WLED.git
synced 2025-07-09 11:56:35 +00:00
LDR_Dusk_Dawn: use pinManager, check ldrPin before use (quick-fix for #3490)
This commit is contained in:
parent
6d28de77d1
commit
da488f76d2
@ -1,6 +1,11 @@
|
||||
#pragma once
|
||||
#include "wled.h"
|
||||
|
||||
#ifndef ARDUINO_ARCH_ESP32
|
||||
// 8266 does not support analogRead on user selectable pins
|
||||
#error only ESP32 is supported by usermod LDR_DUSK_DAWN
|
||||
#endif
|
||||
|
||||
class LDR_Dusk_Dawn_v2 : public Usermod {
|
||||
private:
|
||||
// Defaults
|
||||
@ -12,22 +17,30 @@ class LDR_Dusk_Dawn_v2 : public Usermod {
|
||||
int ldrOffPreset = 2; // Default "Off" Preset
|
||||
|
||||
// Variables
|
||||
bool initDone = false;
|
||||
bool ldrEnabledPreviously = false; // Was LDR enabled for the previous check? First check is always no.
|
||||
int ldrOffCount; // Number of readings above the threshold
|
||||
int ldrOnCount; // Number of readings below the threshold
|
||||
int ldrReading; // Last LDR reading
|
||||
int ldrReading = 0; // Last LDR reading
|
||||
int ldrLEDState; // Current LED on/off state
|
||||
unsigned long lastMillis = 0;
|
||||
static const char _name[];
|
||||
|
||||
public:
|
||||
void setup() {
|
||||
// register ldrPin
|
||||
if ((ldrPin >= 0) && (digitalPinToAnalogChannel(ldrPin) >= 0)) {
|
||||
if(!pinManager.allocatePin(ldrPin, false, PinOwner::UM_LDR_DUSK_DAWN)) ldrEnabled = false; // pin already in use -> disable usermod
|
||||
else pinMode(ldrPin, INPUT); // alloc success -> configure pin for input
|
||||
} else ldrEnabled = false; // invalid pin -> disable usermod
|
||||
initDone = true;
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Only update every 10 seconds
|
||||
if (millis() - lastMillis > 10000) {
|
||||
if (ldrEnabled == true) {
|
||||
if ( (ldrEnabled == true)
|
||||
&& (ldrPin >= 0) && (digitalPinToAnalogChannel(ldrPin) >= 0) ) { // make sure that pin is valid for analogread()
|
||||
// Default state is off
|
||||
if (ldrEnabledPreviously == false) {
|
||||
applyPreset(ldrOffPreset);
|
||||
@ -85,6 +98,7 @@ class LDR_Dusk_Dawn_v2 : public Usermod {
|
||||
}
|
||||
|
||||
bool readFromConfig(JsonObject& root) {
|
||||
int8_t oldLdrPin = ldrPin;
|
||||
JsonObject top = root[FPSTR(_name)];
|
||||
bool configComplete = !top.isNull();
|
||||
configComplete &= getJsonValue(top["Enabled"], ldrEnabled);
|
||||
@ -93,6 +107,12 @@ class LDR_Dusk_Dawn_v2 : public Usermod {
|
||||
configComplete &= getJsonValue(top["Threshold"], ldrThreshold);
|
||||
configComplete &= getJsonValue(top["On Preset"], ldrOnPreset);
|
||||
configComplete &= getJsonValue(top["Off Preset"], ldrOffPreset);
|
||||
|
||||
if (initDone && (ldrPin != oldLdrPin)) {
|
||||
// pin changed - un-register previous pin, register new pin
|
||||
if (oldLdrPin >= 0) pinManager.deallocatePin(oldLdrPin, PinOwner::UM_LDR_DUSK_DAWN);
|
||||
setup(); // setup new pin
|
||||
}
|
||||
return configComplete;
|
||||
}
|
||||
|
||||
@ -103,6 +123,7 @@ class LDR_Dusk_Dawn_v2 : public Usermod {
|
||||
|
||||
JsonArray LDR_Enabled = user.createNestedArray("LDR dusk/dawn enabled");
|
||||
LDR_Enabled.add(ldrEnabled);
|
||||
if (!ldrEnabled) return; // do not add more if usermod is disabled
|
||||
|
||||
JsonArray LDR_Reading = user.createNestedArray("LDR reading");
|
||||
LDR_Reading.add(ldrReading);
|
||||
@ -116,6 +137,12 @@ class LDR_Dusk_Dawn_v2 : public Usermod {
|
||||
|
||||
//JsonArray LDR_Off_Count = user.createNestedArray("LDR off count");
|
||||
//LDR_Off_Count.add(ldrOffCount);
|
||||
|
||||
//bool pinValid = ((ldrPin >= 0) && (digitalPinToAnalogChannel(ldrPin) >= 0));
|
||||
//if (pinManager.getPinOwner(ldrPin) != PinOwner::UM_LDR_DUSK_DAWN) pinValid = false;
|
||||
//JsonArray LDR_valid = user.createNestedArray(F("LDR pin"));
|
||||
//LDR_valid.add(ldrPin);
|
||||
//LDR_valid.add(pinValid ? F(" OK"): F(" invalid"));
|
||||
}
|
||||
|
||||
uint16_t getId() {
|
||||
|
@ -60,7 +60,8 @@ enum struct PinOwner : uint8_t {
|
||||
UM_BME280 = USERMOD_ID_BME280, // 0x1E // Usermod "usermod_bme280.h -- Uses "standard" HW_I2C pins
|
||||
UM_Audioreactive = USERMOD_ID_AUDIOREACTIVE, // 0x20 // Usermod "audio_reactive.h"
|
||||
UM_SdCard = USERMOD_ID_SD_CARD, // 0x25 // Usermod "usermod_sd_card.h"
|
||||
UM_PWM_OUTPUTS = USERMOD_ID_PWM_OUTPUTS // 0x26 // Usermod "usermod_pwm_outputs.h"
|
||||
UM_PWM_OUTPUTS = USERMOD_ID_PWM_OUTPUTS, // 0x26 // Usermod "usermod_pwm_outputs.h"
|
||||
UM_LDR_DUSK_DAWN = USERMOD_ID_LDR_DUSK_DAWN // 0x2B // Usermod "usermod_LDR_Dusk_Dawn_v2.h"
|
||||
};
|
||||
static_assert(0u == static_cast<uint8_t>(PinOwner::None), "PinOwner::None must be zero, so default array initialization works as expected");
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user