ft6336u_driver/ft6336u/
types.rs

1//! Type definitions for the FT6336U touch controller.
2//!
3//! This module contains enums and structs representing the various
4//! states and data structures used by the touch controller.
5
6/// Device operating mode
7///
8/// The FT6336U can operate in different modes for normal operation or factory testing.
9///
10/// # Examples
11///
12/// ```rust
13/// use ft6336u_driver::DeviceMode;
14///
15/// let mode = DeviceMode::Working;
16/// assert_eq!(mode.to_register(), 0x00);
17///
18/// let parsed = DeviceMode::from_register(0x00).unwrap();
19/// assert_eq!(parsed, DeviceMode::Working);
20/// ```
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22#[repr(u8)]
23pub enum DeviceMode {
24    /// Working mode (normal operation)
25    Working = 0b000,
26    /// Factory mode (calibration/testing)
27    Factory = 0b100,
28}
29
30impl DeviceMode {
31    /// Convert from raw register value
32    pub fn from_register(val: u8) -> Option<Self> {
33        match val & 0b111 {
34            0b000 => Some(Self::Working),
35            0b100 => Some(Self::Factory),
36            _ => None,
37        }
38    }
39
40    /// Convert to register value
41    pub fn to_register(self) -> u8 {
42        (self as u8) << 4
43    }
44}
45
46/// Control mode for power management
47///
48/// Controls whether the device stays in active mode or switches to lower-power monitor mode.
49///
50/// # Examples
51///
52/// ```rust
53/// use ft6336u_driver::CtrlMode;
54///
55/// let mode = CtrlMode::KeepActive;
56/// assert_eq!(CtrlMode::from_register(0).unwrap(), CtrlMode::KeepActive);
57/// assert_eq!(CtrlMode::from_register(1).unwrap(), CtrlMode::SwitchToMonitor);
58/// ```
59#[derive(Debug, Clone, Copy, PartialEq, Eq)]
60#[repr(u8)]
61pub enum CtrlMode {
62    /// Keep the device in active mode
63    KeepActive = 0,
64    /// Switch to monitor mode
65    SwitchToMonitor = 1,
66}
67
68impl CtrlMode {
69    /// Convert from raw register value
70    pub fn from_register(val: u8) -> Option<Self> {
71        match val {
72            0 => Some(Self::KeepActive),
73            1 => Some(Self::SwitchToMonitor),
74            _ => None,
75        }
76    }
77}
78
79/// Gesture mode (interrupt trigger configuration)
80///
81/// Configures whether the device generates interrupts on touch events or requires polling.
82///
83/// # Examples
84///
85/// ```rust
86/// use ft6336u_driver::GestureMode;
87///
88/// let mode = GestureMode::Trigger;
89/// assert_eq!(GestureMode::from_register(1).unwrap(), GestureMode::Trigger);
90/// assert_eq!(GestureMode::from_register(0).unwrap(), GestureMode::Polling);
91/// ```
92#[derive(Debug, Clone, Copy, PartialEq, Eq)]
93#[repr(u8)]
94pub enum GestureMode {
95    /// Polling mode - no interrupts
96    Polling = 0,
97    /// Trigger mode - generate interrupts on touch events
98    Trigger = 1,
99}
100
101impl GestureMode {
102    /// Convert from raw register value
103    pub fn from_register(val: u8) -> Option<Self> {
104        match val {
105            0 => Some(Self::Polling),
106            1 => Some(Self::Trigger),
107            _ => None,
108        }
109    }
110}
111
112/// Touch event status for a single touch point
113///
114/// Indicates whether a touch is new, continuing, or has been released.
115///
116/// # Examples
117///
118/// ```rust
119/// use ft6336u_driver::TouchStatus;
120///
121/// // A new touch starts as Touch, then becomes Stream for continuous contact
122/// let status = TouchStatus::Touch;
123/// ```
124#[derive(Debug, Clone, Copy, PartialEq, Eq)]
125pub enum TouchStatus {
126    /// Initial touch detected
127    Touch,
128    /// Continuous touch (streaming)
129    Stream,
130    /// Touch released
131    Release,
132}
133
134/// A single touch point with coordinates and status
135///
136/// Represents one touch point detected by the FT6336U. The controller can detect
137/// up to 2 simultaneous touch points.
138///
139/// # Examples
140///
141/// ```rust
142/// use ft6336u_driver::{TouchPoint, TouchStatus};
143///
144/// let point = TouchPoint {
145///     status: TouchStatus::Touch,
146///     x: 120,
147///     y: 240,
148/// };
149///
150/// println!("Touch detected at ({}, {})", point.x, point.y);
151/// ```
152#[derive(Debug, Clone, Copy)]
153pub struct TouchPoint {
154    /// Touch status
155    pub status: TouchStatus,
156    /// X coordinate
157    pub x: u16,
158    /// Y coordinate
159    pub y: u16,
160}
161
162impl Default for TouchPoint {
163    fn default() -> Self {
164        Self {
165            status: TouchStatus::Release,
166            x: 0,
167            y: 0,
168        }
169    }
170}
171
172/// Complete touch data including up to 2 touch points
173///
174/// Contains the results of a touch scan, including the number of active touches
175/// and data for each detected touch point.
176///
177/// # Examples
178///
179/// ```rust
180/// use ft6336u_driver::{TouchData, TouchStatus};
181///
182/// let mut data = TouchData::default();
183/// data.touch_count = 1;
184/// data.points[0].status = TouchStatus::Touch;
185/// data.points[0].x = 100;
186/// data.points[0].y = 200;
187///
188/// if data.touch_count > 0 {
189///     println!("Touch at ({}, {})", data.points[0].x, data.points[0].y);
190/// }
191/// ```
192#[derive(Debug, Clone, Copy, Default)]
193pub struct TouchData {
194    /// Number of active touch points (0-2)
195    pub touch_count: u8,
196    /// Touch point data (up to 2 points)
197    pub points: [TouchPoint; 2],
198}