-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeypad.py
More file actions
482 lines (406 loc) · 18.4 KB
/
keypad.py
File metadata and controls
482 lines (406 loc) · 18.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
`keypad` - Support for scanning keys and key matrices
===========================================================
See `CircuitPython:keypad` in CircuitPython for more details.
* Author(s): Melissa LeBlanc-Williams
"""
import time
import threading
from collections import deque
import digitalio
class Event:
"""A key transition event."""
def __init__(self, key_number=0, pressed=True):
"""
Create a key transition event, which reports a key-pressed or key-released transition.
:param int key_number: the key number
:param bool pressed: ``True`` if the key was pressed; ``False`` if it was released.
"""
self._key_number = key_number
self._pressed = pressed
@property
def key_number(self):
"""The key number."""
return self._key_number
@property
def pressed(self):
"""
``True`` if the event represents a key down (pressed) transition.
The opposite of `released`.
"""
return self._pressed
@property
def released(self):
"""
``True`` if the event represents a key up (released) transition.
The opposite of `pressed`.
"""
return not self._pressed
def __eq__(self, other):
"""
Two `Event` objects are equal if their `key_number`
and `pressed`/`released` values are equal.
"""
return self.key_number == other.key_number and self.pressed == other.pressed
def __hash__(self):
"""Returns a hash for the `Event`, so it can be used in dictionaries, etc.."""
return hash(self._key_number)
def __repr__(self):
"""Return a textual representation of the object"""
return "<Event: key_number {} {}>".format(
self.key_number, "pressed" if self._pressed else "released"
)
class EventQueue:
"""
A queue of `Event` objects, filled by a `keypad` scanner such as `Keys` or `KeyMatrix`.
You cannot create an instance of `EventQueue` directly. Each scanner creates an
instance when it is created.
"""
def __init__(self, max_events):
self._events = deque([], max_events)
self._overflowed = False
def get(self):
"""
Return the next key transition event. Return ``None`` if no events are pending.
Note that the queue size is limited; see ``max_events`` in the constructor of
a scanner such as `Keys` or `KeyMatrix`.
If a new event arrives when the queue is full, the event is discarded, and
`overflowed` is set to ``True``.
:return: the next queued key transition `Event`
:rtype: Optional[Event]
"""
if not self._events:
return None
return self._events.popleft()
def get_into(self, event):
"""Store the next key transition event in the supplied event, if available,
and return ``True``.
If there are no queued events, do not touch ``event`` and return ``False``.
The advantage of this method over ``get()`` is that it does not allocate storage.
Instead you can reuse an existing ``Event`` object.
Note that the queue size is limited; see ``max_events`` in the constructor of
a scanner such as `Keys` or `KeyMatrix`.
:return ``True`` if an event was available and stored, ``False`` if not.
:rtype: bool
"""
if not self._events:
return False
next_event = self._events.popleft()
# pylint: disable=protected-access
event._key_number = next_event._key_number
event._pressed = next_event._pressed
# pylint: enable=protected-access
return True
def clear(self):
"""
Clear any queued key transition events. Also sets `overflowed` to ``False``.
"""
self._events.clear()
self._overflowed = False
def __bool__(self):
"""``True`` if `len()` is greater than zero.
This is an easy way to check if the queue is empty.
"""
return len(self._events) > 0
def __len__(self):
"""Return the number of events currently in the queue. Used to implement ``len()``."""
return len(self._events)
@property
def overflowed(self):
"""
``True`` if an event could not be added to the event queue because it was full. (read-only)
Set to ``False`` by `clear()`.
"""
return self._overflowed
def keypad_eventqueue_record(self, key_number, current):
"""Record a new event"""
if len(self._events) == self._events.maxlen:
self._overflowed = True
else:
self._events.append(Event(key_number, current))
class _KeysBase:
def __init__(self, interval, max_events, scanning_function):
self._interval = interval
self._last_scan = time.monotonic()
self._events = EventQueue(max_events)
self._scanning_function = scanning_function
self._scan_thread = threading.Thread(target=self._scanning_loop, daemon=True)
self._scan_thread.start()
@property
def events(self):
"""The EventQueue associated with this Keys object. (read-only)"""
return self._events
def deinit(self):
"""Stop scanning"""
if self._scan_thread.is_alive():
self._scan_thread.join()
def __enter__(self):
"""No-op used by Context Managers."""
return self
def __exit__(self, exception_type, exception_value, traceback):
"""
Automatically deinitializes when exiting a context. See
:ref:`lifetime-and-contextmanagers` for more info.
"""
self.deinit()
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()
class Keys(_KeysBase):
"""Manage a set of independent keys."""
def __init__(
self, pins, *, value_when_pressed, pull=True, interval=0.02, max_events=64
):
"""
Create a `Keys` object that will scan keys attached to the given sequence of pins.
Each key is independent and attached to its own pin.
An `EventQueue` is created when this object is created and is available in the
`events` attribute.
:param Sequence[microcontroller.Pin] pins: The pins attached to the keys.
The key numbers correspond to indices into this sequence.
:param bool value_when_pressed: ``True`` if the pin reads high when the key is pressed.
``False`` if the pin reads low (is grounded) when the key is pressed.
All the pins must be connected in the same way.
:param bool pull: ``True`` if an internal pull-up or pull-down should be
enabled on each pin. A pull-up will be used if ``value_when_pressed`` is ``False``;
a pull-down will be used if it is ``True``.
If an external pull is already provided for all the pins, you can set
``pull`` to ``False``.
However, enabling an internal pull when an external one is already present is not
a problem;
it simply uses slightly more current.
:param float interval: Scan keys no more often than ``interval`` to allow for debouncing.
``interval`` is in float seconds. The default is 0.020 (20 msecs).
:param int max_events: maximum size of `events` `EventQueue`:
maximum number of key transition events that are saved.
Must be >= 1.
If a new event arrives when the queue is full, the oldest event is discarded.
"""
self._digitalinouts = []
for pin in pins:
dio = digitalio.DigitalInOut(pin)
if pull:
dio.pull = (
digitalio.Pull.DOWN if value_when_pressed else digitalio.Pull.UP
)
self._digitalinouts.append(dio)
self._currently_pressed = [False] * len(pins)
self._previously_pressed = [False] * len(pins)
self._value_when_pressed = value_when_pressed
super().__init__(interval, max_events, self._keypad_keys_scan)
def deinit(self):
"""Stop scanning and release the pins."""
super().deinit()
for dio in self._digitalinouts:
dio.deinit()
def reset(self):
"""Reset the internal state of the scanner to assume that all keys are now released.
Any key that is already pressed at the time of this call will therefore immediately cause
a new key-pressed event to occur.
"""
self._currently_pressed = self._previously_pressed = [False] * self.key_count
@property
def key_count(self):
"""The number of keys that are being scanned. (read-only)"""
return len(self._digitalinouts)
def _keypad_keys_scan(self):
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
self._currently_pressed[key_number] = current
if self._previously_pressed[key_number] != current:
self._events.keypad_eventqueue_record(key_number, current)
class KeyMatrix(_KeysBase):
"""Manage a 2D matrix of keys with row and column pins."""
# pylint: disable=too-many-arguments
def __init__(
self,
row_pins,
column_pins,
columns_to_anodes=True,
interval=0.02,
max_events=64,
):
"""
Create a `Keys` object that will scan the key matrix attached to the given row and
column pins.
There should not be any external pull-ups or pull-downs on the matrix:
``KeyMatrix`` enables internal pull-ups or pull-downs on the pins as necessary.
The keys are numbered sequentially from zero. A key number can be computed
by ``row * len(column_pins) + column``.
An `EventQueue` is created when this object is created and is available in the `events`
attribute.
:param Sequence[microcontroller.Pin] row_pins: The pins attached to the rows.
:param Sequence[microcontroller.Pin] column_pins: The pins attached to the colums.
:param bool columns_to_anodes: Default ``True``.
If the matrix uses diodes, the diode anodes are typically connected to the column pins,
and the cathodes should be connected to the row pins. If your diodes are reversed,
set ``columns_to_anodes`` to ``False``.
:param float interval: Scan keys no more often than ``interval`` to allow for debouncing.
``interval`` is in float seconds. The default is 0.020 (20 msecs).
:param int max_events: maximum size of `events` `EventQueue`:
maximum number of key transition events that are saved.
Must be >= 1.
If a new event arrives when the queue is full, the oldest event is discarded.
"""
self._row_digitalinouts = []
for row_pin in row_pins:
row_dio = digitalio.DigitalInOut(row_pin)
row_dio.switch_to_input(
pull=(digitalio.Pull.UP if columns_to_anodes else digitalio.Pull.DOWN)
)
self._row_digitalinouts.append(row_dio)
self._column_digitalinouts = []
for column_pin in column_pins:
col_dio = digitalio.DigitalInOut(column_pin)
col_dio.switch_to_input(
pull=(digitalio.Pull.UP if columns_to_anodes else digitalio.Pull.DOWN)
)
self._column_digitalinouts.append(col_dio)
self._currently_pressed = [False] * len(column_pins) * len(row_pins)
self._previously_pressed = [False] * len(column_pins) * len(row_pins)
self._columns_to_anodes = columns_to_anodes
super().__init__(interval, max_events, self._keypad_keymatrix_scan)
# pylint: enable=too-many-arguments
@property
def key_count(self):
"""The number of keys that are being scanned. (read-only)"""
return len(self._row_digitalinouts) * len(self._column_digitalinouts)
def deinit(self):
"""Stop scanning and release the pins."""
super().deinit()
for row_dio in self._row_digitalinouts:
row_dio.deinit()
for col_dio in self._column_digitalinouts:
col_dio.deinit()
def reset(self):
"""
Reset the internal state of the scanner to assume that all keys are now released.
Any key that is already pressed at the time of this call will therefore immediately cause
a new key-pressed event to occur.
"""
self._previously_pressed = self._currently_pressed = [False] * self.key_count
def _row_column_to_key_number(self, row, column):
return row * len(self._column_digitalinouts) + column
def _keypad_keymatrix_scan(self):
for row, row_dio in enumerate(self._row_digitalinouts):
row_dio.switch_to_output(
value=(not self._columns_to_anodes),
drive_mode=digitalio.DriveMode.PUSH_PULL,
)
for col, col_dio in enumerate(self._column_digitalinouts):
key_number = self._row_column_to_key_number(row, col)
self._previously_pressed[key_number] = self._currently_pressed[
key_number
]
current = col_dio.value != self._columns_to_anodes
self._currently_pressed[key_number] = current
if self._previously_pressed[key_number] != current:
self._events.keypad_eventqueue_record(key_number, current)
row_dio.value = self._columns_to_anodes
row_dio.switch_to_input(
pull=(
digitalio.Pull.UP
if self._columns_to_anodes
else digitalio.Pull.DOWN
)
)
class ShiftRegisterKeys(_KeysBase):
"""Manage a set of keys attached to an incoming shift register."""
def __init__(
self,
*,
clock,
data,
latch,
value_to_latch=True,
key_count,
value_when_pressed,
interval=0.02,
max_events=64,
):
"""
Create a `Keys` object that will scan keys attached to a parallel-in serial-out
shift register like the 74HC165 or CD4021.
Note that you may chain shift registers to load in as many values as you need.
Key number 0 is the first (or more properly, the zero-th) bit read. In the
74HC165, this bit is labeled ``Q7``. Key number 1 will be the value of ``Q6``, etc.
An `EventQueue` is created when this object is created and is available in the
`events` attribute.
:param microcontroller.Pin clock: The shift register clock pin.
The shift register should clock on a low-to-high transition.
:param microcontroller.Pin data: the incoming shift register data pin
:param microcontroller.Pin latch:
Pin used to latch parallel data going into the shift register.
:param bool value_to_latch: Pin state to latch data being read.
``True`` if the data is latched when ``latch`` goes high
``False`` if the data is latched when ``latch goes low.
The default is ``True``, which is how the 74HC165 operates. The CD4021 latch is
the opposite. Once the data is latched, it will be shifted out by toggling the
clock pin.
:param int key_count: number of data lines to clock in
:param bool value_when_pressed: ``True`` if the pin reads high when the key is pressed.
``False`` if the pin reads low (is grounded) when the key is pressed.
:param float interval: Scan keys no more often than ``interval`` to allow for debouncing.
``interval`` is in float seconds. The default is 0.020 (20 msecs).
:param int max_events: maximum size of `events` `EventQueue`:
maximum number of key transition events that are saved.
Must be >= 1.
If a new event arrives when the queue is full, the oldest event is discarded.
"""
clock_dio = digitalio.DigitalInOut(clock)
clock_dio.switch_to_output(
value=False, drive_mode=digitalio.DriveMode.PUSH_PULL
)
self._clock = clock_dio
data_dio = digitalio.DigitalInOut(data)
data_dio.switch_to_input()
self._data = data_dio
latch_dio = digitalio.DigitalInOut(latch)
latch_dio.switch_to_output(value=True, drive_mode=digitalio.DriveMode.PUSH_PULL)
self._latch = latch_dio
self._value_to_latch = value_to_latch
self._currently_pressed = [False] * key_count
self._previously_pressed = [False] * key_count
self._value_when_pressed = value_when_pressed
self._key_count = key_count
super().__init__(interval, max_events, self._keypad_shiftregisterkeys_scan)
def deinit(self):
"""Stop scanning and release the pins."""
super().deinit()
self._clock.deinit()
self._data.deinit()
self._latch.deinit()
def reset(self):
"""
Reset the internal state of the scanner to assume that all keys are now released.
Any key that is already pressed at the time of this call will therefore immediately cause
a new key-pressed event to occur.
"""
self._currently_pressed = self._previously_pressed = [False] * self._key_count
@property
def key_count(self):
"""The number of keys that are being scanned. (read-only)"""
return self._key_count
@property
def events(self):
"""The ``EventQueue`` associated with this `Keys` object. (read-only)"""
return self._events
def _keypad_shiftregisterkeys_scan(self):
self._latch.value = self._value_to_latch
for key_number in range(self._key_count):
self._clock.value = False
self._previously_pressed[key_number] = self._currently_pressed[key_number]
current = self._data.value == self._value_when_pressed
self._currently_pressed[key_number] = current
self._clock.value = True
if self._previously_pressed[key_number] != current:
self._events.keypad_eventqueue_record(key_number, current)
self._latch.value = not self._value_to_latch