From 3a43aff3194a48790349f1f2c5db81c1938971a0 Mon Sep 17 00:00:00 2001 From: Sofia Date: Mon, 1 Jun 2026 20:25:20 +0300 Subject: [PATCH] Add menu for selecting PIN --- src/async_io.rs | 8 +-- src/at_commands.rs | 2 + src/states.rs | 125 ++++++++++++++++++++++++++------------------- 3 files changed, 80 insertions(+), 55 deletions(-) diff --git a/src/async_io.rs b/src/async_io.rs index 5194be7..f44fbd1 100644 --- a/src/async_io.rs +++ b/src/async_io.rs @@ -8,6 +8,7 @@ use alloc::{ vec::Vec, }; use critical_section::Mutex; +use esp_hal::delay::Delay; use crate::at_commands::{ATCommand, ATParseError, ATResponseParser, SimpleATCommand}; @@ -144,9 +145,10 @@ impl ATPromise { pub fn poll(&self, io: &mut AsyncIO) -> Option>> { match io.poll_at_response() { Some(response) => match response { - Some(response) => Some(Some(T::parse_response(&mut ATResponseParser::from( - response, - )))), + Some(response) => { + let mut parser = ATResponseParser::from(response); + Some(Some(T::parse_response(&mut parser))) + } None => Some(None), }, None => Some(None), diff --git a/src/at_commands.rs b/src/at_commands.rs index f353d34..ccf1ea5 100644 --- a/src/at_commands.rs +++ b/src/at_commands.rs @@ -112,6 +112,8 @@ impl<'a, 'd> ATCommands<'a, 'd> { response = self.parse_response(cmd, &start); } + log::info!("{:?}", response); + response.unwrap() } diff --git a/src/states.rs b/src/states.rs index bf4a7b3..14b14c2 100644 --- a/src/states.rs +++ b/src/states.rs @@ -119,8 +119,6 @@ where #[derive(Debug, Clone)] pub struct InitATState { ati: ATCommandHelper, - enter_pin: ATCommandHelper, - check_pin: ATCommandHelper, sms_charset: ATCommandHelper, te_charset: ATCommandHelper, message: DotsMessage, @@ -130,8 +128,6 @@ impl Default for InitATState { fn default() -> Self { Self { ati: ATCommandHelper::new(ATInformationCommand), - enter_pin: ATCommandHelper::new(EnterPinCommand("1234".to_owned())), - check_pin: ATCommandHelper::new(CheckPinCommand), sms_charset: ATCommandHelper::new(SelectSMSFormatCommand(SMSFormat::TextMode)), te_charset: ATCommandHelper::new(SetTECharsetCommand(TECharset::IRA)), message: DotsMessage::default(), @@ -151,50 +147,6 @@ impl State for InitATState { return None; }; - if let Some(resp) = self.enter_pin.poll(&mut data.io) { - match resp.unwrap() { - EnterPinResult::Ok => {} - EnterPinResult::Error => { - return Some(Box::new(TextState { - text: "ERROR!".to_owned(), - after: InitATState::default(), - })); - } - EnterPinResult::ErrorMessage(msg) => { - return Some(Box::new(TextState { - text: format!("Error:\n{}", msg), - after: InitATState::default(), - })); - } - } - } else { - self.message.message = "Entering PIN".to_owned(); - return None; - }; - - if let Some(resp) = self.check_pin.poll(&mut data.io) { - match resp.unwrap() { - CheckPinResult::Status(status) => { - log::info!("Status: {}", status) - } - CheckPinResult::Error => { - return Some(Box::new(TextState { - text: "ERROR!".to_owned(), - after: InitATState::default(), - })); - } - CheckPinResult::ErrorMessage(msg) => { - return Some(Box::new(TextState { - text: format!("Error:\n{}", msg), - after: InitATState::default(), - })); - } - } - } else { - self.message.message = "Checking PIN".to_owned(); - return None; - }; - if let Some(resp) = self.sms_charset.poll(&mut data.io) { match resp.unwrap() { SimpleATResponse::Ok => {} @@ -225,10 +177,7 @@ impl State for InitATState { return None; }; - Some(Box::new(TextState { - text: "All done!".to_owned(), - after: PhoneNumberState::default(), - })) + Some(Box::new(EnterPinState::default())) } fn draw(&self, data: &mut StateData) { @@ -271,6 +220,78 @@ impl State for TextState { } } +#[derive(Clone, Default)] +pub struct EnterPinState { + written: String, + helper: Option>, +} + +impl State for EnterPinState { + fn update(&mut self, data: &mut StateData) -> Option> { + if let Some(helper) = &mut self.helper { + match helper.poll(&mut data.io) { + Some(response) => match response.unwrap() { + EnterPinResult::Ok => { + return Some(Box::new(TextState { + text: "SIM entered\nsuccessfully".to_owned(), + after: PhoneNumberState::default(), + })); + } + EnterPinResult::Error => { + return Some(Box::new(TextState { + text: "ERROR!".to_owned(), + after: EnterPinState::default(), + })); + } + EnterPinResult::ErrorMessage(msg) => { + return Some(Box::new(TextState { + text: format!("Error:\n{}", msg), + after: EnterPinState::default(), + })); + } + }, + None => return None, + } + } + + 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(); + } + } + + if data.io.keypad.get_presses(KeypadButton::KeypadA) > 0 { + self.helper = Some(ATCommandHelper::new(EnterPinCommand(self.written.clone()))); + } + None + } + + fn draw(&self, data: &mut StateData) { + data.clear_screen(Rgb565::black().as_color()); + data.draw_text("PIN:", Position::new(0, 0), TextSettings::default()); + data.draw_text( + format!("{}", self.written), + Position::new(0, 30), + TextSettings::default(), + ); + } +} + #[derive(Clone, Default)] pub struct PhoneNumberState { written: String,