Close

Dynamic USB Descriptors

A project log for Chocolad Keyboard Hacking

Make it run CircuitPython

dehipudeʃhipu 05/15/2021 at 13:042 Comments

A new feature landed in CircuitPython recently, that allows us to do a neat little trick: we can show the CIRCUITPY disk and Python REPL console, or not, depending on whether a certain key is pressed when the keyboard is powered up or reset.

Why is this important?

I find it rather distracting when my keyboard shows up as a disk and serial device, especially when I'm working with other CircuitPython devices — it's sometimes hard to tell which is which. On the other hand, the way you could disable the disk and console previously involved compiling and flashing a special CircuitPython image, as this was a compile-time option. Having to re-flash your firmware to see the disk again every time you want to make some small change or tweak is not very convenient.

So now we can make it so that it's just a keypress away.

All I needed to do is to create the following boot.py file:

import board
import digitalio
import storage
import usb_cdc
import usb_hid

row = digitalio.DigitalInOut(board.D4)
col = digitalio.DigitalInOut(board.A2)
col.switch_to_output(value=0)
row.switch_to_input(pull=digitalio.Pull.UP)

if row.value:
    storage.disable_usb_drive()
    usb_cdc.disable()

usb_hid.enable(devices=(usb_hid.KEYBOARD,))

row.deinit()
col.deinit()

The board.D4 and board.A2 are the row and column of the key that I want to use for the check. The rest is pretty self-explanatory. 

Discussions

Dan Halbert wrote 05/15/2021 at 13:40 point

Thanks! I plan to add "boot" keyboard support next.

  Are you sure? yes | no

deʃhipu wrote 05/15/2021 at 14:57 point

That's great. And I also suppose that with the ability to configure your own HID descriptors, an n-key rollover keyboard should be already possible too.

  Are you sure? yes | no