From 0e9e0c1b130f06136f44044fdd80403c64c7f407 Mon Sep 17 00:00:00 2001 From: Victoria Fischer Date: Sat, 11 Oct 2025 16:32:49 +0200 Subject: [PATCH] idle: implement an idle framework module --- src/idle.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/idle.rs diff --git a/src/idle.rs b/src/idle.rs new file mode 100644 index 0000000..baf5684 --- /dev/null +++ b/src/idle.rs @@ -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 + } + } +} \ No newline at end of file