Skip to content

Commit 58f9d9a

Browse files
committed
wip: smart sleep on all peripherals
1 parent 31960e4 commit 58f9d9a

File tree

8 files changed

+55
-9
lines changed

8 files changed

+55
-9
lines changed

src/battery.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::{consts::BATTERY_SEND_INTERVAL_MS, utils::rolling_average::RollingAverage};
1+
use crate::{
2+
consts::BATTERY_SEND_INTERVAL_MS, state::sleep_state, utils::rolling_average::RollingAverage,
3+
};
24
use embassy_time::{Instant, Timer};
35
use esp_hal::{
46
analog::adc::{Adc, AdcConfig, Attenuation},
@@ -54,6 +56,11 @@ pub async fn battery_read_task(
5456

5557
loop {
5658
Timer::after_millis(100).await;
59+
if sleep_state() {
60+
Timer::after_millis(500).await;
61+
continue;
62+
}
63+
5764
let read = macros::nb_to_fut!(adc.read_oneshot(&mut adc_pin))
5865
.await
5966
.unwrap_or(0);

src/consts.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
pub const SLEEP_AFTER_MS: u64 = 60000;
2+
13
pub const LOG_SEND_INTERVAL_MS: u64 = 5000;
24
pub const PRINT_HEAP_INTERVAL_MS: u64 = 30000;
35

src/lcd.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use embassy_time::{Delay, Duration, Instant, Timer};
55
use embedded_hal::{delay::DelayNs, digital::OutputPin};
66

77
use crate::{
8-
consts::{INSPECTION_TIME_PLUS2, LCD_INSPECTION_FRAME_TIME, SCROLL_TICKER_INVERVAL_MS},
8+
consts::{
9+
INSPECTION_TIME_PLUS2, LCD_INSPECTION_FRAME_TIME, SCROLL_TICKER_INVERVAL_MS, SLEEP_AFTER_MS,
10+
},
911
state::{sleep_state, GlobalState, Scene, SignaledGlobalStateInner},
1012
translations::{get_translation, get_translation_params},
1113
utils::{
@@ -122,7 +124,7 @@ pub async fn lcd_task(
122124
lcd_driver.display_on_lcd(&mut lcd).unwrap();
123125
}
124126

125-
if !sleep_state() && (Instant::now() - last_update).as_secs() > 60 * 5 {
127+
if !sleep_state() && (Instant::now() - last_update).as_millis() > SLEEP_AFTER_MS {
126128
lcd.backlight_off();
127129

128130
unsafe {

src/main.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use esp_hal::{
1717
timer::timg::TimerGroup,
1818
};
1919
use esp_storage::FlashStorage;
20-
use state::{ota_state, GlobalStateInner, SavedGlobalState, Scene};
20+
use state::{ota_state, sleep_state, GlobalStateInner, SavedGlobalState, Scene};
2121
use structs::ConnSettings;
2222
use translations::init_translations;
2323
use utils::{logger::FkmLogger, set_brownout_detection};
@@ -294,13 +294,15 @@ async fn main(spawner: Spawner) {
294294

295295
set_brownout_detection(true);
296296
global_state.state.lock().await.scene = Scene::WaitingForCompetitor;
297+
/*
297298
if let Some(saved_state) = SavedGlobalState::from_nvs(&nvs).await {
298299
global_state
299300
.state
300301
.lock()
301302
.await
302303
.parse_saved_state(saved_state);
303304
}
305+
*/
304306

305307
// only mark ota valid after wifi connection!
306308
{
@@ -312,7 +314,9 @@ async fn main(spawner: Spawner) {
312314
}
313315
}
314316

317+
log::info!("main loop enter");
315318
let mut heap_start = Instant::now();
319+
let mut last_sleep = false;
316320
loop {
317321
Timer::after_millis(LOG_SEND_INTERVAL_MS).await;
318322

@@ -322,7 +326,17 @@ async fn main(spawner: Spawner) {
322326
tmp_logs.push(msg);
323327
}
324328

325-
if ota_state() {
329+
if sleep_state() && !last_sleep {
330+
wifi_res.disc.signal(true);
331+
last_sleep = true;
332+
}
333+
334+
if !sleep_state() && last_sleep {
335+
wifi_res.disc.signal(false);
336+
last_sleep = false;
337+
}
338+
339+
if ota_state() || sleep_state() {
326340
continue;
327341
}
328342

src/rfid.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::consts::RFID_RETRY_INIT_MS;
2-
use crate::state::{current_epoch, GlobalState};
2+
use crate::state::{current_epoch, sleep_state, GlobalState};
33
use crate::structs::{CardInfoResponsePacket, SolveConfirmPacket};
44
use alloc::string::ToString;
55
use anyhow::{anyhow, Result};
@@ -73,8 +73,22 @@ pub async fn rfid_task(
7373

7474
log::debug!("PCD ver: {:?}", mfrc522.pcd_get_version().await);
7575

76+
let mut rfid_sleep = false;
7677
loop {
7778
Timer::after(Duration::from_millis(10)).await;
79+
if sleep_state() && !rfid_sleep {
80+
_ = mfrc522.pcd_soft_power_down().await;
81+
rfid_sleep = true;
82+
} else if !sleep_state() && rfid_sleep {
83+
_ = mfrc522.pcd_soft_power_up().await;
84+
rfid_sleep = false;
85+
}
86+
87+
if sleep_state() && rfid_sleep {
88+
Timer::after(Duration::from_millis(500)).await;
89+
continue;
90+
}
91+
7892
if mfrc522.picc_is_new_card_present().await.is_err() {
7993
continue;
8094
}

src/stackmat.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
consts::{INSPECTION_TIME_DNF, INSPECTION_TIME_PLUS2},
3-
state::{GlobalState, Scene},
3+
state::{sleep_state, GlobalState, Scene},
44
utils::stackmat::{
55
ms_to_time_str, parse_stackmat_data, time_str_to_display, StackmatTimerState,
66
},
@@ -26,6 +26,11 @@ pub async fn stackmat_task(
2626
let mut last_state = None;
2727
let mut last_stackmat_state = StackmatTimerState::Unknown;
2828
loop {
29+
if sleep_state() {
30+
Timer::after_millis(500).await;
31+
continue;
32+
}
33+
2934
if (esp_hal::time::now() - last_read).to_millis() > 500 && last_state != Some(false) {
3035
global_state.state.lock().await.stackmat_connected = Some(false);
3136
last_state = Some(false);

src/utils/logger.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::state::ota_state;
1+
use crate::state::{ota_state, sleep_state};
22
use alloc::string::String;
33
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, channel::Channel};
44

@@ -52,7 +52,7 @@ impl log::Log for FkmLogger {
5252
esp_println::println!("{}{} - {}{}", color, record.level(), record.args(), reset);
5353

5454
#[cfg(not(feature = "bat_dev_lcd"))]
55-
if !ota_state() {
55+
if !ota_state() && !sleep_state() {
5656
if LOGS_CHANNEL.is_full() {
5757
_ = LOGS_CHANNEL.try_receive();
5858
}

src/ws.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ async fn ws_loop(
105105
*addr
106106
};
107107

108+
Timer::after_millis(10000000).await;
109+
continue;
108110
let mut socket = TcpSocket::new(stack, rx_buf, tx_buf);
109111
socket.set_timeout(Some(embassy_time::Duration::from_secs(15)));
110112

0 commit comments

Comments
 (0)