From 83f234b5f7600a1743a3d7fea122a6db01b37bc6 Mon Sep 17 00:00:00 2001 From: fly Date: Mon, 10 Jun 2024 00:08:44 +0200 Subject: [PATCH] Working with 3 input axis Signed-off-by: fly --- Pedestal/firmware/src/device.rs | 17 +++++++++-------- Pedestal/firmware/src/main.rs | 21 +++++++++------------ 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/Pedestal/firmware/src/device.rs b/Pedestal/firmware/src/device.rs index 02749e7..a3e9eb4 100644 --- a/Pedestal/firmware/src/device.rs +++ b/Pedestal/firmware/src/device.rs @@ -7,7 +7,7 @@ use usbd_human_interface_device::{ descriptor::InterfaceProtocol, device::DeviceClass, interface::{ - InBytes8, Interface, InterfaceBuilder, InterfaceConfig, OutBytes8, ReportSingle, + InBytes64, Interface, InterfaceBuilder, InterfaceConfig, OutBytes64, ReportSingle, UsbAllocatable, }, UsbHidError, @@ -23,10 +23,11 @@ pub const CUSTOM_DESCRIPTOR: &[u8] = &[ 0x09, 0x01, // UsageId(Pointer[0x0001]) 0xA1, 0x00, // Collection(Physical) 0x09, 0x30, // UsageId(X[0x0030]) + 0x09, 0x31, // UsageId(Y[0x0031]) 0x09, 0x32, // UsageId(Z[0x0032]) 0x15, 0x00, // LogicalMinimum(0) 0x26, 0xFF, 0x0F, // LogicalMaximum(4,095) - 0x95, 0x02, // ReportCount(2) + 0x95, 0x03, // ReportCount(3) 0x75, 0x10, // ReportSize(16) 0x81, 0x02, // Input(Data, Variable, Absolute, NoWrap, Linear, PreferredState, NoNullPosition, BitField) 0xC0, // EndCollection() @@ -54,12 +55,12 @@ pub const CUSTOM_DESCRIPTOR: &[u8] = &[ ]; #[derive(Clone, Copy, Debug, Eq, PartialEq, Default, PackedStruct)] -#[packed_struct(endian = "lsb", size_bytes = "7")] +#[packed_struct(endian = "lsb", size_bytes = "9")] // MUST be <= 64 else we get problem cause InBytes64 or OutBytes64 pub struct CustomInputReport { #[packed_field] pub report_id: u8, #[packed_field] - pub axis: [u16; 2], + pub axis: [u16; 3], #[packed_field] pub buttons: u16, } @@ -76,7 +77,7 @@ pub struct CustomOutputReport { } pub struct CustomDevice<'a, B: UsbBus> { - interface: Interface<'a, B, InBytes8, OutBytes8, ReportSingle>, + interface: Interface<'a, B, InBytes64, OutBytes64, ReportSingle>, } impl<'a, B: UsbBus> CustomDevice<'a, B> { @@ -98,7 +99,7 @@ impl<'a, B: UsbBus> CustomDevice<'a, B> { } impl<'a, B: UsbBus> DeviceClass<'a> for CustomDevice<'a, B> { - type I = Interface<'a, B, InBytes8, OutBytes8, ReportSingle>; + type I = Interface<'a, B, InBytes64, OutBytes64, ReportSingle>; fn interface(&mut self) -> &mut Self::I { &mut self.interface @@ -112,7 +113,7 @@ impl<'a, B: UsbBus> DeviceClass<'a> for CustomDevice<'a, B> { } pub struct CustomConfig<'a> { - interface: InterfaceConfig<'a, InBytes8, OutBytes8, ReportSingle>, + interface: InterfaceConfig<'a, InBytes64, OutBytes64, ReportSingle>, } impl<'a> Default for CustomConfig<'a> { @@ -134,7 +135,7 @@ impl<'a> Default for CustomConfig<'a> { impl<'a> CustomConfig<'a> { #[must_use] - pub fn new(interface: InterfaceConfig<'a, InBytes8, OutBytes8, ReportSingle>) -> Self { + pub fn new(interface: InterfaceConfig<'a, InBytes64, OutBytes64, ReportSingle>) -> Self { Self { interface } } } diff --git a/Pedestal/firmware/src/main.rs b/Pedestal/firmware/src/main.rs index 8b5f5fb..06fd672 100644 --- a/Pedestal/firmware/src/main.rs +++ b/Pedestal/firmware/src/main.rs @@ -37,7 +37,7 @@ struct MyPins { pa2: Pin<'A', 2, Analog>, pb0: Pin<'B', 0, Output>, pc1: Pin<'C', 1, Input>, - pc3: Pin<'C', 3, Output>, + pc3: Pin<'C', 3, Analog>, pc15: Pin<'C', 15, Input>, } @@ -140,7 +140,7 @@ fn main() -> ! { pa2: gpioa.pa2.into_analog(&mut gpioa.crl), pb0: gpiob.pb0.into_push_pull_output(&mut gpiob.crl), pc1: gpioc.pc1.into_pull_down_input(&mut gpioc.crl), - pc3: gpioc.pc3.into_push_pull_output(&mut gpioc.crl), + pc3: gpioc.pc3.into_analog(&mut gpioc.crl), pc15: gpioc.pc15.into_pull_down_input(&mut gpioc.crh), }; @@ -166,12 +166,15 @@ fn main() -> ! { // if report != last { match consumer.device().write_report(&report) { Err(UsbHidError::WouldBlock) => {} + Err(UsbHidError::UsbError(usb_device::UsbError::BufferOverflow)) => { + core::panic!("Failed to write consumer report, report is too big: {:?}", e) + } Ok(_) => { // last = report; } Err(e) => { // TODO use a suitable pin once we know the pinout - input_pins.pb0.set_high(); // set as indicator that this has happened +// input_pins.pb0.set_high(); // set as indicator that this has happened core::panic!("Failed to write consumer report: {:?}", e) } } @@ -198,12 +201,6 @@ fn main() -> ! { else { input_pins.pb0.set_low(); } - if output.leds & 0x2 == 0x2 { - input_pins.pc3.set_high(); - } - else { - input_pins.pc3.set_low(); - } // Check generic input field // Calibration bit @@ -259,13 +256,13 @@ fn calculate_factor(min: u16, max: u16) -> f32 { // Returns a CustomInputReport from the inputs given fn get_report(pins: &mut MyPins, adc1: &mut adc::Adc, cal: &Calibration) -> CustomInputReport { - let mut values: [u16; 2] = [0; 2]; + let mut values: [u16; 3] = [0; 3]; values[0] = adc1.read(&mut pins.pa1).unwrap(); - values[1] = adc1.read(&mut pins.pa2).unwrap(); + values[1] = adc1.read(&mut pins.pc3).unwrap(); let mut buttons: u16 = 0; // Axis - let mut values_norm: [u16; 2] = [0; 2]; + let mut values_norm: [u16; 3] = [0; 3]; let mut i = 0; loop { if values[i] < cal.data[i].min {