From 22f2b362449bbd22e8125ed29c95d502cc3d7754 Mon Sep 17 00:00:00 2001 From: Sofia Date: Sun, 17 May 2026 02:27:08 +0300 Subject: [PATCH] Implement AT init in InitATState --- src/main.rs | 63 ++++++--------------------------------- src/state.rs | 18 ++--------- src/states.rs | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 70 deletions(-) create mode 100644 src/states.rs diff --git a/src/main.rs b/src/main.rs index 6d76c98..4275976 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,18 +7,14 @@ )] #![deny(clippy::large_stack_frames)] -use core::fmt::Write; - -use alloc::{borrow::ToOwned, boxed::Box, str}; -use embedded_hal::delay::DelayNs; -use esp_alloc::export::enumset::EnumSet; +use alloc::boxed::Box; use esp_hal::{ clock::CpuClock, delay::Delay, - gpio::{Input, InputConfig, Level, Output, OutputConfig}, + gpio::{InputConfig, Level, Output, OutputConfig}, main, spi::master::{Config, Spi}, - time::{Duration, Instant, Rate}, + time::Rate, uart::{self, Uart}, }; @@ -26,9 +22,9 @@ use esp_backtrace as _; use crate::{ at_commands::ATCommands, - display::{Display, Position, Rgb565, SetAddressMode}, - font::{FontRenderer, HorizontalAlignment, VerticalAlignment}, - state::{HelloState, StateManager}, + display::{Display, SetAddressMode}, + state::StateManager, + states::InitATState, }; extern crate alloc; @@ -37,6 +33,7 @@ mod at_commands; mod display; mod font; mod state; +mod states; // This creates a default app-descriptor required by the esp-idf bootloader. // For more information see: @@ -105,8 +102,6 @@ fn main() -> ! { }); display.set_tearing(display::TearingMode::Off); - let font_renderer = FontRenderer::create(30); - let sim_rst = Output::new(peripherals.GPIO27, Level::High, OutputConfig::default()); let sim_pwr_key = Output::new(peripherals.GPIO12, Level::High, OutputConfig::default()); @@ -122,57 +117,17 @@ fn main() -> ! { .with_rx(peripherals.GPIO16) .with_tx(peripherals.GPIO17); - let mut at_commands = ATCommands { + let at_commands = ATCommands { rst: sim_rst, pwr_key: sim_pwr_key, uart, delay: Delay::new(), }; - font_renderer.render( - &mut display, - "Hello\nWorld!", - Position::new(0, 0), - HorizontalAlignment::LeftToRight, - VerticalAlignment::TopToBottom, - Rgb565::black(), - Rgb565::white(), - ); - - let mut render_response = |response: alloc::string::String| { - log::info!("Rendering: {}", response); - display.draw_rect( - Position::new(0, 0), - Position::new(240, 240), - Rgb565::black().as_color(), - ); - - font_renderer.render( - &mut display, - response, - Position::new(0, 0), - HorizontalAlignment::LeftToRight, - VerticalAlignment::TopToBottom, - Rgb565::black(), - Rgb565::white(), - ); - }; - - // at_commands.init(); - // render_response(at_commands.raw_command("ATI".to_owned())); - // render_response(at_commands.raw_command("AT+CMGF?".to_owned())); - // render_response(at_commands.raw_command("AT+CPIN=1234".to_owned())); - // render_response(at_commands.raw_command("AT+CPIN?".to_owned())); - // render_response(at_commands.raw_command("AT+CMGF=1".to_owned())); - // render_response(at_commands.raw_command("AT+CSCS=?".to_owned())); - let mut state_mgr = StateManager { data: state::StateData::from(display, at_commands), - curr_state: Box::new(HelloState), + curr_state: Box::new(InitATState::default()), }; - // render_response( - // at_commands.raw_two_part_command("AT+CMGS=\"number\"".to_owned(), "hello".to_owned()), - // ); let pull_down_cfg = InputConfig::default().with_pull(esp_hal::gpio::Pull::None); let pull_up_cfg = InputConfig::default().with_pull(esp_hal::gpio::Pull::None); diff --git a/src/state.rs b/src/state.rs index 6cb5d30..809cef5 100644 --- a/src/state.rs +++ b/src/state.rs @@ -34,7 +34,7 @@ pub enum DrawCommands { pub struct StateData<'a> { display: Display<'a, Blocking, Delay>, - at_commands: ATCommands<'a, 'a>, + pub at_commands: ATCommands<'a, 'a>, font_renderer: FontRenderer<'a>, pub delay: Delay, @@ -69,7 +69,7 @@ impl<'a> StateData<'a> { } pub trait State { - fn update(&self, data: &mut StateData) -> Option>; + fn update(&mut self, data: &mut StateData) -> Option>; fn draw(&self, data: &mut StateData); } @@ -110,17 +110,3 @@ impl<'a> StateManager<'a> { } } } - -pub struct HelloState; - -impl State for HelloState { - fn update(&self, data: &mut StateData) -> Option> { - data.delay.delay_millis(100); - None - } - - fn draw(&self, data: &mut StateData) { - data.clear_screen(Rgb565::black().as_color()); - data.draw_text("Hello World", Position::new(0, 0), TextSettings::default()); - } -} diff --git a/src/states.rs b/src/states.rs new file mode 100644 index 0000000..1907b17 --- /dev/null +++ b/src/states.rs @@ -0,0 +1,82 @@ +use alloc::{borrow::ToOwned, boxed::Box, string::String}; + +use crate::{ + display::{Position, Rgb565}, + state::{State, StateData, TextSettings}, +}; + +pub struct InitATState { + inner_state: u8, + response: String, +} + +impl Default for InitATState { + fn default() -> Self { + Self { + inner_state: 0, + response: "Initializing AT..".to_owned(), + } + } +} + +impl State for InitATState { + fn update(&mut self, data: &mut StateData) -> Option> { + let res: Option> = match self.inner_state { + 0 => { + data.at_commands.init(); + self.response = data.at_commands.raw_command("ATI".to_owned()); + None + } + 1 => { + self.response = data.at_commands.raw_command("AT+CMGF?".to_owned()); + None + } + 2 => { + self.response = data.at_commands.raw_command("AT+CPIN=1234".to_owned()); + None + } + 3 => { + self.response = data.at_commands.raw_command("AT+CPIN?".to_owned()); + None + } + 4 => { + self.response = data.at_commands.raw_command("AT+CMGF=1".to_owned()); + None + } + 5 => { + self.response = data.at_commands.raw_command("AT+CSCS=?".to_owned()); + None + } + _ => { + data.delay.delay_millis(1000); + Some(Box::new(TextState { + text: "AT init done!".to_owned(), + })) + } + }; + + self.inner_state = (self.inner_state + 1).min(7); + res + } + + fn draw(&self, data: &mut StateData) { + data.clear_screen(Rgb565::black().as_color()); + data.draw_text(&self.response, Position::new(0, 0), TextSettings::default()); + } +} + +pub struct TextState { + text: String, +} + +impl State for TextState { + fn update(&mut self, data: &mut StateData) -> Option> { + data.delay.delay_millis(100); + None + } + + fn draw(&self, data: &mut StateData) { + data.clear_screen(Rgb565::black().as_color()); + data.draw_text(&self.text, Position::new(0, 0), TextSettings::default()); + } +}