eculocate

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 2b303059363b9aa20aa7afc8500ef6ad856d4dfe
parent 93dc2055cf4791bb3b9f561a0e369faf947d1dcb
Author: Daniel Barlow <dan@telent.net>
Date:   Sun, 16 Nov 2025 21:31:54 +0000

extract ecu code to own file

Diffstat:
Asrc/ecu.rs | 46++++++++++++++++++++++++++++++++++++++++++++++
Msrc/main.rs | 51+++++----------------------------------------------
2 files changed, 51 insertions(+), 46 deletions(-)

diff --git a/src/ecu.rs b/src/ecu.rs @@ -0,0 +1,46 @@ +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"); + 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 @@ -10,16 +10,15 @@ use esp_backtrace as _; use esp_println as _; pub mod wibble; +pub mod ecu; use esp_hal::{ timer::timg::TimerGroup, gpio::{Level, Output, OutputConfig}, clock::CpuClock, - uart::{Uart,DataBits}, }; use embassy_executor::Spawner; -use embassy_time::Timer; use embassy_futures::join::join; use log::{info}; @@ -61,48 +60,8 @@ async fn main(_s : Spawner) { let _ = join( wibble::run_ble(peripherals.BT), - run_ecu(peripherals.UART0, - peripherals.GPIO21.reborrow(), - peripherals.GPIO20) - ).await; -} - -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"); - 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; - } + ecu::run_ecu(peripherals.UART0, + peripherals.GPIO21.reborrow(), + peripherals.GPIO20) + ).await; }