hid-descriptor 0.1.0

Tools for building USB HID report descriptors
Documentation
  • Coverage
  • 100%
    18 out of 18 items documented1 out of 17 items with examples
  • Size
  • Source code size: 19.8 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 509.4 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 13s Average build duration of successful builds.
  • all releases: 8s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • ericseppanen

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"
// `build.rs`
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();
}
// In your crate, compile in the descriptor bytes.
static GAMEPAD_DESCRIPTOR: &[u8] =
    include_bytes!(concat!(env!("OUT_DIR"), "/descriptor.bin"));