integrate calculator

This commit is contained in:
2025-07-18 20:31:23 +01:00
parent 87bb879fab
commit 998623413d
3 changed files with 136 additions and 17 deletions

View File

@@ -1,6 +1,8 @@
#![no_std]
#![no_main]
use calc_math::calc::StackCalc;
use calc_math::Decimal;
use core::ops::{Index, IndexMut};
use arduino_hal::{
@@ -476,7 +478,31 @@ impl NumberInput {
}
}
fn apply(&self, display: &mut DispalyState) {
fn to_decimal(&self) -> Decimal<5, u8> {
Decimal {
minus: self.minus,
significant: self.significant,
minus_exponent: self.minus_exponent,
exponent: self.exponent[0] * 10 + self.exponent[1],
}
}
fn set_decimal(&mut self, dec: Decimal<5, u8>) {
self.minus = dec.minus;
self.significant = dec.significant;
self.minus_exponent = dec.minus_exponent;
self.exponent[0] = dec.exponent / 10;
self.exponent[1] = dec.exponent - dec.exponent / 10;
}
}
// Show data on segment display
trait Show {
fn show(&self, display: &mut DispalyState);
}
impl Show for NumberInput {
fn show(&self, display: &mut DispalyState) {
display[0] = if self.minus {
SegmentState::Minus
} else {
@@ -497,6 +523,13 @@ impl NumberInput {
}
}
enum State {
EnterSignificant,
EnterExponent,
EnterOperation,
Err,
}
#[arduino_hal::entry]
fn main() -> ! {
let dp = arduino_hal::Peripherals::take().unwrap();
@@ -539,7 +572,10 @@ fn main() -> ! {
let mut number_input = NumberInput::default();
let mut display = DispalyState::default();
number_input.apply(&mut display);
number_input.show(&mut display);
let mut calc = StackCalc::<2, 5, u8>::default();
let mut state = State::EnterSignificant;
loop {
let mut key: Option<(usize, bool, bool)> = None;
@@ -558,25 +594,84 @@ fn main() -> ! {
}
let key = key.and_then(|(no, kn, ko)| InputKey::map(no, kn, ko));
// TODO: impl main state
let mut err = Ok(());
if let Some(key) = debounce.input(key) {
ufmt::uwriteln!(&mut serial, "key: {:?}", key).unwrap_infallible();
match key {
InputKey::Up => todo!(),
InputKey::C => number_input.reset(),
InputKey::Num(val) => {
err = number_input.input(val);
}
InputKey::Mul => todo!(),
InputKey::Div => todo!(),
InputKey::Plus => todo!(),
InputKey::Minus => number_input.toggle_minus(),
InputKey::Down => todo!(),
InputKey::E => number_input.toggle_enter_exponent(),
let res = match state {
State::EnterSignificant => match key {
InputKey::C => Ok(number_input.reset()),
InputKey::Num(val) => number_input.input(val),
InputKey::Minus => Ok(number_input.toggle_minus()),
InputKey::E => {
number_input.toggle_enter_exponent();
state = State::EnterExponent;
Ok(())
}
_ => Ok(()),
},
State::EnterExponent => match key {
InputKey::C => {
number_input.reset();
state = State::EnterSignificant;
Ok(())
}
InputKey::Num(val) => number_input.input(val),
InputKey::Minus => Ok(number_input.toggle_minus()),
InputKey::E => {
number_input.toggle_enter_exponent();
match calc.push(number_input.to_decimal()) {
Ok(()) => {
number_input.reset();
state = State::EnterSignificant;
}
Err(_) => {
state = State::EnterOperation;
}
}
Ok(())
}
_ => Ok(()),
},
State::EnterOperation => match key {
InputKey::Up => todo!(),
InputKey::C => {
number_input.reset();
state = State::EnterSignificant;
Ok(())
}
InputKey::Num(_) => todo!(),
InputKey::Mul => todo!(),
InputKey::Div => todo!(),
InputKey::Plus => match calc.add() {
Ok(dec) => {
number_input.set_decimal(dec);
Ok(())
}
Err(_) => Err(()),
},
InputKey::Minus => todo!(),
InputKey::Down => todo!(),
InputKey::E => todo!(),
},
State::Err => match key {
InputKey::C => {
number_input.reset();
state = State::EnterSignificant;
Ok(())
}
_ => Ok(()),
},
};
if res.is_err() {
state = State::Err
}
match state {
State::EnterSignificant | State::EnterExponent | State::EnterOperation => {
number_input.show(&mut display)
}
State::Err => todo!("display error"),
}
number_input.apply(&mut display);
}
arduino_hal::delay_ms(1);
}