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.
This commit is contained in:
Russell Gilbert 2026-03-13 09:06:26 +00:00
parent ee01746956
commit 33e7de18cf
9 changed files with 100 additions and 4 deletions

View file

@ -1 +1 @@
142
146

View file

@ -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) {

View file

@ -0,0 +1,6 @@
#[derive(Debug, Clone, Copy)]
pub struct Point3D {
pub x: f32,
pub y: f32,
pub z: f32
}

View file

@ -12,7 +12,7 @@ pub enum LifeState {
Flight,
Panic,
Dying,
Buried
Buried,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@ -34,3 +34,4 @@ pub struct LifecycleReceipt {
pub response: LifecycleCommandResponse,
pub new_state: LifeState
}

View file

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

View file

@ -0,0 +1,2 @@
pub mod heart;
pub mod organ;

View file

@ -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<OrganCommandEnvelope>,
tx: mpsc::Sender<BrainMessage>,
}
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);
}
}
}

View file

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

View file

@ -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,
}