From 5714b3a396baef7f59750e56fdf4f4c3a058ea46 Mon Sep 17 00:00:00 2001 From: Russell Gilbert Date: Sat, 28 Feb 2026 06:55:12 +0000 Subject: [PATCH] Introduces periodic beat mechanism Adds a `LedPump` component to the `Brain` to execute logic periodically while in the `Running` state. This establishes a basic timing mechanism that currently logs debug messages at a fixed interval, providing a foundation for future timed operations or state transitions. The default logging level is adjusted to `debug` to ensure these periodic events are visible. --- src/rustylee/src/brain.rs | 7 +++++-- src/rustylee/src/brain/led_pump.rs | 28 ++++++++++++++++++++++++++++ src/rustylee/src/main.rs | 2 +- 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 src/rustylee/src/brain/led_pump.rs diff --git a/src/rustylee/src/brain.rs b/src/rustylee/src/brain.rs index 3117499..09980f1 100644 --- a/src/rustylee/src/brain.rs +++ b/src/rustylee/src/brain.rs @@ -1,13 +1,15 @@ pub mod state; +pub mod led_pump; pub use self::state::State; use tracing::info; use std::thread::JoinHandle; - +use self::led_pump::LedPump; pub struct Brain { pub state: State, device_threads: Vec>, + led_pump: LedPump } impl Brain { @@ -15,6 +17,7 @@ impl Brain { Self { state: State::Starting, device_threads: Vec::new(), + led_pump: LedPump::new() } } @@ -31,7 +34,7 @@ impl Brain { self.state = State::Running; } State::Running => { - // Logic will go here eventually + self.led_pump.beat(); } State::Stopping => { info!("Brain: Waiting for all threads to join..."); diff --git a/src/rustylee/src/brain/led_pump.rs b/src/rustylee/src/brain/led_pump.rs new file mode 100644 index 0000000..9360466 --- /dev/null +++ b/src/rustylee/src/brain/led_pump.rs @@ -0,0 +1,28 @@ +use std::time::{Duration, Instant}; +use tracing::debug; +pub struct LedPump { + last_beat: Option, + interval: Duration, +} + +impl LedPump { + pub fn new() -> LedPump { + Self{ + last_beat: None, + interval: Duration::from_millis(500), + } + } + + pub fn beat(&mut self) { + let now = Instant::now(); + + + let should_beat = self.last_beat.map_or( + true, |last_beat| now.duration_since(last_beat) > self.interval); + + if should_beat { + self.last_beat = Some(now); + debug!("LED Pump beat."); + } + } +} \ No newline at end of file diff --git a/src/rustylee/src/main.rs b/src/rustylee/src/main.rs index 0cabfc7..4a99184 100644 --- a/src/rustylee/src/main.rs +++ b/src/rustylee/src/main.rs @@ -45,7 +45,7 @@ fn setup_logging() { .compact(); tracing_subscriber::registry() - .with(EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"))) + .with(EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("debug"))) .with(console_layer) .init(); } \ No newline at end of file