Skip to main content

Crate hid_descriptor

Crate hid_descriptor 

Source
Expand description

§Tools for building USB HID report descriptors.

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.

Alternatively, you can edit one of the example binaries, and copy the resulting descriptor file into your project.

See Device Class Definition for Human Interface Devices for a detailed description of HID report descriptors.

§Example

// `build.rs`
use hid_descriptor::*;
use hid_types::id::{IoFlags, KnownCollectionType, KnownUsagePage};
use hid_types::id::usage::{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::Gamepad),
        collection(KnownCollectionType::Application),
        usage_page(KnownUsagePage::Button),
        usage_minimum(1u16),
        usage_maximum(8u16),
        logical_minimum(0),
        logical_maximum(1),
        report_size(1),
        report_count(8),
        input(IoFlags::data().variable()),
        end_collection(),
    ];
    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"));

Re-exports§

pub use hid_types;

Traits§

EncodeItem
A trait for items that can be encoded into the HID report descriptor binary format.

Functions§

collection
Create a Collection.
concatenate_bytes
Concatenate a bunch of descriptor bytes into one iterator.
designator_index
Create a Designator Index.
designator_maximum
Create a Designator Maximum.
designator_minimum
Create a Designator Minimum.
encode_items
Encode a sequence of items into a binary HID report descriptor.
end_collection
Create an End Collection.
extended_usage
Create an extended Usage value with a Usage Page attached.
feature
Create a Feature item.
input
Create an Input item.
logical_maximum
Create a Logical Maximum.
logical_minimum
Create a Logical Minimum.
output
Create an Output item.
physical_maximum
Create a Physical Maximum.
physical_minimum
Create a Physical Minimum.
pop
Create a Pop.
push
Create a Push.
report_count
Create a Report Count value.
report_id
Create a Report ID.
report_size
Create a Report Size value.
string_
Create a Delimiter.
string_index
Create a String Index.
string_maximum
Create a String Maximum.
string_minimum
Create a String Minimum.
unit
Create a Unit.
unit_exponent
Create a Unit Exponent.
usage
Create a Usage value.
usage_maximum
Create a Usage Maximum value.
usage_minimum
Create a Usage Minimum value.
usage_page
Create a Usage Page.
write_to_file
Write a binary encoded descriptor to a file.

Type Aliases§

DescriptorBytes
A variable-length short chunk of descriptor bytes.