Implement NumberInput

This commit is contained in:
Sofia 2026-06-01 20:42:46 +03:00
parent 3a43aff319
commit da2eb1baad
2 changed files with 54 additions and 73 deletions

View File

@ -1,10 +1,10 @@
use core::{cell::RefCell, marker::PhantomData};
use core::{cell::RefCell, char, marker::PhantomData};
use alloc::{
boxed::Box,
collections::{btree_map::BTreeMap, btree_set::BTreeSet},
rc::Rc,
string::String,
string::{String, ToString},
vec::Vec,
};
use critical_section::Mutex;
@ -205,3 +205,38 @@ impl AsyncIO {
})
}
}
#[derive(Default, Clone)]
pub struct NumberInput {
written: String,
}
impl NumberInput {
pub fn poll(&mut self, io: &mut AsyncIO) -> bool {
let mut wrote = false;
for button in io.keypad.just_pressed_buttons() {
let character = match button {
KeypadButton::Keypad1 => Some('1'),
KeypadButton::Keypad2 => Some('2'),
KeypadButton::Keypad3 => Some('3'),
KeypadButton::Keypad4 => Some('4'),
KeypadButton::Keypad5 => Some('5'),
KeypadButton::Keypad6 => Some('6'),
KeypadButton::Keypad7 => Some('7'),
KeypadButton::Keypad8 => Some('8'),
KeypadButton::Keypad9 => Some('9'),
KeypadButton::Keypad0 => Some('0'),
_ => None,
};
if let Some(character) = character {
self.written += &character.to_string();
wrote = true;
}
}
wrote
}
pub fn read<'s>(&'s self) -> &'s String {
&self.written
}
}

View File

@ -9,7 +9,7 @@ use alloc::{
use esp_hal::time::{Duration, Instant};
use crate::{
async_io::{ATPromise, KeypadButton},
async_io::{ATPromise, KeypadButton, NumberInput},
at_commands::{
ATCommand, ATInformationCommand, ATParseError, CheckPinCommand, CheckPinResult,
EnterPinCommand, EnterPinResult, SMSFormat, SelectSMSFormatCommand, SendSMSCommand,
@ -222,7 +222,7 @@ impl<T: State + Clone> State for TextState<T> {
#[derive(Clone, Default)]
pub struct EnterPinState {
written: String,
input: NumberInput,
helper: Option<ATCommandHelper<EnterPinCommand>>,
}
@ -254,29 +254,12 @@ impl State for EnterPinState {
}
}
for button in data.io.keypad.just_pressed_buttons() {
let character = match button {
KeypadButton::Keypad1 => Some('1'),
KeypadButton::Keypad2 => Some('2'),
KeypadButton::Keypad3 => Some('3'),
KeypadButton::Keypad4 => Some('4'),
KeypadButton::Keypad5 => Some('5'),
KeypadButton::Keypad6 => Some('6'),
KeypadButton::Keypad7 => Some('7'),
KeypadButton::Keypad8 => Some('8'),
KeypadButton::Keypad9 => Some('9'),
KeypadButton::Keypad0 => Some('0'),
KeypadButton::KeypadStar => Some('*'),
KeypadButton::KeypadHash => Some('#'),
_ => None,
};
if let Some(character) = character {
self.written += &character.to_string();
}
}
self.input.poll(&mut data.io);
if data.io.keypad.get_presses(KeypadButton::KeypadA) > 0 {
self.helper = Some(ATCommandHelper::new(EnterPinCommand(self.written.clone())));
self.helper = Some(ATCommandHelper::new(EnterPinCommand(
self.input.read().clone(),
)));
}
None
}
@ -285,7 +268,7 @@ impl State for EnterPinState {
data.clear_screen(Rgb565::black().as_color());
data.draw_text("PIN:", Position::new(0, 0), TextSettings::default());
data.draw_text(
format!("{}", self.written),
format!("{}", self.input.read()),
Position::new(0, 30),
TextSettings::default(),
);
@ -294,35 +277,17 @@ impl State for EnterPinState {
#[derive(Clone, Default)]
pub struct PhoneNumberState {
written: String,
input: NumberInput,
}
impl State for PhoneNumberState {
fn update(&mut self, data: &mut StateData) -> Option<Box<dyn State>> {
for button in data.io.keypad.just_pressed_buttons() {
let character = match button {
KeypadButton::Keypad1 => Some('1'),
KeypadButton::Keypad2 => Some('2'),
KeypadButton::Keypad3 => Some('3'),
KeypadButton::Keypad4 => Some('4'),
KeypadButton::Keypad5 => Some('5'),
KeypadButton::Keypad6 => Some('6'),
KeypadButton::Keypad7 => Some('7'),
KeypadButton::Keypad8 => Some('8'),
KeypadButton::Keypad9 => Some('9'),
KeypadButton::Keypad0 => Some('0'),
KeypadButton::KeypadStar => Some('*'),
KeypadButton::KeypadHash => Some('#'),
_ => None,
};
if let Some(character) = character {
self.written += &character.to_string();
}
}
self.input.poll(&mut data.io);
if data.io.keypad.get_presses(KeypadButton::KeypadA) > 0 {
Some(Box::new(MessageState {
number: self.written.clone(),
written: String::new(),
number: self.input.read().clone(),
input: Default::default(),
}))
} else {
None
@ -333,7 +298,7 @@ impl State for PhoneNumberState {
data.clear_screen(Rgb565::black().as_color());
data.draw_text("Phone num:", Position::new(0, 0), TextSettings::default());
data.draw_text(
format!("{}", self.written),
format!("{}", self.input.read()),
Position::new(0, 30),
TextSettings::default(),
);
@ -342,38 +307,19 @@ impl State for PhoneNumberState {
pub struct MessageState {
number: String,
written: String,
input: NumberInput,
}
impl State for MessageState {
fn update(&mut self, data: &mut StateData) -> Option<Box<dyn State>> {
for button in data.io.keypad.just_pressed_buttons() {
let character = match button {
KeypadButton::Keypad1 => Some('1'),
KeypadButton::Keypad2 => Some('2'),
KeypadButton::Keypad3 => Some('3'),
KeypadButton::Keypad4 => Some('4'),
KeypadButton::Keypad5 => Some('5'),
KeypadButton::Keypad6 => Some('6'),
KeypadButton::Keypad7 => Some('7'),
KeypadButton::Keypad8 => Some('8'),
KeypadButton::Keypad9 => Some('9'),
KeypadButton::Keypad0 => Some('0'),
KeypadButton::KeypadStar => Some('*'),
KeypadButton::KeypadHash => Some('#'),
_ => None,
};
if let Some(character) = character {
self.written += &character.to_string();
}
}
self.input.poll(&mut data.io);
if data.io.keypad.get_presses(KeypadButton::KeypadA) > 0 {
Some(Box::new(ATCommandState::with(
"Sending SMS..".to_owned(),
SendSMSCommand {
destination: self.number.clone(),
message: self.written.clone(),
message: self.input.read().clone(),
},
|resp| match resp.unwrap() {
SendSMSResponse::MessageRefrence(refrence) => Box::new(TextState {
@ -399,7 +345,7 @@ impl State for MessageState {
data.clear_screen(Rgb565::black().as_color());
data.draw_text("Message:", Position::new(0, 0), TextSettings::default());
data.draw_text(
format!("{}", self.written),
format!("{}", self.input.read()),
Position::new(0, 30),
TextSettings::default(),
);