idle: implement an idle framework module
This commit is contained in:
37
src/idle.rs
Normal file
37
src/idle.rs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
use embassy_time::{Duration, Instant};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct IdleClock {
|
||||||
|
duration: Duration,
|
||||||
|
last_active: Instant,
|
||||||
|
fired: bool
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IdleClock {
|
||||||
|
pub fn new(duration: Duration) -> Self {
|
||||||
|
Self {
|
||||||
|
last_active: Instant::now(),
|
||||||
|
duration,
|
||||||
|
fired: false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn wake(&mut self) -> bool {
|
||||||
|
self.last_active = Instant::now();
|
||||||
|
if self.fired {
|
||||||
|
self.fired = false;
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn check(&mut self) -> bool {
|
||||||
|
if !self.fired && Instant::now().duration_since(self.last_active) >= self.duration {
|
||||||
|
self.fired = true;
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user