X-Git-Url: https://git.ayoreis.com/Adafruit_Blinka-hackapet.git/blobdiff_plain/d8fb7b4e16ecf2b114845a9915ea8c6d26a80e8b..41cdc3d72f6e55025d173bccc679e44d593ba8b7:/src/keypad.py diff --git a/src/keypad.py b/src/keypad.py index b602a68..b379c20 100644 --- a/src/keypad.py +++ b/src/keypad.py @@ -1,3 +1,6 @@ +# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries +# +# SPDX-License-Identifier: MIT """ `keypad` - Support for scanning keys and key matrices =========================================================== @@ -180,8 +183,11 @@ class _KeysBase: def _scanning_loop(self): while True: + remaining_delay = self._interval - (time.monotonic() - self._last_scan) + if remaining_delay > 0: + time.sleep(remaining_delay) + self._last_scan = time.monotonic() self._scanning_function() - time.sleep(0.001) class Keys(_KeysBase): @@ -251,10 +257,6 @@ class Keys(_KeysBase): return len(self._digitalinouts) def _keypad_keys_scan(self): - if time.monotonic() - self._last_scan < self._interval: - return - self._last_scan = time.monotonic() - for key_number, dio in enumerate(self._digitalinouts): self._previously_pressed[key_number] = self._currently_pressed[key_number] current = dio.value == self._value_when_pressed @@ -348,10 +350,6 @@ class KeyMatrix(_KeysBase): return row * len(self._column_digitalinouts) + column def _keypad_keymatrix_scan(self): - if time.monotonic() - self._last_scan < self._interval: - return - self._last_scan = time.monotonic() - for row, row_dio in enumerate(self._row_digitalinouts): row_dio.switch_to_output( value=(not self._columns_to_anodes), @@ -389,7 +387,7 @@ class ShiftRegisterKeys(_KeysBase): key_count, value_when_pressed, interval=0.02, - max_events=64 + max_events=64, ): """ Create a `Keys` object that will scan keys attached to a parallel-in serial-out @@ -467,14 +465,10 @@ class ShiftRegisterKeys(_KeysBase): @property def events(self): - """The `EventQueue` associated with this `Keys` object. (read-only)""" + """The ``EventQueue`` associated with this `Keys` object. (read-only)""" return self._events def _keypad_shiftregisterkeys_scan(self): - if time.monotonic() - self._last_scan < self._interval: - return - self._last_scan = time.monotonic() - self._latch.value = self._value_to_latch for key_number in range(self._key_count): self._clock.value = False