From 4f5aa708810ba57a436ca848f78dab2474a6c951 Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Sun, 27 Nov 2022 16:03:18 -0500 Subject: event_filter: add filters for converting abs mouse to trackball --- src/bin/luchie.rs | 88 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 47 insertions(+), 41 deletions(-) (limited to 'src/bin') diff --git a/src/bin/luchie.rs b/src/bin/luchie.rs index 2799609..be89fa7 100755 --- a/src/bin/luchie.rs +++ b/src/bin/luchie.rs @@ -3,10 +3,9 @@ //extern crate panic_semihosting; -use core::cmp; - use luchie::{ cirque::Cirque, + event_filter::{AbsToRel, TrackBall}, log, logger, logln, }; @@ -25,13 +24,14 @@ use stm32f1xx_hal::{ }; use usb_device::prelude::*; use usbd_human_interface_device::{ - prelude::*, device::mouse::{WheelMouseInterface, WheelMouseReport}, + prelude::*, }; use usbd_serial::{SerialPort, USB_CLASS_CDC}; #[entry] fn main() -> ! { + let cp = cortex_m::Peripherals::take().unwrap(); let dp = pac::Peripherals::take().unwrap(); let mut flash = dp.FLASH.constrain(); @@ -68,6 +68,14 @@ fn main() -> ! { logln!("🐁 luchie starting…"); + logln!("⏲️ init timer"); + let mut syst = cp.SYST; + syst.set_clock_source(cortex_m::peripheral::syst::SystClkSource::Core); + let syst_reload = 0xffffff; + syst.set_reload(syst_reload); + syst.clear_current(); + syst.enable_counter(); + // cirque spi connections to spi1: // // pb0 - dr @@ -106,8 +114,6 @@ fn main() -> ! { // will not reset your device when you upload new firmware. let mut usb_dp = gpioa.pa12.into_push_pull_output(&mut gpioa.crh); usb_dp.set_low(); - // let mut delay = dp.TIM2.delay_us(&clocks); - // delay.delay_ms(10u8); cortex_m::asm::delay(clocks.sysclk().raw() / 100); let usb = usb::Peripheral { @@ -131,9 +137,9 @@ fn main() -> ! { .device_class(USB_CLASS_CDC) .build(); - while usb_dev.state() != UsbDeviceState::Configured { - usb_dev.poll(&mut [&mut serial, &mut mouse]); - } + // while usb_dev.state() != UsbDeviceState::Configured { + // usb_dev.poll(&mut [&mut serial, &mut mouse]); + // } logln!("💡 init led"); let mut gpiob = dp.GPIOB.split(); @@ -142,11 +148,20 @@ fn main() -> ! { logln!("🎉 luchie started!"); - let mut is_pressed = false; - let mut last_x = 0u16; - let mut last_y = 0u16; + let mut abs_to_rel = AbsToRel::new(); + let mut trackball = TrackBall::new(1); + let ticks_per_microsec = clocks.sysclk().to_MHz(); + let mut last_time = 0; + let mut time_error = 0; loop { - // logln!("."); + let mut time = syst_reload - cortex_m::peripheral::SYST::get_current(); + if syst.has_wrapped() { + time += syst_reload + 1; + } + let elapsed = time.wrapping_sub(last_time) + time_error; + time_error = elapsed % ticks_per_microsec; + let elapsed = elapsed / ticks_per_microsec; + last_time = time & syst_reload; usb_dev.poll(&mut [&mut serial, &mut mouse]); let mut buf = [0u8; 64]; @@ -176,38 +191,29 @@ fn main() -> ! { _ => {} } - if let Ok(td) = cirque.poll(&mut spi) { - logln!("td: {:?}", td); - if td.is_pressed { - if is_pressed { - /* - * The trackpad's actual valid return values are - * only about 2^11, so overflow isn't possible - * when converting to signed 16 bit integers. - */ - let s_x: i16 = td.x as i16 - last_x as i16; - let s_y: i16 = td.y as i16 - last_y as i16; - - // Clamp to i8 range. - let raw_x = cmp::max(i8::MIN as i16, cmp::min(i8::MAX as i16, s_x)); - let raw_y = cmp::max(i8::MIN as i16, cmp::min(i8::MAX as i16, s_y)); - mouse_report.x = raw_x as i8; - mouse_report.y = raw_y as i8; - } - is_pressed = true; - last_x = td.x; - last_y = td.y; - } else { - mouse_report.x = 0; - mouse_report.y = 0; - is_pressed = false; - } + // TODO: get current µs for passing in to event filters. + let rel_p = cirque + .poll(&mut spi) + .ok() + .map(|td| { + //logln!("td: {:?}", td); + abs_to_rel.update(&td) + }) + .flatten(); + if let Some(p) = rel_p { + logln!("rel_p: {:?}", p); + } + let p = trackball.update(rel_p, elapsed); + if p.0 != 0 || p.1 != 0 { + mouse_report.x = p.0; + mouse_report.y = p.1; + logln!("p: {:?}", p); match mouse.interface().write_report(&mouse_report) { - Err(UsbHidError::WouldBlock) => {}, + Err(UsbHidError::WouldBlock) => {} Err(e) => { panic!("couldn't write mouse report: {:?}", e) - }, - _ => {}, + } + _ => {} } } } -- cgit v1.2.3