chore: clean up rumble logic

This commit is contained in:
Naxdy 2024-04-03 16:16:55 +02:00
parent b62f8f2d5c
commit d0960f2c2d
Signed by: Naxdy
GPG key ID: CC15075846BCE91B
2 changed files with 34 additions and 19 deletions

View file

@ -7,13 +7,13 @@ use core::default::Default;
use defmt::{debug, info, trace, warn, Format}; use defmt::{debug, info, trace, warn, Format};
use embassy_futures::join::join; use embassy_futures::join::join;
use embassy_rp::{ use embassy_rp::{
peripherals::{PWM_CH4, PWM_CH6, USB}, peripherals::{PIN_25, PIN_29, PWM_CH4, PWM_CH6, USB},
pwm::Pwm, pwm::Pwm,
usb::Driver, usb::Driver,
}; };
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, signal::Signal}; use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, signal::Signal};
use embassy_time::{Duration, Instant, Ticker}; use embassy_time::{Duration, Instant, Ticker, Timer};
use embassy_usb::{ use embassy_usb::{
class::hid::{HidReaderWriter, ReportId, RequestHandler, State}, class::hid::{HidReaderWriter, ReportId, RequestHandler, State},
control::OutResponse, control::OutResponse,
@ -430,7 +430,23 @@ fn calc_rumble_power(strength: u8) -> u16 {
} }
#[embassy_executor::task] #[embassy_executor::task]
pub async fn rumble_task(pwm_rumble: Pwm<'static, PWM_CH4>, pwm_brake: Pwm<'static, PWM_CH6>) { pub async fn rumble_task(
pin_rumble: PIN_25,
pin_brake: PIN_29,
pwm_ch_rumble: PWM_CH4,
pwm_ch_brake: PWM_CH6,
) {
let mut rumble_config: embassy_rp::pwm::Config = Default::default();
rumble_config.top = 0;
rumble_config.enable = true;
rumble_config.compare_b = 255;
let mut brake_config = rumble_config.clone();
brake_config.compare_b = 255;
let mut pwm_rumble = Pwm::new_output_b(pwm_ch_rumble, pin_rumble, rumble_config.clone());
let mut pwm_brake = Pwm::new_output_b(pwm_ch_brake, pin_brake, brake_config.clone());
let mut rumble_power = { let mut rumble_power = {
let strength = SIGNAL_CHANGE_RUMBLE_STRENGTH.wait().await; let strength = SIGNAL_CHANGE_RUMBLE_STRENGTH.wait().await;
calc_rumble_power(strength) calc_rumble_power(strength)
@ -439,16 +455,24 @@ pub async fn rumble_task(pwm_rumble: Pwm<'static, PWM_CH4>, pwm_brake: Pwm<'stat
loop { loop {
let new_rumble_status = SIGNAL_RUMBLE.wait().await; let new_rumble_status = SIGNAL_RUMBLE.wait().await;
debug!("Received rumble signal: {}", new_rumble_status);
if let Some(new_strength) = SIGNAL_CHANGE_RUMBLE_STRENGTH.try_take() { if let Some(new_strength) = SIGNAL_CHANGE_RUMBLE_STRENGTH.try_take() {
rumble_power = calc_rumble_power(new_strength); rumble_power = calc_rumble_power(new_strength);
} }
if new_rumble_status { if new_rumble_status {
pwm_rumble.set_counter(rumble_power); rumble_config.compare_b = rumble_power;
pwm_brake.set_counter(0); brake_config.compare_b = 0;
pwm_rumble.set_config(&rumble_config);
pwm_brake.set_config(&brake_config);
} else { } else {
pwm_rumble.set_counter(0); rumble_config.compare_b = 0;
pwm_brake.set_counter(255); brake_config.compare_b = 255;
pwm_rumble.set_config(&rumble_config);
pwm_brake.set_config(&brake_config);
} }
} }
} }

View file

@ -83,24 +83,15 @@ fn main() -> ! {
let spi_acs = Output::new(AnyPin::from(p_acs), Level::High); // active low let spi_acs = Output::new(AnyPin::from(p_acs), Level::High); // active low
let spi_ccs = Output::new(AnyPin::from(p_ccs), Level::High); // active low let spi_ccs = Output::new(AnyPin::from(p_ccs), Level::High); // active low
let mut rumble_config: embassy_rp::pwm::Config = Default::default();
rumble_config.top = 255;
rumble_config.enable = true;
rumble_config.compare_b = 0;
let mut brake_config = rumble_config.clone();
brake_config.compare_b = 255;
let pwm_rumble = Pwm::new_output_b(p.PWM_CH4, p.PIN_25, rumble_config.clone());
let pwm_brake = Pwm::new_output_b(p.PWM_CH6, p.PIN_29, brake_config.clone());
spawn_core1(p.CORE1, unsafe { &mut CORE1_STACK }, move || { spawn_core1(p.CORE1, unsafe { &mut CORE1_STACK }, move || {
let executor1 = EXECUTOR1.init(Executor::new()); let executor1 = EXECUTOR1.init(Executor::new());
debug!("Mana"); debug!("Mana");
executor1.run(|spawner| { executor1.run(|spawner| {
spawner.spawn(usb_transfer_task(uid, driver)).unwrap(); spawner.spawn(usb_transfer_task(uid, driver)).unwrap();
spawner.spawn(enter_config_mode_task()).unwrap(); spawner.spawn(enter_config_mode_task()).unwrap();
spawner.spawn(rumble_task(pwm_rumble, pwm_brake)).unwrap(); spawner
.spawn(rumble_task(p.PIN_25, p.PIN_29, p.PWM_CH4, p.PWM_CH6))
.unwrap();
// spawner.spawn(input_integrity_benchmark()).unwrap(); // spawner.spawn(input_integrity_benchmark()).unwrap();
spawner spawner
.spawn(update_button_state_task( .spawn(update_button_state_task(