commit bdf3013b005931a0de80218572dd1cb3e985daad
parent 35630bcbb2842ad7794e733f398dc148fe8391ea
Author: Daniel Barlow <dan@telent.net>
Date: Wed, 19 Nov 2025 21:01:57 +0000
turn Ecu into a struct we can call methods on
Diffstat:
| M | src/ecu.rs | | | 81 | ++++++++++++++++++++++++++++++++++++++++++++++++------------------------------- |
| M | src/main.rs | | | 22 | ++++++++++------------ |
2 files changed, 59 insertions(+), 44 deletions(-)
diff --git a/src/ecu.rs b/src/ecu.rs
@@ -2,45 +2,62 @@ use esp_hal::{
gpio::{Level, Output, OutputConfig},
uart::{Uart,DataBits},
};
+
use embassy_time::Timer;
use log::{info};
-pub async fn run_ecu(periph_uart : esp_hal::peripherals::UART0<'_>,
- mut tx_peripheral : esp_hal::peripherals::GPIO21<'_>,
- rx_peripheral : esp_hal::peripherals::GPIO20<'_>) {
- let tx = tx_peripheral.reborrow();
-
- let mut output = Output::new(tx, Level::High, OutputConfig::default());
-
- output.set_low();
- Timer::after_millis(70).await;
- output.set_high();
- Timer::after_millis(120).await;
-
- let mut uart = Uart::new(
- periph_uart,
- esp_hal::uart::Config::default()
- .with_baudrate(10_400)
- .with_data_bits(DataBits::_8)
- )
- .expect("now I got a reason")
- .with_rx(rx_peripheral)
- .with_tx(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();
-
- loop {
- info!("waiting for input");
+pub enum Command { Nop }
+
+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>,
+}
+
+impl Ecu<'_> {
+ pub fn new<'a>(
+ tx_peripheral : esp_hal::peripherals::GPIO21<'a>,
+ rx_peripheral : esp_hal::peripherals::GPIO20<'a>,
+ uart_peripheral: esp_hal::peripherals::UART0<'a>
+ ) -> Ecu<'a> {
+ Ecu {
+ uart_peripheral,
+ tx_peripheral,
+ rx_peripheral
+ }
+ }
+ 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 uart = Uart::new(
+ self.uart_peripheral.reborrow(),
+ esp_hal::uart::Config::default()
+ .with_baudrate(10_400)
+ .with_data_bits(DataBits::_8)
+ )
+ .expect("now I got a reason")
+ .with_rx(self.rx_peripheral.reborrow())
+ .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];
let bytes = uart.read_async(&mut buf[..]).await.expect("read failed");
// if bytes>0 { led.toggle(); }
info!("got {}", bytes);
- Timer::after_secs(1).await;
}
}
diff --git a/src/main.rs b/src/main.rs
@@ -14,7 +14,6 @@ pub mod ecu;
use esp_hal::{
timer::timg::TimerGroup,
- gpio::{Level, Output, OutputConfig},
clock::CpuClock,
};
@@ -37,11 +36,6 @@ async fn main(_s : Spawner) {
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
let mut peripherals = esp_hal::init(config);
- // XXX what is this for?
- // apparently it's copyable whatever it is as no complaints
- // when I call >1 function with it as param
- let output_config = OutputConfig::default();
-
esp_alloc::heap_allocator!(#[esp_hal::ram(reclaimed)] size: 66320);
// handwaving: we need the timer to schedule tasks for the rtos,
@@ -52,16 +46,20 @@ async fn main(_s : Spawner) {
esp_hal::interrupt::software::SoftwareInterruptControl::new(peripherals.SW_INTERRUPT);
esp_rtos::start(timg0.timer0, sw_interrupt.software_interrupt0);
- let mut led = Output::new(peripherals.GPIO8, Level::High, output_config);
- led.set_high();
info!("let's go!");
- led.set_low();
+
+ let mut ecu = ecu::Ecu::new(
+ peripherals.GPIO21,
+ peripherals.GPIO20,
+ peripherals.UART0
+ );
let _ = join(
wibble::run_ble(peripherals.BT),
- ecu::run_ecu(peripherals.UART0,
- peripherals.GPIO21.reborrow(),
- peripherals.GPIO20)
+ async {
+ ecu.run_command(ecu::Command::Nop).await;
+ loop { }
+ }
).await;
}