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:
parent
3e13aa2261
commit
5714b3a396
3 changed files with 34 additions and 3 deletions
|
|
@ -1,13 +1,15 @@
|
||||||
pub mod state;
|
pub mod state;
|
||||||
|
pub mod led_pump;
|
||||||
|
|
||||||
pub use self::state::State;
|
pub use self::state::State;
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
use std::thread::JoinHandle;
|
use std::thread::JoinHandle;
|
||||||
|
use self::led_pump::LedPump;
|
||||||
|
|
||||||
pub struct Brain {
|
pub struct Brain {
|
||||||
pub state: State,
|
pub state: State,
|
||||||
device_threads: Vec<JoinHandle<()>>,
|
device_threads: Vec<JoinHandle<()>>,
|
||||||
|
led_pump: LedPump
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Brain {
|
impl Brain {
|
||||||
|
|
@ -15,6 +17,7 @@ impl Brain {
|
||||||
Self {
|
Self {
|
||||||
state: State::Starting,
|
state: State::Starting,
|
||||||
device_threads: Vec::new(),
|
device_threads: Vec::new(),
|
||||||
|
led_pump: LedPump::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -31,7 +34,7 @@ impl Brain {
|
||||||
self.state = State::Running;
|
self.state = State::Running;
|
||||||
}
|
}
|
||||||
State::Running => {
|
State::Running => {
|
||||||
// Logic will go here eventually
|
self.led_pump.beat();
|
||||||
}
|
}
|
||||||
State::Stopping => {
|
State::Stopping => {
|
||||||
info!("Brain: Waiting for all threads to join...");
|
info!("Brain: Waiting for all threads to join...");
|
||||||
|
|
|
||||||
28
src/rustylee/src/brain/led_pump.rs
Normal file
28
src/rustylee/src/brain/led_pump.rs
Normal 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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -45,7 +45,7 @@ fn setup_logging() {
|
||||||
.compact();
|
.compact();
|
||||||
|
|
||||||
tracing_subscriber::registry()
|
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)
|
.with(console_layer)
|
||||||
.init();
|
.init();
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue