diff options
| author | matteo <email> | 2025-10-01 18:56:38 +0200 |
|---|---|---|
| committer | matteo <email> | 2025-10-01 18:56:38 +0200 |
| commit | 176649e71ad442ca9856af6c11989b0b2f228c4b (patch) | |
| tree | b425294213a520f09c24dd215c00efadfad69149 /examples/stm32l5/src/bin/usb_hid_mouse.rs | |
| parent | d79d433d02ab154e5f8570392fd0ca1ffdf9cac1 (diff) | |
update hid mouse and keyboard examples
Diffstat (limited to 'examples/stm32l5/src/bin/usb_hid_mouse.rs')
| -rw-r--r-- | examples/stm32l5/src/bin/usb_hid_mouse.rs | 50 |
1 files changed, 39 insertions, 11 deletions
diff --git a/examples/stm32l5/src/bin/usb_hid_mouse.rs b/examples/stm32l5/src/bin/usb_hid_mouse.rs index f64fde3cb..8c7cdbef5 100644 --- a/examples/stm32l5/src/bin/usb_hid_mouse.rs +++ b/examples/stm32l5/src/bin/usb_hid_mouse.rs | |||
| @@ -1,13 +1,17 @@ | |||
| 1 | #![no_std] | 1 | #![no_std] |
| 2 | #![no_main] | 2 | #![no_main] |
| 3 | 3 | ||
| 4 | use core::sync::atomic::{AtomicU8, Ordering}; | ||
| 5 | |||
| 4 | use defmt::*; | 6 | use defmt::*; |
| 5 | use embassy_executor::Spawner; | 7 | use embassy_executor::Spawner; |
| 6 | use embassy_futures::join::join; | 8 | use embassy_futures::join::join; |
| 7 | use embassy_stm32::usb::Driver; | 9 | use embassy_stm32::usb::Driver; |
| 8 | use embassy_stm32::{bind_interrupts, peripherals, usb, Config}; | 10 | use embassy_stm32::{bind_interrupts, peripherals, usb, Config}; |
| 9 | use embassy_time::Timer; | 11 | use embassy_time::Timer; |
| 10 | use embassy_usb::class::hid::{HidBootProtocol, HidSubclass, HidWriter, ReportId, RequestHandler, State}; | 12 | use embassy_usb::class::hid::{ |
| 13 | HidBootProtocol, HidProtocolMode, HidSubclass, HidWriter, ReportId, RequestHandler, State, | ||
| 14 | }; | ||
| 11 | use embassy_usb::control::OutResponse; | 15 | use embassy_usb::control::OutResponse; |
| 12 | use embassy_usb::Builder; | 16 | use embassy_usb::Builder; |
| 13 | use usbd_hid::descriptor::{MouseReport, SerializedDescriptor}; | 17 | use usbd_hid::descriptor::{MouseReport, SerializedDescriptor}; |
| @@ -17,6 +21,8 @@ bind_interrupts!(struct Irqs { | |||
| 17 | USB_FS => usb::InterruptHandler<peripherals::USB>; | 21 | USB_FS => usb::InterruptHandler<peripherals::USB>; |
| 18 | }); | 22 | }); |
| 19 | 23 | ||
| 24 | static HID_PROTOCOL_MODE: AtomicU8 = AtomicU8::new(HidProtocolMode::Boot as u8); | ||
| 25 | |||
| 20 | #[embassy_executor::main] | 26 | #[embassy_executor::main] |
| 21 | async fn main(_spawner: Spawner) { | 27 | async fn main(_spawner: Spawner) { |
| 22 | let mut config = Config::default(); | 28 | let mut config = Config::default(); |
| @@ -96,16 +102,26 @@ async fn main(_spawner: Spawner) { | |||
| 96 | Timer::after_millis(500).await; | 102 | Timer::after_millis(500).await; |
| 97 | 103 | ||
| 98 | y = -y; | 104 | y = -y; |
| 99 | let report = MouseReport { | 105 | |
| 100 | buttons: 0, | 106 | if HID_PROTOCOL_MODE.load(Ordering::Relaxed) == HidProtocolMode::Boot as u8 { |
| 101 | x: 0, | 107 | let buttons = 0u8; |
| 102 | y, | 108 | let x = 0i8; |
| 103 | wheel: 0, | 109 | match writer.write(&[buttons, x as u8, y as u8]).await { |
| 104 | pan: 0, | 110 | Ok(()) => {} |
| 105 | }; | 111 | Err(e) => warn!("Failed to send boot report: {:?}", e), |
| 106 | match writer.write_serialize(&report).await { | 112 | } |
| 107 | Ok(()) => {} | 113 | } else { |
| 108 | Err(e) => warn!("Failed to send report: {:?}", e), | 114 | let report = MouseReport { |
| 115 | buttons: 0, | ||
| 116 | x: 0, | ||
| 117 | y, | ||
| 118 | wheel: 0, | ||
| 119 | pan: 0, | ||
| 120 | }; | ||
| 121 | match writer.write_serialize(&report).await { | ||
| 122 | Ok(()) => {} | ||
| 123 | Err(e) => warn!("Failed to send report: {:?}", e), | ||
| 124 | } | ||
| 109 | } | 125 | } |
| 110 | } | 126 | } |
| 111 | }; | 127 | }; |
| @@ -128,6 +144,18 @@ impl RequestHandler for MyRequestHandler { | |||
| 128 | OutResponse::Accepted | 144 | OutResponse::Accepted |
| 129 | } | 145 | } |
| 130 | 146 | ||
| 147 | fn get_protocol(&self) -> HidProtocolMode { | ||
| 148 | let protocol = HidProtocolMode::from(HID_PROTOCOL_MODE.load(Ordering::Relaxed)); | ||
| 149 | info!("The current HID protocol mode is: {}", protocol); | ||
| 150 | protocol | ||
| 151 | } | ||
| 152 | |||
| 153 | fn set_protocol(&mut self, protocol: HidProtocolMode) -> OutResponse { | ||
| 154 | info!("Switching to HID protocol mode: {}", protocol); | ||
| 155 | HID_PROTOCOL_MODE.store(protocol as u8, Ordering::Relaxed); | ||
| 156 | OutResponse::Accepted | ||
| 157 | } | ||
| 158 | |||
| 131 | fn set_idle_ms(&mut self, id: Option<ReportId>, dur: u32) { | 159 | fn set_idle_ms(&mut self, id: Option<ReportId>, dur: u32) { |
| 132 | info!("Set idle rate for {:?} to {:?}", id, dur); | 160 | info!("Set idle rate for {:?} to {:?}", id, dur); |
| 133 | } | 161 | } |
