Removed unnecessary ISR from PIR sensor switch UM.

This commit is contained in:
Blaz Kristan 2021-05-13 16:18:22 +02:00
parent ec0feb68f4
commit d4a3cadd09

View File

@ -48,11 +48,11 @@ public:
/** /**
* Enable/Disable the PIR sensor * Enable/Disable the PIR sensor
*/ */
void EnablePIRsensor(bool enable) { m_PIRenabled = enable; } void EnablePIRsensor(bool en) { enabled = en; }
/** /**
* Get PIR sensor enabled/disabled state * Get PIR sensor enabled/disabled state
*/ */
bool PIRsensorEnabled() { return m_PIRenabled; } bool PIRsensorEnabled() { return enabled; }
private: private:
// PIR sensor pin // PIR sensor pin
@ -64,9 +64,9 @@ private:
// off timer start time // off timer start time
uint32_t m_offTimerStart = 0; uint32_t m_offTimerStart = 0;
// current PIR sensor pin state // current PIR sensor pin state
byte m_PIRsensorPinState = LOW; byte sensorPinState = LOW;
// PIR sensor enabled - ISR attached // PIR sensor enabled
bool m_PIRenabled = true; bool enabled = true;
// status of initialisation // status of initialisation
bool initDone = false; bool initDone = false;
// on and off presets // on and off presets
@ -88,16 +88,6 @@ private:
static const char _nightTime[]; static const char _nightTime[];
static const char _mqttOnly[]; static const char _mqttOnly[];
/**
* return or change if new PIR sensor state is available
*/
static volatile bool newPIRsensorState(bool changeState = false, bool newState = false);
/**
* PIR sensor state has changed
*/
static void IRAM_ATTR ISR_PIRstateChange();
/** /**
* check if it is daytime * check if it is daytime
* if sunrise/sunset is not defined (no NTP or lat/lon) default to nighttime * if sunrise/sunset is not defined (no NTP or lat/lon) default to nighttime
@ -159,10 +149,11 @@ private:
*/ */
bool updatePIRsensorState() bool updatePIRsensorState()
{ {
if (newPIRsensorState()) { bool pinState = digitalRead(PIRsensorPin);
m_PIRsensorPinState = digitalRead(PIRsensorPin); if (pinState != sensorPinState) {
sensorPinState = pinState; // change previous state
if (m_PIRsensorPinState == HIGH) { if (sensorPinState == HIGH) {
m_offTimerStart = 0; m_offTimerStart = 0;
if (!m_mqttOnly && (!m_nightTimeOnly || (m_nightTimeOnly && !isDayTime()))) switchStrip(true); if (!m_mqttOnly && (!m_nightTimeOnly || (m_nightTimeOnly && !isDayTime()))) switchStrip(true);
publishMqtt("on"); publishMqtt("on");
@ -170,7 +161,6 @@ private:
// start switch off timer // start switch off timer
m_offTimerStart = millis(); m_offTimerStart = millis();
} }
newPIRsensorState(true, false);
return true; return true;
} }
return false; return false;
@ -183,7 +173,7 @@ private:
{ {
if (m_offTimerStart > 0 && millis() - m_offTimerStart > m_switchOffDelay) if (m_offTimerStart > 0 && millis() - m_offTimerStart > m_switchOffDelay)
{ {
if (m_PIRenabled == true) if (enabled == true)
{ {
if (!m_mqttOnly && (!m_nightTimeOnly || (m_nightTimeOnly && !isDayTime()))) switchStrip(false); if (!m_mqttOnly && (!m_nightTimeOnly || (m_nightTimeOnly && !isDayTime()))) switchStrip(false);
publishMqtt("off"); publishMqtt("off");
@ -206,15 +196,13 @@ public:
// pin retrieved from cfg.json (readFromConfig()) prior to running setup() // pin retrieved from cfg.json (readFromConfig()) prior to running setup()
if (!pinManager.allocatePin(PIRsensorPin,false)) { if (!pinManager.allocatePin(PIRsensorPin,false)) {
PIRsensorPin = -1; // allocation failed PIRsensorPin = -1; // allocation failed
m_PIRenabled = false; enabled = false;
DEBUG_PRINTLN(F("PIRSensorSwitch pin allocation failed.")); DEBUG_PRINTLN(F("PIRSensorSwitch pin allocation failed."));
} else { } else {
// PIR Sensor mode INPUT_PULLUP // PIR Sensor mode INPUT_PULLUP
pinMode(PIRsensorPin, INPUT_PULLUP); pinMode(PIRsensorPin, INPUT_PULLUP);
if (m_PIRenabled) { if (enabled) {
// assign interrupt function and set CHANGE mode sensorPinState = digitalRead(PIRsensorPin);
attachInterrupt(digitalPinToInterrupt(PIRsensorPin), ISR_PIRstateChange, CHANGE);
newPIRsensorState(true, false);
} }
} }
initDone = true; initDone = true;
@ -253,7 +241,7 @@ public:
JsonObject user = root["u"]; JsonObject user = root["u"];
if (user.isNull()) user = root.createNestedObject("u"); if (user.isNull()) user = root.createNestedObject("u");
if (m_PIRenabled) if (enabled)
{ {
// off timer // off timer
String uiDomString = F("PIR <i class=\"icons\">&#xe325;</i>"); String uiDomString = F("PIR <i class=\"icons\">&#xe325;</i>");
@ -319,7 +307,7 @@ public:
void addToConfig(JsonObject &root) void addToConfig(JsonObject &root)
{ {
JsonObject top = root.createNestedObject(FPSTR(_name)); JsonObject top = root.createNestedObject(FPSTR(_name));
top[FPSTR(_enabled)] = m_PIRenabled; top[FPSTR(_enabled)] = enabled;
top[FPSTR(_switchOffDelay)] = m_switchOffDelay / 1000; top[FPSTR(_switchOffDelay)] = m_switchOffDelay / 1000;
top["pin"] = PIRsensorPin; top["pin"] = PIRsensorPin;
top[FPSTR(_onPreset)] = m_onPreset; top[FPSTR(_onPreset)] = m_onPreset;
@ -335,7 +323,7 @@ public:
*/ */
void readFromConfig(JsonObject &root) void readFromConfig(JsonObject &root)
{ {
bool oldEnabled = m_PIRenabled; bool oldEnabled = enabled;
int8_t oldPin = PIRsensorPin; int8_t oldPin = PIRsensorPin;
JsonObject top = root[FPSTR(_name)]; JsonObject top = root[FPSTR(_name)];
@ -347,11 +335,11 @@ public:
if (top[FPSTR(_enabled)] != nullptr) { if (top[FPSTR(_enabled)] != nullptr) {
if (top[FPSTR(_enabled)].is<bool>()) { if (top[FPSTR(_enabled)].is<bool>()) {
m_PIRenabled = top[FPSTR(_enabled)].as<bool>(); // reading from cfg.json enabled = top[FPSTR(_enabled)].as<bool>(); // reading from cfg.json
} else { } else {
// change from settings page // change from settings page
String str = top[FPSTR(_enabled)]; // checkbox -> off or on String str = top[FPSTR(_enabled)]; // checkbox -> off or on
m_PIRenabled = (bool)(str!="off"); // off is guaranteed to be present enabled = (bool)(str!="off"); // off is guaranteed to be present
} }
} }
@ -391,11 +379,7 @@ public:
// reading config prior to setup() // reading config prior to setup()
DEBUG_PRINTLN(F("PIR config loaded.")); DEBUG_PRINTLN(F("PIR config loaded."));
} else { } else {
if (oldPin != PIRsensorPin || oldEnabled != m_PIRenabled) { if (oldPin != PIRsensorPin || oldEnabled != enabled) {
if (oldEnabled) {
// remove old ISR if disabling usermod
detachInterrupt(oldPin);
}
// check if pin is OK // check if pin is OK
if (oldPin != PIRsensorPin && oldPin >= 0) { if (oldPin != PIRsensorPin && oldPin >= 0) {
// if we are changing pin in settings page // if we are changing pin in settings page
@ -406,12 +390,11 @@ public:
} else { } else {
// allocation failed // allocation failed
PIRsensorPin = -1; PIRsensorPin = -1;
m_PIRenabled = false; enabled = false;
} }
} }
if (m_PIRenabled) { if (enabled) {
attachInterrupt(digitalPinToInterrupt(PIRsensorPin), ISR_PIRstateChange, CHANGE); sensorPinState = digitalRead(PIRsensorPin);
newPIRsensorState(true, false);
} }
DEBUG_PRINTLN(F("PIR config (re)loaded.")); DEBUG_PRINTLN(F("PIR config (re)loaded."));
} }
@ -428,21 +411,6 @@ public:
} }
}; };
//////////////////////////////////////////////////////
// PIRsensorSwitch static method implementations
volatile bool PIRsensorSwitch::newPIRsensorState(bool changeState, bool newState)
{
static volatile bool s_PIRsensorState = false;
if (changeState) s_PIRsensorState = newState;
return s_PIRsensorState;
}
void IRAM_ATTR PIRsensorSwitch::ISR_PIRstateChange()
{
newPIRsensorState(true, true);
}
// strings to reduce flash memory usage (used more than twice) // strings to reduce flash memory usage (used more than twice)
const char PIRsensorSwitch::_name[] PROGMEM = "PIRsensorSwitch"; const char PIRsensorSwitch::_name[] PROGMEM = "PIRsensorSwitch";
const char PIRsensorSwitch::_enabled[] PROGMEM = "PIRenabled"; const char PIRsensorSwitch::_enabled[] PROGMEM = "PIRenabled";