Last active
April 22, 2023 13:24
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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