hid_report/
main_items.rs

1use crate::macros::*;
2use std::fmt::Display;
3
4__impls_for_short_items! {
5    /// Refers to the data from one or more similar controls on a device.
6    ///
7    /// For example, variable data such as reading the position of a single axis
8    /// or a group of levers or array data such as one or more push buttons or
9    /// switches.
10    ///
11    /// # Data (Little-Endian)
12    ///
13    /// * Bit 0: Data(0) | Constant(1)
14    /// * Bit 1: Array(0) | Variable(1)
15    /// * Bit 2: Absolute(0) | Relative(1)
16    /// * Bit 3: No Wrap(0) | Wrap(1)
17    /// * Bit 4: Linear(0) | Non Linear(1)
18    /// * Bit 5: Preferred State(0) | No Preferred(1)
19    /// * Bit 6: No Null Position(0) | Null State(1)
20    /// * Bit 7: Reserved
21    /// * Bit 8: Bit Field(0) | Buffered Bytes(1)
22    /// * Bit 31-9: Reserved
23    Input: 0b1000_0000;
24    /// Refers to the data to one or more similar controls on a device
25    /// such as setting the position of a single axis or a group of levers (variable data).
26    /// Or, it can represent data to one or more LEDs (array data).
27    ///
28    /// # Data (Little-Endian)
29    ///
30    /// * Bit 0: Data(0) | Constant(1)
31    /// * Bit 1: Array(0) | Variable(1)
32    /// * Bit 2: Absolute(0) | Relative(1)
33    /// * Bit 3: No Wrap(0) | Wrap(1)
34    /// * Bit 4: Linear(0) | Non Linear(1)
35    /// * Bit 5: Preferred State(0) | No Preferred(1)
36    /// * Bit 6: No Null Position(0) | Null State(1)
37    /// * Bit 7: Non Volatile(0) | Volatile(1)
38    /// * Bit 8: Bit Field(0) | Buffered Bytes(1)
39    /// * Bit 31-9: Reserved
40    Output: 0b1001_0000;
41    /// Describes device input and output not intended for
42    /// consumption by the end user.
43    ///
44    /// For example, a software feature or Control Panel toggle.
45    ///
46    /// # Data (Little-Endian)
47    ///
48    /// * Bit 0: Data(0) | Constant(1)
49    /// * Bit 1: Array(0) | Variable(1)
50    /// * Bit 2: Absolute(0) | Relative(1)
51    /// * Bit 3: No Wrap(0) | Wrap(1)
52    /// * Bit 4: Linear(0) | Non Linear(1)
53    /// * Bit 5: Preferred State(0) | No Preferred(1)
54    /// * Bit 6: No Null Position(0) | Null State(1)
55    /// * Bit 7: Non Volatile(0) | Volatile(1)
56    /// * Bit 8: Bit Field(0) | Buffered Bytes(1)
57    /// * Bit 31-9: Reserved
58    Feature: 0b1011_0000;
59    /// A meaningful grouping of [Input], [Output], and [Feature] items.
60    ///
61    /// For example, mouse, keyboard, joystick, and pointer.
62    ///
63    /// # Data (Little-Endian)
64    ///
65    /// * 0x00: Physical
66    /// * 0x01: Application
67    /// * 0x02: Logical
68    /// * 0x03: Report
69    /// * 0x04: Named Array
70    /// * 0x05: Usage Switch
71    /// * 0x06: Usage Modifier
72    /// * 0x07-0x7F: Reserved
73    /// * 0x80-0xFF: Vendor Defined
74    Collection: 0b1010_0000;
75    /// A terminating item used to specify the end of a
76    /// [collection](Collection) of items.
77    EndCollection: 0b1100_0000;
78}
79
80impl Display for Input {
81    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
82        match self.data().len() {
83            0 => write!(f, "Input"),
84            1 => write!(
85                f,
86                "Input ({}, {}, {}, {}, {}, {}, {})",
87                __matches_bit!(self.data()[0], 0, "Data", "Constant"),
88                __matches_bit!(self.data()[0], 1, "Array", "Variable"),
89                __matches_bit!(self.data()[0], 2, "Absolute", "Relative"),
90                __matches_bit!(self.data()[0], 3, "No Wrap", "Wrap"),
91                __matches_bit!(self.data()[0], 4, "Linear", "Non Linear"),
92                __matches_bit!(self.data()[0], 5, "Preferred State", "No Preferred"),
93                __matches_bit!(self.data()[0], 6, "No Null Position", "Null State"),
94            ),
95            2.. => write!(
96                f,
97                "Input ({}, {}, {}, {}, {}, {}, {}, {})",
98                __matches_bit!(self.data()[0], 0, "Data", "Constant"),
99                __matches_bit!(self.data()[0], 1, "Array", "Variable"),
100                __matches_bit!(self.data()[0], 2, "Absolute", "Relative"),
101                __matches_bit!(self.data()[0], 3, "No Wrap", "Wrap"),
102                __matches_bit!(self.data()[0], 4, "Linear", "Non Linear"),
103                __matches_bit!(self.data()[0], 5, "Preferred State", "No Preferred"),
104                __matches_bit!(self.data()[0], 6, "No Null Position", "Null State"),
105                __matches_bit!(self.data()[1], 0, "Bit Field", "Buffered Bytes"),
106            ),
107        }
108    }
109}
110
111impl Display for Output {
112    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
113        match self.data().len() {
114            0 => write!(f, "Output"),
115            1 => write!(
116                f,
117                "Output ({}, {}, {}, {}, {}, {}, {}, {})",
118                __matches_bit!(self.data()[0], 0, "Data", "Constant"),
119                __matches_bit!(self.data()[0], 1, "Array", "Variable"),
120                __matches_bit!(self.data()[0], 2, "Absolute", "Relative"),
121                __matches_bit!(self.data()[0], 3, "No Wrap", "Wrap"),
122                __matches_bit!(self.data()[0], 4, "Linear", "Non Linear"),
123                __matches_bit!(self.data()[0], 5, "Preferred State", "No Preferred"),
124                __matches_bit!(self.data()[0], 6, "No Null Position", "Null State"),
125                __matches_bit!(self.data()[0], 7, "Non Volatile", "Volatile"),
126            ),
127            2.. => write!(
128                f,
129                "Output ({}, {}, {}, {}, {}, {}, {}, {}, {})",
130                __matches_bit!(self.data()[0], 0, "Data", "Constant"),
131                __matches_bit!(self.data()[0], 1, "Array", "Variable"),
132                __matches_bit!(self.data()[0], 2, "Absolute", "Relative"),
133                __matches_bit!(self.data()[0], 3, "No Wrap", "Wrap"),
134                __matches_bit!(self.data()[0], 4, "Linear", "Non Linear"),
135                __matches_bit!(self.data()[0], 5, "Preferred State", "No Preferred"),
136                __matches_bit!(self.data()[0], 6, "No Null Position", "Null State"),
137                __matches_bit!(self.data()[0], 7, "Non Volatile", "Volatile"),
138                __matches_bit!(self.data()[1], 0, "Bit Field", "Buffered Bytes"),
139            ),
140        }
141    }
142}
143
144impl Display for Feature {
145    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
146        match self.data().len() {
147            0 => write!(f, "Feature"),
148            1 => write!(
149                f,
150                "Feature ({}, {}, {}, {}, {}, {}, {}, {})",
151                __matches_bit!(self.data()[0], 0, "Data", "Constant"),
152                __matches_bit!(self.data()[0], 1, "Array", "Variable"),
153                __matches_bit!(self.data()[0], 2, "Absolute", "Relative"),
154                __matches_bit!(self.data()[0], 3, "No Wrap", "Wrap"),
155                __matches_bit!(self.data()[0], 4, "Linear", "Non Linear"),
156                __matches_bit!(self.data()[0], 5, "Preferred State", "No Preferred"),
157                __matches_bit!(self.data()[0], 6, "No Null Position", "Null State"),
158                __matches_bit!(self.data()[0], 7, "Non Volatile", "Volatile"),
159            ),
160            2.. => write!(
161                f,
162                "Feature ({}, {}, {}, {}, {}, {}, {}, {}, {})",
163                __matches_bit!(self.data()[0], 0, "Data", "Constant"),
164                __matches_bit!(self.data()[0], 1, "Array", "Variable"),
165                __matches_bit!(self.data()[0], 2, "Absolute", "Relative"),
166                __matches_bit!(self.data()[0], 3, "No Wrap", "Wrap"),
167                __matches_bit!(self.data()[0], 4, "Linear", "Non Linear"),
168                __matches_bit!(self.data()[0], 5, "Preferred State", "No Preferred"),
169                __matches_bit!(self.data()[0], 6, "No Null Position", "Null State"),
170                __matches_bit!(self.data()[0], 7, "Non Volatile", "Volatile"),
171                __matches_bit!(self.data()[1], 0, "Bit Field", "Buffered Bytes"),
172            ),
173        }
174    }
175}
176
177impl Display for Collection {
178    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
179        match self.data().len() {
180            0 => write!(f, "Collection"),
181            1.. => write!(
182                f,
183                "Collection ({})",
184                match self.data()[0] {
185                    0 => "Physical",
186                    1 => "Application",
187                    2 => "Logical",
188                    3 => "Report",
189                    4 => "Named Array",
190                    5 => "Usage Switch",
191                    6 => "Usage Modifier",
192                    7..=0x7f => "Reserved",
193                    0x80..=0xff => "Vendor Defined",
194                }
195            ),
196        }
197    }
198}
199
200impl Display for EndCollection {
201    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
202        write!(f, "End Collection")
203    }
204}