factored out transition state

This commit is contained in:
2025-07-18 20:31:23 +01:00
parent 86b4b227bf
commit f4dde687cf

View File

@@ -205,96 +205,134 @@ impl Show for NumberInput {
#[derive(uDebug)] #[derive(uDebug)]
enum State { enum State {
EnterSignificant, Trans(TransState),
EnterExponent, Calc(CalcState),
EnterOperation, }
#[derive(uDebug)]
enum TransState {
Err { timeout: usize }, Err { timeout: usize },
} }
impl State { impl TransState {
fn on_frame(
&mut self,
key: Option<KeyPress>,
number_input: &mut NumberInput,
calc: &mut Calc,
) -> Option<CalcState> {
match self {
TransState::Err { timeout: 0 } => {
number_input.reset();
calc.reset();
Some(CalcState::EnterSignificant)
}
TransState::Err { timeout } => {
*timeout -= 1;
match key {
Some(KeyPress::C) => {
number_input.reset();
calc.reset();
Some(CalcState::EnterSignificant)
}
_ => None,
}
}
}
}
}
#[derive(uDebug)]
enum CalcState {
EnterSignificant,
EnterExponent,
EnterOperation,
}
impl CalcState {
fn on_key( fn on_key(
&self, &self,
key: KeyPress, key: KeyPress,
number_input: &mut NumberInput, number_input: &mut NumberInput,
calc: &mut Calc, calc: &mut Calc,
) -> Result<State, ()> { ) -> Result<CalcState, ()> {
match self { match self {
State::EnterSignificant => match key { CalcState::EnterSignificant => match key {
KeyPress::C => { KeyPress::C => {
number_input.reset(); number_input.reset();
calc.reset(); calc.reset();
Ok(State::EnterSignificant) Ok(CalcState::EnterSignificant)
} }
KeyPress::Num(val) => { KeyPress::Num(val) => {
number_input.input(val)?; number_input.input(val)?;
Ok(State::EnterSignificant) Ok(CalcState::EnterSignificant)
} }
KeyPress::Minus => { KeyPress::Minus => {
number_input.toggle_minus(); number_input.toggle_minus();
Ok(State::EnterSignificant) Ok(CalcState::EnterSignificant)
} }
KeyPress::E => { KeyPress::E => {
number_input.enter_exponent(); number_input.enter_exponent();
Ok(State::EnterExponent) Ok(CalcState::EnterExponent)
} }
_ => Ok(State::EnterSignificant), _ => Ok(CalcState::EnterSignificant),
}, },
State::EnterExponent => match key { CalcState::EnterExponent => match key {
KeyPress::C => { KeyPress::C => {
number_input.reset(); number_input.reset();
calc.reset(); calc.reset();
Ok(State::EnterSignificant) Ok(CalcState::EnterSignificant)
} }
KeyPress::Num(val) => { KeyPress::Num(val) => {
number_input.input(val)?; number_input.input(val)?;
Ok(State::EnterExponent) Ok(CalcState::EnterExponent)
} }
KeyPress::Minus => { KeyPress::Minus => {
number_input.toggle_minus(); number_input.toggle_minus();
Ok(State::EnterExponent) Ok(CalcState::EnterExponent)
} }
KeyPress::E => { KeyPress::E => {
number_input.done(); number_input.done();
match calc.push(number_input.to_decimal()) { match calc.push(number_input.to_decimal()) {
Ok(()) => Ok(State::EnterOperation), Ok(()) => Ok(CalcState::EnterOperation),
Err(_) => Err(()), Err(_) => Err(()),
} }
} }
_ => Ok(State::EnterExponent), _ => Ok(CalcState::EnterExponent),
}, },
State::EnterOperation => match key { CalcState::EnterOperation => match key {
KeyPress::Up => todo!(), KeyPress::Up => todo!(),
KeyPress::C => { KeyPress::C => {
number_input.reset(); number_input.reset();
calc.reset(); calc.reset();
Ok(State::EnterSignificant) Ok(CalcState::EnterSignificant)
} }
KeyPress::Num(_) => todo!(), KeyPress::Num(_) => todo!(),
KeyPress::Mul => match calc.mul() { KeyPress::Mul => match calc.mul() {
Ok(dec) => { Ok(dec) => {
number_input.set_result(dec)?; number_input.set_result(dec)?;
Ok(State::EnterOperation) Ok(CalcState::EnterOperation)
} }
Err(_) => Err(()), Err(_) => Err(()),
}, },
KeyPress::Div => match calc.div() { KeyPress::Div => match calc.div() {
Ok(dec) => { Ok(dec) => {
number_input.set_result(dec)?; number_input.set_result(dec)?;
Ok(State::EnterOperation) Ok(CalcState::EnterOperation)
} }
Err(_) => Err(()), Err(_) => Err(()),
}, },
KeyPress::Plus => match calc.add() { KeyPress::Plus => match calc.add() {
Ok(dec) => { Ok(dec) => {
number_input.set_result(dec)?; number_input.set_result(dec)?;
Ok(State::EnterOperation) Ok(CalcState::EnterOperation)
} }
Err(_) => Err(()), Err(_) => Err(()),
}, },
KeyPress::Minus => match calc.sub() { KeyPress::Minus => match calc.sub() {
Ok(dec) => { Ok(dec) => {
number_input.set_result(dec)?; number_input.set_result(dec)?;
Ok(State::EnterOperation) Ok(CalcState::EnterOperation)
} }
Err(_) => Err(()), Err(_) => Err(()),
}, },
@@ -304,18 +342,10 @@ impl State {
Err(()) Err(())
} else { } else {
number_input.reset(); number_input.reset();
Ok(State::EnterSignificant) Ok(CalcState::EnterSignificant)
} }
} }
}, },
State::Err { timeout } => match key {
KeyPress::C => {
number_input.reset();
calc.reset();
Ok(State::EnterSignificant)
}
_ => Ok(State::Err { timeout: *timeout }),
},
} }
} }
} }
@@ -361,7 +391,7 @@ fn main() -> ! {
number_input.show(&mut display); number_input.show(&mut display);
let mut calc = Calc::default(); let mut calc = Calc::default();
let mut state = State::EnterSignificant; let mut state = State::Calc(CalcState::EnterSignificant);
loop { loop {
let mut last_key_readout: Option<KeyReadout> = None; let mut last_key_readout: Option<KeyReadout> = None;
@@ -378,40 +408,37 @@ fn main() -> ! {
io_select.set_off(); io_select.set_off();
} }
match &mut state { let key = debounce.input(last_key_readout);
State::Err { timeout } if *timeout == 0 => {
number_input.reset(); if let Some(key) = &key {
calc.reset(); ufmt::uwriteln!(&mut serial, "key: {:?} state: {:?}", key, state).unwrap_infallible();
state = State::EnterSignificant;
number_input.show(&mut display);
ufmt::uwriteln!(&mut serial, "err timout state: {:?}", state).unwrap_infallible();
}
State::Err { timeout } => {
*timeout -= 1;
}
_ => (),
} }
if let Some(key) = debounce.input(last_key_readout) { match &mut state {
ufmt::uwriteln!(&mut serial, "key: {:?} state: {:?}", key, state).unwrap_infallible(); State::Trans(trans_state) => {
match state.on_key(key, &mut number_input, &mut calc) { if let Some(calc_state) = trans_state.on_frame(key, &mut number_input, &mut calc) {
Ok(new_state) => state = new_state, state = State::Calc(calc_state);
Err(()) => { }
state = State::Err { }
timeout: ERROR_TIMEOUT, State::Calc(calc_state) => {
if let Some(key) = key {
match calc_state.on_key(key, &mut number_input, &mut calc) {
Ok(new_state) => *calc_state = new_state,
Err(()) => {
state = State::Trans(TransState::Err {
timeout: ERROR_TIMEOUT,
});
}
} }
ufmt::uwriteln!(&mut serial, "state: {:?} stack: {}", state, calc.len())
.unwrap_infallible();
} }
} }
ufmt::uwriteln!(&mut serial, "state: {:?} stack: {}", state, calc.len()) }
.unwrap_infallible();
match &state { match &state {
State::EnterSignificant | State::EnterExponent | State::EnterOperation => { State::Trans(_) => display.error(),
number_input.show(&mut display) State::Calc(_) => number_input.show(&mut display),
}
State::Err { .. } => {
display.error();
}
}
} }
arduino_hal::delay_ms(1); arduino_hal::delay_ms(1);