Skip to content

Commit

Permalink
more skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanCavers committed Oct 1, 2024
1 parent 70287f4 commit 6ed117c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 34 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 10 additions & 7 deletions lib/state_machine/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
[package]
name = "hyped_state_machine"
version = "0.1.0"
edition = "2021"

[features]
std = []
[package]
name = "hyped_state_machine"
version = "0.1.0"
edition = "2021"

[features]
std = []

[dependencies]
heapless = "0.8.0"
32 changes: 27 additions & 5 deletions lib/state_machine/src/state_machine.rs
Original file line number Diff line number Diff line change
@@ -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<SourceAndTarget, State> (use heapless::FnvIndexMap)?
pub(crate) current_state: State,
pub(crate) transition_map: FnvIndexMap<SourceAndTarget, State, 32>, // 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::<SourceAndTarget, State, 32>::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
}
}
}
44 changes: 22 additions & 22 deletions lib/state_machine/src/types.rs
Original file line number Diff line number Diff line change
@@ -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,
}

0 comments on commit 6ed117c

Please sign in to comment.