From 33e7de18cf6ba9c2907b78bd29ea0440e5f6f4c8 Mon Sep 17 00:00:00 2001 From: Russell Gilbert Date: Fri, 13 Mar 2026 09:06:26 +0000 Subject: [PATCH] Adds initial organ system and protocols Establishes the core `organs` module, including a basic `Heart` implementation, to structure internal components. Introduces a `protocols` layer to define message passing and communication between the `Brain` and various `organs`. Adds a `coordinates` module with a `Point3D` struct for spatial definitions. Increases the `Brain`'s resting sleep duration for minor tuning. --- src/rustylee/build_number.txt | 2 +- src/rustylee/src/brain.rs | 2 +- src/rustylee/src/coordinates.rs | 6 +++++ src/rustylee/src/lifecycle.rs | 5 ++-- src/rustylee/src/main.rs | 3 +++ src/rustylee/src/organs.rs | 2 ++ src/rustylee/src/organs/heart.rs | 39 ++++++++++++++++++++++++++++++++ src/rustylee/src/organs/organ.rs | 9 ++++++++ src/rustylee/src/protocols.rs | 36 +++++++++++++++++++++++++++++ 9 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 src/rustylee/src/coordinates.rs create mode 100644 src/rustylee/src/organs.rs create mode 100644 src/rustylee/src/organs/heart.rs create mode 100644 src/rustylee/src/organs/organ.rs create mode 100644 src/rustylee/src/protocols.rs diff --git a/src/rustylee/build_number.txt b/src/rustylee/build_number.txt index 83248fb..bc768da 100644 --- a/src/rustylee/build_number.txt +++ b/src/rustylee/build_number.txt @@ -1 +1 @@ -142 \ No newline at end of file +146 \ No newline at end of file diff --git a/src/rustylee/src/brain.rs b/src/rustylee/src/brain.rs index c3180ac..e61eb84 100644 --- a/src/rustylee/src/brain.rs +++ b/src/rustylee/src/brain.rs @@ -37,7 +37,7 @@ impl Brain { fn rest() { debug!("Brain is resting."); - std::thread::sleep(std::time::Duration::from_millis(100)) + std::thread::sleep(std::time::Duration::from_millis(200)) } fn handle_divine_command(&mut self, command: LifecycleCommand) { diff --git a/src/rustylee/src/coordinates.rs b/src/rustylee/src/coordinates.rs new file mode 100644 index 0000000..e6089f8 --- /dev/null +++ b/src/rustylee/src/coordinates.rs @@ -0,0 +1,6 @@ +#[derive(Debug, Clone, Copy)] +pub struct Point3D { + pub x: f32, + pub y: f32, + pub z: f32 +} \ No newline at end of file diff --git a/src/rustylee/src/lifecycle.rs b/src/rustylee/src/lifecycle.rs index add9628..626ca11 100644 --- a/src/rustylee/src/lifecycle.rs +++ b/src/rustylee/src/lifecycle.rs @@ -12,7 +12,7 @@ pub enum LifeState { Flight, Panic, Dying, - Buried + Buried, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -33,4 +33,5 @@ pub struct LifecycleReceipt { pub command: LifecycleCommand, pub response: LifecycleCommandResponse, pub new_state: LifeState -} \ No newline at end of file +} + diff --git a/src/rustylee/src/main.rs b/src/rustylee/src/main.rs index d265926..510a735 100644 --- a/src/rustylee/src/main.rs +++ b/src/rustylee/src/main.rs @@ -1,5 +1,8 @@ pub mod lifecycle; pub mod brain; +pub mod organs; +pub mod coordinates; +pub mod protocols; use std::fmt::Alignment::Left; use tracing::{info, debug}; diff --git a/src/rustylee/src/organs.rs b/src/rustylee/src/organs.rs new file mode 100644 index 0000000..6833b1a --- /dev/null +++ b/src/rustylee/src/organs.rs @@ -0,0 +1,2 @@ +pub mod heart; +pub mod organ; \ No newline at end of file diff --git a/src/rustylee/src/organs/heart.rs b/src/rustylee/src/organs/heart.rs new file mode 100644 index 0000000..98f34a8 --- /dev/null +++ b/src/rustylee/src/organs/heart.rs @@ -0,0 +1,39 @@ +use std::sync::mpsc; +use std::time::{SystemTime, UNIX_EPOCH}; +use tracing::{info, instrument}; +use crate::protocols::{BrainMessage, OrganCommandEnvelope, OrganResponse}; + +pub struct Heart { + id: u32, + rx: mpsc::Receiver, + tx: mpsc::Sender, +} + +impl Heart { + #[instrument(skip(self), fields(heart_id = self.id))] + pub fn start(self) { + info!("Heart listener active"); + + while let Ok(envelope) = self.rx.recv() { + // 1. Process the command (Logic goes here later) + let response = OrganResponse::Ok; + + // 2. Capture the "now" timestamp + let now = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap_or_default() + .as_millis() as u64; + + // 3. Package the reply + let reply = BrainMessage { + organ_command: envelope, // Move the original envelope back + responded_at: now, + response, + }; + + // 4. Trace the reply and send + info!(?reply, "Sending response to Brain"); + let _ = self.tx.send(reply); + } + } +} \ No newline at end of file diff --git a/src/rustylee/src/organs/organ.rs b/src/rustylee/src/organs/organ.rs new file mode 100644 index 0000000..16cf238 --- /dev/null +++ b/src/rustylee/src/organs/organ.rs @@ -0,0 +1,9 @@ +use crate::coordinates::Point3D; +use crate::lifecycle::LifeState; + + + +pub trait Organ: Send { + /// Returns the immutable U32 ID assigned by the factory. + fn id(&self) -> u32; +} \ No newline at end of file diff --git a/src/rustylee/src/protocols.rs b/src/rustylee/src/protocols.rs new file mode 100644 index 0000000..1eb05a1 --- /dev/null +++ b/src/rustylee/src/protocols.rs @@ -0,0 +1,36 @@ +use crate::coordinates::Point3D; + +#[derive(Debug)] +pub enum OrganCommand { + Sleep, + Waken, + Pause, + Resume, + Beat (u32), + ChangeSpeed (u32), + GoFaster(u32), + GoSlower(u32), + MoveTo { + relative_to_center: Point3D, + speed: u32 + } +} + +#[derive(Debug)] +pub enum OrganResponse { + Ok, + Rejected +} + +#[derive(Debug)] +pub struct OrganCommandEnvelope { + pub command: OrganCommand, + pub issued_at: u64 +} + +#[derive(Debug)] +pub struct BrainMessage { + pub organ_command: OrganCommandEnvelope, + pub responded_at: u64, + pub response: OrganResponse, +} \ No newline at end of file