Crate usbd_human_interface_device

source ·
Expand description
use usbd_human_interface_device::page::Keyboard;
use usbd_human_interface_device::device::keyboard::{KeyboardLedsReport, NKROBootKeyboardConfig};
use usbd_human_interface_device::prelude::*;

let usb_alloc = UsbBusAllocator::new(usb_bus);

let mut keyboard = UsbHidClassBuilder::new()
    .add_device(
        NKROBootKeyboardConfig::default(),
    )
    .build(&usb_alloc);

let mut usb_dev = UsbDeviceBuilder::new(&usb_alloc, UsbVidPid(0x1209, 0x0001))
    .strings(&[
        StringDescriptors::default()
            .manufacturer("usbd-human-interface-device")
            .product("NKRO Keyboard")
            .serial_number("TEST")]
    ).unwrap()       
    .build();

let mut tick_timer = timer.count_down();
tick_timer.start(1.millis());

loop {
    let keys = if pin.is_high().unwrap() {
            [Keyboard::A]
        } else {
            [Keyboard::NoEventIndicated]
    };

    keyboard.device().write_report(keys).ok();

    // tick once per ms/at 1kHz
    if tick_timer.wait().is_ok() {
        keyboard.tick().unwrap();
    }

    if usb_dev.poll(&mut [&mut keyboard]) {
        match keyboard.device().read_report() {

            Ok(l) => {
                update_leds(l);
            }
            _ => {}

        }
    }
}

Library build Example build Security audit

crates.io docs.rs

Batteries included embedded USB HID library for usb-device. Includes Keyboard (boot and NKRO), Mouse, Joystick and Consumer Control implementations as well as support for building your own HID classes.

Tested on the RP2040, but should work on any platform supported by usb-device.

Devices created with this library should work with any USB host. Tested on Windows, Linux, MacOS and Android.

Note: Managed interfaces that support HID idle, such as NKROBootKeyboardInterface and BootKeyboardInterface, require the UsbHidClass::tick() method calling every 1ms.

§Features

  • Keyboard - boot compliant keyboard, boot compliant NKRO(N-Key Roll Over) keyboard
  • Mouse - boot compliant mouse, boot compliant mouse with scroll wheel and pan
  • Joystick - two axis joystick with eight buttons
  • Consumer Control - Media control device, generic consumer control device
  • Enums for the Consumer, Desktop, Game, Keyboard, LED, Simulation and Telephony HID usage pages
  • Support for multi-interface devices
  • Support for HID idle and HID protocol changing
  • Support for both single and multi report interfaces
  • Compatible with RTIC

§Examples

See examples for demonstrations of how to use this library on the RP2040 (Raspberry Pi Pico)

§Road map

  • Examples and testing for other micro-controllers such as the SAM D2x family.
  • Support for host device remote wakeup

§Contact

https://github.com/dlkj/usbd-human-interface-device/issues

§License

Distributed under the MIT License, see LICENSE.

§Contributing

Contributions are welcome via pull requests

§Acknowledgements

This library was inspired by existing rust USB libraries and the following sources of USB information:

Modules§

  • HID descriptor constants and enumerations
  • Concrete implementation of Human Interface Devices
  • Human Interface Device Interfaces
  • USB HID usage pages
  • Prelude for using USB HID devices
  • USB Class for implementing Human Interface Devices

Enums§