Skip to content

Commit d6ab446

Browse files
committed
simple_repeater: Use constexpr variables for powersaving timings
This makes the code easier to read and allows for easier changing of the hardcoded values. Signed-off-by: Frieder Schrempf <frieder@fris.de>
1 parent 10db48a commit d6ab446

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

examples/simple_repeater/main.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ void halt() {
2020
static char command[160];
2121

2222
// For power saving
23+
constexpr unsigned long ACTIVE_TIME_SEC_INUSE = 2 * 60; // 2 minutes
24+
constexpr unsigned long ACTIVE_TIME_SEC_IDLE = 5; // 5 seconds
25+
constexpr unsigned long IDLE_PERIOD_SEC = 30 * 60; // 30 minutes
26+
2327
unsigned long lastActive = 0; // mark last active time
24-
unsigned long nextSleepinSecs = 120; // next sleep in seconds. The first sleep (if enabled) is after 2 minutes from boot
28+
unsigned long nextSleepinSecs = ACTIVE_TIME_SEC_INUSE; // next sleep in seconds
2529

2630
#if defined(PIN_USER_BTN) && defined(_SEEED_SENSECAP_SOLAR_H_)
2731
static unsigned long userBtnDownAt = 0;
@@ -156,14 +160,14 @@ void loop() {
156160

157161
if (the_mesh.getNodePrefs()->powersaving_enabled && !the_mesh.hasPendingWork()) {
158162
#if defined(NRF52_PLATFORM)
159-
board.sleep(1800); // nrf ignores seconds param, sleeps whenever possible
163+
board.sleep(IDLE_PERIOD_SEC); // nrf ignores seconds param, sleeps whenever possible
160164
#else
161165
if (the_mesh.millisHasNowPassed(lastActive + nextSleepinSecs * 1000)) { // To check if it is time to sleep
162-
board.sleep(1800); // To sleep. Wake up after 30 minutes or when receiving a LoRa packet
166+
board.sleep(IDLE_PERIOD_SEC); // To sleep. Wake up after 30 minutes or when receiving a LoRa packet
163167
lastActive = millis();
164-
nextSleepinSecs = 5; // Default: To work for 5s and sleep again
168+
nextSleepinSecs = ACTIVE_TIME_SEC_IDLE; // Default: To work for 5s and sleep again
165169
} else {
166-
nextSleepinSecs += 5; // When there is pending work, to work another 5s
170+
nextSleepinSecs += ACTIVE_TIME_SEC_IDLE; // When there is pending work, to work another 5s
167171
}
168172
#endif
169173
}

0 commit comments

Comments
 (0)