mirror of
https://github.com/wled/WLED.git
synced 2025-07-10 04:16:36 +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
|
#pragma once
|
||||||
#include "wled.h"
|
#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 {
|
class LDR_Dusk_Dawn_v2 : public Usermod {
|
||||||
private:
|
private:
|
||||||
// Defaults
|
// Defaults
|
||||||
@ -12,22 +17,30 @@ class LDR_Dusk_Dawn_v2 : public Usermod {
|
|||||||
int ldrOffPreset = 2; // Default "Off" Preset
|
int ldrOffPreset = 2; // Default "Off" Preset
|
||||||
|
|
||||||
// Variables
|
// Variables
|
||||||
|
bool initDone = false;
|
||||||
bool ldrEnabledPreviously = false; // Was LDR enabled for the previous check? First check is always no.
|
bool ldrEnabledPreviously = false; // Was LDR enabled for the previous check? First check is always no.
|
||||||
int ldrOffCount; // Number of readings above the threshold
|
int ldrOffCount; // Number of readings above the threshold
|
||||||
int ldrOnCount; // Number of readings below 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
|
int ldrLEDState; // Current LED on/off state
|
||||||
unsigned long lastMillis = 0;
|
unsigned long lastMillis = 0;
|
||||||
static const char _name[];
|
static const char _name[];
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void setup() {
|
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() {
|
void loop() {
|
||||||
// Only update every 10 seconds
|
// Only update every 10 seconds
|
||||||
if (millis() - lastMillis > 10000) {
|
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
|
// Default state is off
|
||||||
if (ldrEnabledPreviously == false) {
|
if (ldrEnabledPreviously == false) {
|
||||||
applyPreset(ldrOffPreset);
|
applyPreset(ldrOffPreset);
|
||||||
@ -85,6 +98,7 @@ class LDR_Dusk_Dawn_v2 : public Usermod {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool readFromConfig(JsonObject& root) {
|
bool readFromConfig(JsonObject& root) {
|
||||||
|
int8_t oldLdrPin = ldrPin;
|
||||||
JsonObject top = root[FPSTR(_name)];
|
JsonObject top = root[FPSTR(_name)];
|
||||||
bool configComplete = !top.isNull();
|
bool configComplete = !top.isNull();
|
||||||
configComplete &= getJsonValue(top["Enabled"], ldrEnabled);
|
configComplete &= getJsonValue(top["Enabled"], ldrEnabled);
|
||||||
@ -93,6 +107,12 @@ class LDR_Dusk_Dawn_v2 : public Usermod {
|
|||||||
configComplete &= getJsonValue(top["Threshold"], ldrThreshold);
|
configComplete &= getJsonValue(top["Threshold"], ldrThreshold);
|
||||||
configComplete &= getJsonValue(top["On Preset"], ldrOnPreset);
|
configComplete &= getJsonValue(top["On Preset"], ldrOnPreset);
|
||||||
configComplete &= getJsonValue(top["Off Preset"], ldrOffPreset);
|
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;
|
return configComplete;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,7 +122,8 @@ class LDR_Dusk_Dawn_v2 : public Usermod {
|
|||||||
if (user.isNull()) user = root.createNestedObject("u");
|
if (user.isNull()) user = root.createNestedObject("u");
|
||||||
|
|
||||||
JsonArray LDR_Enabled = user.createNestedArray("LDR dusk/dawn enabled");
|
JsonArray LDR_Enabled = user.createNestedArray("LDR dusk/dawn enabled");
|
||||||
LDR_Enabled.add(ldrEnabled);
|
LDR_Enabled.add(ldrEnabled);
|
||||||
|
if (!ldrEnabled) return; // do not add more if usermod is disabled
|
||||||
|
|
||||||
JsonArray LDR_Reading = user.createNestedArray("LDR reading");
|
JsonArray LDR_Reading = user.createNestedArray("LDR reading");
|
||||||
LDR_Reading.add(ldrReading);
|
LDR_Reading.add(ldrReading);
|
||||||
@ -116,6 +137,12 @@ class LDR_Dusk_Dawn_v2 : public Usermod {
|
|||||||
|
|
||||||
//JsonArray LDR_Off_Count = user.createNestedArray("LDR off count");
|
//JsonArray LDR_Off_Count = user.createNestedArray("LDR off count");
|
||||||
//LDR_Off_Count.add(ldrOffCount);
|
//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() {
|
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_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_Audioreactive = USERMOD_ID_AUDIOREACTIVE, // 0x20 // Usermod "audio_reactive.h"
|
||||||
UM_SdCard = USERMOD_ID_SD_CARD, // 0x25 // Usermod "usermod_sd_card.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");
|
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