1use num_enum::{FromPrimitive, IntoPrimitive};
4
5use crate::feature::DecodeEvent;
6
7#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, IntoPrimitive, FromPrimitive)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize))]
10#[non_exhaustive]
11#[repr(u8)]
12pub enum RotationState {
13 Inactive = 0,
15 Start = 1,
17 Active = 2,
19 Stop = 3,
21 #[num_enum(catch_all)]
23 Other(u8),
24}
25
26#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, IntoPrimitive, FromPrimitive)]
28#[cfg_attr(feature = "serde", derive(serde::Serialize))]
29#[non_exhaustive]
30#[repr(u8)]
31pub enum ActivityState {
32 Inactive = 0,
34 Start = 1,
36 Active = 2,
38 Stop = 3,
40 #[num_enum(catch_all)]
42 Other(u8),
43}
44
45#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, IntoPrimitive, FromPrimitive)]
47#[cfg_attr(feature = "serde", derive(serde::Serialize))]
48#[non_exhaustive]
49#[repr(u8)]
50pub enum CrownGesture {
51 None = 0,
53 Tap = 1,
55 DoubleTap = 2,
57 #[num_enum(catch_all)]
59 Other(u8),
60}
61
62#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, IntoPrimitive, FromPrimitive)]
64#[cfg_attr(feature = "serde", derive(serde::Serialize))]
65#[non_exhaustive]
66#[repr(u8)]
67pub enum ButtonState {
68 Inactive = 0,
70 Press = 1,
72 ShortPressActive = 2,
74 LongPress = 3,
76 LongPressActive = 4,
78 Release = 5,
80 #[num_enum(catch_all)]
82 Other(u8),
83}
84
85#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
87#[cfg_attr(feature = "serde", derive(serde::Serialize))]
88#[non_exhaustive]
89pub enum CrownEvent {
90 Update(CrownUpdate),
95}
96
97#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
99#[cfg_attr(feature = "serde", derive(serde::Serialize))]
100#[non_exhaustive]
101pub struct CrownUpdate {
102 pub rotation_state: RotationState,
104 pub relative_slot_rotation: i8,
106 pub relative_ratchet_rotation: i8,
108 pub proximity: ActivityState,
110 pub touch: ActivityState,
112 pub gesture: CrownGesture,
114 pub button: ButtonState,
116 pub speed: i16,
118}
119
120pub(super) fn decode_event(sub_id: u8, payload: &[u8; 16]) -> Option<CrownEvent> {
122 match sub_id {
123 0 => Some(CrownEvent::Update(CrownUpdate {
124 rotation_state: RotationState::from(payload[0]),
125 relative_slot_rotation: payload[1].cast_signed(),
126 relative_ratchet_rotation: payload[2].cast_signed(),
127 proximity: ActivityState::from(payload[3]),
128 touch: ActivityState::from(payload[4]),
129 gesture: CrownGesture::from(payload[5]),
130 button: ButtonState::from(payload[6]),
131 speed: i16::from_be_bytes([payload[14], payload[15]]),
132 })),
133 _ => None,
134 }
135}
136
137impl DecodeEvent for CrownEvent {
138 fn decode(sub_id: u8, payload: &[u8; 16]) -> Option<Self> {
139 decode_event(sub_id, payload)
140 }
141}