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§
- Encode
Item - 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§
- Descriptor
Bytes - A variable-length short chunk of descriptor bytes.