From d7bdcb1a3f7725820c919fe30d88ecea9c060607 Mon Sep 17 00:00:00 2001 From: fly Date: Fri, 12 Apr 2024 01:02:59 +0200 Subject: [PATCH] Make LEDs work with full resolution Signed-off-by: fly --- firmware/src/device.rs | 60 +++++++++++++++++++++--------------------- firmware/src/main.rs | 28 ++++++++++---------- 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/firmware/src/device.rs b/firmware/src/device.rs index 5fe86a5..cb1e7b5 100644 --- a/firmware/src/device.rs +++ b/firmware/src/device.rs @@ -16,33 +16,33 @@ use usbd_human_interface_device::{ // Generated using Waratah #[rustfmt::skip] pub const CUSTOM_DESCRIPTOR: &[u8] = &[ - 0x05, 0x01, // UsagePage(Generic Desktop[0x0001]) - 0x09, 0x04, // UsageId(Joystick[0x0004]) - 0xA1, 0x01, // Collection(Application) - 0x09, 0x01, // UsageId(Pointer[0x0001]) - 0xA1, 0x00, // Collection(Physical) - 0x09, 0x30, // UsageId(X[0x0030]) - 0x09, 0x32, // UsageId(Z[0x0032]) - 0x15, 0x00, // LogicalMinimum(0) - 0x26, 0xFF, 0x0F, // LogicalMaximum(4,095) - 0x95, 0x02, // ReportCount(2) - 0x75, 0x10, // ReportSize(16) - 0x81, 0x02, // Input(Data, Variable, Absolute, NoWrap, Linear, PreferredState, NoNullPosition, BitField) - 0xC0, // EndCollection() - 0x05, 0x09, // UsagePage(Button[0x0009]) - 0x19, 0x01, // UsageIdMin(Button 1[0x0001]) - 0x29, 0x10, // UsageIdMax(Button 16[0x0010]) - 0x25, 0x01, // LogicalMaximum(1) - 0x95, 0x10, // ReportCount(16) - 0x75, 0x01, // ReportSize(1) - 0x81, 0x02, // Input(Data, Variable, Absolute, NoWrap, Linear, PreferredState, NoNullPosition, BitField) - 0x05, 0x0E, // UsagePage(Haptics[0x000E]) - 0x09, 0x21, // UsageId(Manual Trigger[0x0021]) - 0x26, 0xFF, 0x00, // LogicalMaximum(255) - 0x95, 0x01, // ReportCount(1) - 0x75, 0x08, // ReportSize(8) - 0x91, 0x02, // Output(Data, Variable, Absolute, NoWrap, Linear, PreferredState, NoNullPosition, NonVolatile, BitField) - 0xC0, // EndCollection() + 0x05, 0x01, // UsagePage(Generic Desktop[0x0001]) + 0x09, 0x04, // UsageId(Joystick[0x0004]) + 0xA1, 0x01, // Collection(Application) + 0x09, 0x01, // UsageId(Pointer[0x0001]) + 0xA1, 0x00, // Collection(Physical) + 0x09, 0x30, // UsageId(X[0x0030]) + 0x09, 0x32, // UsageId(Z[0x0032]) + 0x15, 0x00, // LogicalMinimum(0) + 0x26, 0xFF, 0x0F, // LogicalMaximum(4,095) + 0x95, 0x02, // ReportCount(2) + 0x75, 0x10, // ReportSize(16) + 0x81, 0x02, // Input(Data, Variable, Absolute, NoWrap, Linear, PreferredState, NoNullPosition, BitField) + 0xC0, // EndCollection() + 0x05, 0x09, // UsagePage(Button[0x0009]) + 0x19, 0x01, // UsageIdMin(Button 1[0x0001]) + 0x29, 0x10, // UsageIdMax(Button 16[0x0010]) + 0x25, 0x01, // LogicalMaximum(1) + 0x95, 0x10, // ReportCount(16) + 0x75, 0x01, // ReportSize(1) + 0x81, 0x02, // Input(Data, Variable, Absolute, NoWrap, Linear, PreferredState, NoNullPosition, BitField) + 0x05, 0x0E, // UsagePage(Haptics[0x000E]) + 0x09, 0x21, // UsageId(Manual Trigger[0x0021]) + 0x27, 0x80, 0xBB, 0x00, 0x00, // LogicalMaximum(48,000) + 0x95, 0x01, // ReportCount(1) + 0x75, 0x10, // ReportSize(16) + 0x91, 0x02, // Output(Data, Variable, Absolute, NoWrap, Linear, PreferredState, NoNullPosition, NonVolatile, BitField) + 0xC0, // EndCollection() ]; #[derive(Clone, Copy, Debug, Eq, PartialEq, Default, PackedStruct)] @@ -57,10 +57,10 @@ pub struct CustomInputReport { } #[derive(Clone, Copy, Debug, Eq, PartialEq, Default, PackedStruct)] -#[packed_struct(endian = "lsb", size_bytes = "1")] +#[packed_struct(endian = "lsb", size_bytes = "2")] pub struct CustomOutputReport { #[packed_field] - pub int: u8, + pub int: u16, } pub struct CustomDevice<'a, B: UsbBus> { @@ -77,7 +77,7 @@ impl<'a, B: UsbBus> CustomDevice<'a, B> { } pub fn read_report(&mut self) -> Result { - let mut data = [0]; + let mut data = [0, 0]; self.interface .read_report(&mut data[..]) .map(|_| CustomOutputReport::unpack(&data).unwrap()) diff --git a/firmware/src/main.rs b/firmware/src/main.rs index b12f4dd..07948d7 100644 --- a/firmware/src/main.rs +++ b/firmware/src/main.rs @@ -6,8 +6,6 @@ mod device; use panic_halt as _; -//use nb::block; - use cortex_m::asm::delay; use cortex_m_rt::entry; use stm32f1xx_hal::{ @@ -83,7 +81,6 @@ fn main() -> ! { }; let mut last = get_report(&mut input_pins, &mut adc1); - let mut last_output = CustomOutputReport { int: 0 }; // ====================== PWM setup ================= let mut afio = p.AFIO.constrain(); @@ -94,6 +91,7 @@ fn main() -> ! { pwm.enable(Channel::C1); let pwm_max = pwm.get_max_duty() as u16; //48000 in our case + // ====================== Main loop ================= loop { let report = get_report(&mut input_pins, &mut adc1); if report != last { @@ -112,7 +110,13 @@ fn main() -> ! { match consumer.device().read_report() { Err(UsbHidError::WouldBlock) => {} Ok(output) => { - let pwm_val = output.int as u16 * 188; + let pwm_val: u16; + if output.int > pwm_max { + pwm_val = pwm_max; + } + else { + pwm_val = output.int; + } pwm.set_duty(Channel::C1, pwm_val); } Err(e) => { @@ -121,23 +125,19 @@ fn main() -> ! { } } - // Get data from pots -// let data: u16 = adc1.read(&mut input_pins.pa2).unwrap(); -// let mut pwm_val = data as f32 / 0xfff as f32; -// let pwm_duty = (pwm_max as f32 * pwm_val as f32) as u16; -// pwm.set_duty(Channel::C1, pwm_duty); - } } + +// Returns a CustomInputReport from the inputs given fn get_report(pins: &mut MyPins, adc1: &mut adc::Adc) -> CustomInputReport { - let integLT: u16 = adc1.read(&mut pins.pa1).unwrap(); - let floodLT: u16 = adc1.read(&mut pins.pa2).unwrap(); + let integ_lt: u16 = adc1.read(&mut pins.pa1).unwrap(); + let flood_lt: u16 = adc1.read(&mut pins.pa2).unwrap(); let buttons: u16 = 0; CustomInputReport { - x: integLT.into(), - y: floodLT.into(), + x: integ_lt.into(), + y: flood_lt.into(), buttons, } }