77#include " main.h"
88#include < Throttle.h>
99
10+ // When using this module with power-save enabled and in role sensor you can benefit from very low power consumption on nRF52 and
11+ // ESP32/S/S2 Mind setting a valid, non-used GPIO pin. On ESP it is manditory to choose a 'RTC GPIO'.
12+
1013#ifdef ARCH_NRF52
1114#include " sleep.h"
1215// Reserved values for GPREGRET are: 0xF5 (NRF52_MAGIC_LFS_IS_CORRUPT)
13- // and for the bootloader 0xB1, 0xA8, 0x4E, 0x57, 0x6D, lets choose free ones,
16+ // and for the bootloader 0xB1, 0xA8, 0x4E, 0x57, 0x6D
1417// although the bootloader shouldn't be called in the following soft reset.
1518constexpr uint32_t BOOT_FROM_COLD = 0x00 ;
1619constexpr uint32_t BOOT_FROM_TIMEOUT = 0xC0 ;
@@ -73,8 +76,8 @@ int32_t DetectionSensorModule::runOnce()
7376 // moduleConfig.detection_sensor.enabled = true;
7477 // moduleConfig.detection_sensor.monitor_pin = 10; // WisBlock PIR IO6
7578 // moduleConfig.detection_sensor.monitor_pin = 21; // WisBlock RAK12013 Radar IO6
76- // moduleConfig.detection_sensor.minimum_broadcast_secs = 30;
77- // moduleConfig.detection_sensor.state_broadcast_secs = 120;
79+ // moduleConfig.detection_sensor.message_rate_limit = 30;
80+ // moduleConfig.detection_sensor.state_broadcast_interval = 120;
7881 // moduleConfig.detection_sensor.detection_trigger_type =
7982 // meshtastic_ModuleConfig_DetectionSensorConfig_TriggerType_LOGIC_HIGH;
8083 // strcpy(moduleConfig.detection_sensor.name, "Motion");
@@ -105,23 +108,22 @@ int32_t DetectionSensorModule::runOnce()
105108 regret = NRF_POWER->GPREGRET ;
106109 }
107110 if (NRF_P0->LATCH || NRF_P1->LATCH ) {
108- LOG_INFO (" Woke up from eternal sleep by GPIO." , __builtin_ctz (NRF_P0->LATCH ? NRF_P0->LATCH : NRF_P1->LATCH ),
109- NRF_P0->LATCH ? 0 : 1 );
111+ LOG_INFO (" Woke up from eternal sleep by GPIO." );
110112 NRF_P1->LATCH = 0xFFFFFFFF ;
111113 NRF_P0->LATCH = 0xFFFFFFFF ;
112114 sendDetectionMessage ();
113115 } else if (regret == BOOT_FROM_TIMEOUT) {
114116 LOG_INFO (" Woke up by timeout." );
115117 // When encountering a timeout without setting the timer ourself the source must be another module running
116118 // concurrently
117- if (moduleConfig.detection_sensor .state_broadcast_secs )
119+ if (moduleConfig.detection_sensor .state_broadcast_interval )
118120 sendCurrentStateMessage (getState ());
119121 } else if (regret == BOOT_FROM_GPIOEVENT) {
120122 LOG_INFO (" Woke up from interval sleep by GPIO." );
121123 sendDetectionMessage ();
122124 } else {
123125 // We booted fresh. Enforce sending on first detection event.
124- lastSentToMesh = -Default::getConfiguredOrDefaultMs (moduleConfig.detection_sensor .minimum_broadcast_secs );
126+ lastSentToMesh = -Default::getConfiguredOrDefaultMs (moduleConfig.detection_sensor .message_rate_limit );
125127 }
126128 if (!(sd_power_gpregret_clr (0 , 0xFF ) == NRF_SUCCESS)) {
127129 NRF_POWER->GPREGRET = BOOT_FROM_COLD;
@@ -144,28 +146,28 @@ int32_t DetectionSensorModule::runOnce()
144146 switch (wakeCause) {
145147 case ESP_SLEEP_WAKEUP_EXT0:
146148 case ESP_SLEEP_WAKEUP_EXT1:
147- LOG_INFO (" Woke up from interval sleep by GPIO. Sending detection message. " );
149+ LOG_INFO (" Woke up from interval sleep by GPIO" );
148150 timeNow_us = rtc_time_slowclk_to_us (rtc_time_get (), esp_clk_slowclk_cal_get ());
149151 timeDiff_s = (timeNow_us / 1000000 - sleepTime_s) + config.power .min_wake_secs ;
150152 // LOG_INFO("sleeptime (effectivly lastSentToMesh): %u", timeDiff_s - config.power.min_wake_secs);
151153 // LOG_INFO("time since last run (effectivly lastSentToMesh): %us", timeDiff_s);
152154
153- if (timeDiff_s + time_acc_s > moduleConfig.detection_sensor .minimum_broadcast_secs ) {
155+ if (timeDiff_s + time_acc_s > moduleConfig.detection_sensor .message_rate_limit ) {
154156 // LOG_INFO("sending as %u > %u", timeDiff_s + time_acc_s,
155- // moduleConfig.detection_sensor.minimum_broadcast_secs );
157+ // moduleConfig.detection_sensor.message_rate_limit );
156158 time_acc_s = 0 ;
157159 sendDetectionMessage ();
158160 } else {
159161 // LOG_INFO("not sending as did not reach broadcast threshold. %us < %us", timeDiff_s + time_acc_s,
160- // moduleConfig.detection_sensor.minimum_broadcast_secs );
162+ // moduleConfig.detection_sensor.message_rate_limit );
161163 time_acc_s += timeDiff_s;
162164 }
163165 break ;
164166 case ESP_SLEEP_WAKEUP_TIMER:
165167 // When encountering a timeout without setting the timer ourself the source must be another module running
166168 // concurrently
167169 LOG_INFO (" Woke up by timeout." );
168- if (moduleConfig.detection_sensor .state_broadcast_secs )
170+ if (moduleConfig.detection_sensor .state_broadcast_interval )
169171 sendCurrentStateMessage (getState ());
170172 break ;
171173 case ESP_SLEEP_WAKEUP_TOUCHPAD:
@@ -185,8 +187,7 @@ int32_t DetectionSensorModule::runOnce()
185187 rtc_gpio_pullup_dis ((gpio_num_t )moduleConfig.detection_sensor .monitor_pin );
186188 }
187189 if (esp_sleep_enable_ext0_wakeup ((gpio_num_t )moduleConfig.detection_sensor .monitor_pin ,
188- (moduleConfig.detection_sensor .detection_trigger_type & 1 ) ? 1 : 0 ) !=
189- ESP_OK)
190+ (moduleConfig.detection_sensor .detection_trigger_type & 1 )) != ESP_OK)
190191 LOG_ERROR (" error enabling ext0 on gpio %d" , moduleConfig.detection_sensor .monitor_pin );
191192 return Default::getConfiguredOrDefaultMs (config.power .min_wake_secs , 90 );
192193 } else {
@@ -212,14 +213,14 @@ int32_t DetectionSensorModule::runOnce()
212213
213214#if defined(ARCH_NRF52) || defined(ESP32_WITH_EXT0)
214215 if ((config.device .role == meshtastic_Config_DeviceConfig_Role_SENSOR && config.power .is_power_saving )) {
215- // If 'State Broadcast Interval' (moduleConfig.detection_sensor.state_broadcast_secs ) is specified it will be used, if
216+ // If 'State Broadcast Interval' (moduleConfig.detection_sensor.state_broadcast_interval ) is specified it will be used, if
216217 // unset the sleep will last 'forever', interrupted by specified GPIO event
217218 // nRF52: Using a timeout the module enters a low power loop. Without, it will enter a low power delay to comply with the
218- // minimum_broadcast_secs and finally 'shutdown' while sensing the GPIO.
219+ // message_rate_limit and finally 'shutdown' while sensing the GPIO.
219220 // ESP32: Always uses deep sleep with RTC
220221
221222 uint32_t nightyNightMs =
222- Default::getConfiguredOrDefault (moduleConfig.detection_sensor .state_broadcast_secs * 1000 , portMAX_DELAY);
223+ Default::getConfiguredOrDefault (moduleConfig.detection_sensor .state_broadcast_interval * 1000 , portMAX_DELAY);
223224
224225#ifdef ESP32_WITH_EXT0
225226 sleepTime_s = rtc_time_slowclk_to_us (rtc_time_get (), esp_clk_slowclk_cal_get ()) / 1000000 ;
@@ -229,7 +230,7 @@ int32_t DetectionSensorModule::runOnce()
229230#endif
230231
231232 if (!Throttle::isWithinTimespanMs (lastSentToMesh,
232- Default::getConfiguredOrDefaultMs (moduleConfig.detection_sensor .minimum_broadcast_secs ))) {
233+ Default::getConfiguredOrDefaultMs (moduleConfig.detection_sensor .message_rate_limit ))) {
233234 bool isDetected = hasDetectionEvent ();
234235 DetectionSensorTriggerVerdict verdict =
235236 handlers[moduleConfig.detection_sensor .detection_trigger_type ](wasDetected, isDetected);
@@ -248,9 +249,9 @@ int32_t DetectionSensorModule::runOnce()
248249 // Even if we haven't detected an event, broadcast our current state to the mesh on the scheduled interval as a sort
249250 // of heartbeat. We only do this if the minimum broadcast interval is greater than zero, otherwise we'll only broadcast state
250251 // change detections.
251- if (moduleConfig.detection_sensor .state_broadcast_secs > 0 &&
252+ if (moduleConfig.detection_sensor .state_broadcast_interval > 0 &&
252253 !Throttle::isWithinTimespanMs (lastSentToMesh,
253- Default::getConfiguredOrDefaultMs (moduleConfig.detection_sensor .state_broadcast_secs ,
254+ Default::getConfiguredOrDefaultMs (moduleConfig.detection_sensor .state_broadcast_interval ,
254255 default_telemetry_broadcast_interval_secs))) {
255256 sendCurrentStateMessage (getState ());
256257 return DELAYED_INTERVAL;
@@ -325,16 +326,16 @@ boolean DetectionSensorModule::shouldLoop()
325326void DetectionSensorModule::lpDelay ()
326327{
327328 sd_power_mode_set (NRF_POWER_MODE_LOWPWR);
328- delay (moduleConfig.detection_sensor .minimum_broadcast_secs - config.power .min_wake_secs );
329+ delay (moduleConfig.detection_sensor .message_rate_limit - config.power .min_wake_secs );
329330}
330331
331332void DetectionSensorModule::lpLoop (uint32_t msecToWake)
332333{
333334 for (uint32_t i = msecToWake / 100 ;; i--) {
334335 delay (100 );
335336 if (hasDetectionEvent () &&
336- !Throttle::isWithinTimespanMs (
337- lastSentToMesh, Default::getConfiguredOrDefaultMs (moduleConfig.detection_sensor .minimum_broadcast_secs ))) {
337+ !Throttle::isWithinTimespanMs (lastSentToMesh,
338+ Default::getConfiguredOrDefaultMs (moduleConfig.detection_sensor .message_rate_limit ))) {
338339 if (!(sd_power_gpregret_clr (0 , 0xFF ) == NRF_SUCCESS &&
339340 sd_power_gpregret_set (0 , BOOT_FROM_GPIOEVENT) == NRF_SUCCESS)) {
340341 // necessary if softdevice is not enabled yet or never was
0 commit comments