Batteries included embedded USB HID library for usb-device
.
Includes concrete Keyboard (boot and NKRO), Mouse and Consumer Control implementations as well as
support for building your own HID classes.
This library has been 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. It has been tested on Windows, Linux, MacOS and Android.
Note: Managed interfaces that support HID idle, such as
NKROBootKeyboardInterface
and BootKeyboardInterface
,
require their tick()
method calling every 1ms/at 1kHz.
```rust,norun use usbdhumaninterfacedevice::page::Keyboard; use usbdhumaninterfacedevice::device::keyboard::{KeyboardLedsReport, NKROBootKeyboardInterface}; use usbdhumaninterfacedevice::prelude::*;
let usballoc = UsbBusAllocator::new(usbbus);
let mut keyboard = UsbHidClassBuilder::new() .addinterface( NKROBootKeyboardInterface::defaultconfig(), ) .build(&usb_alloc);
let mut usbdev = UsbDeviceBuilder::new(&usballoc, UsbVidPid(0x1209, 0x0001)) .manufacturer("usbd-human-interface-device") .product("NKRO Keyboard") .serial_number("TEST") .build();
let mut ticktimer = timer.countdown(); tick_timer.start(1.millis());
loop { let keys = if pin.is_high().unwrap() { [Keyboard::A] } else { [Keyboard::NoEventIndicated] };
keyboard.interface().write_report(keys).ok();
//tick once per ms/at 1kHz
if tick_timer.wait().is_ok() {
keyboard.interface().tick().unwrap();
}
if usb_dev.poll(&mut [&mut keyboard]) {
match keyboard.interface().read_report() {
Ok(l) => {
update_leds(l);
}
_ => {}
}
}
} ```
See examples for demonstration of how to use this library on the RP2040 (Raspberry Pi Pico)
https://github.com/dlkj/usbd-human-interface-device/issues
Distributed under the MIT License. See MIT for details.
Contributions are welcome. Please for the project, create a feature branch and open a pull request
Any contribution submitted for inclusion in the work by you will be licenced under the MIT License without any additional terms or conditions.
This library was inspired by existing rust USB libraries and the following sources of USB information: