Skip to content

Commit 9535f19

Browse files
committed
feat: ws smart sleep
1 parent 58f9d9a commit 9535f19

File tree

8 files changed

+86
-49
lines changed

8 files changed

+86
-49
lines changed

Cargo.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/buttons.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,11 @@ async fn room_left(
9999
) -> Result<bool, ()> {
100100
let mut state = state.state.lock().await;
101101
if state.scene == Scene::RoundSelect {
102-
state.round_select = state.round_select.saturating_sub(1);
102+
state.round_select = state
103+
.round_select
104+
.wrapping_sub(1)
105+
.min(state.possible_rounds.len() - 1);
106+
103107
return Ok(true);
104108
}
105109

@@ -114,7 +118,11 @@ async fn room_right(
114118
) -> Result<bool, ()> {
115119
let mut state = state.state.lock().await;
116120
if state.scene == Scene::RoundSelect {
117-
state.round_select = (state.round_select + 1).min(state.possible_rounds.len() - 1);
121+
state.round_select += 1;
122+
if state.round_select == state.possible_rounds.len() {
123+
state.round_select = 0;
124+
}
125+
118126
return Ok(true);
119127
}
120128

src/consts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub const SLEEP_AFTER_MS: u64 = 60000;
1+
pub const SLEEP_AFTER_MS: u64 = 60000 * 5;
22

33
pub const LOG_SEND_INTERVAL_MS: u64 = 5000;
44
pub const PRINT_HEAP_INTERVAL_MS: u64 = 30000;

src/lcd.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -277,19 +277,20 @@ async fn process_lcd<T: OutputPin, D: DelayNs>(
277277
.ok()?;
278278
}
279279
Scene::RoundSelect => {
280-
if current_state.round_select > 0 {
281-
lcd_driver.print(0, "<", PrintAlign::Left, false).ok()?;
282-
lcd_driver.print(1, "<", PrintAlign::Left, false).ok()?;
283-
}
284-
285-
if current_state.round_select < current_state.possible_rounds.len() - 1 {
286-
lcd_driver.print(0, ">", PrintAlign::Right, false).ok()?;
287-
lcd_driver.print(1, ">", PrintAlign::Right, false).ok()?;
288-
}
280+
lcd_driver.print(0, "<", PrintAlign::Left, false).ok()?;
281+
lcd_driver.print(1, "<", PrintAlign::Left, false).ok()?;
282+
lcd_driver.print(0, ">", PrintAlign::Right, false).ok()?;
283+
lcd_driver.print(1, ">", PrintAlign::Right, false).ok()?;
289284

290285
lcd_driver
291-
.print(0, "Select room", PrintAlign::Center, false)
286+
.print(
287+
0,
288+
&get_translation("SELECT_ROUND"),
289+
PrintAlign::Center,
290+
false,
291+
)
292292
.ok()?;
293+
293294
lcd_driver
294295
.print(
295296
1,

src/main.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -286,23 +286,25 @@ async fn main(spawner: Spawner) {
286286
};
287287

288288
utils::backtrace_store::read_saved_backtrace().await;
289+
290+
let ws_sleep_sig = Rc::new(Signal::new());
289291
_ = spawner.spawn(ws::ws_task(
290292
wifi_res.sta_stack,
291293
ws_url,
292294
global_state.clone(),
295+
ws_sleep_sig.clone(),
293296
));
297+
_ = spawner.spawn(logger_task());
294298

295299
set_brownout_detection(true);
296300
global_state.state.lock().await.scene = Scene::WaitingForCompetitor;
297-
/*
298301
if let Some(saved_state) = SavedGlobalState::from_nvs(&nvs).await {
299302
global_state
300303
.state
301304
.lock()
302305
.await
303306
.parse_saved_state(saved_state);
304307
}
305-
*/
306308

307309
// only mark ota valid after wifi connection!
308310
{
@@ -314,28 +316,32 @@ async fn main(spawner: Spawner) {
314316
}
315317
}
316318

317-
log::info!("main loop enter");
318-
let mut heap_start = Instant::now();
319319
let mut last_sleep = false;
320+
loop {
321+
Timer::after_millis(100).await;
322+
if sleep_state() != last_sleep {
323+
last_sleep = sleep_state();
324+
ws_sleep_sig.signal(last_sleep);
325+
326+
match last_sleep {
327+
true => wifi_res.stop_radio(),
328+
false => wifi_res.restart_radio(),
329+
}
330+
}
331+
}
332+
}
333+
334+
#[embassy_executor::task]
335+
async fn logger_task() {
336+
let mut heap_start = Instant::now();
320337
loop {
321338
Timer::after_millis(LOG_SEND_INTERVAL_MS).await;
322339

323-
// TODO: move to own task
324340
let mut tmp_logs: Vec<String> = Vec::new();
325341
while let Ok(msg) = utils::logger::LOGS_CHANNEL.try_receive() {
326342
tmp_logs.push(msg);
327343
}
328344

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-
339345
if ota_state() || sleep_state() {
340346
continue;
341347
}

src/rfid.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,16 @@ pub async fn rfid_task(
7676
let mut rfid_sleep = false;
7777
loop {
7878
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;
79+
if sleep_state() != rfid_sleep {
80+
rfid_sleep = sleep_state();
81+
82+
match rfid_sleep {
83+
true => _ = mfrc522.pcd_soft_power_down().await,
84+
false => _ = mfrc522.pcd_soft_power_up().await,
85+
}
8586
}
8687

87-
if sleep_state() && rfid_sleep {
88+
if rfid_sleep {
8889
Timer::after(Duration::from_millis(500)).await;
8990
continue;
9091
}

src/translations.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ pub fn init_translations() {
3636
"of a competitor ({0})",
3737
));
3838

39+
t.push(TranslationRecord::new("SELECT_ROUND", "Select round"));
40+
3941
t.push(TranslationRecord::new("CONFIRM_TIME", "Confirm the time"));
4042
t.push(TranslationRecord::new(
4143
"SCAN_JUDGE_CARD",

src/ws.rs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ use crate::{
33
state::{GlobalState, Scene},
44
structs::{ApiError, FromPacket, TimerPacket, TimerPacketInner},
55
};
6-
use alloc::string::String;
6+
use alloc::{rc::Rc, string::String};
77
use core::str::FromStr;
88
use embassy_net::{tcp::TcpSocket, IpAddress, Stack};
99
use embassy_sync::{
1010
blocking_mutex::raw::CriticalSectionRawMutex, channel::Channel, pubsub::PubSubChannel,
11+
signal::Signal,
1112
};
1213
use embassy_time::{Instant, Timer};
1314
use embedded_io_async::Write;
@@ -22,7 +23,12 @@ static TAGGED_RETURN: PubSubChannel<CriticalSectionRawMutex, (u64, TimerPacket),
2223
PubSubChannel::new();
2324

2425
#[embassy_executor::task]
25-
pub async fn ws_task(stack: Stack<'static>, ws_url: String, global_state: GlobalState) {
26+
pub async fn ws_task(
27+
stack: Stack<'static>,
28+
ws_url: String,
29+
global_state: GlobalState,
30+
ws_sleep_sig: Rc<Signal<CriticalSectionRawMutex, bool>>,
31+
) {
2632
let ws_url = WsUrl::from_str(&ws_url).expect("Ws url parse error");
2733

2834
let mut rx_buf = [0; 8192];
@@ -41,7 +47,7 @@ pub async fn ws_task(stack: Stack<'static>, ws_url: String, global_state: Global
4147
}
4248

4349
loop {
44-
let res = ws_loop(
50+
let ws_fut = ws_loop(
4551
&global_state,
4652
&ws_url,
4753
stack,
@@ -51,11 +57,26 @@ pub async fn ws_task(stack: Stack<'static>, ws_url: String, global_state: Global
5157
&mut ws_tx_buf,
5258
&mut ssl_rx_buf,
5359
&mut ssl_tx_buf,
54-
)
55-
.await;
60+
);
61+
62+
let res = embassy_futures::select::select(ws_fut, ws_sleep_sig.wait()).await;
5663

57-
if let Err(e) = res {
58-
log::error!("Ws_loop errored! {e:?}");
64+
match res {
65+
embassy_futures::select::Either::First(res) => {
66+
if let Err(e) = res {
67+
log::error!("Ws_loop errored! {e:?}");
68+
}
69+
}
70+
embassy_futures::select::Either::Second(sleep) => {
71+
if sleep {
72+
loop {
73+
let sleep = ws_sleep_sig.wait().await;
74+
if !sleep {
75+
break;
76+
}
77+
}
78+
}
79+
}
5980
}
6081

6182
Timer::after_millis(500).await;
@@ -105,8 +126,6 @@ async fn ws_loop(
105126
*addr
106127
};
107128

108-
Timer::after_millis(10000000).await;
109-
continue;
110129
let mut socket = TcpSocket::new(stack, rx_buf, tx_buf);
111130
socket.set_timeout(Some(embassy_time::Duration::from_secs(15)));
112131

0 commit comments

Comments
 (0)