]> Repositories - Adafruit_Blinka-hackapet.git/blobdiff - src/keypad.py
add all files, untested
[Adafruit_Blinka-hackapet.git] / src / keypad.py
index b602a68f08e1628e1c688ddd42250993f93ff125..b379c20f137c7e18a74e7c8d6eb239fd52a1c659 100644 (file)
@@ -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