Compare commits

..

2 commits

Author SHA1 Message Date
Robert Schauklies
502afc9a5f cleanup of the nextion code 2026-01-14 23:32:01 +01:00
Robert Schauklies
1a5c58c31c using the way of the arduino map function 2026-01-14 23:31:36 +01:00
3 changed files with 40 additions and 18 deletions

View file

@ -1,14 +1,9 @@
#![no_std]
use esp_hal::aes::Operation;
use esp_hal::mcpwm::PwmPeripheral;
use esp_hal::mcpwm::operator::PwmPin; use esp_hal::mcpwm::operator::PwmPin;
use esp_hal::mcpwm::operator::Operator;
use esp_hal::mcpwm::McPwm;
use esp_hal::peripherals::MCPWM0;
use esp_hal::delay::Delay; use esp_hal::delay::Delay;
use crate::dc_driver::EscState;
pub struct AfroEsc<'a>{ pub struct AfroEsc<'a>{
pub pwm_pin: &'a mut PwmPin<'a, esp_hal::peripherals::MCPWM0<'a>, 0,true> pub pwm_pin: &'a mut PwmPin<'a, esp_hal::peripherals::MCPWM0<'a>, 0,true>,
state: crate::dc_driver::EscState,
} }
const ARMING_SEQUENCE:u16 = 1055; const ARMING_SEQUENCE:u16 = 1055;
@ -19,13 +14,14 @@ const GAP:u16 = (MAX_THROTTLE - MIN_THROTTLE)/100;
impl AfroEsc<'_>{ impl AfroEsc<'_>{
//this is a little hacky tbh //this is a little hacky tbh
pub fn new<'a>(pwm_pin:&'a mut PwmPin<'a, esp_hal::peripherals::MCPWM0<'a>, 0,true>)-> AfroEsc<'a> { pub fn new<'a>(pwm_pin:&'a mut PwmPin<'a, esp_hal::peripherals::MCPWM0<'a>, 0,true>)-> AfroEsc<'a> {
let mut esc = AfroEsc{pwm_pin:pwm_pin}; let mut esc = AfroEsc{pwm_pin:pwm_pin,state:EscState::Starting};
let delay = Delay::new(); let delay = Delay::new();
esc.set_arming_sequence(); esc.send_arming_sequence();
delay.delay_millis(3000); delay.delay_millis(3000);
esc esc
} }
pub fn set_timestamp(&mut self,value:u16){ pub fn set_timestamp(&mut self,value:u16){
self.pwm_pin.set_timestamp(value); self.pwm_pin.set_timestamp(value);
} }
//range is from 1121 till 1421 //range is from 1121 till 1421
@ -37,8 +33,24 @@ impl AfroEsc<'_>{
let new_timestamp = MIN_THROTTLE+value*GAP; let new_timestamp = MIN_THROTTLE+value*GAP;
self.pwm_pin.set_timestamp(new_timestamp); self.pwm_pin.set_timestamp(new_timestamp);
} }
pub fn set_arming_sequence(&mut self){ pub fn send_arming_sequence(&mut self){
self.set_timestamp(1055); self.set_timestamp(1055);
} }
fn set_state(&mut self, state:EscState){
self.state = state;
}
pub fn get_state(self) -> EscState{
self.state
}
pub fn map_duty(&mut self, x:i32){
if x <=0 {
self.pwm_pin.set_timestamp(1055);
}
else{
let duty:u16 = crate::dc_driver::arduino_map(x, 0, 100, MIN_THROTTLE.into(), MAX_THROTTLE.into()).try_into().expect("INTEGER TOO LARGE");
self.pwm_pin.set_timestamp(duty);
}
}
} }

View file

@ -1 +1,17 @@
pub mod afroesc; pub mod afroesc;
pub enum EscState {
Starting,
Running,
Stopping,
Stop
}
//taken from: https://docs.arduino.cc/language-reference/en/functions/math/map/
//used for the ramp up of the spincoater
pub fn arduino_map(x:i32, in_min:i32, in_max:i32, out_min:i32, out_max:i32) -> i32 {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

View file

@ -29,11 +29,6 @@ impl<'a> Nextion<'a> {
let _ = &self.interface.write(b"\xff\xff\xff"); let _ = &self.interface.write(b"\xff\xff\xff");
} }
fn read_frame(&mut self, buf: &mut [u8]) -> Result<usize, RxError> { fn read_frame(&mut self, buf: &mut [u8]) -> Result<usize, RxError> {
let size: usize = 0;
let mut tmp_buf: [u8; 20] = [0; 20];
let mut cmd_end_ctr = 0;
let mut i = 0;
let mut read_header = false;
loop { loop {
if !self.interface.read_ready() { if !self.interface.read_ready() {
continue; continue;
@ -70,7 +65,6 @@ 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);