diff --git a/Cargo.lock b/Cargo.lock index 52d96bd0..b029171f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -428,6 +428,9 @@ dependencies = [ [[package]] name = "hyped_state_machine" version = "0.1.0" +dependencies = [ + "heapless", +] [[package]] name = "indexmap" diff --git a/lib/state_machine/Cargo.toml b/lib/state_machine/Cargo.toml index 023d1ba2..1b0b149f 100644 --- a/lib/state_machine/Cargo.toml +++ b/lib/state_machine/Cargo.toml @@ -1,7 +1,10 @@ -[package] -name = "hyped_state_machine" -version = "0.1.0" -edition = "2021" - -[features] -std = [] \ No newline at end of file +[package] +name = "hyped_state_machine" +version = "0.1.0" +edition = "2021" + +[features] +std = [] + +[dependencies] +heapless = "0.8.0" diff --git a/lib/state_machine/src/state_machine.rs b/lib/state_machine/src/state_machine.rs index 01f6c945..293fe9e4 100644 --- a/lib/state_machine/src/state_machine.rs +++ b/lib/state_machine/src/state_machine.rs @@ -1,12 +1,34 @@ -use crate::types::State; +use crate::types::{SourceAndTarget, State}; +use heapless::FnvIndexMap; pub struct StateMachine { - current_state: State, - // transition_map: HashMap (use heapless::FnvIndexMap)? + pub(crate) current_state: State, + pub(crate) transition_map: FnvIndexMap, // TODO: bring constant out } impl StateMachine { - pub fn handle_transition(&mut self) { - self.current_state = State::KStopped; + pub fn new() -> Self { + StateMachine { + current_state: State::Idle, + transition_map: FnvIndexMap::::new(), + } + // TODO: populate transition_map (idk if this can be done inplace which is annoying) + } + + pub fn handle_transition(&mut self, to_state: &State) { + let to_from_state = SourceAndTarget { + source: self.current_state.clone(), + target: to_state.clone(), + }; + match self.transition_map.get(&to_from_state) { + Some(&new_state) => self.current_state = new_state, + None => (), + } + } + + pub fn run(&mut self) { + loop { + // TODO: consume, update, publish + } } } diff --git a/lib/state_machine/src/types.rs b/lib/state_machine/src/types.rs index 8af0c905..a4711d28 100644 --- a/lib/state_machine/src/types.rs +++ b/lib/state_machine/src/types.rs @@ -1,27 +1,27 @@ -#[derive(Hash)] +#[derive(Hash, PartialEq, Eq, Clone, Copy)] pub enum State { - KIdle, - KCalibrate, - KPrecharge, - KReadyForLevitation, - KBeginLevitation, - KLevitating, - KReady, - KAccelerate, - KLimBrake, - KFrictionBrake, - KStopLevitation, - KStopped, - KBatteryRecharge, - KCapacitorDischarge, - KFailureBrake, - KFailure, - KSafe, - KShutdown, + Idle, + Calibrate, + Precharge, + ReadyForLevitation, + BeginLevitation, + Levitating, + Ready, + Accelerate, + LimBrake, + FrictionBrake, + StopLevitation, + Stopped, + BatteryRecharge, + CapacitorDischarge, + FailureBrake, + Failure, + Safe, + Shutdown, } -#[derive(Hash)] +#[derive(Hash, PartialEq, Eq)] pub struct SourceAndTarget { - source: State, - target: State, + pub(crate) source: State, + pub(crate) target: State, }