A basic 2x2 matrix that can auto-flash to a Feather with a compatible bootloader
This commit is contained in:
0
kmk/__init__.py
Normal file
0
kmk/__init__.py
Normal file
0
kmk/circuitpython/__init__.py
Normal file
0
kmk/circuitpython/__init__.py
Normal file
61
kmk/circuitpython/matrix.py
Normal file
61
kmk/circuitpython/matrix.py
Normal file
@@ -0,0 +1,61 @@
|
||||
import digitalio
|
||||
|
||||
from kmk.common.consts import DiodeOrientation
|
||||
|
||||
|
||||
class MatrixScanner:
|
||||
def __init__(self, cols, rows, diode_orientation=DiodeOrientation.COLUMNS):
|
||||
# A pin cannot be both a row and column, detect this by combining the
|
||||
# two tuples into a set and validating that the length did not drop
|
||||
#
|
||||
# repr() hackery is because CircuitPython Pin objects are not hashable
|
||||
unique_pins = {repr(c) for c in cols} | {repr(r) for r in rows}
|
||||
if len(unique_pins) != len(cols) + len(rows):
|
||||
raise ValueError('Cannot use a pin as both a column and row')
|
||||
|
||||
self.cols = [digitalio.DigitalInOut(pin) for pin in cols]
|
||||
self.rows = [digitalio.DigitalInOut(pin) for pin in rows]
|
||||
self.diode_orientation = diode_orientation
|
||||
|
||||
if self.diode_orientation == DiodeOrientation.COLUMNS:
|
||||
self.outputs = self.cols
|
||||
self.inputs = self.rows
|
||||
elif self.diode_orientation == DiodeOrientation.ROWS:
|
||||
self.outputs = self.rows
|
||||
self.inputs = self.cols
|
||||
else:
|
||||
raise ValueError('Invalid DiodeOrientation: {}'.format(
|
||||
self.diode_orientation,
|
||||
))
|
||||
|
||||
for pin in self.outputs:
|
||||
pin.switch_to_output()
|
||||
|
||||
for pin in self.inputs:
|
||||
pin.switch_to_input(pull=digitalio.Pull.DOWN)
|
||||
|
||||
def _normalize_matrix(self, matrix):
|
||||
'''
|
||||
We always want to internally look at a keyboard as a list of rows,
|
||||
where a "row" is a list of keycodes (columns).
|
||||
|
||||
This will convert DiodeOrientation.COLUMNS matrix scans into a
|
||||
ROWS scan, so we never have to think about these things again.
|
||||
'''
|
||||
if self.diode_orientation == DiodeOrientation.ROWS:
|
||||
return matrix
|
||||
|
||||
return [
|
||||
[col[col_entry] for col in matrix]
|
||||
for col_entry in range(max(len(col) for col in matrix))
|
||||
]
|
||||
|
||||
def raw_scan(self):
|
||||
matrix = []
|
||||
|
||||
for opin in self.outputs:
|
||||
opin.value = True
|
||||
matrix.append([ipin.value for ipin in self.inputs])
|
||||
opin.value = False
|
||||
|
||||
return self._normalize_matrix(matrix)
|
22
kmk/circuitpython/util.py
Normal file
22
kmk/circuitpython/util.py
Normal file
@@ -0,0 +1,22 @@
|
||||
import board
|
||||
import digitalio
|
||||
import time
|
||||
import sys
|
||||
|
||||
|
||||
def feather_signal_error_with_led_flash(rate=0.5):
|
||||
'''
|
||||
Flash the red LED for 10 seconds, alternating every $rate
|
||||
Could be useful as an uncaught exception handler later on,
|
||||
but is for now unused
|
||||
'''
|
||||
|
||||
rled = digitalio.DigitalInOut(board.LED1)
|
||||
rled.direction = digitalio.Direction.OUTPUT
|
||||
|
||||
# blink for 5 seconds and exit
|
||||
for cycle in range(10):
|
||||
rled.value = cycle % 2
|
||||
time.sleep(rate)
|
||||
|
||||
sys.exit(1)
|
0
kmk/common/__init__.py
Normal file
0
kmk/common/__init__.py
Normal file
9
kmk/common/consts.py
Normal file
9
kmk/common/consts.py
Normal file
@@ -0,0 +1,9 @@
|
||||
class DiodeOrientation:
|
||||
'''
|
||||
Orientation of diodes on handwired boards. You can think of:
|
||||
COLUMNS = vertical
|
||||
ROWS = horizontal
|
||||
'''
|
||||
|
||||
COLUMNS = 0
|
||||
ROWS = 1
|
18
kmk/common/keymap.py
Normal file
18
kmk/common/keymap.py
Normal file
@@ -0,0 +1,18 @@
|
||||
class Keymap:
|
||||
def __init__(self, map):
|
||||
self.map = map
|
||||
self.state = [
|
||||
[False for _ in row]
|
||||
for row in self.map
|
||||
]
|
||||
|
||||
def parse(self, matrix):
|
||||
for ridx, row in enumerate(matrix):
|
||||
for cidx, col in enumerate(row):
|
||||
if col != self.state[ridx][cidx]:
|
||||
print('{}: {}'.format(
|
||||
'KEYDOWN' if col else 'KEYUP',
|
||||
self.map[ridx][cidx],
|
||||
))
|
||||
|
||||
self.state = matrix
|
0
kmk/contrib/__init__.py
Normal file
0
kmk/contrib/__init__.py
Normal file
0
kmk/micropython/__init__.py
Normal file
0
kmk/micropython/__init__.py
Normal file
Reference in New Issue
Block a user