Split usb_report_descriptor into usb_hid_{item,usage}

This is cleaner IMO.

* usb_hid_item deals with parsing the report descriptor.
* usb_hid_usage has tables that define each usage.
This commit is contained in:
David Hoppenbrouwers
2022-09-09 02:42:00 +02:00
parent 7f6eb97569
commit 2175e1ed74
11 changed files with 121 additions and 122 deletions

8
usb_hid_usage/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "usb_hid_usage"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -0,0 +1,17 @@
use core::num::NonZeroU16;
pub const PAGE: u16 = 0x09;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Usage {
NoButton,
Button(NonZeroU16),
}
impl TryFrom<u16> for Usage {
type Error = super::UnknownUsage;
fn try_from(n: u16) -> Result<Self, Self::Error> {
Ok(NonZeroU16::new(n).map_or(Self::NoButton, Self::Button))
}
}

View File

@ -0,0 +1,52 @@
usage! {
[0x01]
0x01 Pointer
0x02 Mouse
0x04 Joystick
0x05 Gamepad
0x06 Keyboard
0x07 Keypad
0x08 MultiAxisController
0x09 TabletPcSystemControls
0x0a WaterCoolingDevice
0x0b ComputerChassisDevice
0x0c WirelessRadioControls
0x0d PortableDeviceControl
0x0e SystemMultiAxisController
0x0f SpatialController
0x10 AssistiveControl
0x11 DeviceDock
0x12 DockableDevice
0x13 CallStateManagementControl
0x30 X
0x31 Y
0x32 Z
0x33 Rx
0x34 Ry
0x35 Rz
0x36 Slider
0x37 Dial
0x38 Wheel
0x39 HatSwitch
0x3a CountedBuffer
0x3b ByteCount
0x3c MotionWakeup
0x3d Start
0x3e Select
0x40 Vx
0x41 Vy
0x42 Vz
0x43 Vbrx
0x44 Vbry
0x45 Vbrz
0x46 Vno
0x47 FeatureNotification
0x48 ResolutionMultiplier
0x49 Qx
0x4a Qy
0x4b Qz
0x4c Qw
}

63
usb_hid_usage/src/lib.rs Normal file
View File

@ -0,0 +1,63 @@
//!
//! ## References
//!
//! * <https://www.usb.org/sites/default/files/hut1_3_0.pdf>
#![no_std]
macro_rules! usage {
{ [$page:literal] $($i:literal $v:ident)* } => {
pub const PAGE: u16 = $page;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum Usage {
$($v,)*
}
impl TryFrom<u16> for Usage {
type Error = super::UnknownUsage;
fn try_from(n: u16) -> Result<Self, Self::Error> {
Ok(match n {
$($i => Self::$v,)*
_ => return Err(super::UnknownUsage),
})
}
}
};
}
macro_rules! page {
{ $($v:ident $m:ident)* } => {
$(pub mod $m;)*
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum UsagePage {
$($v,)*
}
impl TryFrom<u16> for UsagePage {
type Error = UnknownPage;
fn try_from(n: u16) -> Result<Self, Self::Error> {
Ok(match n {
$($m::PAGE => Self::$v,)*
_ => return Err(UnknownPage),
})
}
}
};
}
page! {
GenericDesktop generic_desktop
Button button
}
#[derive(Debug)]
pub struct UnknownUsage;
#[derive(Debug)]
pub struct UnknownPage;