commit d1453db01ebfd98f71888155141b23bf209493db
parent 37f7780e8f514762e04d8d42a2d017757123fb9a
Author: Daniel Barlow <dan@telent.net>
Date: Sat, 22 Nov 2025 17:03:56 +0000
skip initializing the ecu if we did it already
Diffstat:
| M | src/ecu.rs | | | 71 | +++++++++++++++++++++++++++++++++++++++++++++-------------------------- |
1 file changed, 45 insertions(+), 26 deletions(-)
diff --git a/src/ecu.rs b/src/ecu.rs
@@ -4,7 +4,12 @@ use esp_hal::{
Async
};
-use embassy_time::Timer;
+use embassy_time::{
+ Timer,
+ Instant,
+ Duration
+};
+
use log::{info};
// implement iterator
@@ -16,6 +21,8 @@ pub struct Ecu<'a> {
tx_peripheral : esp_hal::peripherals::GPIO21<'a>,
rx_peripheral : esp_hal::peripherals::GPIO20<'a>,
uart_peripheral: esp_hal::peripherals::UART0<'a>,
+ initialized_at: Option<Instant>
+
}
impl Ecu<'_> {
@@ -27,7 +34,15 @@ impl Ecu<'_> {
Ecu {
uart_peripheral,
tx_peripheral,
- rx_peripheral
+ rx_peripheral,
+ initialized_at: None
+ }
+ }
+
+ fn is_initialized(&self) -> bool {
+ match self.initialized_at {
+ None => false,
+ Some(i) => i.elapsed() < Duration::from_millis(2500)
}
}
@@ -76,16 +91,20 @@ impl Ecu<'_> {
}
pub async fn run_command(&mut self, _command: Command) {
- let mut output = Output::new(
- self.tx_peripheral.reborrow(),
- Level::High, OutputConfig::default()
- );
-
- output.set_low();
- Timer::after_millis(70).await;
- output.set_high();
- Timer::after_millis(120).await;
-
+ let mut buf = [0u8; 80];
+ let need_init = !self.is_initialized();
+
+ if need_init {
+ let mut output = Output::new(
+ self.tx_peripheral.reborrow(),
+ Level::High, OutputConfig::default()
+ );
+ info!("pull");
+ output.set_low();
+ Timer::after_millis(70).await;
+ output.set_high();
+ Timer::after_millis(120).await;
+ }
let mut uart = Uart::new(
self.uart_peripheral.reborrow(),
esp_hal::uart::Config::default()
@@ -97,20 +116,20 @@ impl Ecu<'_> {
.with_tx(self.tx_peripheral.reborrow())
.into_async();
- let mut buf = [0u8; 80];
-
- let _ = Self::chat(&mut uart,
- &mut [0xfe, 0x04, 0xff, 0xff],
- &mut buf[0..0]).await;
- Timer::after_millis(200).await;
-
- // checksum is also correct on this packet
- let handshake = Self::chat(&mut uart,
- &mut [0x72, 0x05, 0x00, 0xF0, 0x99],
- &mut buf).await;
- info!("matches {}", handshake == b"\x02\x04\x00\xfa");
-
- for i in (0..0xff) {
+ if need_init {
+ info!("atz");
+ let _ = Self::chat(&mut uart,
+ &mut [0xfe, 0x04, 0xff, 0xff],
+ &mut buf[0..0]).await;
+ Timer::after_millis(200).await;
+
+ let handshake = Self::chat(&mut uart,
+ &mut [0x72, 0x05, 0x00, 0xF0, 0x99],
+ &mut buf).await;
+ info!("matches {}", handshake == b"\x02\x04\x00\xfa");
+ self.initialized_at = Some(Instant::now());
+ }
+ for i in [0] {
let table = Self::chat(&mut uart,
&mut [0x72, 0x07, 0x72, i, 0, 0x14, 0x0],
&mut buf).await;