Skip to content

Instantly share code, notes, and snippets.

@Neradoc
Last active April 22, 2023 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Neradoc/ca0dcc382aaa328b0c34a64791a6885e to your computer and use it in GitHub Desktop.
Save Neradoc/ca0dcc382aaa328b0c34a64791a6885e to your computer and use it in GitHub Desktop.
Concept of a resilient HID keyboard. Restarts when USB is lost. Waits for UAB to be back.
"""
Resilient HID:
Test at the start to wait for the USB connection to be available
"""
import microcontroller
import supervisor
import time
# wait for connection
while not supervisor.runtime.usb_connected:
# blink a warning
time.sleep(1)
"""
Catch any error caused by the disconnection and reset the board to try to re-enumerate
"""
try:
# put here your main loop
#
except OSError:
# reset if we lost USB
if not supervisor.runtime.usb_connected:
# show some warning maybe
print("Resetting because of an error")
time.sleep(2)
microcontroller.reset()
# raise the error if it's something else
raise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment