Skip to content

Commit 219c94e

Browse files
committed
Keyboard class: add __getitem__.
1 parent fb7f206 commit 219c94e

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

v3/docs/EVENTS.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,10 +519,16 @@ asyncio.run(main())
519519
Constructor mandatory args:
520520
* `rowpins` A list or tuple of initialised output pins.
521521
* `colpins` A list or tuple of initialised input pins (pulled down).
522+
522523
Constructor optional keyword only args:
523524
* `buffer=bytearray(10)` Keyboard buffer.
524525
* `db_delay=50` Debounce delay in ms.
525526

527+
Magic method:
528+
* `__getitem__(self, scan_code)` Return the state of a given pin. Enables code
529+
that causes actions after a button press, for example on release or auto-repeat
530+
while pressed.
531+
526532
The `Keyboard` class is subclassed from [Ringbuf queue](./EVENTS.md#7-ringbuf-queue)
527533
enabling scan codes to be retrieved with an asynchronous iterator.
528534

@@ -532,6 +538,13 @@ is not removed from the buffer, on overflow the oldest scan code is discarded.
532538
There is no limit on the number of rows or columns however if more than 256 keys
533539
are used, the `buffer` arg would need to be adapted to handle scan codes > 255.
534540

541+
##### Application note
542+
543+
Scanning of the keyboard occurs rapidly, and built-in pull-down resistors have a
544+
high value. If the capacitance between wires is high, spurious keypresses may be
545+
registed. To prevent this it is wise to add physical resistors between the input
546+
pins and gnd. A value in the region of 1KΩ to 5KΩ is recommended.
547+
535548
###### [Contents](./EVENTS.md#0-contents)
536549

537550
# 7. Ringbuf Queue

v3/primitives/events.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,15 @@ def __init__(self, rowpins, colpins, *, buffer=bytearray(10), db_delay=50):
174174
self.rowpins = rowpins
175175
self.colpins = colpins
176176
self.db_delay = db_delay # Deounce delay in ms
177+
self._state = 0 # State of all keys as bit array
177178
for opin in self.rowpins: # Initialise output pins
178179
opin(0)
179180
asyncio.create_task(self.scan(len(rowpins) * len(colpins)))
180181

182+
def __getitem__(self, scan_code):
183+
return bool(self._state & (1 << scan_code))
184+
181185
async def scan(self, nbuttons):
182-
prev = 0
183186
while True:
184187
await asyncio.sleep_ms(0)
185188
cur = 0
@@ -189,9 +192,9 @@ async def scan(self, nbuttons):
189192
cur <<= 1
190193
cur |= ipin()
191194
opin(0)
192-
if cur != prev: # State change
193-
pressed = cur & ~prev
194-
prev = cur
195+
if cur != self._state: # State change
196+
pressed = cur & ~self._state
197+
self._state = cur
195198
if pressed: # Ignore button release
196199
for v in range(nbuttons): # Find button index
197200
if pressed & 1:

0 commit comments

Comments
 (0)