From 2ebfcea94eaf8e4826ce52795c43c316aada0a39 Mon Sep 17 00:00:00 2001 From: Augusto Zanellato Date: Wed, 18 Sep 2024 19:43:13 +0200 Subject: [PATCH] DetectionSensor: broadcast all state changes Closes #4753 --- src/modules/DetectionSensorModule.cpp | 14 ++++++++++---- src/modules/DetectionSensorModule.h | 1 + 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/modules/DetectionSensorModule.cpp b/src/modules/DetectionSensorModule.cpp index 20d91a381..d637fa7c6 100644 --- a/src/modules/DetectionSensorModule.cpp +++ b/src/modules/DetectionSensorModule.cpp @@ -49,10 +49,16 @@ int32_t DetectionSensorModule::runOnce() // LOG_DEBUG("Detection Sensor Module: Current pin state: %i\n", digitalRead(moduleConfig.detection_sensor.monitor_pin)); - if ((millis() - lastSentToMesh) >= Default::getConfiguredOrDefaultMs(moduleConfig.detection_sensor.minimum_broadcast_secs) && - hasDetectionEvent()) { - sendDetectionMessage(); - return DELAYED_INTERVAL; + if ((millis() - lastSentToMesh) >= Default::getConfiguredOrDefaultMs(moduleConfig.detection_sensor.minimum_broadcast_secs)) { + if (hasDetectionEvent()) { + wasDetected = true; + sendDetectionMessage(); + return DELAYED_INTERVAL; + } else if (wasDetected) { + wasDetected = false; + sendCurrentStateMessage(); + return DELAYED_INTERVAL; + } } // Even if we haven't detected an event, broadcast our current state to the mesh on the scheduled interval as a sort // of heartbeat. We only do this if the minimum broadcast interval is greater than zero, otherwise we'll only broadcast state diff --git a/src/modules/DetectionSensorModule.h b/src/modules/DetectionSensorModule.h index ed6cddda5..eb17bf3a2 100644 --- a/src/modules/DetectionSensorModule.h +++ b/src/modules/DetectionSensorModule.h @@ -15,6 +15,7 @@ class DetectionSensorModule : public SinglePortModule, private concurrency::OSTh private: bool firstTime = true; uint32_t lastSentToMesh = 0; + bool wasDetected = false; void sendDetectionMessage(); void sendCurrentStateMessage(); bool hasDetectionEvent();