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
This commit is contained in:
Ben Meadors 2023-08-15 12:23:07 -05:00 committed by GitHub
parent 144dfe9805
commit 03fe4c629a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,9 +18,10 @@ int32_t DetectionSensorModule::runOnce()
*/ */
// moduleConfig.detection_sensor.enabled = true; // moduleConfig.detection_sensor.enabled = true;
// moduleConfig.detection_sensor.monitor_pin = 10; // WisBlock PIR IO6 // 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.state_broadcast_secs = 120;
// moduleConfig.detection_sensor.detection_triggered_high = false; // moduleConfig.detection_sensor.detection_triggered_high = true;
// strcpy(moduleConfig.detection_sensor.name, "Motion"); // strcpy(moduleConfig.detection_sensor.name, "Motion");
if (moduleConfig.detection_sensor.enabled == false) if (moduleConfig.detection_sensor.enabled == false)
@ -40,10 +41,12 @@ int32_t DetectionSensorModule::runOnce()
return DELAYED_INTERVAL; 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) && if ((millis() - lastSentToMesh) >= getConfiguredOrDefaultMs(moduleConfig.detection_sensor.minimum_broadcast_secs) &&
hasDetectionEvent()) { hasDetectionEvent()) {
sendDetectionMessage(); 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 // 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 // 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 && else if (moduleConfig.detection_sensor.state_broadcast_secs > 0 &&
(millis() - lastSentToMesh) >= getConfiguredOrDefaultMs(moduleConfig.detection_sensor.state_broadcast_secs)) { (millis() - lastSentToMesh) >= getConfiguredOrDefaultMs(moduleConfig.detection_sensor.state_broadcast_secs)) {
sendCurrentStateMessage(); sendCurrentStateMessage();
return DELAYED_INTERVAL; return moduleConfig.detection_sensor.minimum_broadcast_secs;
} }
return GPIO_POLLING_INTERVAL; return GPIO_POLLING_INTERVAL;
} }