Created
December 18, 2024 09:00
-
-
Save piit79/4ff5238be23c73dbb461f1617bb1d5bc to your computer and use it in GitHub Desktop.
Revisions
-
piit79 created this gist
Dec 18, 2024 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,81 @@ import board import digitalio import usb_hid from time import sleep # from adafruit_hid.keyboard import Keyboard # from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS LOOP_INTERVAL = 50 # milliseconds class CachedDigitalInput: def __init__(self, pin, name=None, pull=digitalio.Pull.UP): self.pin = pin self.name = name self.io = digitalio.DigitalInOut(pin) self.io.pull = pull self.value_prev = False @property def value(self) -> bool: return self.io.value @property def value_str(self) -> str: return 'HIGH' if self.value else 'LOW' @property def changed(self) -> bool: return self.value != self.value_prev def save(self): self.value_prev = self.value def xr(start: int, end: int): """Return an inclusive range of integers in a list""" return list(range(start, end+1)) def log(*args): s = ' '.join(str(arg) for arg in args) print(s) # keyboard_layout.write(s + "\n") # sleep(1) # Sleep for a bit to avoid a race condition on some systems # keyboard = Keyboard(usb_hid.devices) # keyboard_layout = KeyboardLayoutUS(keyboard) # We're in the US :) pin_numbers = xr(0, 9) + xr(12, 16) + xr(20, 23) + xr(26, 29) pin_names = [f'D{pn}' for pn in pin_numbers] pins = [] led = digitalio.DigitalInOut(board.LED) led.direction = digitalio.Direction.OUTPUT flash_duration = 50 # milliseconds flash_interval = 1000 # milliseconds off_duration = flash_interval - flash_duration interval = flash_interval - flash_duration for pin_name in pin_names: pin_def = getattr(board, pin_name) i = CachedDigitalInput(pin_def, name=pin_name) i.value_prev = False pins.append(i) ms = 0 while True: for i in pins: if i.changed: log(f'{i.name:-3}', i.value_str) i.save() if ms >= off_duration if not led.value else flash_duration: led.value = not led.value ms = 0 sleep(LOOP_INTERVAL / 1000.0) ms += LOOP_INTERVAL