Skip to main content

HidDevice

Trait HidDevice 

Source
pub trait HidDevice<B: UsbBus> {
    // Required method
    fn spec(&self) -> HidSpec;

    // Provided methods
    fn get_report(
        &mut self,
        _report_type: ReportType,
        _report_id: u8,
        _buf: &mut [u8],
    ) -> Option<usize> { ... }
    fn set_report(
        &mut self,
        _report_type: ReportType,
        _report_id: u8,
        _data: &[u8],
    ) -> bool { ... }
    fn output_report(&mut self, _report_id: u8, _data: &[u8]) { ... }
    fn reset(&mut self) { ... }
    fn configure(&mut self) { ... }
    fn deconfigure(&mut self) { ... }
    fn poll(&mut self, _input: &EndpointIn<'_, B>) { ... }
}
Expand description

A HID function: a report descriptor plus the reports it exchanges with the host.

Implement it to build a custom HID device, or use one of the ready-made ones: Keyboard, Mouse, CustomHid.

Wrap the implementation in a HidClass, which owns the endpoints and handles the USB side:

ⓘ
let mut hid = HidClass::new(&alloc, MyHidDevice::new());
loop {
    usb_dev.poll(&mut [&mut hid]);
    hid.device_mut().update(...);
}

All methods except spec have default implementations, so a device only writes what it actually supports.

Required Methods§

Source

fn spec(&self) -> HidSpec

Report descriptor, endpoint sizes and interface class/protocol.

Provided Methods§

Source

fn get_report( &mut self, _report_type: ReportType, _report_id: u8, _buf: &mut [u8], ) -> Option<usize>

Get Report — copy the report selected by report_type and report_id into buf and return its length, including the report ID byte when the spec declares report IDs.

Return None to stall the request.

Source

fn set_report( &mut self, _report_type: ReportType, _report_id: u8, _data: &[u8], ) -> bool

Set Report — handle a report written by the host.

data starts at the first report byte; the report ID is passed in report_id, not included in data (HID 1.11 §7.2.1). Return false to stall the request.

Source

fn output_report(&mut self, _report_id: u8, _data: &[u8])

Output report received on the interrupt OUT endpoint.

The class splits the packet for you: with report_ids set, the first byte is passed as report_id and data holds the rest; otherwise report_id is 0 and data is the whole packet.

Source

fn reset(&mut self)

Called after a USB bus reset.

Source

fn configure(&mut self)

Called when the host configures the device.

Devices that push input reports should mark everything as pending here so the host gets a snapshot of the current state.

Source

fn deconfigure(&mut self)

Called when the host de-configures the device.

Source

fn poll(&mut self, _input: &EndpointIn<'_, B>)

Called on every device poll while configured.

Push input reports with input; a report that cannot be sent yet (WouldBlock) stays the device’s responsibility to retry on the next call.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§