From 36ab15255f2dbbb742967f2e77c1f1a027ba6d4b Mon Sep 17 00:00:00 2001 From: Robert Schauklies Date: Sat, 10 Jan 2026 22:35:06 +0100 Subject: [PATCH] additional bugfixing --- spinnyboy_rust/src/bin/main.rs | 56 +++++++++++++++---- spinnyboy_rust/src/bin/peripherals/mod.rs | 3 +- spinnyboy_rust/src/bin/peripherals/nextion.rs | 21 +++++-- 3 files changed, 64 insertions(+), 16 deletions(-) diff --git a/spinnyboy_rust/src/bin/main.rs b/spinnyboy_rust/src/bin/main.rs index 9ec1140..170dbc8 100644 --- a/spinnyboy_rust/src/bin/main.rs +++ b/spinnyboy_rust/src/bin/main.rs @@ -6,6 +6,7 @@ holding buffers for the duration of a data transfer." )] use core::fmt::Write; +use alloc::string::ToString; use esp_hal::uart::{self, Config, Uart}; use esp_hal::clock::CpuClock; @@ -21,27 +22,30 @@ use esp_hal::time::Rate; use esp_hal::handler; use core::cell::RefCell; +use critical_section::Mutex; +use esp_backtrace as _; use esp_hal::time::{Duration, Instant}; use esp_hal::timer::timg::TimerGroup; -use esp_backtrace as _; -use critical_section::Mutex; use rtt_target::rprintln; mod dc_driver; mod peripherals; use dc_driver::afroesc::AfroEsc; use peripherals::nextion::Nextion; + +use crate::peripherals::ErrCommand; #[panic_handler] fn panic(_: &core::panic::PanicInfo) -> ! { rprintln!("PANIC!"); loop {} } +use alloc::format; // static EMERGENCY_BUTTON: Mutex>> = Mutex::new(RefCell::new(None)); extern crate alloc; //target RPM -const DEFAULT_TARGET_RPM: u32 = 1000; +const DEFAULT_TARGET_RPM: u32 = 2000; //in seconds -const DEFAULT_SPIN_TIME:u32 = 10; +const DEFAULT_SPIN_TIME: u32 = 10; // This creates a default app-descriptor required by the esp-idf bootloader. // For more information see: esp_bootloader_esp_idf::esp_app_desc!(); @@ -80,6 +84,7 @@ fn main() -> ! { // delay.delay_millis(3000); //gpio 9 is the button let input_config = InputConfig::default().with_pull(esp_hal::gpio::Pull::Up); + use crate::peripherals::Command; //let emergency_button = peripherals.GPIO9; let mut emergency_button = Input::new(peripherals.GPIO9, input_config); critical_section::with(|cs| { @@ -102,15 +107,44 @@ fn main() -> ! { .with_rx(peripherals.GPIO5) .with_tx(peripherals.GPIO7); let mut display = Nextion::new(&mut uart0); + display.send_command(b"page page0"); + let mut rpm = DEFAULT_TARGET_RPM; + let mut timer = DEFAULT_SPIN_TIME; loop { - let mut buf:[u8;20] = [0;20]; - if display.read_ready(){ - display.read_command(&mut buf); - display.send_command(b"page page0"); - rprintln!("RECV BUF:{:?}",buf); - rprintln!("WTF"); - } + if display.read_ready() { + match display.read_command() { + Ok(Command::CommandSuccess) => { + rprintln!("COMMAND SUCCESSFULLY executed"); + } + Ok(Command::Start) => { + rprintln!("START") + }, + Ok(Command::Stop) => { + rprintln!("STOP") + }, + Ok(Command::SetRpm(x)) => { + rprintln!("SET_RPM with {}", x); + rpm = x; + }, + Ok(Command::SetTimer(x)) => { + rprintln!("SETTING TIMER {}", x); + timer = x; + }, + Ok(Command::SendConfig) => { + rprintln!("SEND CONFIG"); + let command = format!("rpm.val={}",DEFAULT_TARGET_RPM); + display.send_command(command.to_string().as_bytes()); + }, + Err(ErrCommand::NoValidCmd) => { + rprintln!(" NOT A VALID CMD!"); + }, + Err(ErrCommand::ReadError) =>{ + rprintln!("READ FAILED!"); + } + } + // display.send_command(b"page page0"); + } //display.send_command(b"page page0"); //uart0.write(b"page page0\xff\xff\xff"); } diff --git a/spinnyboy_rust/src/bin/peripherals/mod.rs b/spinnyboy_rust/src/bin/peripherals/mod.rs index 3de3e42..fa7fc3a 100644 --- a/spinnyboy_rust/src/bin/peripherals/mod.rs +++ b/spinnyboy_rust/src/bin/peripherals/mod.rs @@ -6,9 +6,10 @@ pub enum Command{ Start, Stop, SendConfig, + CommandSuccess } #[derive(Debug)] pub enum ErrCommand{ NoValidCmd, - READ_ERROR + ReadError } \ No newline at end of file diff --git a/spinnyboy_rust/src/bin/peripherals/nextion.rs b/spinnyboy_rust/src/bin/peripherals/nextion.rs index 2998284..74a74e4 100644 --- a/spinnyboy_rust/src/bin/peripherals/nextion.rs +++ b/spinnyboy_rust/src/bin/peripherals/nextion.rs @@ -2,6 +2,7 @@ use esp_hal::uart::RxError; #[no_std] use esp_hal::uart::{Uart}; use esp_hal::{Blocking }; +use rtt_target::rprintln; use crate::peripherals::{ErrCommand,Command}; @@ -70,6 +71,7 @@ impl<'a> Nextion<'a> { UartStatemachine::Terminator(n) => { if byte == b'\xff' { if n >= 2 { + rprintln!("SENDING {:?}",buf); let idx = self.idx; self.reset(); return Ok(idx); @@ -100,20 +102,31 @@ impl<'a> Nextion<'a> { } pub fn read_command(&mut self) -> Result{ let mut buf:[u8;8] = [0;8]; - let read_bytes = self.read_frame(&mut buf).unwrap_or(return Err(ErrCommand::READ_ERROR)); + let _read_bytes = match self.read_frame(&mut buf){ + Ok(x) => x, + Err(e) => {rprintln!("ERROR while reading"); + return Err(ErrCommand::NoValidCmd); + } + }; + + rprintln!("READ SUCCESS!:{:?}",buf); match buf[0]{ + 01 => {Ok(Command::Start)}, 02 =>{Ok(Command::Stop)}, 03 => { - let rpm = u32::from_be_bytes(buf[1..4].try_into().expect("failed to parse rpm!")); + let rpm = u32::from_le_bytes(buf[1..5].try_into().expect("failed to parse rpm!")); Ok(Command::SetRpm(rpm)) }, 04 => { - let time = u32::from_be_bytes(buf[1..4].try_into().expect("failed to parse time!")); + let time = u32::from_le_bytes(buf[1..5].try_into().expect("failed to parse time!")); Ok(Command::SetTimer(time)) } - 05 => { + 05 =>{ Ok(Command::SendConfig) + } + 00 => { + Ok(Command::CommandSuccess) }, _ => { Err(ErrCommand::NoValidCmd)