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/rp235x/src | |
| parent | d79d433d02ab154e5f8570392fd0ca1ffdf9cac1 (diff) | |
update hid mouse and keyboard examples
Diffstat (limited to 'examples/rp235x/src')
| -rw-r--r-- | examples/rp235x/src/bin/usb_hid_keyboard.rs | 77 |
1 files changed, 53 insertions, 24 deletions
diff --git a/examples/rp235x/src/bin/usb_hid_keyboard.rs b/examples/rp235x/src/bin/usb_hid_keyboard.rs index b740a07b3..d8f64c470 100644 --- a/examples/rp235x/src/bin/usb_hid_keyboard.rs +++ b/examples/rp235x/src/bin/usb_hid_keyboard.rs | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | #![no_std] | 1 | #![no_std] |
| 2 | #![no_main] | 2 | #![no_main] |
| 3 | 3 | ||
| 4 | use core::sync::atomic::{AtomicBool, Ordering}; | 4 | use core::sync::atomic::{AtomicBool, AtomicU8, Ordering}; |
| 5 | 5 | ||
| 6 | use defmt::*; | 6 | use defmt::*; |
| 7 | use embassy_executor::Spawner; | 7 | use embassy_executor::Spawner; |
| @@ -11,7 +11,7 @@ use embassy_rp::gpio::{Input, Pull}; | |||
| 11 | use embassy_rp::peripherals::USB; | 11 | use embassy_rp::peripherals::USB; |
| 12 | use embassy_rp::usb::{Driver as UsbDriver, InterruptHandler}; | 12 | use embassy_rp::usb::{Driver as UsbDriver, InterruptHandler}; |
| 13 | use embassy_usb::class::hid::{ | 13 | use embassy_usb::class::hid::{ |
| 14 | HidBootProtocol, HidReaderWriter, HidSubclass, ReportId, RequestHandler, State as HidState, | 14 | HidBootProtocol, HidProtocolMode, HidReaderWriter, HidSubclass, ReportId, RequestHandler, State as HidState, |
| 15 | }; | 15 | }; |
| 16 | use embassy_usb::control::OutResponse; | 16 | use embassy_usb::control::OutResponse; |
| 17 | use embassy_usb::{Builder, Config, Handler}; | 17 | use embassy_usb::{Builder, Config, Handler}; |
| @@ -22,6 +22,8 @@ bind_interrupts!(struct Irqs { | |||
| 22 | USBCTRL_IRQ => InterruptHandler<USB>; | 22 | USBCTRL_IRQ => InterruptHandler<USB>; |
| 23 | }); | 23 | }); |
| 24 | 24 | ||
| 25 | static HID_PROTOCOL_MODE: AtomicU8 = AtomicU8::new(HidProtocolMode::Boot as u8); | ||
| 26 | |||
| 25 | #[embassy_executor::main] | 27 | #[embassy_executor::main] |
| 26 | async fn main(_spawner: Spawner) { | 28 | async fn main(_spawner: Spawner) { |
| 27 | let p = embassy_rp::init(Default::default()); | 29 | let p = embassy_rp::init(Default::default()); |
| @@ -94,30 +96,45 @@ async fn main(_spawner: Spawner) { | |||
| 94 | info!("Waiting for HIGH on pin 16"); | 96 | info!("Waiting for HIGH on pin 16"); |
| 95 | signal_pin.wait_for_high().await; | 97 | signal_pin.wait_for_high().await; |
| 96 | info!("HIGH DETECTED"); | 98 | info!("HIGH DETECTED"); |
| 97 | // Create a report with the A key pressed. (no shift modifier) | 99 | |
| 98 | let report = KeyboardReport { | 100 | if HID_PROTOCOL_MODE.load(Ordering::Relaxed) == HidProtocolMode::Boot as u8 { |
| 99 | keycodes: [4, 0, 0, 0, 0, 0], | 101 | match writer.write(&[0, 0, 4, 0, 0, 0, 0, 0]).await { |
| 100 | leds: 0, | 102 | Ok(()) => {} |
| 101 | modifier: 0, | 103 | Err(e) => warn!("Failed to send boot report: {:?}", e), |
| 102 | reserved: 0, | 104 | }; |
| 103 | }; | 105 | } else { |
| 104 | // Send the report. | 106 | // Create a report with the A key pressed. (no shift modifier) |
| 105 | match writer.write_serialize(&report).await { | 107 | let report = KeyboardReport { |
| 106 | Ok(()) => {} | 108 | keycodes: [4, 0, 0, 0, 0, 0], |
| 107 | Err(e) => warn!("Failed to send report: {:?}", e), | 109 | leds: 0, |
| 108 | }; | 110 | modifier: 0, |
| 111 | reserved: 0, | ||
| 112 | }; | ||
| 113 | // Send the report. | ||
| 114 | match writer.write_serialize(&report).await { | ||
| 115 | Ok(()) => {} | ||
| 116 | Err(e) => warn!("Failed to send report: {:?}", e), | ||
| 117 | }; | ||
| 118 | } | ||
| 109 | signal_pin.wait_for_low().await; | 119 | signal_pin.wait_for_low().await; |
| 110 | info!("LOW DETECTED"); | 120 | info!("LOW DETECTED"); |
| 111 | let report = KeyboardReport { | 121 | if HID_PROTOCOL_MODE.load(Ordering::Relaxed) == HidProtocolMode::Boot as u8 { |
| 112 | keycodes: [0, 0, 0, 0, 0, 0], | 122 | match writer.write(&[0, 0, 0, 0, 0, 0, 0, 0]).await { |
| 113 | leds: 0, | 123 | Ok(()) => {} |
| 114 | modifier: 0, | 124 | Err(e) => warn!("Failed to send boot report: {:?}", e), |
| 115 | reserved: 0, | 125 | }; |
| 116 | }; | 126 | } else { |
| 117 | match writer.write_serialize(&report).await { | 127 | let report = KeyboardReport { |
| 118 | Ok(()) => {} | 128 | keycodes: [0, 0, 0, 0, 0, 0], |
| 119 | Err(e) => warn!("Failed to send report: {:?}", e), | 129 | leds: 0, |
| 120 | }; | 130 | modifier: 0, |
| 131 | reserved: 0, | ||
| 132 | }; | ||
| 133 | match writer.write_serialize(&report).await { | ||
| 134 | Ok(()) => {} | ||
| 135 | Err(e) => warn!("Failed to send report: {:?}", e), | ||
| 136 | }; | ||
| 137 | } | ||
| 121 | } | 138 | } |
| 122 | }; | 139 | }; |
| 123 | 140 | ||
| @@ -143,6 +160,18 @@ impl RequestHandler for MyRequestHandler { | |||
| 143 | OutResponse::Accepted | 160 | OutResponse::Accepted |
| 144 | } | 161 | } |
| 145 | 162 | ||
| 163 | fn get_protocol(&self) -> HidProtocolMode { | ||
| 164 | let protocol = HidProtocolMode::from(HID_PROTOCOL_MODE.load(Ordering::Relaxed)); | ||
| 165 | info!("The current HID protocol mode is: {}", protocol); | ||
| 166 | protocol | ||
| 167 | } | ||
| 168 | |||
| 169 | fn set_protocol(&mut self, protocol: HidProtocolMode) -> OutResponse { | ||
| 170 | info!("Switching to HID protocol mode: {}", protocol); | ||
| 171 | HID_PROTOCOL_MODE.store(protocol as u8, Ordering::Relaxed); | ||
| 172 | OutResponse::Accepted | ||
| 173 | } | ||
| 174 | |||
| 146 | fn set_idle_ms(&mut self, id: Option<ReportId>, dur: u32) { | 175 | fn set_idle_ms(&mut self, id: Option<ReportId>, dur: u32) { |
| 147 | info!("Set idle rate for {:?} to {:?}", id, dur); | 176 | info!("Set idle rate for {:?} to {:?}", id, dur); |
| 148 | } | 177 | } |
