1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! Interface for touch input devices

use crate::ErrorCode;

/// Touch Event Status
#[derive(Debug, Copy, Clone)]
pub enum TouchStatus {
    Unstarted,
    Pressed,
    Released,
    Moved,
}

/// Gesture event
#[derive(Debug, Copy, Clone)]
pub enum GestureEvent {
    SwipeUp,
    SwipeDown,
    SwipeLeft,
    SwipeRight,
    ZoomIn,
    ZoomOut,
}

/// A single touch event's data
#[derive(Copy, Clone)]
pub struct TouchEvent {
    pub status: TouchStatus,
    /// touch (x, y) position
    pub x: u16,
    pub y: u16,

    /// Numeric ID assigned to this touch. This ID allows the client to
    /// to match different `TouchEvent`s to the same physical touch.
    ///
    /// The driver must assign a unique ID to each touch.
    pub id: usize,

    /// Optional scaled value for the size of the touch. A larger value
    /// corresponds to a "fatter" touch. The size values range from 0
    /// to 65535.
    ///
    /// If a touchscreen does not provide information about the size of the touch,
    /// this must be set to `None`.
    pub size: Option<u16>,

    /// Optional scaled value for the pressure of the touch. A larger value
    /// corresponds to a "firmer" press. The pressure values range from 0
    /// to 65536.
    ///
    /// If a touchscreen does not provide information about the pressure of a touch,
    /// this must be set to `None`.
    pub pressure: Option<u16>,
}

/// Single touch panels should implement this
pub trait Touch<'a> {
    /// Enable the touch panel
    ///
    /// returns Ok(()) even if device is already enabled
    fn enable(&self) -> Result<(), ErrorCode>;

    /// Disable the touch panel
    ///
    /// returns Ok(()) even if device is already disabled
    fn disable(&self) -> Result<(), ErrorCode>;

    /// Set the touch client
    fn set_client(&self, touch_client: &'a dyn TouchClient);
}

/// Multi-touch panels should implement this
pub trait MultiTouch<'a> {
    /// Enable the touche panel
    ///
    /// returns Ok(()) even if device is already enabled
    fn enable(&self) -> Result<(), ErrorCode>;

    /// Disable the touch panel
    ///
    /// returns Ok(()) even if device is already disabled
    fn disable(&self) -> Result<(), ErrorCode>;

    /// Returns the number of maximum concurently supported touches.
    fn get_num_touches(&self) -> usize;

    /// Returns the touch event at index or `None`.
    ///
    /// This function must be called in the same interrupt
    /// as the event, otherwise data might not be available.
    fn get_touch(&self, index: usize) -> Option<TouchEvent>;

    /// Set the multi-touch client
    fn set_client(&self, multi_touch_client: &'a dyn MultiTouchClient);
}

/// The single touch client
pub trait TouchClient {
    /// Report a touch event
    fn touch_event(&self, touch_event: TouchEvent);
}

/// The multi touch client
pub trait MultiTouchClient {
    /// Report a multi touch event
    /// num touches represents the number of touches detected
    fn touch_events(&self, touch_events: &[TouchEvent], len: usize);
}

/// Touch panels that support gestures
pub trait Gesture<'a> {
    /// Set the gesture client
    fn set_client(&self, gesture_client: &'a dyn GestureClient);
}

/// The gesture client
pub trait GestureClient {
    fn gesture_event(&self, gesture_event: GestureEvent);
}