Skip to main content

usbd_hid_descriptors/
lib.rs

1#![no_std]
2
3use bitfield::bitfield;
4
5/// GlobalItemKind describes global item tags as described in section 6.2.2.7
6/// 'Report Descriptor' of the spec, version 1.11.
7#[repr(u8)]
8#[allow(unused)]
9#[derive(Copy, Debug, Clone, Eq, PartialEq)]
10pub enum GlobalItemKind {
11    UsagePage = 0,
12    LogicalMin = 1,
13    LogicalMax = 2,
14    PhysicalMin = 3,
15    PhysicalMax = 4,
16    UnitExponent = 5,
17    Unit = 6,
18    ReportSize = 7,
19    ReportID = 8,
20    ReportCount = 9,
21}
22
23impl From<GlobalItemKind> for u8 {
24    fn from(kind: GlobalItemKind) -> u8 {
25        kind as u8
26    }
27}
28
29/// LocalItemKind describes local item tags as described in section 6.2.2.8
30/// 'Local Items' of the spec, version 1.11.
31#[repr(u8)]
32#[allow(unused)]
33#[derive(Copy, Debug, Clone, Eq, PartialEq)]
34pub enum LocalItemKind {
35    Usage = 0,
36    UsageMin = 1,
37    UsageMax = 2,
38    DesignatorIdx = 3,
39    DesignatorMin = 4,
40    DesignatorMax = 5,
41    StringIdx = 7,
42    StringMin = 8,
43    StringMax = 9,
44    Delimiter = 10,
45}
46
47impl From<LocalItemKind> for u8 {
48    fn from(kind: LocalItemKind) -> u8 {
49        kind as u8
50    }
51}
52
53/// MainItemKind describes main item tags as described in section 6.2.2.4
54/// 'Report Descriptor' of the spec, version 1.11.
55#[repr(u8)]
56#[allow(unused)]
57#[derive(Copy, Debug, Default, Clone, Eq, PartialEq)]
58pub enum MainItemKind {
59    #[default]
60    Input = 0b1000,
61    Output = 0b1001,
62    Feature = 0b1011,
63    Collection = 0b1010,
64    EndCollection = 0b1100,
65}
66
67impl From<MainItemKind> for u8 {
68    fn from(kind: MainItemKind) -> u8 {
69        kind as u8
70    }
71}
72
73impl From<&str> for MainItemKind {
74    fn from(s: &str) -> Self {
75        match s {
76            "feature" => MainItemKind::Feature,
77            "output" => MainItemKind::Output,
78            "collection" => MainItemKind::Collection,
79            "ecollection" => MainItemKind::EndCollection,
80            "input" => MainItemKind::Input,
81            _ => MainItemKind::Input,
82        }
83    }
84}
85
86/// ItemType describes types of items as described in section 6.2.2.7
87/// 'Report Descriptor' of the spec, version 1.11.
88#[repr(u8)]
89#[allow(unused)]
90#[derive(Copy, Debug, Default, Clone, Eq, PartialEq)]
91pub enum ItemType {
92    #[default]
93    Main = 0,
94    Global = 1,
95    Local = 2,
96}
97
98impl From<ItemType> for u8 {
99    fn from(kind: ItemType) -> u8 {
100        kind as u8
101    }
102}
103
104bitfield! {
105    /// MainItemSetting describes the bits which configure invariants on a MainItem.
106    #[derive(Clone,Debug)]
107    pub struct MainItemSetting(u8);
108    pub is_constant, set_constant: 0;
109    pub is_variable, set_variable: 1;
110    pub is_relative, set_relative: 2;
111    pub is_wrap, set_wrap: 3;
112    pub is_non_linear, set_non_linear: 4;
113    pub has_no_preferred_state, set_no_preferred_state: 5;
114    pub has_null_state, set_has_null_state: 6;
115    pub volatile, set_volatile: 7;
116}
117
118bitfield! {
119    /// ItemPrefix describes the 1 byte prefix describing an item in a descriptor.
120    pub struct ItemPrefix(u8);
121    impl Debug;
122    pub byte_count, set_byte_count: 1, 0;
123    pub typ, set_type: 3, 2;
124    pub tag, set_tag: 7, 4;
125}