Skip to content

Commit 9a9b0a2

Browse files
committed
Keyboard: Allow simultaneous keystrokes.
1 parent 6422422 commit 9a9b0a2

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

v3/primitives/events.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,12 @@ def __init__(self, rowpins, colpins, *, buffer=bytearray(10), db_delay=50):
176176
self._state = 0 # State of all keys as bitmap
177177
for opin in self.rowpins: # Initialise output pins
178178
opin(0)
179-
asyncio.create_task(self.scan(db_delay))
179+
asyncio.create_task(self.scan(len(rowpins) * len(colpins), db_delay))
180180

181181
def __getitem__(self, scan_code):
182182
return bool(self._state & (1 << scan_code))
183183

184-
async def scan(self, db_delay):
184+
async def scan(self, nkeys, db_delay):
185185
while True:
186186
cur = 0 # Current bitmap of key states
187187
for opin in self.rowpins:
@@ -190,16 +190,14 @@ async def scan(self, db_delay):
190190
cur <<= 1
191191
cur |= ipin()
192192
opin(0)
193-
pressed = cur & ~self._state # Newly pressed
194-
if pressed: # There is a newly pressed button
195-
sc = 0 # Find its scan code
196-
while not pressed & 1:
193+
if pressed := (cur & ~self._state): # 1's are newly pressed button(s)
194+
for sc in range(nkeys):
195+
if pressed & 1:
196+
try:
197+
self.put_nowait(sc)
198+
except IndexError: # q full. Overwrite oldest
199+
pass
197200
pressed >>= 1
198-
sc += 1
199-
try:
200-
self.put_nowait(sc)
201-
except IndexError: # q full. Overwrite oldest
202-
pass
203201
changed = cur ^ self._state # Any new press or release
204202
self._state = cur
205203
await asyncio.sleep_ms(db_delay if changed else 0) # Wait out bounce

0 commit comments

Comments
 (0)