kmk_firmware/docs/scanners.md
Ellie 57ba0fe8b2 Custom matrix scanners
- Introduce Scanner base class for MatrixScanner
- Create new Scanner using built-in keypad module
- Allow overriding the scanner used by KMKKeyboard
2022-02-20 12:00:15 -08:00

67 lines
2.1 KiB
Markdown

# Scanners
Smaller boards and macro pads sometimes assign a GPIO pin to each key, rather
than using a full matrix. Boards like this aren't compatible with the default
matrix scanner, so you will need to swap it out with an alternative scanner.
Beside the default `Matrix` scanner, KMK includes the following:
## keypad.Keys
The `keypad.Keys` scanner treats individual GPIO pins as discrete keys. To use
this scanner, provide a sequence of pins that describes the layout of your
board then include it in the initialisation sequence of your keyboard class.
Since the `_init_sanity_check` method in the `KMKKeyboard` class will attempt
to validate a number of settings that are no longer needed, you need to
override it with a modified version that doesn't fail when they aren't present.
```python
import board
from kmk.kmk_keyboard import KMKKeyboard
from kmk.scanners.native_keypad_scanner import keys_scanner
# GPIO to key mapping - each line is a new row.
_KEY_CFG = [
[board.SW3, board.SW7, board.SW11, board.SW15],
[board.SW2, board.SW6, board.SW10, board.SW14],
[board.SW1, board.SW5, board.SW9, board.SW13],
[board.SW0, board.SW4, board.SW8, board.SW12],
]
# Keyboard implementation class
class MyKeyboard(KMKKeyboard):
def __init__(self):
# create and register the scanner
self.matrix = keys_scanner(_KEY_CFG)
# copy the key coordinates from the scanner
self.coord_mapping = self.matrix.coord_mapping
def _init_sanity_check(self):
return self
```
## keypad.KeyMatrix
The `keypad.KeyMatrix` scanner is an alternative implementation of the default
matrix scanner using CircuitPython's builtin keypad objects. This is currently
experimental and ***not recommended for use***.
Using this scanner is similar to the `keypad.Keys` scanner. Create the scanner
using `keypad_matrix()` instead of `keys_scanner()`.
## `Scanner` base class
If you require a different type of scanner, you can create your own by
providing a subclass of `Scanner`. This is a very simple interface, it only
contains a single method, `scan_for_changes(self)` which returns a key report
if one exists, or `None` otherwise.