Skip to content

Commit

Permalink
Switch to enums for gesture types
Browse files Browse the repository at this point in the history
  • Loading branch information
tstellanova committed Apr 10, 2020
1 parent 189f60c commit 1bd5dd0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cst816s"
version = "0.1.2"
version = "0.1.3"
authors = ["Todd Stellanova <[email protected]>"]
edition = "2018"
description = "CST816S touchscreen driver for embedded hal / no_std"
Expand Down
27 changes: 12 additions & 15 deletions examples/touchpad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ use p_hal::nrf52832_pac as pac;
use p_hal::{delay::Delay, rng::RngExt, spim, twim};

use cortex_m_rt as rt;
use cst816s::{
TouchEvent, CST816S, GESTURE_LONG_PRESS, GESTURE_SINGLE_CLICK, GESTURE_SLIDE_DOWN,
GESTURE_SLIDE_LEFT, GESTURE_SLIDE_RIGHT, GESTURE_SLIDE_UP,
};
use cst816s::{TouchEvent, TouchGesture, CST816S};
use embedded_graphics::pixelcolor::{raw::RawU16, Rgb565};
use embedded_graphics::{prelude::*, primitives::*, style::*};
use embedded_hal::digital::v2::OutputPin;
Expand Down Expand Up @@ -116,22 +113,22 @@ fn main() -> ! {

if let Some(evt) = touchpad.read_one_touch_event(true) {
refresh_count += 1;
if refresh_count > 100 {
draw_background(&mut display);
refresh_count = 0;
}

draw_marker(&mut display, &evt, rand_color);
let vibe_time = match evt.gesture {
cst816s::GESTURE_LONG_PRESS => {
refresh_count = 100;
TouchGesture::LongPress => {
refresh_count = 1000;
50_000
}
cst816s::GESTURE_SINGLE_CLICK => 5_000,
TouchGesture::SingleClick => 5_000,
_ => 0,
};

pulse_vibe(&mut vibe, &mut delay_source, vibe_time);
if refresh_count > 40 {
draw_background(&mut display);
refresh_count = 0;
}
} else {
delay_source.delay_us(1u32);
}
Expand Down Expand Up @@ -160,7 +157,7 @@ fn draw_marker(display: &mut impl DrawTarget<Rgb565>, event: &TouchEvent, color:
let y_pos = event.y;

match event.gesture {
GESTURE_SLIDE_LEFT | GESTURE_SLIDE_RIGHT => {
TouchGesture::SlideLeft | TouchGesture::SlideRight => {
Rectangle::new(
Point::new(x_pos - SWIPE_LENGTH, y_pos - SWIPE_WIDTH),
Point::new(x_pos + SWIPE_LENGTH, y_pos + SWIPE_WIDTH),
Expand All @@ -170,7 +167,7 @@ fn draw_marker(display: &mut impl DrawTarget<Rgb565>, event: &TouchEvent, color:
.map_err(|_| ())
.unwrap();
}
GESTURE_SLIDE_UP | GESTURE_SLIDE_DOWN => {
TouchGesture::SlideUp | TouchGesture::SlideDown => {
Rectangle::new(
Point::new(x_pos - SWIPE_WIDTH, y_pos - SWIPE_LENGTH),
Point::new(x_pos + SWIPE_WIDTH, y_pos + SWIPE_LENGTH),
Expand All @@ -180,12 +177,12 @@ fn draw_marker(display: &mut impl DrawTarget<Rgb565>, event: &TouchEvent, color:
.map_err(|_| ())
.unwrap();
}
GESTURE_SINGLE_CLICK => Circle::new(Point::new(x_pos, y_pos), 20)
TouchGesture::SingleClick => Circle::new(Point::new(x_pos, y_pos), 20)
.into_styled(PrimitiveStyle::with_fill(color))
.draw(display)
.map_err(|_| ())
.unwrap(),
GESTURE_LONG_PRESS => {
TouchGesture::LongPress => {
Circle::new(Point::new(x_pos, y_pos), 40)
.into_styled(PrimitiveStyle::with_stroke(color, 4))
.draw(display)
Expand Down
58 changes: 38 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct TouchEvent {
pub x: i32,
pub y: i32,
/// the gesture that this touch is part of
pub gesture: u8,
pub gesture: TouchGesture,
/// 0 down, 1 lift, 2 contact
pub action: u8,
/// identifies the finger that touched (0-9)
Expand Down Expand Up @@ -100,7 +100,7 @@ where
let mut touch = TouchEvent {
x: 0,
y: 0,
gesture: 0,
gesture: TouchGesture::None,
action: 0,
finger_id: 0,
pressure: 0,
Expand Down Expand Up @@ -137,23 +137,22 @@ where
///
pub fn read_one_touch_event(&mut self, check_int_pin: bool) -> Option<TouchEvent> {
let mut one_event: Option<TouchEvent> = None;
// the interrupt pin should be low if there is data available;
// the interrupt pin should typically be low if there is data available;
// otherwise, attempting to read i2c will cause a stall
let data_available = !check_int_pin || self.pin_int.is_low().unwrap_or(false);
if data_available {
if self.read_truncated_registers().is_ok() {
let gesture_id = self.blob_buf[Self::GESTURE_ID_OFF];
let num_points = (self.blob_buf[Self::NUM_POINTS_OFF] & 0x0F) as usize;
if num_points > 1 {
if num_points <= Self::MAX_TOUCH_CHANNELS {
//In testing with a PineTime we only ever seem to get one event
panic!("num_points {}", num_points);
}
let evt_start: usize = Self::GESTURE_HEADER_LEN;
if let Some(mut evt) = Self::touch_event_from_data(
self.blob_buf[evt_start..evt_start + Self::RAW_TOUCH_EVENT_LEN].as_ref(),
) {
evt.gesture = gesture_id;
one_event = Some(evt);
let evt_start: usize = Self::GESTURE_HEADER_LEN;
if let Some(mut evt) = Self::touch_event_from_data(
self.blob_buf[evt_start..evt_start + Self::RAW_TOUCH_EVENT_LEN].as_ref(),
) {
evt.gesture = gesture_id.into();
one_event = Some(evt);
}
}
}
}
Expand Down Expand Up @@ -193,14 +192,33 @@ where
const BLOB_BUF_LEN: usize = (10 * 6) + 3; // (MAX_TOUCH_CHANNELS * RAW_TOUCH_EVENT_LEN) + GESTURE_HEADER_LEN;
const ONE_EVENT_LEN: usize = 6 + 3; // RAW_TOUCH_EVENT_LEN + GESTURE_HEADER_LEN

pub const GESTURE_NONE: u8 = 0x00;
pub const GESTURE_SLIDE_DOWN: u8 = 0x01;
pub const GESTURE_SLIDE_UP: u8 = 0x02;
pub const GESTURE_SLIDE_LEFT: u8 = 0x03;
pub const GESTURE_SLIDE_RIGHT: u8 = 0x04;
pub const GESTURE_SINGLE_CLICK: u8 = 0x05;
pub const GESTURE_DOUBLE_CLICK: u8 = 0x0B;
pub const GESTURE_LONG_PRESS: u8 = 0x0C;
#[derive(Debug)]
#[repr(u8)]
pub enum TouchGesture {
None = 0x00,
SlideDown = 0x01,
SlideUp = 0x02,
SlideLeft = 0x03,
SlideRight = 0x04,
SingleClick = 0x05,
DoubleClick = 0x0B,
LongPress = 0x0C,
}

impl core::convert::From<u8> for TouchGesture {
fn from(val: u8) -> Self {
match val {
0x01 => Self::SlideDown,
0x02 => Self::SlideUp,
0x03 => Self::SlideLeft,
0x04 => Self::SlideRight,
0x05 => Self::SingleClick,
0x0B => Self::DoubleClick,
0x0C => Self::LongPress,
_ => Self::None,
}
}
}

#[cfg(test)]
mod tests {
Expand Down

0 comments on commit 1bd5dd0

Please sign in to comment.