diff --git a/.forgejo/workflows/clippy-check.yml b/.forgejo/workflows/clippy-check.yml new file mode 100644 index 0000000..d94dadd --- /dev/null +++ b/.forgejo/workflows/clippy-check.yml @@ -0,0 +1,23 @@ +name: Code quality + +on: pull_request + +jobs: + check: + runs-on: nix-flakes + + steps: + - name: Set up packages + run: | + echo "extra-substituters = https://builder.naxdy.org/attic" >> /etc/nix/nix.conf + echo "extra-trusted-public-keys = attic:05LdE8Nav5Qd1E+KOJqSwdr+WE1z8AUmSb3oKL7s8dk=" >> /etc/nix/nix.conf + nix profile install nixpkgs#nodejs "github:zhaofengli/attic?ref=6eabc3f02fae3683bffab483e614bebfcd476b21" + echo "PATH=/nix/var/nix/profiles/per-user/root/profile/bin:$PATH" >> "$GITHUB_ENV" + - name: Set up attic binary cache + run: | + attic login "${{ vars.BINARY_CACHE_NAME }}" "${{ vars.BINARY_CACHE_URL }}" "${{ secrets.BINARY_CACHE_AUTH_KEY }}" + attic use "${{ vars.BINARY_CACHE_NAME }}" + - uses: actions/checkout@v4 + - name: Run Clippy + run: | + nix develop . --command cargo clippy -- -Dwarnings diff --git a/Cargo.toml b/Cargo.toml index dcd1b66..e1cc914 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -72,7 +72,9 @@ debug = 2 debug-assertions = false incremental = false lto = 'fat' -opt-level = 1 +# opt level needs to be benchmarked after every major feature +# due to the changes in binary size and alignment +opt-level = 2 overflow-checks = false # do not optimize proc-macro crates = faster builds from scratch diff --git a/src/config.rs b/src/config.rs index 473d3e1..b1514b6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -13,7 +13,9 @@ use embassy_rp::{ use packed_struct::{derive::PackedStruct, PackedStruct}; use crate::{ - gcc_hid::{SIGNAL_CHANGE_RUMBLE_STRENGTH, SIGNAL_INPUT_CONSISTENCY_MODE_STATUS}, + gcc_hid::{ + Buttons1, Buttons2, SIGNAL_CHANGE_RUMBLE_STRENGTH, SIGNAL_INPUT_CONSISTENCY_MODE_STATUS, + }, helpers::{PackedFloat, ToPackedFloatArray, ToRegularArray, XyValuePair}, input::{ read_ext_adc, Stick, StickAxis, FLOAT_ORIGIN, SPI_ACS_SHARED, SPI_CCS_SHARED, SPI_SHARED, @@ -104,12 +106,12 @@ const LSTICK_SNAPBACK_X_INCREASE_COMBO: [AwaitableButtons; 4] = [ AwaitableButtons::A, AwaitableButtons::X, AwaitableButtons::Up, - AwaitableButtons::Wildcard, + AwaitableButtons::NotZ, ]; const LSTICK_SNAPBACK_Y_INCREASE_COMBO: [AwaitableButtons; 4] = [ AwaitableButtons::A, - AwaitableButtons::Wildcard, + AwaitableButtons::NotZ, AwaitableButtons::Up, AwaitableButtons::Y, ]; @@ -118,14 +120,14 @@ const LSTICK_SNAPBACK_X_DECREASE_COMBO: [AwaitableButtons; 4] = [ AwaitableButtons::A, AwaitableButtons::X, AwaitableButtons::Down, - AwaitableButtons::Wildcard, + AwaitableButtons::NotZ, ]; const LSTICK_SNAPBACK_Y_DECREASE_COMBO: [AwaitableButtons; 4] = [ AwaitableButtons::A, AwaitableButtons::Y, AwaitableButtons::Down, - AwaitableButtons::Wildcard, + AwaitableButtons::NotZ, ]; const RSTICK_SNAPBACK_X_INCREASE_COMBO: [AwaitableButtons; 4] = [ @@ -159,28 +161,28 @@ const RSTICK_SNAPBACK_Y_DECREASE_COMBO: [AwaitableButtons; 4] = [ const LSTICK_WAVESHAPING_X_INCREASE_COMBO: [AwaitableButtons; 4] = [ AwaitableButtons::X, AwaitableButtons::Up, - AwaitableButtons::Wildcard, + AwaitableButtons::NotZ, AwaitableButtons::L, ]; const LSTICK_WAVESHAPING_Y_INCREASE_COMBO: [AwaitableButtons; 4] = [ AwaitableButtons::Y, AwaitableButtons::Up, - AwaitableButtons::Wildcard, + AwaitableButtons::NotZ, AwaitableButtons::L, ]; const LSTICK_WAVESHAPING_X_DECREASE_COMBO: [AwaitableButtons; 4] = [ AwaitableButtons::X, AwaitableButtons::Down, - AwaitableButtons::Wildcard, + AwaitableButtons::NotZ, AwaitableButtons::L, ]; const LSTICK_WAVESHAPING_Y_DECREASE_COMBO: [AwaitableButtons; 4] = [ AwaitableButtons::Y, AwaitableButtons::Down, - AwaitableButtons::Wildcard, + AwaitableButtons::NotZ, AwaitableButtons::L, ]; @@ -216,28 +218,28 @@ const LSTICK_SMOOTH_X_INCREASE_COMBO: [AwaitableButtons; 4] = [ AwaitableButtons::R, AwaitableButtons::X, AwaitableButtons::Up, - AwaitableButtons::Wildcard, + AwaitableButtons::NotZ, ]; const LSTICK_SMOOTH_Y_INCREASE_COMBO: [AwaitableButtons; 4] = [ AwaitableButtons::R, AwaitableButtons::Y, AwaitableButtons::Up, - AwaitableButtons::Wildcard, + AwaitableButtons::NotZ, ]; const LSTICK_SMOOTH_X_DECREASE_COMBO: [AwaitableButtons; 4] = [ AwaitableButtons::R, AwaitableButtons::X, AwaitableButtons::Down, - AwaitableButtons::Wildcard, + AwaitableButtons::NotZ, ]; const LSTICK_SMOOTH_Y_DECREASE_COMBO: [AwaitableButtons; 4] = [ AwaitableButtons::R, AwaitableButtons::Y, AwaitableButtons::Down, - AwaitableButtons::Wildcard, + AwaitableButtons::NotZ, ]; const RSTICK_SMOOTH_X_INCREASE_COMBO: [AwaitableButtons; 4] = [ @@ -272,14 +274,14 @@ const LSTICK_CARDINALSNAP_INCREASE_COMBO: [AwaitableButtons; 4] = [ AwaitableButtons::R, AwaitableButtons::A, AwaitableButtons::Up, - AwaitableButtons::Wildcard, + AwaitableButtons::NotZ, ]; const LSTICK_CARDINALSNAP_DECREASE_COMBO: [AwaitableButtons; 4] = [ AwaitableButtons::R, AwaitableButtons::A, AwaitableButtons::Down, - AwaitableButtons::Wildcard, + AwaitableButtons::NotZ, ]; const RSTICK_CARDINALSNAP_INCREASE_COMBO: [AwaitableButtons; 4] = [ @@ -300,14 +302,14 @@ const LSTICK_SCALING_INCREASE_COMBO: [AwaitableButtons; 4] = [ AwaitableButtons::L, AwaitableButtons::A, AwaitableButtons::Up, - AwaitableButtons::Wildcard, + AwaitableButtons::NotZ, ]; const LSTICK_SCALING_DECREASE_COMBO: [AwaitableButtons; 4] = [ AwaitableButtons::L, AwaitableButtons::A, AwaitableButtons::Down, - AwaitableButtons::Wildcard, + AwaitableButtons::NotZ, ]; const RSTICK_SCALING_INCREASE_COMBO: [AwaitableButtons; 4] = [ @@ -456,6 +458,8 @@ enum AwaitableButtons { L, R, Z, + // special, because Z is used for cstick calibration + NotZ, /// Used for padding arrays to the correct length. Wildcard, /// Used for disabling certain button combinations.\ @@ -552,17 +556,15 @@ impl Default for ControllerConfig { impl ControllerConfig { pub fn from_flash_memory( - mut flash: &mut Flash<'static, FLASH, Async, FLASH_SIZE>, + flash: &mut Flash<'static, FLASH, Async, FLASH_SIZE>, ) -> Result { let mut controller_config_packed: ::ByteArray = ControllerConfig::default().pack().unwrap(); let r = flash.blocking_read(ADDR_OFFSET, &mut controller_config_packed); - if let Err(_) = r { + if r.is_err() { warn!("Controller config not found in flash, using default."); controller_config_packed = [0u8; 659]; - } else { - r.unwrap(); } match ControllerConfig::unpack(&controller_config_packed) { @@ -582,7 +584,7 @@ impl ControllerConfig { let cfg = ControllerConfig::default(); info!("Going to save default controller config."); - cfg.write_to_flash(&mut flash)?; + cfg.write_to_flash(flash)?; Ok(cfg) } @@ -725,7 +727,7 @@ impl<'a, T: RawMutex, const I: usize, const J: usize, const K: usize> WaitForBut } } - return None; + None } } @@ -745,6 +747,7 @@ fn is_awaitable_button_pressed(report: &GcReport, button_to_wait_for: &Awaitable AwaitableButtons::Z => report.buttons_2.button_z, AwaitableButtons::Wildcard => true, AwaitableButtons::Impossible => false, + AwaitableButtons::NotZ => !report.buttons_2.button_z, } } @@ -779,7 +782,9 @@ impl<'a> StickCalibrationProcess<'a> { if self.applied_calibration.cleaned_calibration.notch_status[notch_idx] == NotchStatus::TertInactive - {} + { + return; + } // assumes a tick rate of 1ms match notch_adjustment_type { @@ -828,7 +833,7 @@ impl<'a> StickCalibrationProcess<'a> { stick_config.angles = *legalize_notches( self.calibration_step as usize, &self.applied_calibration.measured_notch_angles, - &stick_config.angles.to_regular_array(), + stick_config.angles.to_regular_array(), ) .to_packed_float_array(); @@ -883,7 +888,7 @@ impl<'a> StickCalibrationProcess<'a> { self.applied_calibration = AppliedCalibration::from_points( &self.cal_points.map(|e| e.x), &self.cal_points.map(|e| e.y), - &stick_config, + stick_config, ); stick_config.angles = *legalize_notches( @@ -907,7 +912,7 @@ impl<'a> StickCalibrationProcess<'a> { stick_config.angles = *legalize_notches( self.calibration_step as usize, &self.applied_calibration.measured_notch_angles, - &stick_config.angles.to_regular_array(), + stick_config.angles.to_regular_array(), ) .to_packed_float_array(); @@ -935,7 +940,7 @@ impl<'a> StickCalibrationProcess<'a> { SIGNAL_CONFIG_CHANGE.signal(self.gcc_config.clone()); } - return false; + false } pub async fn calibrate_stick(&mut self) { @@ -1042,9 +1047,8 @@ fn get_stick_display_coords(current_step: usize) -> (f32, f32) { let idx = CALIBRATION_ORDER[current_step]; if idx % 2 != 0 { let notch_idx = idx / 2; - match calc_stick_values(DEFAULT_ANGLES[notch_idx]) { - (x, y) => (x + FLOAT_ORIGIN, y + FLOAT_ORIGIN), - } + let (x, y) = calc_stick_values(DEFAULT_ANGLES[notch_idx]); + (x + FLOAT_ORIGIN, y + FLOAT_ORIGIN) } else { (127.5, 127.5) } @@ -1063,7 +1067,7 @@ async fn configuration_main_loop< const P: usize, >( current_config: &ControllerConfig, - mut flash: &mut Flash<'static, FLASH, Async, FLASH_SIZE>, + flash: &mut Flash<'static, FLASH, Async, FLASH_SIZE>, gcc_subscriber: &mut Subscriber<'a, M, GcReport, C, S, P>, ) -> ControllerConfig { let mut final_config = current_config.clone(); @@ -1112,511 +1116,547 @@ async fn configuration_main_loop< ]; 'main: loop { - match gcc_subscriber + let selection = gcc_subscriber .wait_and_filter_simultaneous_button_presses(&config_options) - .await - { - selection => match selection { - // exit - 0 => { - override_gcc_state_and_wait(&OverrideGcReportInstruction { - report: match GcReport::default() { - mut a => { - a.trigger_r = 255; - a.trigger_l = 255; - a.buttons_2.button_l = true; - a.buttons_2.button_r = true; - a.buttons_1.button_x = true; - a.buttons_1.button_y = true; - a.buttons_1.button_a = true; - a.stick_x = 127; - a.stick_y = 127; - a.cstick_x = 127; - a.cstick_y = 127; - a - } + .await; + match selection { + // exit + 0 => { + override_gcc_state_and_wait(&OverrideGcReportInstruction { + report: GcReport { + trigger_r: 255, + trigger_l: 255, + buttons_2: Buttons2 { + button_r: true, + button_l: true, + ..Default::default() }, - duration_ms: 1000, - }) - .await; - - break 'main; - } - // calibrate lstick - 1 => { - override_gcc_state_and_wait(&OverrideGcReportInstruction { - report: match GcReport::default() { - mut a => { - a.trigger_r = 255; - a.trigger_l = 255; - a.buttons_2.button_r = true; - a.buttons_2.button_l = true; - a.buttons_1.button_x = true; - a.buttons_1.button_a = true; - a.stick_x = 255; - a.stick_y = 255; - a.cstick_x = 127; - a.cstick_y = 127; - a - } + buttons_1: Buttons1 { + button_x: true, + button_y: true, + button_a: true, + ..Default::default() }, - duration_ms: 1000, - }) - .await; - StickCalibrationProcess::new(&mut final_config, Stick::ControlStick) - .calibrate_stick() - .await; - } - // calibrate rstick - 2 => { - override_gcc_state_and_wait(&OverrideGcReportInstruction { - report: match GcReport::default() { - mut a => { - a.trigger_r = 255; - a.trigger_l = 255; - a.buttons_2.button_r = true; - a.buttons_2.button_l = true; - a.buttons_1.button_x = true; - a.buttons_1.button_a = true; - a.stick_x = 127; - a.stick_y = 127; - a.cstick_x = 255; - a.cstick_y = 255; - a - } + stick_x: 127, + stick_y: 127, + cstick_x: 127, + cstick_y: 127, + }, + duration_ms: 1000, + }) + .await; + + break 'main; + } + // calibrate lstick + 1 => { + override_gcc_state_and_wait(&OverrideGcReportInstruction { + report: GcReport { + trigger_r: 255, + trigger_l: 255, + buttons_2: Buttons2 { + button_r: true, + button_l: true, + ..Default::default() }, - duration_ms: 1000, - }) - .await; - StickCalibrationProcess::new(&mut final_config, Stick::CStick) - .calibrate_stick() - .await; - } - // snapback changes - i if i >= 3 && i <= 10 => { - let stick = match i { - 3 | 4 | 5 | 6 => Stick::ControlStick, - 7 | 8 | 9 | 10 => Stick::CStick, - _ => unreachable!(), - }; - - let axis = match i { - 3 | 7 | 5 | 9 => StickAxis::XAxis, - 4 | 8 | 6 | 10 => StickAxis::YAxis, - _ => unreachable!(), - }; - - let stick_config = match stick { - Stick::ControlStick => &mut final_config.astick_config, - Stick::CStick => &mut final_config.cstick_config, - }; - - let to_adjust = match axis { - StickAxis::XAxis => &mut stick_config.x_snapback, - StickAxis::YAxis => &mut stick_config.y_snapback, - }; - - *to_adjust = (*to_adjust - + match i { - 3 | 7 | 4 | 8 => 1, - 5 | 9 | 6 | 10 => -1, - _ => unreachable!(), - }) - .clamp(-ABS_MAX_SNAPBACK, ABS_MAX_SNAPBACK); - - override_gcc_state_and_wait(&OverrideGcReportInstruction { - report: match GcReport::default() { - mut a => { - a.trigger_r = 255; - a.trigger_l = 255; - a.buttons_2.button_r = true; - a.buttons_2.button_l = true; - a.buttons_1.button_x = true; - a.buttons_1.button_a = true; - a.stick_x = (127 - + match stick { - Stick::ControlStick => match axis { - StickAxis::XAxis => *to_adjust, - StickAxis::YAxis => 0, - }, - Stick::CStick => 0, - }) as u8; - a.stick_y = (127 - + match stick { - Stick::ControlStick => match axis { - StickAxis::XAxis => 0, - StickAxis::YAxis => *to_adjust, - }, - Stick::CStick => 0, - }) as u8; - a.cstick_x = (127 - + match stick { - Stick::ControlStick => 0, - Stick::CStick => match axis { - StickAxis::XAxis => *to_adjust, - StickAxis::YAxis => 0, - }, - }) as u8; - a.cstick_y = (127 - + match stick { - Stick::ControlStick => 0, - Stick::CStick => match axis { - StickAxis::XAxis => 0, - StickAxis::YAxis => *to_adjust, - }, - }) as u8; - a - } + buttons_1: Buttons1 { + button_x: true, + button_y: true, + button_a: true, + ..Default::default() }, - duration_ms: 750, - }) + stick_x: 255, + stick_y: 255, + cstick_x: 127, + cstick_y: 127, + }, + duration_ms: 1000, + }) + .await; + StickCalibrationProcess::new(&mut final_config, Stick::ControlStick) + .calibrate_stick() .await; - - SIGNAL_CONFIG_CHANGE.signal(final_config.clone()); - } - // waveshaping changes - i if i >= 11 && i <= 18 => { - let stick = match i { - 11 | 12 | 13 | 14 => Stick::ControlStick, - 15 | 16 | 17 | 18 => Stick::CStick, - _ => unreachable!(), - }; - - let axis = match i { - 11 | 15 | 13 | 17 => StickAxis::XAxis, - 12 | 16 | 14 | 18 => StickAxis::YAxis, - _ => unreachable!(), - }; - - let stick_config = match stick { - Stick::ControlStick => &mut final_config.astick_config, - Stick::CStick => &mut final_config.cstick_config, - }; - - let to_adjust = match axis { - StickAxis::XAxis => &mut stick_config.x_waveshaping, - StickAxis::YAxis => &mut stick_config.y_waveshaping, - }; - - *to_adjust = (*to_adjust as i8 - + match i { - 11 | 15 | 12 | 16 => 1, - 13 | 17 | 14 | 18 => -1, - _ => unreachable!(), - }) - .clamp(0, MAX_WAVESHAPING) as u8; - - override_gcc_state_and_wait(&OverrideGcReportInstruction { - report: match GcReport::default() { - mut a => { - a.trigger_r = 255; - a.trigger_l = 255; - a.buttons_2.button_r = true; - a.buttons_2.button_l = true; - a.buttons_1.button_x = true; - a.buttons_1.button_a = true; - a.stick_x = (127 - + match stick { - Stick::ControlStick => match axis { - StickAxis::XAxis => *to_adjust, - StickAxis::YAxis => 0, - }, - Stick::CStick => 0, - }) as u8; - a.stick_y = (127 - + match stick { - Stick::ControlStick => match axis { - StickAxis::XAxis => 0, - StickAxis::YAxis => *to_adjust, - }, - Stick::CStick => 0, - }) as u8; - a.cstick_x = (127 - + match stick { - Stick::ControlStick => 0, - Stick::CStick => match axis { - StickAxis::XAxis => *to_adjust, - StickAxis::YAxis => 0, - }, - }) as u8; - a.cstick_y = (127 - + match stick { - Stick::ControlStick => 0, - Stick::CStick => match axis { - StickAxis::XAxis => 0, - StickAxis::YAxis => *to_adjust, - }, - }) as u8; - a - } + } + // calibrate rstick + 2 => { + override_gcc_state_and_wait(&OverrideGcReportInstruction { + report: GcReport { + trigger_r: 255, + trigger_l: 255, + buttons_2: Buttons2 { + button_r: true, + button_l: true, + ..Default::default() }, - duration_ms: 750, - }) - .await; - - SIGNAL_CONFIG_CHANGE.signal(final_config.clone()); - } - // smoothing changes - i if i >= 19 && i <= 26 => { - let stick = match i { - 19 | 20 | 21 | 22 => Stick::ControlStick, - 23 | 24 | 25 | 26 => Stick::CStick, - _ => unreachable!(), - }; - - let axis = match i { - 19 | 23 | 21 | 25 => StickAxis::XAxis, - 20 | 24 | 22 | 26 => StickAxis::YAxis, - _ => unreachable!(), - }; - - let stick_config = match stick { - Stick::ControlStick => &mut final_config.astick_config, - Stick::CStick => &mut final_config.cstick_config, - }; - - let to_adjust = match axis { - StickAxis::XAxis => &mut stick_config.x_smoothing, - StickAxis::YAxis => &mut stick_config.y_smoothing, - }; - - *to_adjust = (*to_adjust as i8 - + match i { - 19 | 23 | 20 | 24 => 1, - 21 | 25 | 22 | 26 => -1, - _ => unreachable!(), - }) - .clamp(0, MAX_SMOOTHING) as u8; - - override_gcc_state_and_wait(&OverrideGcReportInstruction { - report: match GcReport::default() { - mut a => { - a.trigger_r = 255; - a.trigger_l = 255; - a.buttons_2.button_r = true; - a.buttons_2.button_l = true; - a.buttons_1.button_x = true; - a.buttons_1.button_a = true; - a.stick_x = (127 - + match stick { - Stick::ControlStick => match axis { - StickAxis::XAxis => *to_adjust, - StickAxis::YAxis => 0, - }, - Stick::CStick => 0, - }) as u8; - a.stick_y = (127 - + match stick { - Stick::ControlStick => match axis { - StickAxis::XAxis => 0, - StickAxis::YAxis => *to_adjust, - }, - Stick::CStick => 0, - }) as u8; - a.cstick_x = (127 - + match stick { - Stick::ControlStick => 0, - Stick::CStick => match axis { - StickAxis::XAxis => *to_adjust, - StickAxis::YAxis => 0, - }, - }) as u8; - a.cstick_y = (127 - + match stick { - Stick::ControlStick => 0, - Stick::CStick => match axis { - StickAxis::XAxis => 0, - StickAxis::YAxis => *to_adjust, - }, - }) as u8; - a - } + buttons_1: Buttons1 { + button_x: true, + button_y: true, + button_a: true, + ..Default::default() }, - duration_ms: 750, - }) + stick_x: 127, + stick_y: 127, + cstick_x: 255, + cstick_y: 255, + }, + duration_ms: 1000, + }) + .await; + StickCalibrationProcess::new(&mut final_config, Stick::CStick) + .calibrate_stick() .await; + } + // snapback changes + i if (3..=10).contains(&i) => { + let stick = match i { + 3..=6 => Stick::ControlStick, + 7..=10 => Stick::CStick, + _ => unreachable!(), + }; - SIGNAL_CONFIG_CHANGE.signal(final_config.clone()); - } - // cardinalsnap increase/decrease - i if i >= 27 && i <= 30 => { - let stick = match i { - 27 | 28 => Stick::ControlStick, - 29 | 30 => Stick::CStick, + let axis = match i { + 3 | 7 | 5 | 9 => StickAxis::XAxis, + 4 | 8 | 6 | 10 => StickAxis::YAxis, + _ => unreachable!(), + }; + + let stick_config = match stick { + Stick::ControlStick => &mut final_config.astick_config, + Stick::CStick => &mut final_config.cstick_config, + }; + + let to_adjust = match axis { + StickAxis::XAxis => &mut stick_config.x_snapback, + StickAxis::YAxis => &mut stick_config.y_snapback, + }; + + *to_adjust = (*to_adjust + + match i { + 3 | 7 | 4 | 8 => 1, + 5 | 9 | 6 | 10 => -1, _ => unreachable!(), - }; + }) + .clamp(-ABS_MAX_SNAPBACK, ABS_MAX_SNAPBACK); - let to_adjust = match i { - 27 | 29 => &mut final_config.astick_config.cardinal_snapping, - 28 | 30 => &mut final_config.cstick_config.cardinal_snapping, + override_gcc_state_and_wait(&OverrideGcReportInstruction { + report: GcReport { + trigger_r: 255, + trigger_l: 255, + buttons_2: Buttons2 { + button_r: true, + button_l: true, + ..Default::default() + }, + buttons_1: Buttons1 { + button_x: true, + button_y: true, + button_a: true, + ..Default::default() + }, + stick_x: (127 + + match stick { + Stick::ControlStick => match axis { + StickAxis::XAxis => *to_adjust, + StickAxis::YAxis => 0, + }, + Stick::CStick => 0, + }) as u8, + stick_y: (127 + + match stick { + Stick::ControlStick => match axis { + StickAxis::XAxis => 0, + StickAxis::YAxis => *to_adjust, + }, + Stick::CStick => 0, + }) as u8, + cstick_x: (127 + + match stick { + Stick::ControlStick => 0, + Stick::CStick => match axis { + StickAxis::XAxis => *to_adjust, + StickAxis::YAxis => 0, + }, + }) as u8, + cstick_y: (127 + + match stick { + Stick::ControlStick => 0, + Stick::CStick => match axis { + StickAxis::XAxis => 0, + StickAxis::YAxis => *to_adjust, + }, + }) as u8, + }, + duration_ms: 750, + }) + .await; + + SIGNAL_CONFIG_CHANGE.signal(final_config.clone()); + } + // waveshaping changes + i if (11..=18).contains(&i) => { + let stick = match i { + 11..=14 => Stick::ControlStick, + 15..=18 => Stick::CStick, + _ => unreachable!(), + }; + + let axis = match i { + 11 | 15 | 13 | 17 => StickAxis::XAxis, + 12 | 16 | 14 | 18 => StickAxis::YAxis, + _ => unreachable!(), + }; + + let stick_config = match stick { + Stick::ControlStick => &mut final_config.astick_config, + Stick::CStick => &mut final_config.cstick_config, + }; + + let to_adjust = match axis { + StickAxis::XAxis => &mut stick_config.x_waveshaping, + StickAxis::YAxis => &mut stick_config.y_waveshaping, + }; + + *to_adjust = (*to_adjust as i8 + + match i { + 11 | 15 | 12 | 16 => 1, + 13 | 17 | 14 | 18 => -1, _ => unreachable!(), - }; - - *to_adjust = (*to_adjust - + match i { - 27 | 29 => 1, - 28 | 30 => -1, - _ => unreachable!(), - }) - .clamp(-1, MAX_CARDINAL_SNAP); - - override_gcc_state_and_wait(&OverrideGcReportInstruction { - report: match GcReport::default() { - mut a => { - a.trigger_r = 255; - a.trigger_l = 255; - a.buttons_2.button_r = true; - a.buttons_2.button_l = true; - a.buttons_1.button_x = true; - a.buttons_1.button_a = true; - a.stick_x = 127; - a.stick_y = (127 - + match stick { - Stick::ControlStick => *to_adjust, - Stick::CStick => 0, - }) as u8; - a.cstick_x = 127; - a.cstick_y = (127 - + match stick { - Stick::ControlStick => 0, - Stick::CStick => *to_adjust, - }) as u8; - a - } - }, - duration_ms: 750, }) - .await; + .clamp(0, MAX_WAVESHAPING) as u8; - SIGNAL_CONFIG_CHANGE.signal(final_config.clone()); - } - // scaling changes - i if i >= 31 && i <= 34 => { - let stick = match i { - 31 | 32 => Stick::ControlStick, - 33 | 34 => Stick::CStick, + override_gcc_state_and_wait(&OverrideGcReportInstruction { + report: GcReport { + trigger_r: 255, + trigger_l: 255, + buttons_2: Buttons2 { + button_r: true, + button_l: true, + ..Default::default() + }, + buttons_1: Buttons1 { + button_x: true, + button_y: true, + button_a: true, + ..Default::default() + }, + stick_x: (127 + + match stick { + Stick::ControlStick => match axis { + StickAxis::XAxis => *to_adjust, + StickAxis::YAxis => 0, + }, + Stick::CStick => 0, + }) as u8, + stick_y: (127 + + match stick { + Stick::ControlStick => match axis { + StickAxis::XAxis => 0, + StickAxis::YAxis => *to_adjust, + }, + Stick::CStick => 0, + }) as u8, + cstick_x: (127 + + match stick { + Stick::ControlStick => 0, + Stick::CStick => match axis { + StickAxis::XAxis => *to_adjust, + StickAxis::YAxis => 0, + }, + }) as u8, + cstick_y: (127 + + match stick { + Stick::ControlStick => 0, + Stick::CStick => match axis { + StickAxis::XAxis => 0, + StickAxis::YAxis => *to_adjust, + }, + }) as u8, + }, + duration_ms: 750, + }) + .await; + + SIGNAL_CONFIG_CHANGE.signal(final_config.clone()); + } + // smoothing changes + i if (19..=26).contains(&i) => { + let stick = match i { + 19..=22 => Stick::ControlStick, + 23..=26 => Stick::CStick, + _ => unreachable!(), + }; + + let axis = match i { + 19 | 23 | 21 | 25 => StickAxis::XAxis, + 20 | 24 | 22 | 26 => StickAxis::YAxis, + _ => unreachable!(), + }; + + let stick_config = match stick { + Stick::ControlStick => &mut final_config.astick_config, + Stick::CStick => &mut final_config.cstick_config, + }; + + let to_adjust = match axis { + StickAxis::XAxis => &mut stick_config.x_smoothing, + StickAxis::YAxis => &mut stick_config.y_smoothing, + }; + + *to_adjust = (*to_adjust as i8 + + match i { + 19 | 23 | 20 | 24 => 1, + 21 | 25 | 22 | 26 => -1, _ => unreachable!(), - }; + }) + .clamp(0, MAX_SMOOTHING) as u8; - let to_adjust = match i { - 31 | 33 => &mut final_config.astick_config.analog_scaler, - 32 | 34 => &mut final_config.cstick_config.analog_scaler, + override_gcc_state_and_wait(&OverrideGcReportInstruction { + report: GcReport { + trigger_r: 255, + trigger_l: 255, + buttons_2: Buttons2 { + button_r: true, + button_l: true, + ..Default::default() + }, + buttons_1: Buttons1 { + button_x: true, + button_y: true, + button_a: true, + ..Default::default() + }, + stick_x: (127 + + match stick { + Stick::ControlStick => match axis { + StickAxis::XAxis => *to_adjust, + StickAxis::YAxis => 0, + }, + Stick::CStick => 0, + }) as u8, + stick_y: (127 + + match stick { + Stick::ControlStick => match axis { + StickAxis::XAxis => 0, + StickAxis::YAxis => *to_adjust, + }, + Stick::CStick => 0, + }) as u8, + cstick_x: (127 + + match stick { + Stick::ControlStick => 0, + Stick::CStick => match axis { + StickAxis::XAxis => *to_adjust, + StickAxis::YAxis => 0, + }, + }) as u8, + cstick_y: (127 + + match stick { + Stick::ControlStick => 0, + Stick::CStick => match axis { + StickAxis::XAxis => 0, + StickAxis::YAxis => *to_adjust, + }, + }) as u8, + }, + duration_ms: 750, + }) + .await; + + SIGNAL_CONFIG_CHANGE.signal(final_config.clone()); + } + // cardinalsnap increase/decrease + i if (27..=30).contains(&i) => { + let stick = match i { + 27 | 28 => Stick::ControlStick, + 29 | 30 => Stick::CStick, + _ => unreachable!(), + }; + + let to_adjust = match stick { + Stick::ControlStick => &mut final_config.astick_config.cardinal_snapping, + Stick::CStick => &mut final_config.cstick_config.cardinal_snapping, + }; + + *to_adjust = (*to_adjust + + match i { + 27 | 29 => 1, + 28 | 30 => -1, _ => unreachable!(), - }; - - *to_adjust = ((*to_adjust as i8 - + match i { - 31 | 33 => 1, - 32 | 34 => -1, - _ => unreachable!(), - }) as u8) - .clamp(MIN_ANALOG_SCALER, MAX_ANALOG_SCALER); - - override_gcc_state_and_wait(&OverrideGcReportInstruction { - report: match GcReport::default() { - mut a => { - a.trigger_r = 255; - a.trigger_l = 255; - a.buttons_2.button_r = true; - a.buttons_2.button_l = true; - a.buttons_1.button_x = true; - a.buttons_1.button_a = true; - a.stick_x = 127; - a.stick_y = (127 - + match stick { - Stick::ControlStick => *to_adjust, - Stick::CStick => 0, - }) as u8; - a.cstick_x = 127; - a.cstick_y = (127 - + match stick { - Stick::ControlStick => 0, - Stick::CStick => *to_adjust, - }) as u8; - a - } - }, - duration_ms: 750, }) - .await; + .clamp(-1, MAX_CARDINAL_SNAP); - SIGNAL_CONFIG_CHANGE.signal(final_config.clone()); - } - // rumble strength changes - i if i >= 35 && i <= 36 => { - let to_adjust = &mut final_config.rumble_strength; - - *to_adjust = (*to_adjust as i8 - + match i { - 35 => 1, - 36 => -1, - _ => unreachable!(), - }) - .clamp(0, MAX_RUMBLE_STRENGTH) as u8; - - SIGNAL_CHANGE_RUMBLE_STRENGTH.signal(*to_adjust); - - override_gcc_state_and_wait(&OverrideGcReportInstruction { - report: match GcReport::default() { - mut a => { - a.trigger_r = 255; - a.trigger_l = 255; - a.buttons_2.button_r = true; - a.buttons_2.button_l = true; - a.buttons_1.button_x = true; - a.buttons_1.button_a = true; - a.buttons_2.button_z = true; // makes the controller rumble in smashscope - a.stick_x = 127; - a.stick_y = 127; - a.cstick_x = 127; - a.cstick_y = 127; - a - } + override_gcc_state_and_wait(&OverrideGcReportInstruction { + report: GcReport { + trigger_r: 255, + trigger_l: 255, + buttons_2: Buttons2 { + button_r: true, + button_l: true, + ..Default::default() }, - duration_ms: 750, - }) - .await; - - SIGNAL_CONFIG_CHANGE.signal(final_config.clone()); - } - // input consistency toggle - 37 => { - final_config.input_consistency_mode = !final_config.input_consistency_mode; - - override_gcc_state_and_wait(&OverrideGcReportInstruction { - report: match GcReport::default() { - mut a => { - a.trigger_r = 255; - a.trigger_l = 255; - a.buttons_2.button_r = true; - a.buttons_2.button_l = true; - a.buttons_1.button_x = true; - a.buttons_1.button_a = true; - a.stick_x = 127; - a.stick_y = (127 as i8 - + match final_config.input_consistency_mode { - true => 69, - false => -69, - }) as u8; - a.cstick_x = 127; - a.cstick_y = 127; - a - } + buttons_1: Buttons1 { + button_x: true, + button_y: true, + button_a: true, + ..Default::default() }, - duration_ms: 750, - }) - .await; + stick_x: 127, + stick_y: (127 + + match stick { + Stick::ControlStick => *to_adjust, + Stick::CStick => 0, + }) as u8, + cstick_x: 127, + cstick_y: (127 + + match stick { + Stick::ControlStick => 0, + Stick::CStick => *to_adjust, + }) as u8, + }, + duration_ms: 750, + }) + .await; - SIGNAL_CONFIG_CHANGE.signal(final_config.clone()); - } - s => { - error!("Invalid selection in config loop: {}", s); - continue; - } - }, + SIGNAL_CONFIG_CHANGE.signal(final_config.clone()); + } + // scaling changes + i if (31..=34).contains(&i) => { + let stick = match i { + 31 | 32 => Stick::ControlStick, + 33 | 34 => Stick::CStick, + _ => unreachable!(), + }; + + let to_adjust = match stick { + Stick::ControlStick => &mut final_config.astick_config.analog_scaler, + Stick::CStick => &mut final_config.cstick_config.analog_scaler, + }; + + *to_adjust = (*to_adjust as i8 + + match i { + 31 | 33 => 1, + 32 | 34 => -1, + _ => unreachable!(), + }) + .clamp(MIN_ANALOG_SCALER as i8, MAX_ANALOG_SCALER as i8) + as u8; + + override_gcc_state_and_wait(&OverrideGcReportInstruction { + report: GcReport { + trigger_r: 255, + trigger_l: 255, + buttons_2: Buttons2 { + button_r: true, + button_l: true, + ..Default::default() + }, + buttons_1: Buttons1 { + button_x: true, + button_y: true, + button_a: true, + ..Default::default() + }, + stick_x: 127, + stick_y: (127 + + match stick { + Stick::ControlStick => *to_adjust, + Stick::CStick => 0, + }) as u8, + cstick_x: 127, + cstick_y: (127 + + match stick { + Stick::ControlStick => 0, + Stick::CStick => *to_adjust, + }) as u8, + }, + duration_ms: 750, + }) + .await; + + SIGNAL_CONFIG_CHANGE.signal(final_config.clone()); + } + // rumble strength changes + i if (35..=36).contains(&i) => { + let to_adjust = &mut final_config.rumble_strength; + + *to_adjust = (*to_adjust as i8 + + match i { + 35 => 1, + 36 => -1, + _ => unreachable!(), + }) + .clamp(0, MAX_RUMBLE_STRENGTH) as u8; + + SIGNAL_CHANGE_RUMBLE_STRENGTH.signal(*to_adjust); + + override_gcc_state_and_wait(&OverrideGcReportInstruction { + report: GcReport { + trigger_r: 255, + trigger_l: 255, + buttons_2: Buttons2 { + button_r: true, + button_l: true, + button_z: true, + ..Default::default() + }, + buttons_1: Buttons1 { + button_x: true, + button_y: true, + button_a: true, + ..Default::default() + }, + stick_x: 127, + stick_y: 127, + cstick_x: 127, + cstick_y: 127, + }, + duration_ms: 750, + }) + .await; + + SIGNAL_CONFIG_CHANGE.signal(final_config.clone()); + } + // input consistency toggle + 37 => { + final_config.input_consistency_mode = !final_config.input_consistency_mode; + + override_gcc_state_and_wait(&OverrideGcReportInstruction { + report: GcReport { + trigger_r: 255, + trigger_l: 255, + buttons_2: Buttons2 { + button_r: true, + button_l: true, + ..Default::default() + }, + buttons_1: Buttons1 { + button_x: true, + button_y: true, + button_a: true, + ..Default::default() + }, + stick_x: 127, + stick_y: (127_i8 + + match final_config.input_consistency_mode { + true => 69, + false => -69, + }) as u8, + cstick_x: 127, + cstick_y: 127, + }, + duration_ms: 750, + }) + .await; + + SIGNAL_CONFIG_CHANGE.signal(final_config.clone()); + } + s => { + error!("Invalid selection in config loop: {}", s); + continue; + } }; - final_config.write_to_flash(&mut flash).unwrap(); + final_config.write_to_flash(flash).unwrap(); } info!("Exiting config main loop."); @@ -1654,21 +1694,24 @@ pub async fn config_task(mut flash: Flash<'static, FLASH, Async, FLASH_SIZE>) { info!("Entering config mode."); override_gcc_state_and_wait(&OverrideGcReportInstruction { - report: match GcReport::default() { - mut a => { - a.trigger_r = 255; - a.trigger_l = 255; - a.buttons_2.button_l = true; - a.buttons_2.button_r = true; - a.buttons_1.button_x = true; - a.buttons_1.button_y = true; - a.buttons_1.button_a = true; - a.stick_x = 127; - a.stick_y = 127; - a.cstick_x = 127; - a.cstick_y = 127; - a - } + report: GcReport { + trigger_r: 255, + trigger_l: 255, + buttons_2: Buttons2 { + button_l: true, + button_r: true, + ..Default::default() + }, + buttons_1: Buttons1 { + button_x: true, + button_y: true, + button_a: true, + ..Default::default() + }, + stick_x: 127, + stick_y: 127, + cstick_x: 127, + cstick_y: 127, }, duration_ms: 1000, }) diff --git a/src/input.rs b/src/input.rs index b43e141..2e2a759 100644 --- a/src/input.rs +++ b/src/input.rs @@ -117,11 +117,12 @@ pub fn read_ext_adc< spi_ccs.set_high(); } - return temp_value; + temp_value } /// Gets the average stick state over a 1ms interval in a non-blocking fashion. /// Will wait until end_time is reached before continuing after reading the ADCs. +#[allow(clippy::too_many_arguments)] #[link_section = ".time_critical.update_stick_states"] async fn update_stick_states( current_stick_state: &StickState, @@ -198,17 +199,18 @@ async fn update_stick_states( trace!("Raw Stick Values 001: {:?}", raw_stick_values); let (x_pos_filt, y_pos_filt) = - kalman_state.run_kalman(x_z, y_z, &controller_config.astick_config, &filter_gains); + kalman_state.run_kalman(x_z, y_z, &controller_config.astick_config, filter_gains); - let shaped_astick = match run_waveshaping( - x_pos_filt, - y_pos_filt, - controller_config.astick_config.x_waveshaping, - controller_config.astick_config.y_waveshaping, - controlstick_waveshaping_values, - &filter_gains, - ) { - (x, y) => XyValuePair { x, y }, + let shaped_astick = { + let (x, y) = run_waveshaping( + x_pos_filt, + y_pos_filt, + controller_config.astick_config.x_waveshaping, + controller_config.astick_config.y_waveshaping, + controlstick_waveshaping_values, + filter_gains, + ); + XyValuePair { x, y } }; trace!("Shaped Controlstick: {}", shaped_astick); @@ -220,15 +222,16 @@ async fn update_stick_states( old_stick_pos.x = pos_x; old_stick_pos.y = pos_y; - let shaped_cstick = match run_waveshaping( - pos_cx, - pos_cy, - controller_config.cstick_config.x_waveshaping, - controller_config.cstick_config.y_waveshaping, - cstick_waveshaping_values, - &filter_gains, - ) { - (x, y) => XyValuePair { x, y }, + let shaped_cstick = { + let (x, y) = run_waveshaping( + pos_cx, + pos_cy, + controller_config.cstick_config.x_waveshaping, + controller_config.cstick_config.y_waveshaping, + cstick_waveshaping_values, + filter_gains, + ); + XyValuePair { x, y } }; let old_c_pos = XyValuePair { @@ -250,41 +253,45 @@ async fn update_stick_states( trace!("Cstick position: {}, {}", pos_cx, pos_cy); - let mut remapped = match notch_remap( - pos_x, - pos_y, - controlstick_params, - &controller_config.astick_config, - is_calibrating, - ) { - (x, y) => XyValuePair { x, y }, + let mut remapped = { + let (x, y) = notch_remap( + pos_x, + pos_y, + controlstick_params, + &controller_config.astick_config, + is_calibrating, + ); + XyValuePair { x, y } }; - let mut remapped_c = match notch_remap( - pos_cx_filt, - pos_cy_filt, - cstick_params, - &controller_config.cstick_config, - is_calibrating, - ) { - (x, y) => XyValuePair { x, y }, + let mut remapped_c = { + let (x, y) = notch_remap( + pos_cx_filt, + pos_cy_filt, + cstick_params, + &controller_config.cstick_config, + is_calibrating, + ); + XyValuePair { x, y } }; - let remapped_unfiltered = match notch_remap( - raw_stick_values.a_linearized.x, - raw_stick_values.a_linearized.y, - controlstick_params, - &controller_config.astick_config, - is_calibrating, - ) { - (x, y) => XyValuePair { x, y }, + let remapped_unfiltered = { + let (x, y) = notch_remap( + raw_stick_values.a_linearized.x, + raw_stick_values.a_linearized.y, + controlstick_params, + &controller_config.astick_config, + is_calibrating, + ); + XyValuePair { x, y } }; - let remapped_c_unfiltered = match notch_remap( - raw_stick_values.c_linearized.x, - raw_stick_values.c_linearized.y, - cstick_params, - &controller_config.cstick_config, - is_calibrating, - ) { - (x, y) => XyValuePair { x, y }, + let remapped_c_unfiltered = { + let (x, y) = notch_remap( + raw_stick_values.c_linearized.x, + raw_stick_values.c_linearized.y, + cstick_params, + &controller_config.cstick_config, + is_calibrating, + ); + XyValuePair { x, y } }; trace!( @@ -309,20 +316,20 @@ async fn update_stick_states( let mut out_stick_state = current_stick_state.clone(); let diff_x = (remapped.x + FLOAT_ORIGIN) - current_stick_state.ax as f32; - if (diff_x > (1.0 + STICK_HYST_VAL)) || (diff_x < -STICK_HYST_VAL) { + if !(-STICK_HYST_VAL..=(1.0 + STICK_HYST_VAL)).contains(&diff_x) { out_stick_state.ax = (remapped.x + FLOAT_ORIGIN) as u8; } let diff_y = (remapped.y + FLOAT_ORIGIN) - current_stick_state.ay as f32; - if (diff_y > (1.0 + STICK_HYST_VAL)) || (diff_y < -STICK_HYST_VAL) { + if !(-STICK_HYST_VAL..=(1.0 + STICK_HYST_VAL)).contains(&diff_y) { out_stick_state.ay = (remapped.y + FLOAT_ORIGIN) as u8; } let diff_cx = (remapped_c.x + FLOAT_ORIGIN) - current_stick_state.cx as f32; - if (diff_cx > (1.0 + STICK_HYST_VAL)) || (diff_cx < -STICK_HYST_VAL) { + if !(-STICK_HYST_VAL..=(1.0 + STICK_HYST_VAL)).contains(&diff_cx) { out_stick_state.cx = (remapped_c.x + FLOAT_ORIGIN) as u8; } let diff_cy = (remapped_c.y + FLOAT_ORIGIN) - current_stick_state.cy as f32; - if (diff_cy > (1.0 + STICK_HYST_VAL)) || (diff_cy < -STICK_HYST_VAL) { + if !(-STICK_HYST_VAL..=(1.0 + STICK_HYST_VAL)).contains(&diff_cy) { out_stick_state.cy = (remapped_c.y + FLOAT_ORIGIN) as u8; } @@ -337,6 +344,7 @@ async fn update_stick_states( out_stick_state } +#[allow(clippy::too_many_arguments)] fn update_button_states< A: Pin, B: Pin, @@ -405,6 +413,7 @@ pub async fn input_integrity_benchmark() { /// Task responsible for updating the button states. /// Publishes the result to CHANNEL_GCC_STATE. +#[allow(clippy::too_many_arguments)] #[embassy_executor::task] pub async fn update_button_state_task( btn_z: Input<'static, AnyPin>, @@ -424,6 +433,8 @@ pub async fn update_button_state_task( if btn_a.is_low() && btn_x.is_low() && btn_y.is_low() { info!("Detected reset button press, booting into flash."); embassy_rp::rom_data::reset_to_usb_boot(0, 0); + + #[allow(clippy::empty_loop)] loop {} } @@ -474,7 +485,7 @@ pub async fn update_button_state_task( }; if let Some(override_state) = &override_stick_state { - let mut overriden_gcc_state = gcc_state.clone(); + let mut overriden_gcc_state = gcc_state; match override_state.which_stick { Stick::ControlStick => { overriden_gcc_state.stick_x = override_state.x; @@ -561,20 +572,20 @@ pub async fn update_stick_states_task( } } - match Instant::now() { - n => { - match (n - last_loop_time).as_micros() { - a if a > 1666 => debug!("Loop took {} us", a), - _ => {} - }; - last_loop_time = n; - } + { + let n = Instant::now(); + + match (n - last_loop_time).as_micros() { + a if a > 1666 => debug!("Loop took {} us", a), + _ => {} + }; + last_loop_time = n; }; SIGNAL_STICK_STATE.signal(current_stick_state.clone()); - ticker.next().await; yield_now().await; + ticker.next().await; if let Some(new_config) = SIGNAL_CONFIG_CHANGE.try_take() { controller_config = new_config; diff --git a/src/stick.rs b/src/stick.rs index b02028a..1207eaa 100644 --- a/src/stick.rs +++ b/src/stick.rs @@ -19,7 +19,7 @@ pub const NO_OF_CALIBRATION_POINTS: usize = 32; const MAX_ORDER: usize = 20; /// 28 degrees; this is the max angular deflection of the stick. -const MAX_STICK_ANGLE: f32 = 0.4886921906; +const MAX_STICK_ANGLE: f32 = 0.488_692_2; #[rustfmt::skip] // right notch 1 up right notch 2 up notch 3 up left notch 4 left notch 5 down left notch 6 down notch 7 down right notch 8 @@ -123,10 +123,10 @@ impl CleanedCalibrationPoints { out.cleaned_points.x[i + 1] = cal_points_x[i * 2 + 1]; out.cleaned_points.y[i + 1] = cal_points_y[i * 2 + 1]; - (out.notch_points.x[i + 1], out.notch_points.y[i + 1]) = - match calc_stick_values(notch_angles[i]) { - (a, b) => (roundf(a), roundf(b)), - }; + (out.notch_points.x[i + 1], out.notch_points.y[i + 1]) = { + let (a, b) = calc_stick_values(notch_angles[i]); + (roundf(a), roundf(b)) + } } // TODO: put the below in a macro to clean it up a bit, once it's confirmed to work @@ -186,6 +186,7 @@ impl CleanedCalibrationPoints { out.cleaned_points.x[0] /= (NO_OF_NOTCHES - 4) as f32; out.cleaned_points.y[0] /= (NO_OF_NOTCHES - 4) as f32; + #[allow(clippy::needless_range_loop)] for i in 0..NO_OF_NOTCHES { let delta_x = out.cleaned_points.x[i + 1] - out.cleaned_points.x[0]; let delta_y = out.cleaned_points.y[i + 1] - out.cleaned_points.y[0]; @@ -241,11 +242,9 @@ impl LinearizedCalibration { /// /// Generate a fit to linearize the stick response. /// - /// Inputs: - /// cleaned points X and Y, (must be 17 points for each of these, the first being the center, the others starting at 3 oclock and going around counterclockwise) + /// Inputs: cleaned points X and Y, (must be 17 points for each of these, the first being the center, the others starting at 3 oclock and going around counterclockwise) /// - /// Outputs: - /// linearization fit coefficients for X and Y + /// Outputs: linearization fit coefficients for X and Y pub fn from_calibration_points(cleaned_calibration_points: &CleanedCalibrationPoints) -> Self { let mut fit_points_x = [0f64; 5]; let mut fit_points_y = [0f64; 5]; @@ -263,7 +262,7 @@ impl LinearizedCalibration { fit_points_x[1] = (in_x[6 + 1] + in_x[10 + 1]) / 2.0f64; fit_points_x[2] = in_x[0]; fit_points_x[3] = (in_x[2 + 1] + in_x[14 + 1]) / 2.0f64; - fit_points_x[4] = in_x[0 + 1]; + fit_points_x[4] = in_x[1]; fit_points_y[0] = in_y[12 + 1]; fit_points_y[1] = (in_y[10 + 1] + in_y[14 + 1]) / 2.0f64; @@ -282,8 +281,8 @@ impl LinearizedCalibration { let x_zero_error = linearize(fit_points_x[2] as f32, &fit_coeffs_x.map(|e| e as f32)); let y_zero_error = linearize(fit_points_y[2] as f32, &fit_coeffs_y.map(|e| e as f32)); - fit_coeffs_x[3] = fit_coeffs_x[3] - x_zero_error as f64; - fit_coeffs_y[3] = fit_coeffs_y[3] - y_zero_error as f64; + fit_coeffs_x[3] -= x_zero_error as f64; + fit_coeffs_y[3] -= y_zero_error as f64; let mut linearized_points_x = [0f32; NO_OF_NOTCHES + 1]; let mut linearized_points_y = [0f32; NO_OF_NOTCHES + 1]; @@ -376,6 +375,7 @@ impl NotchCalibration { trace!("The transform matrix is: {:?}", a); + #[allow(clippy::needless_range_loop)] for j in 0..2 { for k in 0..2 { out.affine_coeffs[i - 1][j * 2 + k] = a[j][k]; @@ -721,6 +721,7 @@ fn inverse(in_mat: &[[f32; 3]; 3]) -> [[f32; 3]; 3] { out_mat } +#[allow(clippy::needless_range_loop)] fn matrix_mult(a: &[[f32; 3]; 3], b: &[[f32; 3]; 3]) -> [[f32; 3]; 3] { let mut out = [[0f32; 3]; 3]; @@ -777,8 +778,8 @@ fn det(matrix: &[[f64; N]; N]) -> f64 { let mut p = 1f64; - for i in 0..N { - p *= matrix[i][i]; + for (i, elem) in matrix.iter().enumerate().take(N) { + p *= elem[i]; } p * (sign as f64) @@ -798,9 +799,7 @@ fn trianglize(matrix: &mut [[f64; N]; N]) -> i32 { } if max > 0 { sign = -sign; - let tmp = matrix[i]; - matrix[i] = matrix[max]; - matrix[max] = tmp; + matrix.swap(i, max); } if matrix[i][i] == 0. { return 0; @@ -848,11 +847,11 @@ fn fit_curve( for i in 0..N { let x = px[i]; let y = py[i]; - for j in 0..NCOEFFS * 2 - 1 { - s[j] += curve_fit_power(x, j as u32); + for (j, elem) in s.iter_mut().enumerate().take(NCOEFFS * 2 - 1) { + *elem += curve_fit_power(x, j as u32); } - for j in 0..NCOEFFS { - t[j] += y * curve_fit_power(x, j as u32); + for (j, elem) in t.iter_mut().enumerate().take(NCOEFFS) { + *elem += y * curve_fit_power(x, j as u32); } } @@ -860,9 +859,7 @@ fn fit_curve( let mut matrix = [[0f64; NCOEFFS]; NCOEFFS]; for i in 0..NCOEFFS { - for j in 0..NCOEFFS { - matrix[i][j] = s[i + j]; - } + matrix[i][..NCOEFFS].copy_from_slice(&s[i..(NCOEFFS + i)]); } let denom = det(&matrix); @@ -893,7 +890,6 @@ pub fn linearize(point: f32, coefficients: &[f32; NUM_COEFFS]) -> f32 { + coefficients[3] } -// TODO: currently broken! #[link_section = ".time_critical.notch_remap"] pub fn notch_remap( x_in: f32,