- Python 88.9%
- HTML 4.7%
- Makefile 4.2%
- Shell 2.2%
|
Some checks failed
check / check (push) Has been cancelled
CircuitPython firmware for a Pico 2 that reads the button on GP2 and presses F13 over USB HID for as long as it is held. A hardware-free state machine filters cable noise (1 ms low before a press), bounce (10 ms high before a release) and stretches short slaps to 30 ms so the laptop always sees them. A button held at boot sends nothing until the line has been high, and any exception releases the keys and resets. Includes tests against simulated hardware, make deploy for the CIRCUITPY drive, an offline key viewer for the bench test and a Forgejo workflow running make check. |
||
|---|---|---|
| .forgejo/workflows | ||
| firmware | ||
| scripts | ||
| stubs | ||
| tests | ||
| tools | ||
| .gitignore | ||
| .pre-commit-config.yaml | ||
| LICENSE | ||
| Makefile | ||
| pyproject.toml | ||
| README.md | ||
| uv.lock | ||
van-wie-is-die-knop
Firmware for a big red mushroom button on a truss totem. A Raspberry Pi Pico 2 running CircuitPython reads the button contact and shows up on the laptop as a USB keyboard that presses F13.
How it works
Pressing the button presses F13 (HID usage 0x68) and releasing it releases the key, so the laptop sees short taps and long holds alike. F13 is in the HID usage table but on almost no physical keyboard, so binding it on the laptop never collides with normal typing.
The firmware polls GP2 in a tight loop and filters the raw line in software:
| Rule | Default |
|---|---|
| The line must be low without interruption before it counts as a press | 1 ms |
| The line must be high without interruption before it counts as a release | 10 ms |
| The key stays pressed at least this long after the press, even if the contact opened earlier | 30 ms |
A slap of a few milliseconds therefore still reaches the laptop as a 30 ms key press. Microsecond spikes from the long cable never last 1 ms and are ignored. Contact bounce while holding the button lasts less than 10 ms and does not release the key.
If the button is already held down when the Pico starts, nothing is sent. The line first has to be high for 10 ms (the release time) before a press counts. This prevents a phantom key press when someone plugs in the Pico while leaning on the button, and it means a button stuck in the pressed position never produces a key that stays down.
If anything raises an unexpected exception, the firmware releases all keys, waits 0.5 s and resets the Pico. No key can stay stuck.
The onboard LED is on while the key is pressed. On a board without a usable LED the firmware runs without it.
Hardware and wiring
| Pico pin | Physical pin | Connection |
|---|---|---|
| GP2 | 4 | Signal, through a 1 kΩ series resistor |
| GP3 | 5 | Solder support point only, tied to the signal. The firmware never touches it. |
| 3V3(OUT) | 36 | Pull-up supply |
| GND | 3, 38 or any other GND | Button contact, optional capacitor |
| Part | Value | Place |
|---|---|---|
| Pull-up | 2.2 kΩ to 3V3 | Cable side, at the button |
| Series resistor | 1 kΩ | Between the cable and GP2 |
| Capacitor (optional) | 100 nF | Between GP2 and GND, at the Pico |
| Button | NO contact | Closes to GND when pressed |
Pico 2 button end
3V3(OUT) pin 36 ────────────── 5-6 m cable ───────────────────┐
[2.2k]
GP2 pin 4 ───[1k]───┬────────────────────────────────────────┤
│ │
═════ 100 nF (optional) / NO contact
│ │
GND pin 3 ──────────┴────────────────────────────────────────┘
The Pico gets its power from the laptop through the micro-USB cable.
Installing
- Hold BOOTSEL, plug the Pico into the laptop and release BOOTSEL. A drive called
RP2350appears. - Download CircuitPython 10.x for the Pico 2 or the Pico 2 W and copy the
.uf2file ontoRP2350. The Pico restarts and a drive calledCIRCUITPYappears. - Download the CircuitPython library bundle for version 10.x and copy the
adafruit_hidfolder from itslibfolder toCIRCUITPY/lib/. - Run
make deploy.
make deploy copies code.py, button_config.py and button_filter.py to the drive and syncs it. It never deletes anything. It looks for the drive at /Volumes/CIRCUITPY on macOS and /media/$USER/CIRCUITPY on Linux. Use make deploy CIRCUITPY=/path/to/CIRCUITPY for anything else. It stops with a message if the drive or lib/adafruit_hid is missing.
Configuration
All settings live in firmware/button_config.py. Times are in microseconds.
| Setting | Default | Meaning |
|---|---|---|
BUTTON_PIN |
"GP2" |
Name of the board pin the button is on |
KEYCODE |
0x68 |
HID usage sent on press (F13) |
PRESS_CONFIRM_US |
1_000 |
How long the line must stay low before a press counts |
RELEASE_CONFIRM_US |
10_000 |
How long the line must stay high before a release counts, and how long it must be high after start-up before the first press |
MIN_HOLD_US |
30_000 |
Minimum time between press and release sent to the laptop |
RESET_DELAY_US |
500_000 |
Wait between releasing all keys and resetting after an error |
MIN_HOLD_US must not be shorter than PRESS_CONFIRM_US. The tests check this. On the board, ButtonFilter raises an error for such a configuration, which ends in the reset loop of the error path.
Bench test
Before the button and cable are attached, a jumper wire from GP2 (pin 4) to GND (pin 3) stands in for the button. Open tools/keytest.html in a browser on the laptop. It shows every key with its code and how long it was held, and needs no internet.
- The LED is off at rest.
- Touch GP2 to GND: the LED lights up and the page shows
F13 down. - Take the wire away: the LED goes off and the page logs
F13with a hold time. - A quick tap of the wire shows up as a press of about 30 ms or more.
- Holding GP2 on GND for 10 s gives one
F13entry of about 10 000 ms, and no repeats. - Hold GP2 on GND, unplug the USB cable and plug it back in while still holding: nothing is sent. Release, then tap: F13 arrives.
- Press the reset button on the Pico (or unplug and replug): the firmware comes back without any key being pressed.
- Repeat the checks with the real button and the full cable attached.
Troubleshooting
No RP2350 or CIRCUITPY drive appears. Many micro-USB cables only carry power. Try a cable you know transfers data. For RP2350, BOOTSEL must be held while plugging in.
CIRCUITPY is missing after installing. The .uf2 did not take or was for another board. Start again from step 1 with the file for exactly your board (Pico 2 or Pico 2 W).
The key does not arrive. Check that CIRCUITPY/lib/adafruit_hid exists and that code.py is on the drive. Connect to the serial console (screen /dev/ttyACM0 on Linux, screen /dev/tty.usbmodem* on macOS) to see errors. The browser tab with tools/keytest.html must have focus. On Linux, some keyboard layouts map F13 to XF86Tools; the code column still shows F13.
The key works but the LED stays dark. The firmware runs without an LED if it cannot open one. On a Pico 2 W the LED sits behind the wireless chip; if that chip does not start, the button still works.
False presses. Raise PRESS_CONFIRM_US (for example to 3_000), and fit the 100 nF capacitor between GP2 and GND at the Pico. False releases during a hold: raise RELEASE_CONFIRM_US.
Development
Requires uv and make. uv sync installs the dev tools.
| Target | What it does |
|---|---|
make check |
Everything below except format, mpy-check and deploy |
make test |
pytest with coverage. Fails below 100 % line and branch coverage for the logic, reports code.py separately |
make lint |
ruff format --check and ruff check |
make format |
Formats and auto-fixes with ruff |
make typecheck |
mypy in strict mode against the stubs in stubs/ |
make compile |
python -m compileall over the firmware |
make mpy-check |
Compiles the firmware with CircuitPython's own mpy-cross (MPY_CROSS=/path/to/mpy-cross) |
make deploy |
Copies the firmware to CIRCUITPY |
firmware/
code.py hardware layer: pin, clock, LED, USB keyboard, error handling
button_config.py all settings
button_filter.py hardware-free state machine (time + pin level in, event out)
stubs/ type stubs for the CircuitPython modules, for mypy
tests/ pytest suite, including code.py against simulated hardware
tools/keytest.html offline key viewer for the bench test
scripts/deploy.sh used by make deploy
.forgejo/workflows/ CI running make check
button_filter.py knows nothing about pins or USB. New behaviour such as a long press or a second button goes there as a new filter or event, and handle_event in code.py maps events to keys.
Design choices and known limitations
- Internal pull-up as well as the external one. The external 2.2 kΩ pull-up sits at the cable side. If the cable is unplugged, GP2 would float and could produce presses; the internal pull-up keeps it high. Next to 2.2 kΩ its effect is negligible.
- "Stable high" at start-up means high for
RELEASE_CONFIRM_US. There is no use for a separate setting yet. - The minimum hold counts from the press event, which comes 1 ms after the contact closed. A release needs both 30 ms since the press and 10 ms of high line.
- Release sends
release_all. There is only one key, and the error path releases everything too. - The requirement "calling with the same timestamp twice gives the same result" is read as: a repeated call never produces an extra event or changes the state, and identical input sequences give identical events. Both are tested.
- The firmware polls the pin; it uses no interrupts. The loop samples as fast as CircuitPython runs it. A spike that falls between two samples is never seen, which is fine. A garbage collection pause of a few milliseconds can delay an event by that much. The 1 ms filter assumes the loop samples more often than every millisecond; with this little code it does.
code.pycallsmain()unconditionally, without anif __name__ == "__main__"guard, so it does not depend on what CircuitPython sets as__name__.- The firmware has type annotations. MicroPython, which CircuitPython builds on, parses annotations and does not evaluate them. The firmware uses only built-in types in annotations and never imports
typing. Verified by compiling all three files withmpy-crossfrom CircuitPython 10.3.2. The.pyifiles instubs/cover the CircuitPython modules for mypy. - If the laptop suspends USB and someone presses the button, sending the report fails. The firmware then resets, as for any other error, and
Keyboardwaits at start-up until USB is connected again. microcontroller.reset()can corruptCIRCUITPYif the laptop is writing to it at that moment. This only happens on an error, and the firmware never writes to the drive..forgejo/workflows/check.ymlrunsmake checkonubuntu-latest, with the same pinned actions other repositories on code.overheid.nl use. Whether the MinEZK organisation has a runner with that label is not known yet.
Licence
EUPL-1.2, see LICENSE.