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:
parent
ee01746956
commit
33e7de18cf
9 changed files with 100 additions and 4 deletions
|
|
@ -1 +1 @@
|
|||
142
|
||||
146
|
||||
|
|
@ -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) {
|
||||
|
|
|
|||
6
src/rustylee/src/coordinates.rs
Normal file
6
src/rustylee/src/coordinates.rs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Point3D {
|
||||
pub x: f32,
|
||||
pub y: f32,
|
||||
pub z: f32
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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};
|
||||
|
|
|
|||
2
src/rustylee/src/organs.rs
Normal file
2
src/rustylee/src/organs.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
pub mod heart;
|
||||
pub mod organ;
|
||||
39
src/rustylee/src/organs/heart.rs
Normal file
39
src/rustylee/src/organs/heart.rs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
9
src/rustylee/src/organs/organ.rs
Normal file
9
src/rustylee/src/organs/organ.rs
Normal 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;
|
||||
}
|
||||
36
src/rustylee/src/protocols.rs
Normal file
36
src/rustylee/src/protocols.rs
Normal 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,
|
||||
}
|
||||
Loading…
Reference in a new issue