Implement AT init in InitATState
This commit is contained in:
parent
3546cf62c6
commit
22f2b36244
63
src/main.rs
63
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: <https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/app_image_format.html#application-description>
|
||||
@ -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);
|
||||
|
||||
18
src/state.rs
18
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<Box<dyn State>>;
|
||||
fn update(&mut self, data: &mut StateData) -> Option<Box<dyn State>>;
|
||||
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<Box<dyn State>> {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
82
src/states.rs
Normal file
82
src/states.rs
Normal file
@ -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<Box<dyn State>> {
|
||||
let res: Option<Box<dyn State>> = 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<Box<dyn State>> {
|
||||
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());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user