transient state keeps calculator state
This commit is contained in:
123
src/main.rs
123
src/main.rs
@@ -204,135 +204,128 @@ impl Show for NumberInput {
|
||||
}
|
||||
|
||||
#[derive(uDebug)]
|
||||
enum State {
|
||||
Trans(TransState),
|
||||
Calc(CalcState),
|
||||
struct State {
|
||||
transient: TransientState,
|
||||
calculator: CalcluclatorState,
|
||||
}
|
||||
|
||||
#[derive(uDebug)]
|
||||
enum TransState {
|
||||
enum TransientState {
|
||||
Done,
|
||||
Err { timeout: usize },
|
||||
}
|
||||
|
||||
impl TransState {
|
||||
fn on_frame(
|
||||
&mut self,
|
||||
key: Option<KeyPress>,
|
||||
number_input: &mut NumberInput,
|
||||
calc: &mut Calc,
|
||||
) -> Option<CalcState> {
|
||||
impl TransientState {
|
||||
fn on_frame(&mut self, key: Option<KeyPress>) {
|
||||
match self {
|
||||
TransState::Err { timeout: 0 } => {
|
||||
number_input.reset();
|
||||
calc.reset();
|
||||
Some(CalcState::EnterSignificant)
|
||||
TransientState::Err { timeout: 0 } => {
|
||||
*self = TransientState::Done;
|
||||
}
|
||||
TransState::Err { timeout } => {
|
||||
TransientState::Err { timeout } => {
|
||||
*timeout -= 1;
|
||||
match key {
|
||||
Some(KeyPress::C) => {
|
||||
number_input.reset();
|
||||
calc.reset();
|
||||
Some(CalcState::EnterSignificant)
|
||||
*self = TransientState::Done;
|
||||
}
|
||||
_ => None,
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
TransientState::Done => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(uDebug)]
|
||||
enum CalcState {
|
||||
enum CalcluclatorState {
|
||||
EnterSignificant,
|
||||
EnterExponent,
|
||||
EnterOperation,
|
||||
}
|
||||
|
||||
impl CalcState {
|
||||
impl CalcluclatorState {
|
||||
fn on_key(
|
||||
&self,
|
||||
key: KeyPress,
|
||||
number_input: &mut NumberInput,
|
||||
calc: &mut Calc,
|
||||
) -> Result<CalcState, ()> {
|
||||
) -> Result<CalcluclatorState, ()> {
|
||||
match self {
|
||||
CalcState::EnterSignificant => match key {
|
||||
CalcluclatorState::EnterSignificant => match key {
|
||||
KeyPress::C => {
|
||||
number_input.reset();
|
||||
calc.reset();
|
||||
Ok(CalcState::EnterSignificant)
|
||||
Ok(CalcluclatorState::EnterSignificant)
|
||||
}
|
||||
KeyPress::Num(val) => {
|
||||
number_input.input(val)?;
|
||||
Ok(CalcState::EnterSignificant)
|
||||
Ok(CalcluclatorState::EnterSignificant)
|
||||
}
|
||||
KeyPress::Minus => {
|
||||
number_input.toggle_minus();
|
||||
Ok(CalcState::EnterSignificant)
|
||||
Ok(CalcluclatorState::EnterSignificant)
|
||||
}
|
||||
KeyPress::E => {
|
||||
number_input.enter_exponent();
|
||||
Ok(CalcState::EnterExponent)
|
||||
Ok(CalcluclatorState::EnterExponent)
|
||||
}
|
||||
_ => Ok(CalcState::EnterSignificant),
|
||||
_ => Ok(CalcluclatorState::EnterSignificant),
|
||||
},
|
||||
CalcState::EnterExponent => match key {
|
||||
CalcluclatorState::EnterExponent => match key {
|
||||
KeyPress::C => {
|
||||
number_input.reset();
|
||||
calc.reset();
|
||||
Ok(CalcState::EnterSignificant)
|
||||
Ok(CalcluclatorState::EnterSignificant)
|
||||
}
|
||||
KeyPress::Num(val) => {
|
||||
number_input.input(val)?;
|
||||
Ok(CalcState::EnterExponent)
|
||||
Ok(CalcluclatorState::EnterExponent)
|
||||
}
|
||||
KeyPress::Minus => {
|
||||
number_input.toggle_minus();
|
||||
Ok(CalcState::EnterExponent)
|
||||
Ok(CalcluclatorState::EnterExponent)
|
||||
}
|
||||
KeyPress::E => {
|
||||
number_input.done();
|
||||
match calc.push(number_input.to_decimal()) {
|
||||
Ok(()) => Ok(CalcState::EnterOperation),
|
||||
Ok(()) => Ok(CalcluclatorState::EnterOperation),
|
||||
Err(_) => Err(()),
|
||||
}
|
||||
}
|
||||
_ => Ok(CalcState::EnterExponent),
|
||||
_ => Ok(CalcluclatorState::EnterExponent),
|
||||
},
|
||||
CalcState::EnterOperation => match key {
|
||||
CalcluclatorState::EnterOperation => match key {
|
||||
KeyPress::Up => todo!(),
|
||||
KeyPress::C => {
|
||||
number_input.reset();
|
||||
calc.reset();
|
||||
Ok(CalcState::EnterSignificant)
|
||||
Ok(CalcluclatorState::EnterSignificant)
|
||||
}
|
||||
KeyPress::Num(_) => todo!(),
|
||||
KeyPress::Mul => match calc.mul() {
|
||||
Ok(dec) => {
|
||||
number_input.set_result(dec)?;
|
||||
Ok(CalcState::EnterOperation)
|
||||
Ok(CalcluclatorState::EnterOperation)
|
||||
}
|
||||
Err(_) => Err(()),
|
||||
},
|
||||
KeyPress::Div => match calc.div() {
|
||||
Ok(dec) => {
|
||||
number_input.set_result(dec)?;
|
||||
Ok(CalcState::EnterOperation)
|
||||
Ok(CalcluclatorState::EnterOperation)
|
||||
}
|
||||
Err(_) => Err(()),
|
||||
},
|
||||
KeyPress::Plus => match calc.add() {
|
||||
Ok(dec) => {
|
||||
number_input.set_result(dec)?;
|
||||
Ok(CalcState::EnterOperation)
|
||||
Ok(CalcluclatorState::EnterOperation)
|
||||
}
|
||||
Err(_) => Err(()),
|
||||
},
|
||||
KeyPress::Minus => match calc.sub() {
|
||||
Ok(dec) => {
|
||||
number_input.set_result(dec)?;
|
||||
Ok(CalcState::EnterOperation)
|
||||
Ok(CalcluclatorState::EnterOperation)
|
||||
}
|
||||
Err(_) => Err(()),
|
||||
},
|
||||
@@ -342,7 +335,7 @@ impl CalcState {
|
||||
Err(())
|
||||
} else {
|
||||
number_input.reset();
|
||||
Ok(CalcState::EnterSignificant)
|
||||
Ok(CalcluclatorState::EnterSignificant)
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -391,7 +384,10 @@ fn main() -> ! {
|
||||
number_input.show(&mut display);
|
||||
|
||||
let mut calc = Calc::default();
|
||||
let mut state = State::Calc(CalcState::EnterSignificant);
|
||||
let mut state = State {
|
||||
transient: TransientState::Done,
|
||||
calculator: CalcluclatorState::EnterSignificant,
|
||||
};
|
||||
|
||||
loop {
|
||||
let mut last_key_readout: Option<KeyReadout> = None;
|
||||
@@ -414,39 +410,28 @@ fn main() -> ! {
|
||||
ufmt::uwriteln!(&mut serial, "key: {:?} state: {:?}", key, state).unwrap_infallible();
|
||||
}
|
||||
|
||||
match &mut state {
|
||||
State::Trans(trans_state) => {
|
||||
if let Some(calc_state) = trans_state.on_frame(key, &mut number_input, &mut calc) {
|
||||
state = State::Calc(calc_state);
|
||||
}
|
||||
}
|
||||
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,
|
||||
});
|
||||
}
|
||||
if let TransientState::Done = state.transient {
|
||||
if let Some(key) = key {
|
||||
match state.calculator.on_key(key, &mut number_input, &mut calc) {
|
||||
Ok(new_state) => state.calculator = new_state,
|
||||
Err(()) => {
|
||||
state.transient = TransientState::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();
|
||||
}
|
||||
} else {
|
||||
state.transient.on_frame(key)
|
||||
}
|
||||
|
||||
match &state {
|
||||
State::Trans(_) => display.error(),
|
||||
State::Calc(_) => number_input.show(&mut display),
|
||||
match &state.transient {
|
||||
TransientState::Done => number_input.show(&mut display),
|
||||
TransientState::Err { .. } => display.error(),
|
||||
}
|
||||
|
||||
arduino_hal::delay_ms(1);
|
||||
}
|
||||
|
||||
// loop {
|
||||
// led.toggle();
|
||||
// arduino_hal::delay_ms(1000);
|
||||
// ufmt::uwrite!(&mut serial, ".").unwrap_infallible();
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user