Skip to content

Commit 00ef8df

Browse files
committed
feat: wakeup from "sleep" after button press
1 parent 7af2060 commit 00ef8df

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

src/buttons.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
state::{current_epoch, GlobalState, Scene},
2+
state::{current_epoch, sleep_state, GlobalState, Scene},
33
structs::DelegateResponsePacket,
44
utils::buttons::{Button, ButtonTrigger, ButtonsHandler},
55
};
@@ -19,7 +19,7 @@ pub async fn buttons_task(
1919

2020
#[cfg(feature = "esp32")] buttons: [Input<'static>; 4],
2121
) {
22-
let mut handler = ButtonsHandler::new();
22+
let mut handler = ButtonsHandler::new(Some(wakeup_button()));
2323
handler.add_handler(Button::Third, ButtonTrigger::Up, submit_up());
2424
handler.add_handler(
2525
Button::Third,
@@ -75,6 +75,19 @@ async fn button_test(
7575
Ok(false)
7676
}
7777

78+
#[macros::button_handler]
79+
async fn wakeup_button(
80+
_triggered: &ButtonTrigger,
81+
_hold_time: u64,
82+
state: &GlobalState,
83+
) -> Result<bool, ()> {
84+
if sleep_state() {
85+
let _drop = state.state.lock().await;
86+
}
87+
88+
Ok(false)
89+
}
90+
7891
#[macros::button_handler]
7992
async fn submit_up(
8093
_triggered: &ButtonTrigger,

src/utils/buttons.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,19 @@ pub struct ButtonHandler {
4141
}
4242

4343
pub struct ButtonsHandler {
44+
default_handler: Option<HandlersDerive>,
45+
4446
handlers: Vec<ButtonHandler>,
4547
press_time: Instant,
4648
last_hold_execute: Instant,
4749
current_handler_down: Option<usize>,
4850
}
4951

5052
impl ButtonsHandler {
51-
pub fn new() -> Self {
53+
pub fn new(default_handler: Option<HandlersDerive>) -> Self {
5254
Self {
55+
default_handler,
56+
5357
handlers: Vec::new(),
5458
press_time: Instant::now(),
5559
last_hold_execute: Instant::now(),
@@ -139,6 +143,12 @@ impl ButtonsHandler {
139143
.enumerate()
140144
.find(|(_, h)| h.button == button);
141145

146+
if let Some(ref default_handler) = self.default_handler {
147+
_ = default_handler
148+
.execute(&ButtonTrigger::Down, 0, state)
149+
.await;
150+
}
151+
142152
if let Some((i, handler)) = &mut handler {
143153
self.current_handler_down = Some(*i);
144154

@@ -225,10 +235,16 @@ impl ButtonsHandler {
225235
return;
226236
}
227237

238+
let hold_time = (Instant::now() - self.press_time).as_millis();
239+
if let Some(ref default_handler) = self.default_handler {
240+
_ = default_handler
241+
.execute(&ButtonTrigger::Up, hold_time, state)
242+
.await;
243+
}
244+
228245
let handler = &self.handlers[self.current_handler_down.expect("Cant fail")];
229246
let handlers = handler.handlers.iter().filter(|h| h.0 == ButtonTrigger::Up);
230247
for handler in handlers {
231-
let hold_time = (Instant::now() - self.press_time).as_millis();
232248
let res = handler.2.execute(&handler.0, hold_time, state).await;
233249
if let Err(e) = res {
234250
log::error!("buttons_handler:up_err: {e:?}");

0 commit comments

Comments
 (0)