From 756ab08b749708b11e79a13a26b3884bc958d05b Mon Sep 17 00:00:00 2001 From: Sofia Date: Sun, 17 May 2026 18:14:03 +0300 Subject: [PATCH] Utilize async in state loop --- src/states.rs | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/states.rs b/src/states.rs index 94ca599..8349318 100644 --- a/src/states.rs +++ b/src/states.rs @@ -1,8 +1,12 @@ +use core::iter::repeat; + use alloc::{ borrow::ToOwned, boxed::Box, + format, string::{String, ToString}, }; +use esp_hal::time::{Duration, Instant}; use crate::{ async_io::{ATCommand, SMSFormat}, @@ -12,36 +16,51 @@ use crate::{ pub struct InitATState { inner_state: u8, - response: String, + message: String, + dots: u8, + prev_dots: Instant, } impl Default for InitATState { fn default() -> Self { Self { inner_state: 0, - response: "Initializing AT..".to_string(), + message: "Initializing AT..".to_string(), + dots: 0, + prev_dots: Instant::now(), } } } impl State for InitATState { fn update(&mut self, data: &mut StateData) -> Option> { + // Update dots + if self.prev_dots.elapsed() > Duration::from_millis(200) { + self.dots = (self.dots + 1) % 3; + self.prev_dots = Instant::now(); + } + + // Wait for previous AT command to finish if let Some(response) = data.io.poll_at_response() { match response { - Some(response) => self.response = response, + Some(_) => {} None => {} } return None; } + + // Send next AT command let res: Option> = match self.inner_state { 0 => { data.io.send_at_command(ATCommand::ATInformation).unwrap(); + self.message = "Checking info".to_owned(); None } 1 => { data.io .send_at_command(ATCommand::EnterPin("1234".to_owned())) .unwrap(); + self.message = "Entering PIN".to_owned(); None } 2 => { @@ -52,12 +71,14 @@ impl State for InitATState { data.io .send_at_command(ATCommand::ListTECharacterSets) .unwrap(); + self.message = "Checking\ncharsets".to_owned(); None } 4 => { data.io .send_at_command(ATCommand::SelectSMSFormat(SMSFormat::TextMode)) .unwrap(); + self.message = "Selecting SMS\nformat".to_owned(); None } _ => { @@ -74,7 +95,12 @@ impl State for InitATState { fn draw(&self, data: &mut StateData) { data.clear_screen(Rgb565::black().as_color()); - data.draw_text(&self.response, Position::new(0, 0), TextSettings::default()); + let dots = repeat(".").take(self.dots as usize).collect::(); + data.draw_text( + format!("{}{}", self.message, dots), + Position::new(0, 0), + TextSettings::default(), + ); } }