Skip to content

Commit d549e5b

Browse files
committed
Keyboard: Invert pin states for open drain output.
1 parent 9a9b0a2 commit d549e5b

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

v3/docs/EVENTS.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -494,18 +494,18 @@ from primitives import Keyboard
494494
```
495495
A `Keyboard` provides an interface to a set of pushbuttons arranged as a
496496
crosspoint array. If a key is pressed its array index (scan code) is placed on a
497-
queue. Keypresses are retrieved with `async for`. The driver operates by
498-
polling each row, reading the response of each column. N-key rollover is
499-
supported - this is the case where a key is pressed before the prior key has
500-
been released.
497+
queue. Keypresses are retrieved with `async for`. The driver operates by
498+
polling each row, reading the response of each column. 1-key rollover is
499+
supported - this is the case where a key is pressed before the prior key has
500+
been released.
501501

502-
Example usage:
502+
Example usage:
503503
```python
504504
import asyncio
505505
from primitives import Keyboard
506506
from machine import Pin
507-
rowpins = [Pin(p, Pin.OUT) for p in range(10, 14)]
508-
colpins = [Pin(p, Pin.IN, Pin.PULL_DOWN) for p in range(16, 20)]
507+
rowpins = [Pin(p, Pin.OPEN_DRAIN) for p in range(10, 14)]
508+
colpins = [Pin(p, Pin.IN, Pin.PULL_UP) for p in range(16, 20)]
509509

510510
async def main():
511511
kp = Keyboard(rowpins, colpins)
@@ -553,7 +553,7 @@ async def repeat(tim, uart, ch): # Send at least one char
553553
await tim.wait()
554554

555555
async def main(): # Run forever
556-
rowpins = [Pin(p, Pin.OUT) for p in range(10, 14)]
556+
rowpins = [Pin(p, Pin.OPEN_DRAIN) for p in range(10, 14)]
557557
colpins = [Pin(p, Pin.IN, Pin.PULL_DOWN) for p in range(16, 20)]
558558
uart = UART(0, 9600, tx=0, rx=1)
559559
pad = Keyboard(rowpins, colpins)
@@ -570,10 +570,10 @@ asyncio.run(main())
570570
```
571571
##### Application note
572572

573-
Scanning of the keyboard occurs rapidly, and built-in pull-down resistors have a
573+
Scanning of the keyboard occurs rapidly, and built-in pull-up resistors have a
574574
high value. If the capacitance between wires is high, spurious keypresses may be
575-
registed. To prevent this it is wise to add physical resistors between the input
576-
pins and gnd. A value in the region of 1KΩ to 5KΩ is recommended.
575+
registered. To prevent this it is wise to add physical resistors between the
576+
input pins and 3.3V. A value in the region of 1KΩ to 5KΩ is recommended.
577577

578578
###### [Contents](./EVENTS.md#0-contents)
579579

v3/primitives/events.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,21 +175,21 @@ def __init__(self, rowpins, colpins, *, buffer=bytearray(10), db_delay=50):
175175
self.colpins = colpins
176176
self._state = 0 # State of all keys as bitmap
177177
for opin in self.rowpins: # Initialise output pins
178-
opin(0)
178+
opin(1)
179179
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

184184
async def scan(self, nkeys, db_delay):
185185
while True:
186-
cur = 0 # Current bitmap of key states
186+
cur = 0 # Current bitmap of logical key states
187187
for opin in self.rowpins:
188-
opin(1) # Assert output
188+
opin(0) # Assert output
189189
for ipin in self.colpins:
190190
cur <<= 1
191-
cur |= ipin()
192-
opin(0)
191+
cur |= ipin() ^ 1 # Convert physical to logical
192+
opin(1)
193193
if pressed := (cur & ~self._state): # 1's are newly pressed button(s)
194194
for sc in range(nkeys):
195195
if pressed & 1:

0 commit comments

Comments
 (0)