Working with 3 input axis

Signed-off-by: fly <merspieler@alwaysdata.com>
This commit is contained in:
fly 2024-06-10 00:08:44 +02:00
parent 288456f127
commit 83f234b5f7
2 changed files with 18 additions and 20 deletions

View file

@ -7,7 +7,7 @@ use usbd_human_interface_device::{
descriptor::InterfaceProtocol, descriptor::InterfaceProtocol,
device::DeviceClass, device::DeviceClass,
interface::{ interface::{
InBytes8, Interface, InterfaceBuilder, InterfaceConfig, OutBytes8, ReportSingle, InBytes64, Interface, InterfaceBuilder, InterfaceConfig, OutBytes64, ReportSingle,
UsbAllocatable, UsbAllocatable,
}, },
UsbHidError, UsbHidError,
@ -23,10 +23,11 @@ pub const CUSTOM_DESCRIPTOR: &[u8] = &[
0x09, 0x01, // UsageId(Pointer[0x0001]) 0x09, 0x01, // UsageId(Pointer[0x0001])
0xA1, 0x00, // Collection(Physical) 0xA1, 0x00, // Collection(Physical)
0x09, 0x30, // UsageId(X[0x0030]) 0x09, 0x30, // UsageId(X[0x0030])
0x09, 0x31, // UsageId(Y[0x0031])
0x09, 0x32, // UsageId(Z[0x0032]) 0x09, 0x32, // UsageId(Z[0x0032])
0x15, 0x00, // LogicalMinimum(0) 0x15, 0x00, // LogicalMinimum(0)
0x26, 0xFF, 0x0F, // LogicalMaximum(4,095) 0x26, 0xFF, 0x0F, // LogicalMaximum(4,095)
0x95, 0x02, // ReportCount(2) 0x95, 0x03, // ReportCount(3)
0x75, 0x10, // ReportSize(16) 0x75, 0x10, // ReportSize(16)
0x81, 0x02, // Input(Data, Variable, Absolute, NoWrap, Linear, PreferredState, NoNullPosition, BitField) 0x81, 0x02, // Input(Data, Variable, Absolute, NoWrap, Linear, PreferredState, NoNullPosition, BitField)
0xC0, // EndCollection() 0xC0, // EndCollection()
@ -54,12 +55,12 @@ pub const CUSTOM_DESCRIPTOR: &[u8] = &[
]; ];
#[derive(Clone, Copy, Debug, Eq, PartialEq, Default, PackedStruct)] #[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 { pub struct CustomInputReport {
#[packed_field] #[packed_field]
pub report_id: u8, pub report_id: u8,
#[packed_field] #[packed_field]
pub axis: [u16; 2], pub axis: [u16; 3],
#[packed_field] #[packed_field]
pub buttons: u16, pub buttons: u16,
} }
@ -76,7 +77,7 @@ pub struct CustomOutputReport {
} }
pub struct CustomDevice<'a, B: UsbBus> { 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> { 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> { 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 { fn interface(&mut self) -> &mut Self::I {
&mut self.interface &mut self.interface
@ -112,7 +113,7 @@ impl<'a, B: UsbBus> DeviceClass<'a> for CustomDevice<'a, B> {
} }
pub struct CustomConfig<'a> { pub struct CustomConfig<'a> {
interface: InterfaceConfig<'a, InBytes8, OutBytes8, ReportSingle>, interface: InterfaceConfig<'a, InBytes64, OutBytes64, ReportSingle>,
} }
impl<'a> Default for CustomConfig<'a> { impl<'a> Default for CustomConfig<'a> {
@ -134,7 +135,7 @@ impl<'a> Default for CustomConfig<'a> {
impl<'a> CustomConfig<'a> { impl<'a> CustomConfig<'a> {
#[must_use] #[must_use]
pub fn new(interface: InterfaceConfig<'a, InBytes8, OutBytes8, ReportSingle>) -> Self { pub fn new(interface: InterfaceConfig<'a, InBytes64, OutBytes64, ReportSingle>) -> Self {
Self { interface } Self { interface }
} }
} }

View file

@ -37,7 +37,7 @@ struct MyPins {
pa2: Pin<'A', 2, Analog>, pa2: Pin<'A', 2, Analog>,
pb0: Pin<'B', 0, Output<PushPull>>, pb0: Pin<'B', 0, Output<PushPull>>,
pc1: Pin<'C', 1, Input<PullDown>>, pc1: Pin<'C', 1, Input<PullDown>>,
pc3: Pin<'C', 3, Output<PushPull>>, pc3: Pin<'C', 3, Analog>,
pc15: Pin<'C', 15, Input<PullDown>>, pc15: Pin<'C', 15, Input<PullDown>>,
} }
@ -140,7 +140,7 @@ fn main() -> ! {
pa2: gpioa.pa2.into_analog(&mut gpioa.crl), pa2: gpioa.pa2.into_analog(&mut gpioa.crl),
pb0: gpiob.pb0.into_push_pull_output(&mut gpiob.crl), pb0: gpiob.pb0.into_push_pull_output(&mut gpiob.crl),
pc1: gpioc.pc1.into_pull_down_input(&mut gpioc.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), pc15: gpioc.pc15.into_pull_down_input(&mut gpioc.crh),
}; };
@ -166,12 +166,15 @@ fn main() -> ! {
// if report != last { // if report != last {
match consumer.device().write_report(&report) { match consumer.device().write_report(&report) {
Err(UsbHidError::WouldBlock) => {} Err(UsbHidError::WouldBlock) => {}
Err(UsbHidError::UsbError(usb_device::UsbError::BufferOverflow)) => {
core::panic!("Failed to write consumer report, report is too big: {:?}", e)
}
Ok(_) => { Ok(_) => {
// last = report; // last = report;
} }
Err(e) => { Err(e) => {
// TODO use a suitable pin once we know the pinout // 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) core::panic!("Failed to write consumer report: {:?}", e)
} }
} }
@ -198,12 +201,6 @@ fn main() -> ! {
else { else {
input_pins.pb0.set_low(); 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 // Check generic input field
// Calibration bit // Calibration bit
@ -259,13 +256,13 @@ fn calculate_factor(min: u16, max: u16) -> f32 {
// Returns a CustomInputReport from the inputs given // Returns a CustomInputReport from the inputs given
fn get_report(pins: &mut MyPins, adc1: &mut adc::Adc<pac::ADC1>, cal: &Calibration) -> CustomInputReport { fn get_report(pins: &mut MyPins, adc1: &mut adc::Adc<pac::ADC1>, 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[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; let mut buttons: u16 = 0;
// Axis // Axis
let mut values_norm: [u16; 2] = [0; 2]; let mut values_norm: [u16; 3] = [0; 3];
let mut i = 0; let mut i = 0;
loop { loop {
if values[i] < cal.data[i].min { if values[i] < cal.data[i].min {