Utilize async in state loop

This commit is contained in:
Sofia 2026-05-17 18:14:03 +03:00
parent 15bffd04a8
commit 756ab08b74

View File

@ -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<Box<dyn State>> {
// 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<Box<dyn State>> = 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::<String>();
data.draw_text(
format!("{}{}", self.message, dots),
Position::new(0, 0),
TextSettings::default(),
);
}
}