diff options
author | Brian Cully <bjc@kublai.com> | 2022-10-27 20:09:08 -0400 |
---|---|---|
committer | Brian Cully <bjc@kublai.com> | 2022-10-27 20:09:08 -0400 |
commit | 5200c3cc2c87feca2bcb973e32eaeef9808fe032 (patch) | |
tree | 5f25e049051ac9d4ff7d2fd6c7256460c2ce7466 /src | |
parent | 1316d1fe605d0b67571187dc23ea099dd9612d6c (diff) | |
download | luchie-5200c3cc2c87feca2bcb973e32eaeef9808fe032.tar.gz luchie-5200c3cc2c87feca2bcb973e32eaeef9808fe032.zip |
touchpad now works over spi
Diffstat (limited to 'src')
-rw-r--r-- | src/cirque.rs | 49 | ||||
-rwxr-xr-x | src/main.rs | 10 |
2 files changed, 34 insertions, 25 deletions
diff --git a/src/cirque.rs b/src/cirque.rs index 4a2686b..b19b1f4 100644 --- a/src/cirque.rs +++ b/src/cirque.rs @@ -1,9 +1,6 @@ /// Support for the Cirque Pinnacle 1CA027. use core::cmp::{max, min}; -use embedded_hal::{ - digital::v2::{InputPin, OutputPin}, - spi, -}; +use embedded_hal::{digital::v2::OutputPin, spi}; use nb; use stm32f1xx_hal::rcc::Clocks; @@ -116,41 +113,41 @@ impl TouchData { } pub fn scale_to(&mut self, width: u16, height: u16) { - let width_factor = width / (CLAMP_X_MAX - CLAMP_X_MIN); - let height_factor = height / (CLAMP_Y_MAX - CLAMP_Y_MIN); + let width_factor: f32 = f32::from(width) / f32::from(CLAMP_X_MAX - CLAMP_X_MIN); + let height_factor: f32 = f32::from(height) / f32::from(CLAMP_Y_MAX - CLAMP_Y_MIN); self.clamp(); self.x -= CLAMP_X_MIN; self.y -= CLAMP_Y_MIN; - self.x *= width_factor; - self.y *= height_factor; + assert!(!width_factor.is_nan() && width_factor.is_finite()); + assert!(!height_factor.is_nan() && height_factor.is_finite()); + unsafe { + self.x = (f32::from(self.x) * width_factor).to_int_unchecked(); + self.y = (f32::from(self.y) * height_factor).to_int_unchecked(); + } } } -pub struct Cirque<C, D> +pub struct Cirque<C> where C: OutputPin, - D: InputPin, { cs_pin: C, - dr_pin: D, sysclk_speed: u32, } -impl<C, D> Cirque<C, D> +impl<C> Cirque<C> where C: OutputPin, - D: InputPin, { - pub fn new<S>(cs_pin: C, dr_pin: D, spi: &mut S, clocks: Clocks) -> nb::Result<Self, S::Error> + pub fn new<S>(cs_pin: C, spi: &mut S, clocks: Clocks) -> nb::Result<Self, S::Error> where S: spi::FullDuplex<u8>, { let sysclk_speed = clocks.sysclk().raw(); let mut res = Self { cs_pin, - dr_pin, sysclk_speed, }; res.init(spi)?; @@ -162,10 +159,13 @@ where S: spi::FullDuplex<u8>, { self.cs_pin.set_high().ok(); + + // spin until power on reset is flagged. + // while self.read_flags(spi)? & 0x8 != 0x8 {} self.clear_flags(spi)?; - self.wr(spi, RAPAddress::SysConfig1, 0x02)?; - self.wr(spi, RAPAddress::FeedConfig2, 0x1f)?; + self.wr(spi, RAPAddress::SysConfig1, 0x00)?; + self.wr(spi, RAPAddress::FeedConfig2, 0x1e)?; self.wr(spi, RAPAddress::FeedConfig1, 0x03)?; self.wr(spi, RAPAddress::ZIdle, 0x05)?; @@ -184,12 +184,21 @@ where Ok(()) } + fn read_flags<S>(&mut self, spi: &mut S) -> nb::Result<u8, S::Error> + where + S: spi::FullDuplex<u8>, + { + let mut flags: [u8; 1] = [0xfb; 1]; + self.rd(spi, RAPAddress::Status1, &mut flags)?; + Ok(flags[0]) + } + pub fn poll<S>(&mut self, spi: &mut S) -> nb::Result<TouchData, S::Error> where S: spi::FullDuplex<u8>, { - if self.dr_pin.is_high().unwrap_or(false) { - self.read_coords(spi) + if self.read_flags(spi)? & 0x4 == 0x4 { + Ok(self.read_coords(spi)?) } else { Err(nb::Error::WouldBlock) } @@ -206,7 +215,7 @@ where let x = buf[2] as u16 | ((buf[4] as u16 & 0x0f) << 8); let y = buf[3] as u16 | ((buf[4] as u16 & 0xf0) << 4); let z = buf[5] & 0x3f; - let buttons = buf[0] & 0x3f; + let buttons = 0; let is_pressed = x != 0; assert!(x < MAX_X); diff --git a/src/main.rs b/src/main.rs index 02812bd..1697001 100755 --- a/src/main.rs +++ b/src/main.rs @@ -76,7 +76,8 @@ fn main() -> ! { // pa7 - mosi1 logln!("init trackpad"); - let dr_pin = gpiob.pb0; + // TODO: hook an interrupt up to the dr pin to trigger a poll. + //let dr_pin = gpiob.pb1.into_pull_down_input(&mut gpiob.crl); let sck_pin = gpioa.pa5.into_alternate_push_pull(&mut gpioa.crl); let miso_pin = gpioa.pa6; let mosi_pin = gpioa.pa7.into_alternate_push_pull(&mut gpioa.crl); @@ -86,10 +87,10 @@ fn main() -> ! { (sck_pin, miso_pin, mosi_pin), &mut afio.mapr, spi::MODE_1, - 1_500_000.Hz(), + 1_000_000.Hz(), // pinnacle supports up to 13mhz clocks, ); - let mut cirque = match Cirque::new(cs_pin, dr_pin, &mut spi, clocks) { + let mut cirque = match Cirque::new(cs_pin, &mut spi, clocks) { Ok(c) => c, Err(e) => { logln!("err: {:?}", e); @@ -126,8 +127,7 @@ fn main() -> ! { logln!("luchie started!"); loop { - logln!("."); - + // logln!("."); if let Ok(mut td) = cirque.poll(&mut spi) { td.scale_to(1920, 1080); logln!("td: {:?}", td); |