Use FSM to manage device state in main loop

master
linuswck 2024-03-04 11:08:26 +08:00
parent 3aca712e1d
commit df939eb9a3
1 changed files with 86 additions and 65 deletions

View File

@ -35,6 +35,15 @@ pub struct DeviceSettings{
report_readings: bool,
}
#[derive(Default)]
enum State {
#[default]
LoadFlashSettings,
MainLoop,
SaveFlashSettings,
HardReset,
}
#[cfg(not(test))]
#[entry]
fn main() -> ! {
@ -84,10 +93,19 @@ fn main() -> ! {
report_readings: false,
};
let mut state = State::default();
loop {
wd.feed();
if !should_reset {
match state {
State::LoadFlashSettings => {
wd.feed();
// Todo
// State Transition
state = State::MainLoop;
}
State::MainLoop => {
let mut eth_is_pending = false;
let mut has_temp_reading = false;
@ -102,18 +120,6 @@ fn main() -> ! {
thermostat.power_down();
}
// info!("curr_dac_vfb: {:?}", volt_fmt.with(thermostat.get_dac_vfb()));
// info!("curr_vref: {:?}", volt_fmt.with(thermostat.get_vref()));
// info!("curr_tec_i: {:?}", amp_fmt.with(thermostat.get_tec_i()));
// info!("curr_tec_v: {:?}", volt_fmt.with(thermostat.get_tec_v()));
// info!("curr_ld_drive_cuurent: {:?}", milli_amp_fmt.with(laser.get_ld_drive_current()));
// info!("pd_mon_v: {:?}", volt_fmt.with(laser.pd_mon_status().v));
// info!("power_excursion: {:?}", laser.pd_mon_status().pwr_excursion);
// info!("Termination Status: {:?}", laser.get_term_status());
if net::net::eth_is_socket_active() {
if device_settings.report_readings {
unsafe {
@ -150,8 +156,22 @@ fn main() -> ! {
else {
device_settings.report_readings = false;
}
} else {
// Should reset, close all TCP sockets.
// State Transition
if should_reset {
state = State::HardReset;
}
}
State::SaveFlashSettings => {
wd.feed();
// Todo
// State Transition
state = State::MainLoop;
}
State::HardReset => {
wd.feed();
laser.power_down();
thermostat.power_down();
let mut any_socket_alive = false;
if net::net::eth_is_socket_active() {
net::net::eth_close_socket();
@ -165,4 +185,5 @@ fn main() -> ! {
}
}
}
}
}