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.
This commit is contained in:
Russell Gilbert 2026-02-28 06:55:12 +00:00
parent 3e13aa2261
commit 5714b3a396
3 changed files with 34 additions and 3 deletions

View file

@ -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<JoinHandle<()>>,
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...");

View file

@ -0,0 +1,28 @@
use std::time::{Duration, Instant};
use tracing::debug;
pub struct LedPump {
last_beat: Option<Instant>,
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.");
}
}
}

View file

@ -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();
}