usb_stack/device/class/hid/mouse.rs
1//! Boot-compatible mouse.
2
3use crate::device::bus::UsbBus;
4use crate::device::class::hid::{HidDevice, HidSpec, hid_protocol, hid_subclass};
5
6/// Boot mouse report descriptor: 3 buttons, X/Y movement and a wheel, all
7/// relative, in a 4-byte input report.
8pub const MOUSE_REPORT_DESCRIPTOR: &[u8] = &[
9 0x05, 0x01, // Usage Page (Generic Desktop)
10 0x09, 0x02, // Usage (Mouse)
11 0xA1, 0x01, // Collection (Application)
12 0x09, 0x01, // Usage (Pointer)
13 0xA1, 0x00, // Collection (Physical)
14 // Buttons (3 bits + 5 padding)
15 0x05, 0x09, // Usage Page (Buttons)
16 0x19, 0x01, // Usage Minimum (Button 1)
17 0x29, 0x03, // Usage Maximum (Button 3)
18 0x15, 0x00, // Logical Minimum (0)
19 0x25, 0x01, // Logical Maximum (1)
20 0x75, 0x01, // Report Size (1)
21 0x95, 0x03, // Report Count (3)
22 0x81, 0x02, // Input (Data, Variable, Absolute)
23 0x75, 0x05, // Report Size (5) – padding
24 0x95, 0x01, // Report Count (1)
25 0x81, 0x01, // Input (Constant)
26 // X, Y relative movement
27 0x05, 0x01, // Usage Page (Generic Desktop)
28 0x09, 0x30, // Usage (X)
29 0x09, 0x31, // Usage (Y)
30 0x15, 0x81, // Logical Minimum (-127)
31 0x25, 0x7F, // Logical Maximum (127)
32 0x75, 0x08, // Report Size (8)
33 0x95, 0x02, // Report Count (2)
34 0x81, 0x06, // Input (Data, Variable, Relative)
35 // Wheel
36 0x09, 0x38, // Usage (Wheel)
37 0x15, 0x81, // Logical Minimum (-127)
38 0x25, 0x7F, // Logical Maximum (127)
39 0x75, 0x08, // Report Size (8)
40 0x95, 0x01, // Report Count (1)
41 0x81, 0x06, // Input (Data, Variable, Relative)
42 0xC0, // End Collection
43 0xC0, // End Collection
44];
45
46/// Boot mouse input report: buttons, relative movement and wheel.
47#[derive(Clone, Copy, PartialEq, Eq, Default, Debug)]
48#[cfg_attr(feature = "defmt", derive(defmt::Format))]
49pub struct MouseReport {
50 /// Button bits, see [`MouseReport::BUTTON_*`](MouseReport::BUTTON_LEFT).
51 pub buttons: u8,
52 /// Horizontal movement, -127..=127.
53 pub x: i8,
54 /// Vertical movement, -127..=127.
55 pub y: i8,
56 /// Wheel movement, -127..=127.
57 pub wheel: i8,
58}
59
60impl MouseReport {
61 /// Left button bit in [`buttons`](Self::buttons).
62 pub const BUTTON_LEFT: u8 = 0x01;
63 /// Right button bit in [`buttons`](Self::buttons).
64 pub const BUTTON_RIGHT: u8 = 0x02;
65 /// Middle button bit in [`buttons`](Self::buttons).
66 pub const BUTTON_MIDDLE: u8 = 0x04;
67
68 /// Report the state as a 4-byte boot report.
69 pub fn to_bytes(self) -> [u8; 4] {
70 [self.buttons, self.x as u8, self.y as u8, self.wheel as u8]
71 }
72}
73
74/// Boot-compatible mouse: send a report for every movement or button change, and
75/// send [`MouseReport::default`] once the movement has been reported.
76pub struct Mouse;
77
78impl Mouse {
79 /// Create a boot mouse.
80 pub const fn new() -> Self {
81 Self
82 }
83}
84
85impl Default for Mouse {
86 fn default() -> Self {
87 Self::new()
88 }
89}
90
91impl<B: UsbBus> HidDevice<B> for Mouse {
92 fn spec(&self) -> HidSpec {
93 HidSpec::new(MOUSE_REPORT_DESCRIPTOR, 4)
94 .subclass(hid_subclass::BOOT)
95 .protocol(hid_protocol::MOUSE)
96 .poll_interval_ms(8)
97 }
98}