hid-descriptor crate
This is a utility for generating a binary USB HID report descriptor from Rust code.
It can be used from build.rs so that the descriptor is generated at compile time,
and the descriptor bytes can be included in the code, with no runtime cost.
Example
[build-dependencies]
hid-descriptor = "0.1.0"
use std::path::PathBuf;
use hid_descriptor::*;
use hid_types::hid::{KnownCollectionType, ioflags};
use hid_types::id::usage::{KnownUsagePage, generic_desktop, keyboard_keypad};
fn main() {
println!("cargo:rerun-if-changed=build.rs");
let out_dir = PathBuf::from(std::env::var_os("OUT_DIR").unwrap());
let descriptor_path = out_dir.join("descriptor.bin");
println!("cargo:rerun-if-changed={}", descriptor_path.display());
let bytes = [
usage_page(KnownUsagePage::GenericDesktop),
usage(generic_desktop::KnownUsage::Keyboard),
collection(KnownCollectionType::Application),
report_size(1),
report_count(8),
usage_page(KnownUsagePage::KeyboardKeypad),
usage_minimum(keyboard_keypad::KnownUsage::LeftControl),
usage_maximum(keyboard_keypad::KnownUsage::RightGui),
logical_minimum(0),
logical_maximum(1),
input(ioflags::DATA_VARIABLE_ABSOLUTE),
];
write_to_file(bytes, descriptor_path).unwrap();
}
static GAMEPAD_DESCRIPTOR: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/descriptor.bin"));