From 20bcfcdbb959a29db74cfa78fca0dcd3778fe3df Mon Sep 17 00:00:00 2001 From: xs5871 Date: Mon, 6 Mar 2023 20:57:33 +0000 Subject: [PATCH] Add boilerplate method for board configuration at boot time --- kmk/bootcfg.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 kmk/bootcfg.py diff --git a/kmk/bootcfg.py b/kmk/bootcfg.py new file mode 100644 index 0000000..4425788 --- /dev/null +++ b/kmk/bootcfg.py @@ -0,0 +1,60 @@ +import digitalio +import microcontroller + + +def bootcfg( + sense, + source=None, + no_cdc=True, + no_hid=False, + no_midi=True, + no_storage=True, + usb_id=None, +): + + if isinstance(sense, microcontroller.Pin): + sense = digitalio.DigitalInOut(sense) + sense.direction = digitalio.Direction.INPUT + sense.pull = digitalio.Pull.UP + + if isinstance(source, microcontroller.Pin): + source = digitalio.DigitalInOut(source) + source.direction = digitalio.Direction.OUTPUT + source.value = False + else: + return False + + # sense pulled low -> skip boot configuration + if not sense.value: + return False + + if no_cdc: + import usb_cdc + + usb_cdc.disable() + + if no_hid: + import usb_hid + + usb_hid.disable() + + if no_midi: + import usb_midi + + usb_midi.disable() + + if isinstance(usb_id, tuple): + import supervisor + + if hasattr(supervisor, 'set_usb_identification'): + supervisor.set_usb_identification(*usb_id) + + # The no_storage entry is intentionally evaluated last to ensure the drive + # is mountable and rescueable, in case any of the previous code throws an + # exception. + if no_storage: + import storage + + storage.disable_usb_drive() + + return True