diff --git a/src/at_commands.rs b/src/at_commands.rs index 373700e..ae24ce5 100644 --- a/src/at_commands.rs +++ b/src/at_commands.rs @@ -1,3 +1,4 @@ +use alloc::format; use alloc::string::String; use alloc::vec::Vec; use core::fmt::Write; @@ -32,20 +33,19 @@ impl<'a, 'd> ATCommands<'a, 'd> { log::info!("ATCommands Ready!"); self.delay.delay_millis(1_000); - let mut buffer = [0u8; 1024]; - let length = self.uart.read(&mut buffer).unwrap(); - log::info!("text: {:?}", str::from_utf8(&buffer[..length])); + log::info!("text: {}", self.flush_rx()); } - pub fn test(&mut self) { + pub fn raw_command(&mut self, command: String) -> String { + self.flush_rx(); + self.uart.flush().unwrap(); self.delay.delay_millis(250); - self.uart.write_str("ATI\r").unwrap(); + self.uart.write_str(&(command.clone() + "\r")).unwrap(); self.uart.flush().unwrap(); self.delay.delay_millis(500); - log::info!("Wrote command"); - let response = self.read_response(); - log::info!("Response: {}", response); + log::info!("Wrote command {}", command); + self.read_response() } fn read_response(&mut self) -> String { @@ -73,4 +73,19 @@ impl<'a, 'd> ATCommands<'a, 'd> { .collect::>() .join("\n") } + + fn flush_rx(&mut self) -> String { + let mut buffer = [0u8; 1024]; + + let mut contents = String::new(); + while { + self.delay.delay_millis(10); + self.uart.read_ready() + } { + let length = self.uart.read(&mut buffer).unwrap(); + contents += str::from_utf8(&buffer[..length]).unwrap(); + } + + contents + } } diff --git a/src/main.rs b/src/main.rs index 09b98de..acaed31 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,7 @@ use core::fmt::Write; -use alloc::str; +use alloc::{borrow::ToOwned, str}; use embedded_hal::delay::DelayNs; use esp_alloc::export::enumset::EnumSet; use esp_hal::{ @@ -136,7 +136,7 @@ fn main() -> ! { }; at_commands.init(); - at_commands.test(); + log::info!("{}", at_commands.raw_command("ATI".to_owned())); loop { let delay_start = Instant::now();