commit e7f0f62f43e11647054429d30e4e2e41164c8bd3
parent 4b93d0a02b5db7e7f63d33945bc75423cb4e9afa
Author: Daniel Barlow <dan@telent.net>
Date: Fri, 21 Nov 2025 19:35:18 +0000
extract ecu dialog into chat function
Diffstat:
| M | src/ecu.rs | | | 48 | ++++++++++++++++++++++++++++++------------------ |
1 file changed, 30 insertions(+), 18 deletions(-)
diff --git a/src/ecu.rs b/src/ecu.rs
@@ -1,6 +1,7 @@
use esp_hal::{
gpio::{Level, Output, OutputConfig},
uart::{Uart,DataBits},
+ Async
};
use embassy_time::Timer;
@@ -26,6 +27,30 @@ impl Ecu<'_> {
rx_peripheral
}
}
+
+ async fn chat(uart: &mut Uart<'_, Async>,
+ send: &[u8],
+ receive: &mut [u8]) -> usize {
+ uart.write(send).expect("write failed");
+ let _ = uart.flush();
+ let mut received = 0;
+ let mut buf = [0u8; 80];
+ let discard_count = send.len();
+
+ while received < send.len() {
+ let bytes = uart.read_async(&mut buf[..discard_count]).await.expect("read failed");
+ received += bytes;
+ // info!("discard {bytes} {received} of {discard_count}");
+ }
+ received = 0;
+ while received < receive.len() {
+ let bytes = uart.read_async(&mut receive[received..]).await.expect("read failed");
+ received += bytes;
+ // info!("read {bytes} {received} of {}", receive.len());
+ }
+ received
+ }
+
pub async fn run_command(&mut self, _command: Command) {
let mut output = Output::new(
self.tx_peripheral.reborrow(),
@@ -48,25 +73,12 @@ impl Ecu<'_> {
.with_tx(self.tx_peripheral.reborrow())
.into_async();
- uart.write(b"\xfe\x04\xff\xff").expect("write failed");
- let _ = uart.flush();
- Timer::after_millis(200).await;
- uart.write(b"\x72\x05\x00\xF0\x99").expect("write 2 failed");
- let _ = uart.flush();
-
let mut buf = [0u8; 80];
-
- loop {
- let bytes = uart.read_async(&mut buf[..]).await.expect("read failed");
- // if bytes>0 { led.toggle(); }
-
- info!("got {}", bytes);
- if bytes > 0 {
- for b in 0..bytes {
- info!("{}", buf[b]);
- }
-
- }
+ let _ = Self::chat(&mut uart, b"\xfe\x04\xff\xff", &mut buf[0..0]).await;
+ Timer::after_millis(200).await;
+ let _ = Self::chat(&mut uart, b"\x72\x05\x00\xF0\x99", &mut buf[0..4]).await;
+ for b in &buf[0..4] {
+ info!("byte {}", b);
}
}
}