Skip to content

Commit 981a5ff

Browse files
committed
feat: ble display sleep
1 parent 0d9319d commit 981a5ff

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/bluetooth.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ use crate::{
22
state::{BleAction, GlobalState, MenuScene},
33
structs::BleDisplayDevice,
44
};
5-
use alloc::string::ToString;
5+
use alloc::{rc::Rc, string::ToString};
66
use core::cell::RefCell;
77
use embassy_futures::select::{Either, select, select3, select4};
88
use embassy_sync::{
9-
blocking_mutex::raw::NoopRawMutex,
9+
blocking_mutex::raw::{CriticalSectionRawMutex, NoopRawMutex},
1010
channel::{Channel, Sender},
11+
signal::Signal,
1112
};
1213
use embassy_time::{Duration, Timer, with_timeout};
1314
use esp_radio::{Controller as RadioController, ble::controller::BleConnector};
@@ -19,6 +20,35 @@ pub async fn bluetooth_timer_task(
1920
init: &'static RadioController<'static>,
2021
bt: esp_hal::peripherals::BT<'static>,
2122
state: GlobalState,
23+
sleep_sig: Rc<Signal<CriticalSectionRawMutex, bool>>,
24+
) {
25+
loop {
26+
let mut sleep = false;
27+
embassy_futures::select::select(bluetooth_loop(init, &bt, &state), async {
28+
loop {
29+
if sleep_sig.wait().await {
30+
sleep = true;
31+
break;
32+
}
33+
}
34+
})
35+
.await;
36+
37+
Timer::after_millis(1000).await;
38+
if sleep {
39+
loop {
40+
if !sleep_sig.wait().await {
41+
break;
42+
}
43+
}
44+
}
45+
}
46+
}
47+
48+
async fn bluetooth_loop(
49+
init: &'static RadioController<'static>,
50+
bt: &esp_hal::peripherals::BT<'static>,
51+
state: &GlobalState,
2252
) {
2353
loop {
2454
let mut bond_info = if let Some(bond_info) = load_bonding_info(&state.nvs).await {

src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,10 +309,12 @@ async fn main(spawner: Spawner) {
309309
));
310310
spawner.must_spawn(logger_task(global_state.clone()));
311311

312+
let ble_sleep_sig = Rc::new(Signal::new());
312313
spawner.must_spawn(bluetooth::bluetooth_timer_task(
313314
wifi_res.wifi_init,
314315
board.bt,
315316
global_state.clone(),
317+
ble_sleep_sig.clone(),
316318
));
317319

318320
set_brownout_detection(true);
@@ -331,6 +333,7 @@ async fn main(spawner: Spawner) {
331333
if sleep_state() != last_sleep {
332334
last_sleep = sleep_state();
333335
ws_sleep_sig.signal(last_sleep);
336+
ble_sleep_sig.signal(last_sleep);
334337

335338
match last_sleep {
336339
true => {

0 commit comments

Comments
 (0)