1use std::fmt;
2use std::num::NonZeroU64;
3use std::time::Duration;
4
5#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
10pub struct Timestamp(Duration);
11
12impl Timestamp {
13 pub const ZERO: Self = Self(Duration::ZERO);
14
15 pub const fn from_duration(value: Duration) -> Self {
16 Self(value)
17 }
18
19 pub const fn from_millis(value: u64) -> Self {
20 Self(Duration::from_millis(value))
21 }
22
23 pub const fn from_micros(value: u64) -> Self {
24 Self(Duration::from_micros(value))
25 }
26
27 pub const fn as_duration(self) -> Duration {
28 self.0
29 }
30
31 pub fn checked_add(self, duration: Duration) -> Option<Self> {
32 self.0.checked_add(duration).map(Self)
33 }
34
35 pub fn saturating_add(self, duration: Duration) -> Self {
36 Self(self.0.saturating_add(duration))
37 }
38
39 pub fn checked_duration_since(self, earlier: Self) -> Option<Duration> {
40 self.0.checked_sub(earlier.0)
41 }
42}
43
44impl fmt::Display for Timestamp {
45 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46 write!(f, "{:?}", self.0)
47 }
48}
49
50#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
52pub struct KeyId(pub u16);
53
54impl fmt::Display for KeyId {
55 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56 write!(f, "key:{}", self.0)
57 }
58}
59
60#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
62pub struct ChordId(pub u16);
63
64impl fmt::Display for ChordId {
65 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66 write!(f, "chord:{}", self.0)
67 }
68}
69
70#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
71pub enum ButtonPhase {
72 Down,
73 Up,
74}
75
76#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
77pub enum RawLevel {
78 Low,
79 High,
80}
81
82impl From<bool> for RawLevel {
83 fn from(high: bool) -> Self {
84 if high {
85 Self::High
86 } else {
87 Self::Low
88 }
89 }
90}
91
92#[derive(Debug, Clone, Copy)]
94pub struct ButtonSnapshot<'a> {
95 pub at: Timestamp,
96 pub levels: &'a [RawLevel],
97}
98
99impl ButtonPhase {
100 pub const fn is_down(self) -> bool {
101 matches!(self, Self::Down)
102 }
103}
104
105#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
107pub struct ButtonEdge {
108 pub key: KeyId,
109 pub phase: ButtonPhase,
110 pub occurred_at: Timestamp,
112 pub recognized_at: Timestamp,
114}
115
116#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
118pub enum ControlId {
119 Key(KeyId),
120 Chord(ChordId),
121}
122
123#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
125pub struct ControlEdge {
126 pub control: ControlId,
127 pub phase: ButtonPhase,
128 pub occurred_at: Timestamp,
130 pub recognized_at: Timestamp,
132}
133
134#[derive(Debug, Clone, PartialEq, Eq, Hash)]
135pub enum GestureKind {
136 Tap { count: u8 },
138 HoldStart { threshold: Duration },
140 HoldMilestone { threshold: Duration },
142 Repeat { count: NonZeroU64 },
144 HoldEnd { held_for: Duration },
146}
147
148#[derive(Debug, Clone, PartialEq, Eq, Hash)]
149pub enum ButtonEventKind {
150 State {
151 control: ControlId,
152 phase: ButtonPhase,
153 },
154 Gesture {
155 control: ControlId,
156 kind: GestureKind,
157 },
158}
159
160#[derive(Debug, Clone, PartialEq, Eq, Hash)]
162pub struct ButtonEvent {
163 pub occurred_at: Timestamp,
165 pub recognized_at: Timestamp,
167 pub kind: ButtonEventKind,
168}
169
170impl ButtonEvent {
171 pub const fn state(edge: ControlEdge) -> Self {
172 Self {
173 occurred_at: edge.occurred_at,
174 recognized_at: edge.recognized_at,
175 kind: ButtonEventKind::State {
176 control: edge.control,
177 phase: edge.phase,
178 },
179 }
180 }
181
182 pub fn gesture(
183 control: ControlId,
184 kind: GestureKind,
185 occurred_at: Timestamp,
186 recognized_at: Timestamp,
187 ) -> Self {
188 Self {
189 occurred_at,
190 recognized_at,
191 kind: ButtonEventKind::Gesture { control, kind },
192 }
193 }
194}
195
196#[derive(Debug, Clone, Default, PartialEq, Eq)]
198pub struct EventBatch {
199 pub processed_at: Timestamp,
201 pub physical: Vec<ButtonEdge>,
203 pub events: Vec<ButtonEvent>,
205 pub next_wakeup: Option<Timestamp>,
207}
208
209impl EventBatch {
210 pub fn new(processed_at: Timestamp) -> Self {
211 Self {
212 processed_at,
213 physical: Vec::new(),
214 events: Vec::new(),
215 next_wakeup: None,
216 }
217 }
218
219 pub fn is_empty(&self) -> bool {
220 self.physical.is_empty() && self.events.is_empty()
221 }
222}