Add flush_rx

This commit is contained in:
Sofia 2026-05-15 01:45:15 +03:00
parent 851955e3fc
commit 1ca3a3acc4
2 changed files with 25 additions and 10 deletions

View File

@ -1,3 +1,4 @@
use alloc::format;
use alloc::string::String; use alloc::string::String;
use alloc::vec::Vec; use alloc::vec::Vec;
use core::fmt::Write; use core::fmt::Write;
@ -32,20 +33,19 @@ impl<'a, 'd> ATCommands<'a, 'd> {
log::info!("ATCommands Ready!"); log::info!("ATCommands Ready!");
self.delay.delay_millis(1_000); self.delay.delay_millis(1_000);
let mut buffer = [0u8; 1024]; log::info!("text: {}", self.flush_rx());
let length = self.uart.read(&mut buffer).unwrap();
log::info!("text: {:?}", str::from_utf8(&buffer[..length]));
} }
pub fn test(&mut self) { pub fn raw_command(&mut self, command: String) -> String {
self.flush_rx();
self.uart.flush().unwrap(); self.uart.flush().unwrap();
self.delay.delay_millis(250); 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.uart.flush().unwrap();
self.delay.delay_millis(500); self.delay.delay_millis(500);
log::info!("Wrote command"); log::info!("Wrote command {}", command);
let response = self.read_response(); self.read_response()
log::info!("Response: {}", response);
} }
fn read_response(&mut self) -> String { fn read_response(&mut self) -> String {
@ -73,4 +73,19 @@ impl<'a, 'd> ATCommands<'a, 'd> {
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join("\n") .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
}
} }

View File

@ -9,7 +9,7 @@
use core::fmt::Write; use core::fmt::Write;
use alloc::str; use alloc::{borrow::ToOwned, str};
use embedded_hal::delay::DelayNs; use embedded_hal::delay::DelayNs;
use esp_alloc::export::enumset::EnumSet; use esp_alloc::export::enumset::EnumSet;
use esp_hal::{ use esp_hal::{
@ -136,7 +136,7 @@ fn main() -> ! {
}; };
at_commands.init(); at_commands.init();
at_commands.test(); log::info!("{}", at_commands.raw_command("ATI".to_owned()));
loop { loop {
let delay_start = Instant::now(); let delay_start = Instant::now();