From 03fe4c629ae17483617d31848f3d59d602f655e1 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Tue, 15 Aug 2023 12:23:07 -0500 Subject: [PATCH] Dection Sensor module duty cycle interval optimization (#2723) * WIP * Updates * Move it out of the macro guard so portduino can build * Changes from feedback * Use minimum_broadcast_secs as interval if we just broadcasted to avoid wasting cpu cycles * Fmt * Merge conflict resolution boogered me up * Missed a spot --- src/modules/DetectionSensorModule.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/modules/DetectionSensorModule.cpp b/src/modules/DetectionSensorModule.cpp index 8050c209e..c6f71eb56 100644 --- a/src/modules/DetectionSensorModule.cpp +++ b/src/modules/DetectionSensorModule.cpp @@ -18,9 +18,10 @@ int32_t DetectionSensorModule::runOnce() */ // moduleConfig.detection_sensor.enabled = true; // moduleConfig.detection_sensor.monitor_pin = 10; // WisBlock PIR IO6 - // moduleConfig.detection_sensor.minimum_broadcast_secs = 60; + // moduleConfig.detection_sensor.monitor_pin = 21; // WisBlock RAK12013 Radar IO6 + // moduleConfig.detection_sensor.minimum_broadcast_secs = 30; // moduleConfig.detection_sensor.state_broadcast_secs = 120; - // moduleConfig.detection_sensor.detection_triggered_high = false; + // moduleConfig.detection_sensor.detection_triggered_high = true; // strcpy(moduleConfig.detection_sensor.name, "Motion"); if (moduleConfig.detection_sensor.enabled == false) @@ -40,10 +41,12 @@ int32_t DetectionSensorModule::runOnce() return DELAYED_INTERVAL; } + // LOG_DEBUG("Detection Sensor Module: Current pin state: %i\n", digitalRead(moduleConfig.detection_sensor.monitor_pin)); + if ((millis() - lastSentToMesh) >= getConfiguredOrDefaultMs(moduleConfig.detection_sensor.minimum_broadcast_secs) && hasDetectionEvent()) { sendDetectionMessage(); - return DELAYED_INTERVAL; + return moduleConfig.detection_sensor.minimum_broadcast_secs; } // 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 @@ -51,7 +54,7 @@ int32_t DetectionSensorModule::runOnce() else if (moduleConfig.detection_sensor.state_broadcast_secs > 0 && (millis() - lastSentToMesh) >= getConfiguredOrDefaultMs(moduleConfig.detection_sensor.state_broadcast_secs)) { sendCurrentStateMessage(); - return DELAYED_INTERVAL; + return moduleConfig.detection_sensor.minimum_broadcast_secs; } return GPIO_POLLING_INTERVAL; }