boot.py
 1  # SPDX-FileCopyrightText: 2021 Jeff Epler for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  import usb_hid
 6  
 7  REPORT_ID = 0x4
 8  REPORT_BYTES = 16
 9  bitmap_keyboard_descriptor = bytes((
10          0x05, 0x01,                     # Usage Page (Generic Desktop),
11          0x09, 0x06,                     # Usage (Keyboard),
12          0xA1, 0x01,                     # Collection (Application),
13          0x85, REPORT_ID,                #   Report ID
14          # bitmap of modifiers
15          0x75, 0x01,                     #   Report Size (1),
16          0x95, 0x08,                     #   Report Count (8),
17          0x05, 0x07,                     #   Usage Page (Key Codes),
18          0x19, 0xE0,                     #   Usage Minimum (224),
19          0x29, 0xE7,                     #   Usage Maximum (231),
20          0x15, 0x00,                     #   Logical Minimum (0),
21          0x25, 0x01,                     #   Logical Maximum (1),
22          0x81, 0x02,                     #   Input (Data, Variable, Absolute), ;Modifier byte
23          # LED output report
24          0x95, 0x05,                     #   Report Count (5),
25          0x75, 0x01,                     #   Report Size (1),
26          0x05, 0x08,                     #   Usage Page (LEDs),
27          0x19, 0x01,                     #   Usage Minimum (1),
28          0x29, 0x05,                     #   Usage Maximum (5),
29          0x91, 0x02,                     #   Output (Data, Variable, Absolute),
30          0x95, 0x01,                     #   Report Count (1),
31          0x75, 0x03,                     #   Report Size (3),
32          0x91, 0x03,                     #   Output (Constant),
33          # bitmap of keys
34          0x95, (REPORT_BYTES-1)*8,       #   Report Count (),
35          0x75, 0x01,                     #   Report Size (1),
36          0x15, 0x00,                     #   Logical Minimum (0),
37          0x25, 0x01,                     #   Logical Maximum(1),
38          0x05, 0x07,                     #   Usage Page (Key Codes),
39          0x19, 0x00,                     #   Usage Minimum (0),
40          0x29, (REPORT_BYTES-1)*8-1,     #   Usage Maximum (),
41          0x81, 0x02,                     #   Input (Data, Variable, Absolute),
42          0xc0                            # End Collection
43  ))
44  
45  bitmap_keyboard = usb_hid.Device(
46      report_descriptor=bitmap_keyboard_descriptor,
47      usage_page=0x1,
48      usage=0x6,
49      report_ids=(REPORT_ID,),
50      in_report_lengths=(REPORT_BYTES,),
51      out_report_lengths=(1,),
52  )
53  
54  usb_hid.enable(
55      (
56          bitmap_keyboard,
57          usb_hid.Device.MOUSE,
58          usb_hid.Device.CONSUMER_CONTROL,
59      )
60  )
61  print("enabled HID with custom keyboard device")