Scan a keyboard with a MCP23017 using an API modelled after the keypad module.
With a Matrix keyboard, columns (inputs) are on port A and rows (outputs) are on port B.
With direct pins buttons, both ports can be used as inputs.
This cannot scan in the background, so performances are somewhat limited, depending on the needs of the application.
This driver depends on:
Please ensure all dependencies are available on the CircuitPython filesystem. This is easily achieved by downloading the Adafruit library and driver bundle or individual libraries can be installed using circup.
Make sure that you have circup
installed in your Python environment.
Install it with the following command if necessary:
pip3 install circup
With circup
installed and your CircuitPython device connected use the
following command to install:
circup install mcp23017_scanner
Or the following command to update an existing version:
circup update
import board
from supervisor import ticks_ms
from adafruit_mcp230xx.mcp23017 import MCP23017
from mcp23017_scanner import McpMatrixScanner
# MCP23017 port A pins for columns
COLUMNS = [ 0, 1, 2, 3, 4 ]
# MCP23017 port B pins for rows
ROWS = [ 0, 1, 2, 3, 4, 5 ]
mcp = MCP23017(board.I2C())
scanner = McpMatrixScanner(mcp, ROWS, COLUMNS, irq=board.D5) # irq is optional
while True:
scanner.update()
while event := scanner.events.get():
key = scanner.key_number_to_row_column(event.key_number)
if event.pressed:
print(f"Key pressed : {key}")
if event.released:
print(f"Key released: {key}")
API documentation for this library can be found on Read the Docs.
For information on building library documentation, please check out this guide.
Contributions are welcome! Please read our Code of Conduct before contributing to help this project stay welcoming.