Utilize async in state loop
This commit is contained in:
parent
15bffd04a8
commit
756ab08b74
@ -1,8 +1,12 @@
|
|||||||
|
use core::iter::repeat;
|
||||||
|
|
||||||
use alloc::{
|
use alloc::{
|
||||||
borrow::ToOwned,
|
borrow::ToOwned,
|
||||||
boxed::Box,
|
boxed::Box,
|
||||||
|
format,
|
||||||
string::{String, ToString},
|
string::{String, ToString},
|
||||||
};
|
};
|
||||||
|
use esp_hal::time::{Duration, Instant};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
async_io::{ATCommand, SMSFormat},
|
async_io::{ATCommand, SMSFormat},
|
||||||
@ -12,36 +16,51 @@ use crate::{
|
|||||||
|
|
||||||
pub struct InitATState {
|
pub struct InitATState {
|
||||||
inner_state: u8,
|
inner_state: u8,
|
||||||
response: String,
|
message: String,
|
||||||
|
dots: u8,
|
||||||
|
prev_dots: Instant,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for InitATState {
|
impl Default for InitATState {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
inner_state: 0,
|
inner_state: 0,
|
||||||
response: "Initializing AT..".to_string(),
|
message: "Initializing AT..".to_string(),
|
||||||
|
dots: 0,
|
||||||
|
prev_dots: Instant::now(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl State for InitATState {
|
impl State for InitATState {
|
||||||
fn update(&mut self, data: &mut StateData) -> Option<Box<dyn State>> {
|
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() {
|
if let Some(response) = data.io.poll_at_response() {
|
||||||
match response {
|
match response {
|
||||||
Some(response) => self.response = response,
|
Some(_) => {}
|
||||||
None => {}
|
None => {}
|
||||||
}
|
}
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Send next AT command
|
||||||
let res: Option<Box<dyn State>> = match self.inner_state {
|
let res: Option<Box<dyn State>> = match self.inner_state {
|
||||||
0 => {
|
0 => {
|
||||||
data.io.send_at_command(ATCommand::ATInformation).unwrap();
|
data.io.send_at_command(ATCommand::ATInformation).unwrap();
|
||||||
|
self.message = "Checking info".to_owned();
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
1 => {
|
1 => {
|
||||||
data.io
|
data.io
|
||||||
.send_at_command(ATCommand::EnterPin("1234".to_owned()))
|
.send_at_command(ATCommand::EnterPin("1234".to_owned()))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
self.message = "Entering PIN".to_owned();
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
2 => {
|
2 => {
|
||||||
@ -52,12 +71,14 @@ impl State for InitATState {
|
|||||||
data.io
|
data.io
|
||||||
.send_at_command(ATCommand::ListTECharacterSets)
|
.send_at_command(ATCommand::ListTECharacterSets)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
self.message = "Checking\ncharsets".to_owned();
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
4 => {
|
4 => {
|
||||||
data.io
|
data.io
|
||||||
.send_at_command(ATCommand::SelectSMSFormat(SMSFormat::TextMode))
|
.send_at_command(ATCommand::SelectSMSFormat(SMSFormat::TextMode))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
self.message = "Selecting SMS\nformat".to_owned();
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
@ -74,7 +95,12 @@ impl State for InitATState {
|
|||||||
|
|
||||||
fn draw(&self, data: &mut StateData) {
|
fn draw(&self, data: &mut StateData) {
|
||||||
data.clear_screen(Rgb565::black().as_color());
|
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(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user