kernel/hil/touch.rs
1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5//! Interface for touch input devices
6
7use crate::ErrorCode;
8
9/// Touch Event Status
10#[derive(Debug, Copy, Clone)]
11pub enum TouchStatus {
12 Unstarted,
13 Pressed,
14 Released,
15 Moved,
16}
17
18/// Gesture event
19#[derive(Debug, Copy, Clone)]
20pub enum GestureEvent {
21 SwipeUp,
22 SwipeDown,
23 SwipeLeft,
24 SwipeRight,
25 ZoomIn,
26 ZoomOut,
27}
28
29/// A single touch event's data
30#[derive(Copy, Clone)]
31pub struct TouchEvent {
32 pub status: TouchStatus,
33 /// touch (x, y) position
34 pub x: u16,
35 pub y: u16,
36
37 /// Numeric ID assigned to this touch. This ID allows the client to
38 /// to match different `TouchEvent`s to the same physical touch.
39 ///
40 /// The driver must assign a unique ID to each touch.
41 pub id: usize,
42
43 /// Optional scaled value for the size of the touch. A larger value
44 /// corresponds to a "fatter" touch. The size values range from 0
45 /// to 65535.
46 ///
47 /// If a touchscreen does not provide information about the size of the touch,
48 /// this must be set to `None`.
49 pub size: Option<u16>,
50
51 /// Optional scaled value for the pressure of the touch. A larger value
52 /// corresponds to a "firmer" press. The pressure values range from 0
53 /// to 65536.
54 ///
55 /// If a touchscreen does not provide information about the pressure of a touch,
56 /// this must be set to `None`.
57 pub pressure: Option<u16>,
58}
59
60/// Single touch panels should implement this
61pub trait Touch<'a> {
62 /// Enable the touch panel
63 ///
64 /// returns Ok(()) even if device is already enabled
65 fn enable(&self) -> Result<(), ErrorCode>;
66
67 /// Disable the touch panel
68 ///
69 /// returns Ok(()) even if device is already disabled
70 fn disable(&self) -> Result<(), ErrorCode>;
71
72 /// Set the touch client
73 fn set_client(&self, touch_client: &'a dyn TouchClient);
74}
75
76/// Multi-touch panels should implement this
77pub trait MultiTouch<'a> {
78 /// Enable the touche panel
79 ///
80 /// returns Ok(()) even if device is already enabled
81 fn enable(&self) -> Result<(), ErrorCode>;
82
83 /// Disable the touch panel
84 ///
85 /// returns Ok(()) even if device is already disabled
86 fn disable(&self) -> Result<(), ErrorCode>;
87
88 /// Returns the number of maximum concurently supported touches.
89 fn get_num_touches(&self) -> usize;
90
91 /// Returns the touch event at index or `None`.
92 ///
93 /// This function must be called in the same interrupt
94 /// as the event, otherwise data might not be available.
95 fn get_touch(&self, index: usize) -> Option<TouchEvent>;
96
97 /// Set the multi-touch client
98 fn set_client(&self, multi_touch_client: &'a dyn MultiTouchClient);
99}
100
101/// The single touch client
102pub trait TouchClient {
103 /// Report a touch event
104 fn touch_event(&self, touch_event: TouchEvent);
105}
106
107/// The multi touch client
108pub trait MultiTouchClient {
109 /// Report a multi touch event
110 /// num touches represents the number of touches detected
111 fn touch_events(&self, touch_events: &[TouchEvent], len: usize);
112}
113
114/// Touch panels that support gestures
115pub trait Gesture<'a> {
116 /// Set the gesture client
117 fn set_client(&self, gesture_client: &'a dyn GestureClient);
118}
119
120/// The gesture client
121pub trait GestureClient {
122 fn gesture_event(&self, gesture_event: GestureEvent);
123}