From 6d514a0b31a6e480d00a36132ccd41bebfd246cc Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Wed, 6 Apr 2022 02:18:39 +0200 Subject: [PATCH] usb/hid: update for endpoint state changes. --- embassy-usb-hid/src/lib.rs | 5 +++++ examples/nrf/src/bin/usb_hid.rs | 40 +++++++++++++-------------------- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/embassy-usb-hid/src/lib.rs b/embassy-usb-hid/src/lib.rs index 527f014f..e29d485f 100644 --- a/embassy-usb-hid/src/lib.rs +++ b/embassy-usb-hid/src/lib.rs @@ -169,8 +169,11 @@ pub struct ReportReader<'d, D: Driver<'d>, const N: usize> { offset: usize, } +#[derive(Debug, Clone, PartialEq, Eq)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum ReadError { BufferOverflow, + Disabled, Sync(Range), } @@ -179,6 +182,7 @@ impl From for ReadError { use embassy_usb::driver::ReadError::*; match val { BufferOverflow => ReadError::BufferOverflow, + Disabled => ReadError::Disabled, } } } @@ -227,6 +231,7 @@ impl<'d, D: Driver<'d>, const N: usize> ReportReader<'d, D, N> { match self.read(&mut buf).await { Ok(len) => { handler.set_report(ReportId::Out(0), &buf[0..len]); } Err(ReadError::BufferOverflow) => warn!("Host sent output report larger than the configured maximum output report length ({})", N), + Err(ReadError::Disabled) => self.ep_out.wait_enabled().await, Err(ReadError::Sync(_)) => unreachable!(), } } diff --git a/examples/nrf/src/bin/usb_hid.rs b/examples/nrf/src/bin/usb_hid.rs index 6ffb1fd4..741e234b 100644 --- a/examples/nrf/src/bin/usb_hid.rs +++ b/examples/nrf/src/bin/usb_hid.rs @@ -3,9 +3,6 @@ #![feature(generic_associated_types)] #![feature(type_alias_impl_trait)] -#[path = "../example_common.rs"] -mod example_common; - use core::mem; use defmt::*; use embassy::executor::Spawner; @@ -20,6 +17,9 @@ use embassy_usb_hid::{HidClass, ReportId, RequestHandler, State}; use futures::future::join; use usbd_hid::descriptor::{MouseReport, SerializedDescriptor}; +use defmt_rtt as _; // global logger +use panic_probe as _; + #[embassy::main] async fn main(_spawner: Spawner, p: Peripherals) { let clock: pac::CLOCK = unsafe { mem::transmute(()) }; @@ -81,30 +81,22 @@ async fn main(_spawner: Spawner, p: Peripherals) { // Do stuff with the class! let hid_fut = async { + let mut y: i8 = 5; loop { Timer::after(Duration::from_millis(500)).await; - hid.input() - .serialize(&MouseReport { - buttons: 0, - x: 0, - y: 4, - wheel: 0, - pan: 0, - }) - .await - .unwrap(); - Timer::after(Duration::from_millis(500)).await; - hid.input() - .serialize(&MouseReport { - buttons: 0, - x: 0, - y: -4, - wheel: 0, - pan: 0, - }) - .await - .unwrap(); + y = -y; + let report = MouseReport { + buttons: 0, + x: 0, + y, + wheel: 0, + pan: 0, + }; + match hid.input().serialize(&report).await { + Ok(()) => {} + Err(e) => warn!("Failed to send report: {:?}", e), + } } };