From 5b9c43405064c163a116a5d6e88e8e29f79848b1 Mon Sep 17 00:00:00 2001 From: Ken Sedgwick Date: Thu, 20 Oct 2022 11:06:05 -0700 Subject: [PATCH 1/2] Add {get,set}_u8_reg utilities --- src/lib.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 9417ab1..8424253 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -209,7 +209,7 @@ where impl Ft6X06 where - I2C: i2c::WriteRead + i2c::Write, + I2C: i2c::WriteRead + i2c::Write, E: core::fmt::Debug { /// Creates a new sensor associated with an I2C peripheral. /// @@ -274,6 +274,20 @@ where Ok(value == 0) } + /// Get the value of an 8 bit register + pub fn get_u8_reg(&self, i2c: &mut I2C, reg: u8) -> Result { + let mut ibuf: [u8; 1] = [0]; + i2c.write_read(self.addr, &[reg], &mut ibuf)?; + Ok(ibuf[0]) + } + + /// Set the value of an 8 bit register + pub fn set_u8_reg(&self, i2c: &mut I2C, reg: u8, val: u8) -> Result<(), E> { + let obuf: [u8; 2] = [reg, val]; + i2c.write(self.addr, &obuf)?; + Ok(()) + } + /// Run an internal calibration on the FT6X06 pub fn ts_calibration( &mut self, From 6ed60f859bb09e9d86da8d2b255d002b90c10fe7 Mon Sep 17 00:00:00 2001 From: Ken Sedgwick Date: Thu, 20 Oct 2022 13:30:33 -0700 Subject: [PATCH 2/2] Add TouchInterruptPin and wait_touch_interrupt --- src/lib.rs | 62 +++++++++++++++++++----------------------------------- 1 file changed, 22 insertions(+), 40 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8424253..b662f9e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -56,7 +56,6 @@ use core::marker::PhantomData; use embedded_hal as hal; use hal::blocking::{delay::{DelayMs, DelayUs}, i2c}; use hal::digital::v2::OutputPin; -use rtt_target::rprintln; #[derive(Copy, Clone, Debug)] pub struct Ft6x06Capabilities { @@ -181,9 +180,10 @@ pub struct GestureInit { /// FT6x06 driver object. /// I2C bus type and its address are set. -pub struct Ft6X06 { +pub struct Ft6X06 { i2c: PhantomData, addr: u8, + interrupt: TouchInterruptPin, } /// Perform a long hard reset, the FT66206 needs at least 5mS ... @@ -207,17 +207,18 @@ where Ok(()) } -impl Ft6X06 +impl Ft6X06 where I2C: i2c::WriteRead + i2c::Write, E: core::fmt::Debug { /// Creates a new sensor associated with an I2C peripheral. /// /// Phantom I2C ensures that whatever I2C bus the device was created on is the one that is used for all future interations. - pub fn new(_i2c: &I2C, addr: u8) -> Result { + pub fn new(_i2c: &I2C, addr: u8, interrupt: TouchInterruptPin) -> Result { let ft6x06 = Ft6X06 { i2c: PhantomData, addr: addr, + interrupt, }; Ok(ft6x06) } @@ -288,6 +289,11 @@ where Ok(()) } + /// Wait for the touchscreen interrupt to indicate touches + pub fn wait_touch_interrupt(&self) { + while self.interrupt.is_high().unwrap_or_else(|_| panic!("trouble checking interrupt")) {} + } + /// Run an internal calibration on the FT6X06 pub fn ts_calibration( &mut self, @@ -342,17 +348,13 @@ where } /// Is the device being touched? If so, how many fingers? - pub fn detect_touch(&mut self, i2c: &mut I2C) -> Result { - match self.td_status(i2c) { - Err(_e) => Err("Error getting touch data"), - Ok(n) => { - if n <= FT6X06_MAX_NB_TOUCH as u8 { - Ok(n) - } else { - Ok(0) - } - } - } + pub fn detect_touch(&mut self, i2c: &mut I2C) -> Result { + let ntouch = loop { + let n = self.td_status(i2c)?; + if n > 0 { break n; } + }; + assert!(ntouch <= FT6X06_MAX_NB_TOUCH as u8); + Ok(ntouch) } /// Retrieve the FT6X06 firmware id @@ -448,31 +450,11 @@ where Ok(g) } - pub fn get_coordinates(&mut self, i2c: &mut I2C) -> Result<(u16, u16), &str> { - let mut t = self.detect_touch(i2c); - while t.unwrap() == 0 || t == Err("Error getting touch data") { - t = self.detect_touch(i2c); - } - - let num: u8; - match t { - Err(_e) => return Err("Error {} from fetching number of touches"), - Ok(n) => { - num = n; - if num != 0 { - rprintln!("Number of touches in get_coordinates: {}", num); - }; - if num > 0 { - let t = self.get_touch(i2c, 1); - return match t { - Err(_e) => Err("Error fetching touch data"), - Ok(n) => Ok((n.x, n.y)), - }; - } else { - return Err("no"); - } - } - } + pub fn get_coordinates(&mut self, i2c: &mut I2C) -> Result<(u16, u16), E> { + self.wait_touch_interrupt(); + let _ntouch = self.detect_touch(i2c)?; + let pt = self.get_touch(i2c, 1)?; + Ok((pt.x, pt.y)) } // /// Logic for getting the gesture.