aboutsummaryrefslogtreecommitdiff
path: root/examples/stm32l5
diff options
context:
space:
mode:
Diffstat (limited to 'examples/stm32l5')
-rw-r--r--examples/stm32l5/src/bin/usb_hid_mouse.rs50
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
4use core::sync::atomic::{AtomicU8, Ordering};
5
4use defmt::*; 6use defmt::*;
5use embassy_executor::Spawner; 7use embassy_executor::Spawner;
6use embassy_futures::join::join; 8use embassy_futures::join::join;
7use embassy_stm32::usb::Driver; 9use embassy_stm32::usb::Driver;
8use embassy_stm32::{bind_interrupts, peripherals, usb, Config}; 10use embassy_stm32::{bind_interrupts, peripherals, usb, Config};
9use embassy_time::Timer; 11use embassy_time::Timer;
10use embassy_usb::class::hid::{HidBootProtocol, HidSubclass, HidWriter, ReportId, RequestHandler, State}; 12use embassy_usb::class::hid::{
13 HidBootProtocol, HidProtocolMode, HidSubclass, HidWriter, ReportId, RequestHandler, State,
14};
11use embassy_usb::control::OutResponse; 15use embassy_usb::control::OutResponse;
12use embassy_usb::Builder; 16use embassy_usb::Builder;
13use usbd_hid::descriptor::{MouseReport, SerializedDescriptor}; 17use 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
24static HID_PROTOCOL_MODE: AtomicU8 = AtomicU8::new(HidProtocolMode::Boot as u8);
25
20#[embassy_executor::main] 26#[embassy_executor::main]
21async fn main(_spawner: Spawner) { 27async 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 }