Skip to content

Commit

Permalink
new maybe_push_input() method to HIDClass using lazy closure to produ…
Browse files Browse the repository at this point in the history
…ce data only if the underlying device will not block (#34)

Co-authored-by: Robert Forsman <[email protected]>
  • Loading branch information
mutantbob and Robert Forsman authored Mar 25, 2024
1 parent 581de99 commit c1210a9
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/hid_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,24 @@ impl<B: UsbBus> HIDClass<'_, B> {
}
}

pub fn maybe_push_input<'a, IR: AsInputReport>(
&self,
mut producer: impl FnMut() -> IR,
) -> Option<Result<usize>> {
if let Some(ep) = &self.in_ep {
let mut buff: [u8; 64] = [0; 64];
ep.maybe_write(|| {

Check failure on line 417 in src/hid_class.rs

View workflow job for this annotation

GitHub Actions / Build

no method named `maybe_write` found for reference `&usb_device::endpoint::Endpoint<'_, B, usb_device::endpoint::In>` in the current scope

Check failure on line 417 in src/hid_class.rs

View workflow job for this annotation

GitHub Actions / Check

no method named `maybe_write` found for reference `&usb_device::endpoint::Endpoint<'_, B, usb_device::endpoint::In>` in the current scope

Check failure on line 417 in src/hid_class.rs

View workflow job for this annotation

GitHub Actions / Clippy

no method named `maybe_write` found for reference `&usb_device::endpoint::Endpoint<'_, B, usb_device::endpoint::In>` in the current scope

Check failure on line 417 in src/hid_class.rs

View workflow job for this annotation

GitHub Actions / native (ubuntu-latest)

no method named `maybe_write` found for reference `&usb_device::endpoint::Endpoint<'_, B, usb_device::endpoint::In>` in the current scope
let size = match serialize(&mut buff, &producer()) {
Ok(l) => l,
Err(_) => return Err(UsbError::BufferOverflow),
};
Ok(&buff[0..size])
})
} else {
Some(Err(UsbError::InvalidEndpoint))
}
}

/// Tries to write an input (device-to-host) report from the given raw bytes.
/// Data is expected to be a valid HID report for INPUT items. If report ID's
/// were used in the descriptor, the report ID corresponding to this report
Expand Down

0 comments on commit c1210a9

Please sign in to comment.