Skip to content

Commit c1210a9

Browse files
mutantbobRobert Forsman
andauthored
new maybe_push_input() method to HIDClass using lazy closure to produce data only if the underlying device will not block (twitchyliquid64#34)
Co-authored-by: Robert Forsman <git@thoth.purplefrog.com>
1 parent 581de99 commit c1210a9

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/hid_class.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,24 @@ impl<B: UsbBus> HIDClass<'_, B> {
408408
}
409409
}
410410

411+
pub fn maybe_push_input<'a, IR: AsInputReport>(
412+
&self,
413+
mut producer: impl FnMut() -> IR,
414+
) -> Option<Result<usize>> {
415+
if let Some(ep) = &self.in_ep {
416+
let mut buff: [u8; 64] = [0; 64];
417+
ep.maybe_write(|| {
418+
let size = match serialize(&mut buff, &producer()) {
419+
Ok(l) => l,
420+
Err(_) => return Err(UsbError::BufferOverflow),
421+
};
422+
Ok(&buff[0..size])
423+
})
424+
} else {
425+
Some(Err(UsbError::InvalidEndpoint))
426+
}
427+
}
428+
411429
/// Tries to write an input (device-to-host) report from the given raw bytes.
412430
/// Data is expected to be a valid HID report for INPUT items. If report ID's
413431
/// were used in the descriptor, the report ID corresponding to this report

0 commit comments

Comments
 (0)