Use FSM to manage device state in main loop

This commit is contained in:
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, report_readings: bool,
} }
#[derive(Default)]
enum State {
#[default]
LoadFlashSettings,
MainLoop,
SaveFlashSettings,
HardReset,
}
#[cfg(not(test))] #[cfg(not(test))]
#[entry] #[entry]
fn main() -> ! { fn main() -> ! {
@ -84,10 +93,19 @@ fn main() -> ! {
report_readings: false, report_readings: false,
}; };
let mut state = State::default();
loop { loop {
wd.feed(); 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 eth_is_pending = false;
let mut has_temp_reading = false; let mut has_temp_reading = false;
@ -102,18 +120,6 @@ fn main() -> ! {
thermostat.power_down(); 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 net::net::eth_is_socket_active() {
if device_settings.report_readings { if device_settings.report_readings {
unsafe { unsafe {
@ -150,8 +156,22 @@ fn main() -> ! {
else { else {
device_settings.report_readings = false; 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; let mut any_socket_alive = false;
if net::net::eth_is_socket_active() { if net::net::eth_is_socket_active() {
net::net::eth_close_socket(); net::net::eth_close_socket();
@ -166,3 +186,4 @@ fn main() -> ! {
} }
} }
} }
}