Browse Source

No key repeat on an unsettled release or after a stall

A slow panel update blocks key polling; the first reading after it could
fire a repeat although the key was already up but its release not yet
debounced, so one press moved the playlist selection twice. A repeat now
needs the key down in the current reading, and a reading after a gap
longer than the repeat delay fires none.
k4be 2 tuần trước cách đây
mục cha
commit
2096804762
2 tập tin đã thay đổi với 30 bổ sung và 3 xóa
  1. 14 3
      radio/keypad.py
  2. 16 0
      tests/test_ui.py

+ 14 - 3
radio/keypad.py

@@ -125,8 +125,13 @@ class KeyScanner(object):
         self.count = 0
         self.pressed = 0            # debounced, bit set = key down
         self.repeat_at = {}         # bit: monotonic ms of its next repeat
+        self.last_ms = None         # time of the previous reading
 
     def feed(self, raw, now_ms):
+        # readings stalled (a long panel update holds the main loop): whether
+        # a key was held all along is unknown, so no repeat on this reading
+        stalled = self.last_ms is not None and now_ms - self.last_ms > self.delay_ms
+        self.last_ms = now_ms
         down = raw & KEY_MASK
         self.faulty = self.max_keys is not None and _bits(down) > self.max_keys
         if self.faulty:
@@ -155,9 +160,15 @@ class KeyScanner(object):
                         self.repeat_at.pop(bit, None)
             self.pressed = down
         for bit in sorted(self.repeat_at):
-            if now_ms >= self.repeat_at[bit]:
-                events.append((KEY_PINS[bit], 2))
-                self.repeat_at[bit] = now_ms + self.period_ms
+            if now_ms < self.repeat_at[bit]:
+                continue
+            # only while this reading has it down too: a release still
+            # being debounced is not a held key
+            if stalled or not down & (1 << bit):
+                self.repeat_at[bit] = max(self.repeat_at[bit], now_ms + self.period_ms)
+                continue
+            events.append((KEY_PINS[bit], 2))
+            self.repeat_at[bit] = now_ms + self.period_ms
         return events
 
     def pressed_keys(self):

+ 16 - 0
tests/test_ui.py

@@ -526,6 +526,22 @@ class TestKeypad(unittest.TestCase):
         self.assertEqual(sc.feed(_raw(), 360), [("up", 0)])
         self.assertEqual(sc.feed(_raw(), 1000), [])
 
+    def test_no_repeat_on_release_or_after_stall(self):
+        sc = keypad.KeyScanner()
+        sc.feed(_raw("down"), 0)
+        self.assertEqual(sc.feed(_raw("down"), 20), [("down", 1)])
+        # a long panel update: the next reading comes late and the key is up,
+        # but the release is not debounced yet
+        self.assertEqual(sc.feed(_raw(), 600), [])
+        self.assertEqual(sc.feed(_raw(), 620), [("down", 0)])
+
+        sc.feed(_raw("down"), 1000)
+        self.assertEqual(sc.feed(_raw("down"), 1020), [("down", 1)])
+        # still down after the stall: held, but only a later reading repeats
+        self.assertEqual(sc.feed(_raw("down"), 1600), [])
+        self.assertEqual(sc.feed(_raw("down"), 1620), [])
+        self.assertEqual(sc.feed(_raw("down"), 1640), [("down", 2)])
+
     def test_held_at_start_ignored(self):
         sc = keypad.KeyScanner(ignore_initial=True)
         self.assertEqual(sc.feed(_raw("stop"), 0), [])