|
2 | 2 |
|
3 | 3 | ## Example
|
4 | 4 |
|
| 5 | +### Using items directly |
| 6 | + |
5 | 7 | ```rs
|
6 | 8 | const QEMU_USB_TABLET: &[u8] = &[
|
7 | 9 | 0x05, 0x01, 0x09, 0x02, 0xa1, 0x01, 0x09, 0x01, 0xa1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29,
|
@@ -11,7 +13,42 @@ const QEMU_USB_TABLET: &[u8] = &[
|
11 | 13 | 0x25, 0x7f, 0x35, 0x00, 0x45, 0x00, 0x75, 0x08, 0x95, 0x01, 0x81, 0x06, 0xc0, 0xc0,
|
12 | 14 | ];
|
13 | 15 |
|
14 |
| -dbg!(usb_hid_report_descriptor::parse(QEMU_USB_TABLET).collect::<Vec<_>>()); |
| 16 | +dbg!(usb_hid_item::parse(QEMU_USB_TABLET).collect::<Vec<_>>()); |
| 17 | +``` |
| 18 | + |
| 19 | +### Using the tree parser |
| 20 | + |
| 21 | +```rs |
| 22 | +const QEMU_USB_TABLET: &[u8] = &[ |
| 23 | +0x05, 0x01, 0x09, 0x02, 0xa1, 0x01, 0x09, 0x01, 0xa1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, |
| 24 | +0x03, 0x15, 0x00, 0x25, 0x01, 0x95, 0x03, 0x75, 0x01, 0x81, 0x02, 0x95, 0x01, 0x75, 0x05, |
| 25 | +0x81, 0x01, 0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 0x15, 0x00, 0x26, 0xff, 0x7f, 0x35, 0x00, |
| 26 | +0x46, 0xff, 0x7f, 0x75, 0x10, 0x95, 0x02, 0x81, 0x02, 0x05, 0x01, 0x09, 0x38, 0x15, 0x81, |
| 27 | +0x25, 0x7f, 0x35, 0x00, 0x45, 0x00, 0x75, 0x08, 0x95, 0x01, 0x81, 0x06, 0xc0, 0xc0, |
| 28 | +]; |
| 29 | + |
| 30 | +use { |
| 31 | +usb_hid_item::tree::{Value, Field}, |
| 32 | +core::{mem, ops::RangeInclusive}, |
| 33 | +}; |
| 34 | + |
| 35 | +type Usages = Vec<(u16, RangeInclusive<u16>)>; |
| 36 | + |
| 37 | +let mut report = Vec::new(); |
| 38 | +let mut usages = Usages::new(); |
| 39 | + |
| 40 | +fn f(val: Value, fields: &mut Vec<(Usages, Field)>, usages: &mut Usages) { |
| 41 | +match val { |
| 42 | +Value::Collection(c) => c.for_each(|c| f(val.unwrap(), fields, usages)), |
| 43 | +Value::Usage { page, ids } => usages.push((page, ids)), |
| 44 | +Value::Field(f) => fields.push((mem::take(usages), f)), |
| 45 | +} |
| 46 | +} |
| 47 | +usb_hid_item::tree::parse(data) |
| 48 | +.iter() |
| 49 | +.for_each(|c| f(c.unwrap(), &mut fields, &mut usages)); |
| 50 | + |
| 51 | +dbg!(fields); |
15 | 52 | ```
|
16 | 53 |
|
17 | 54 | ## References
|
|
0 commit comments