additional bugfixing

This commit is contained in:
Robert Schauklies 2026-01-10 22:35:06 +01:00
parent 46ff7c865e
commit 36ab15255f
3 changed files with 64 additions and 16 deletions

View file

@ -6,6 +6,7 @@
holding buffers for the duration of a data transfer." holding buffers for the duration of a data transfer."
)] )]
use core::fmt::Write; use core::fmt::Write;
use alloc::string::ToString;
use esp_hal::uart::{self, Config, Uart}; use esp_hal::uart::{self, Config, Uart};
use esp_hal::clock::CpuClock; use esp_hal::clock::CpuClock;
@ -21,25 +22,28 @@ use esp_hal::time::Rate;
use esp_hal::handler; use esp_hal::handler;
use core::cell::RefCell; use core::cell::RefCell;
use critical_section::Mutex;
use esp_backtrace as _;
use esp_hal::time::{Duration, Instant}; use esp_hal::time::{Duration, Instant};
use esp_hal::timer::timg::TimerGroup; use esp_hal::timer::timg::TimerGroup;
use esp_backtrace as _;
use critical_section::Mutex;
use rtt_target::rprintln; use rtt_target::rprintln;
mod dc_driver; mod dc_driver;
mod peripherals; mod peripherals;
use dc_driver::afroesc::AfroEsc; use dc_driver::afroesc::AfroEsc;
use peripherals::nextion::Nextion; use peripherals::nextion::Nextion;
use crate::peripherals::ErrCommand;
#[panic_handler] #[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! { fn panic(_: &core::panic::PanicInfo) -> ! {
rprintln!("PANIC!"); rprintln!("PANIC!");
loop {} loop {}
} }
use alloc::format;
// //
static EMERGENCY_BUTTON: Mutex<RefCell<Option<Input>>> = Mutex::new(RefCell::new(None)); static EMERGENCY_BUTTON: Mutex<RefCell<Option<Input>>> = Mutex::new(RefCell::new(None));
extern crate alloc; extern crate alloc;
//target RPM //target RPM
const DEFAULT_TARGET_RPM: u32 = 1000; const DEFAULT_TARGET_RPM: u32 = 2000;
//in seconds //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. // This creates a default app-descriptor required by the esp-idf bootloader.
@ -80,6 +84,7 @@ fn main() -> ! {
// delay.delay_millis(3000); // delay.delay_millis(3000);
//gpio 9 is the button //gpio 9 is the button
let input_config = InputConfig::default().with_pull(esp_hal::gpio::Pull::Up); let input_config = InputConfig::default().with_pull(esp_hal::gpio::Pull::Up);
use crate::peripherals::Command;
//let emergency_button = peripherals.GPIO9; //let emergency_button = peripherals.GPIO9;
let mut emergency_button = Input::new(peripherals.GPIO9, input_config); let mut emergency_button = Input::new(peripherals.GPIO9, input_config);
critical_section::with(|cs| { critical_section::with(|cs| {
@ -102,14 +107,43 @@ fn main() -> ! {
.with_rx(peripherals.GPIO5) .with_rx(peripherals.GPIO5)
.with_tx(peripherals.GPIO7); .with_tx(peripherals.GPIO7);
let mut display = Nextion::new(&mut uart0); let mut display = Nextion::new(&mut uart0);
display.send_command(b"page page0"); display.send_command(b"page page0");
let mut rpm = DEFAULT_TARGET_RPM;
let mut timer = DEFAULT_SPIN_TIME;
loop { loop {
let mut buf:[u8;20] = [0;20];
if display.read_ready() { if display.read_ready() {
display.read_command(&mut buf); match display.read_command() {
display.send_command(b"page page0"); Ok(Command::CommandSuccess) => {
rprintln!("RECV BUF:{:?}",buf); rprintln!("COMMAND SUCCESSFULLY executed");
rprintln!("WTF"); }
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"); //display.send_command(b"page page0");
//uart0.write(b"page page0\xff\xff\xff"); //uart0.write(b"page page0\xff\xff\xff");

View file

@ -6,9 +6,10 @@ pub enum Command{
Start, Start,
Stop, Stop,
SendConfig, SendConfig,
CommandSuccess
} }
#[derive(Debug)] #[derive(Debug)]
pub enum ErrCommand{ pub enum ErrCommand{
NoValidCmd, NoValidCmd,
READ_ERROR ReadError
} }

View file

@ -2,6 +2,7 @@ use esp_hal::uart::RxError;
#[no_std] #[no_std]
use esp_hal::uart::{Uart}; use esp_hal::uart::{Uart};
use esp_hal::{Blocking }; use esp_hal::{Blocking };
use rtt_target::rprintln;
use crate::peripherals::{ErrCommand,Command}; use crate::peripherals::{ErrCommand,Command};
@ -70,6 +71,7 @@ impl<'a> Nextion<'a> {
UartStatemachine::Terminator(n) => { UartStatemachine::Terminator(n) => {
if byte == b'\xff' { if byte == b'\xff' {
if n >= 2 { if n >= 2 {
rprintln!("SENDING {:?}",buf);
let idx = self.idx; let idx = self.idx;
self.reset(); self.reset();
return Ok(idx); return Ok(idx);
@ -100,20 +102,31 @@ impl<'a> Nextion<'a> {
} }
pub fn read_command(&mut self) -> Result<Command,ErrCommand>{ pub fn read_command(&mut self) -> Result<Command,ErrCommand>{
let mut buf:[u8;8] = [0;8]; 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]{ match buf[0]{
01 => {Ok(Command::Start)}, 01 => {Ok(Command::Start)},
02 =>{Ok(Command::Stop)}, 02 =>{Ok(Command::Stop)},
03 => { 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)) Ok(Command::SetRpm(rpm))
}, },
04 => { 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)) Ok(Command::SetTimer(time))
} }
05 =>{ 05 =>{
Ok(Command::SendConfig) Ok(Command::SendConfig)
}
00 => {
Ok(Command::CommandSuccess)
}, },
_ => { _ => {
Err(ErrCommand::NoValidCmd) Err(ErrCommand::NoValidCmd)