1
0
Fork 0

Make LEDs work with full resolution

Signed-off-by: fly <merspieler@alwaysdata.com>
This commit is contained in:
fly 2024-04-12 01:02:59 +02:00
parent 43bccaf380
commit d7bdcb1a3f
2 changed files with 44 additions and 44 deletions

View file

@ -38,9 +38,9 @@ pub const CUSTOM_DESCRIPTOR: &[u8] = &[
0x81, 0x02, // Input(Data, Variable, Absolute, NoWrap, Linear, PreferredState, NoNullPosition, BitField) 0x81, 0x02, // Input(Data, Variable, Absolute, NoWrap, Linear, PreferredState, NoNullPosition, BitField)
0x05, 0x0E, // UsagePage(Haptics[0x000E]) 0x05, 0x0E, // UsagePage(Haptics[0x000E])
0x09, 0x21, // UsageId(Manual Trigger[0x0021]) 0x09, 0x21, // UsageId(Manual Trigger[0x0021])
0x26, 0xFF, 0x00, // LogicalMaximum(255) 0x27, 0x80, 0xBB, 0x00, 0x00, // LogicalMaximum(48,000)
0x95, 0x01, // ReportCount(1) 0x95, 0x01, // ReportCount(1)
0x75, 0x08, // ReportSize(8) 0x75, 0x10, // ReportSize(16)
0x91, 0x02, // Output(Data, Variable, Absolute, NoWrap, Linear, PreferredState, NoNullPosition, NonVolatile, BitField) 0x91, 0x02, // Output(Data, Variable, Absolute, NoWrap, Linear, PreferredState, NoNullPosition, NonVolatile, BitField)
0xC0, // EndCollection() 0xC0, // EndCollection()
]; ];
@ -57,10 +57,10 @@ pub struct CustomInputReport {
} }
#[derive(Clone, Copy, Debug, Eq, PartialEq, Default, PackedStruct)] #[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 { pub struct CustomOutputReport {
#[packed_field] #[packed_field]
pub int: u8, pub int: u16,
} }
pub struct CustomDevice<'a, B: UsbBus> { pub struct CustomDevice<'a, B: UsbBus> {
@ -77,7 +77,7 @@ impl<'a, B: UsbBus> CustomDevice<'a, B> {
} }
pub fn read_report(&mut self) -> Result<CustomOutputReport, UsbHidError> { pub fn read_report(&mut self) -> Result<CustomOutputReport, UsbHidError> {
let mut data = [0]; let mut data = [0, 0];
self.interface self.interface
.read_report(&mut data[..]) .read_report(&mut data[..])
.map(|_| CustomOutputReport::unpack(&data).unwrap()) .map(|_| CustomOutputReport::unpack(&data).unwrap())

View file

@ -6,8 +6,6 @@ mod device;
use panic_halt as _; use panic_halt as _;
//use nb::block;
use cortex_m::asm::delay; use cortex_m::asm::delay;
use cortex_m_rt::entry; use cortex_m_rt::entry;
use stm32f1xx_hal::{ use stm32f1xx_hal::{
@ -83,7 +81,6 @@ fn main() -> ! {
}; };
let mut last = get_report(&mut input_pins, &mut adc1); let mut last = get_report(&mut input_pins, &mut adc1);
let mut last_output = CustomOutputReport { int: 0 };
// ====================== PWM setup ================= // ====================== PWM setup =================
let mut afio = p.AFIO.constrain(); let mut afio = p.AFIO.constrain();
@ -94,6 +91,7 @@ fn main() -> ! {
pwm.enable(Channel::C1); pwm.enable(Channel::C1);
let pwm_max = pwm.get_max_duty() as u16; //48000 in our case let pwm_max = pwm.get_max_duty() as u16; //48000 in our case
// ====================== Main loop =================
loop { loop {
let report = get_report(&mut input_pins, &mut adc1); let report = get_report(&mut input_pins, &mut adc1);
if report != last { if report != last {
@ -112,7 +110,13 @@ fn main() -> ! {
match consumer.device().read_report() { match consumer.device().read_report() {
Err(UsbHidError::WouldBlock) => {} Err(UsbHidError::WouldBlock) => {}
Ok(output) => { 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); pwm.set_duty(Channel::C1, pwm_val);
} }
Err(e) => { 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<pac::ADC1>) -> CustomInputReport { fn get_report(pins: &mut MyPins, adc1: &mut adc::Adc<pac::ADC1>) -> CustomInputReport {
let integLT: u16 = adc1.read(&mut pins.pa1).unwrap(); let integ_lt: u16 = adc1.read(&mut pins.pa1).unwrap();
let floodLT: u16 = adc1.read(&mut pins.pa2).unwrap(); let flood_lt: u16 = adc1.read(&mut pins.pa2).unwrap();
let buttons: u16 = 0; let buttons: u16 = 0;
CustomInputReport { CustomInputReport {
x: integLT.into(), x: integ_lt.into(),
y: floodLT.into(), y: flood_lt.into(),
buttons, buttons,
} }
} }