hidpp/feature/crown/
event.rs1use num_enum::{IntoPrimitive, TryFromPrimitive};
4
5#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, IntoPrimitive, TryFromPrimitive)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize))]
8#[non_exhaustive]
9#[repr(u8)]
10pub enum RotationState {
11 Inactive = 0,
13 Start = 1,
15 Active = 2,
17 Stop = 3,
19}
20
21#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, IntoPrimitive, TryFromPrimitive)]
23#[cfg_attr(feature = "serde", derive(serde::Serialize))]
24#[non_exhaustive]
25#[repr(u8)]
26pub enum ActivityState {
27 Inactive = 0,
29 Start = 1,
31 Active = 2,
33 Stop = 3,
35}
36
37#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, IntoPrimitive, TryFromPrimitive)]
39#[cfg_attr(feature = "serde", derive(serde::Serialize))]
40#[non_exhaustive]
41#[repr(u8)]
42pub enum CrownGesture {
43 None = 0,
45 Tap = 1,
47 DoubleTap = 2,
49}
50
51#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, IntoPrimitive, TryFromPrimitive)]
53#[cfg_attr(feature = "serde", derive(serde::Serialize))]
54#[non_exhaustive]
55#[repr(u8)]
56pub enum ButtonState {
57 Inactive = 0,
59 Press = 1,
61 ShortPressActive = 2,
63 LongPress = 3,
65 LongPressActive = 4,
67 Release = 5,
69}
70
71#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
73#[cfg_attr(feature = "serde", derive(serde::Serialize))]
74#[non_exhaustive]
75pub enum CrownEvent {
76 Update(CrownUpdate),
81}
82
83#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
85#[cfg_attr(feature = "serde", derive(serde::Serialize))]
86#[non_exhaustive]
87pub struct CrownUpdate {
88 pub rotation_state: RotationState,
90 pub relative_slot_rotation: i8,
92 pub relative_ratchet_rotation: i8,
94 pub proximity: ActivityState,
96 pub touch: ActivityState,
98 pub gesture: CrownGesture,
100 pub button: ButtonState,
102 pub speed: i16,
104}
105
106pub(super) fn decode_event(sub_id: u8, payload: &[u8; 16]) -> Option<CrownEvent> {
108 match sub_id {
109 0 => Some(CrownEvent::Update(CrownUpdate {
110 rotation_state: RotationState::try_from(payload[0]).ok()?,
111 relative_slot_rotation: payload[1] as i8,
112 relative_ratchet_rotation: payload[2] as i8,
113 proximity: ActivityState::try_from(payload[3]).ok()?,
114 touch: ActivityState::try_from(payload[4]).ok()?,
115 gesture: CrownGesture::try_from(payload[5]).ok()?,
116 button: ButtonState::try_from(payload[6]).ok()?,
117 speed: i16::from_be_bytes([payload[14], payload[15]]),
118 })),
119 _ => None,
120 }
121}