From 46ff7c865e51be002dc79b4f4161a5780ef06ca5 Mon Sep 17 00:00:00 2001 From: Robert Schauklies Date: Sat, 10 Jan 2026 20:18:57 +0100 Subject: [PATCH] adds command wrapper --- spinnyboy_rust/src/bin/peripherals/mod.rs | 15 ++++- spinnyboy_rust/src/bin/peripherals/nextion.rs | 61 +++++++++++++------ 2 files changed, 58 insertions(+), 18 deletions(-) diff --git a/spinnyboy_rust/src/bin/peripherals/mod.rs b/spinnyboy_rust/src/bin/peripherals/mod.rs index a8f0deb..3de3e42 100644 --- a/spinnyboy_rust/src/bin/peripherals/mod.rs +++ b/spinnyboy_rust/src/bin/peripherals/mod.rs @@ -1 +1,14 @@ -pub mod nextion; \ No newline at end of file +pub mod nextion; +#[derive(Debug)] +pub enum Command{ + SetRpm(u32), + SetTimer(u32), + Start, + Stop, + SendConfig, +} +#[derive(Debug)] +pub enum ErrCommand{ + NoValidCmd, + READ_ERROR +} \ 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 b6ae4ad..2998284 100644 --- a/spinnyboy_rust/src/bin/peripherals/nextion.rs +++ b/spinnyboy_rust/src/bin/peripherals/nextion.rs @@ -1,9 +1,11 @@ use esp_hal::uart::RxError; #[no_std] -use esp_hal::uart::{self, Config, Uart}; -use esp_hal::{Blocking, riscv::register::hpmcounter13h::read}; -use rtt_target::{rprint, rprintln}; -enum UART_STATEMACHINE { +use esp_hal::uart::{Uart}; +use esp_hal::{Blocking }; + +use crate::peripherals::{ErrCommand,Command}; + +enum UartStatemachine { WaitingP, WaitingE, Reading, @@ -11,14 +13,14 @@ enum UART_STATEMACHINE { } pub struct Nextion<'a> { interface: &'a mut Uart<'static, Blocking>, - state: UART_STATEMACHINE, + state: UartStatemachine, idx: usize, } impl<'a> Nextion<'a> { pub fn new<'b>(uart: &'a mut Uart<'static, Blocking>) -> Self { Nextion { interface: uart, - state: UART_STATEMACHINE::WaitingP, + state: UartStatemachine::WaitingP, idx: 0, } } @@ -26,7 +28,7 @@ impl<'a> Nextion<'a> { let _ = &self.interface.write(cmd); let _ = &self.interface.write(b"\xff\xff\xff"); } - pub fn read_command(&mut self, buf: &mut [u8]) -> Result { + fn read_frame(&mut self, buf: &mut [u8]) -> Result { let size: usize = 0; let mut tmp_buf: [u8; 20] = [0; 20]; let mut cmd_end_ctr = 0; @@ -41,20 +43,20 @@ impl<'a> Nextion<'a> { self.interface.read(&mut char)?; let byte = char[0]; match self.state { - UART_STATEMACHINE::WaitingP => { + UartStatemachine::WaitingP => { if byte == b'P' { - self.state = UART_STATEMACHINE::WaitingE; + self.state = UartStatemachine::WaitingE; } } - UART_STATEMACHINE::WaitingE => { + UartStatemachine::WaitingE => { if byte == b'E' { - self.state = UART_STATEMACHINE::Reading; + self.state = UartStatemachine::Reading; self.idx = 0; } } - UART_STATEMACHINE::Reading => { + UartStatemachine::Reading => { if byte == b'\xff' { - self.state = UART_STATEMACHINE::Terminator(1); + self.state = UartStatemachine::Terminator(1); } else { if self.idx < buf.len() { buf[self.idx] = byte; @@ -65,17 +67,17 @@ impl<'a> Nextion<'a> { } } } - UART_STATEMACHINE::Terminator(n) => { + UartStatemachine::Terminator(n) => { if byte == b'\xff' { if n >= 2 { let idx = self.idx; self.reset(); return Ok(idx); } else { - self.state = UART_STATEMACHINE::Terminator(n + 1) + self.state = UartStatemachine::Terminator(n + 1) } } else { - self.state = UART_STATEMACHINE::Reading; + self.state = UartStatemachine::Reading; let needed: usize = (n + 1).into(); // number of bytes to reinsert (FFs + current byte) @@ -96,12 +98,37 @@ 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)); + 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!")); + Ok(Command::SetRpm(rpm)) + }, + 04 => { + let time = u32::from_be_bytes(buf[1..4].try_into().expect("failed to parse time!")); + Ok(Command::SetTimer(time)) + } + 05 => { + Ok(Command::SendConfig) + }, + _ => { + Err(ErrCommand::NoValidCmd) + } + } + + + + } pub fn read_ready(&mut self) -> bool { self.interface.read_ready() } fn reset(&mut self) { - self.state = UART_STATEMACHINE::WaitingP; + self.state = UartStatemachine::WaitingP; self.idx = 0; } }