diff options
Diffstat (limited to 'src/cirque.rs')
-rw-r--r-- | src/cirque.rs | 49 |
1 files changed, 29 insertions, 20 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); |