2018-09-03 05:06:53 +02:00
|
|
|
import digitalio
|
|
|
|
|
2018-10-12 03:02:13 +02:00
|
|
|
from kmk.consts import DiodeOrientation
|
|
|
|
from kmk.event_defs import matrix_changed
|
2018-09-03 05:06:53 +02:00
|
|
|
|
|
|
|
|
2018-10-11 09:29:59 +02:00
|
|
|
class MatrixScanner:
|
2018-09-03 05:06:53 +02:00
|
|
|
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')
|
|
|
|
|
2018-10-11 09:29:59 +02:00
|
|
|
self.cols = cols
|
|
|
|
self.rows = rows
|
2018-10-16 10:04:36 +02:00
|
|
|
self.len_cols = len(cols)
|
|
|
|
self.len_rows = len(rows)
|
|
|
|
|
2018-09-03 05:06:53 +02:00
|
|
|
self.diode_orientation = diode_orientation
|
2018-10-06 10:32:17 +02:00
|
|
|
self.last_pressed_len = 0
|
2018-09-03 05:06:53 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2018-10-16 10:04:36 +02:00
|
|
|
import kmk_keyboard
|
|
|
|
|
|
|
|
self.swap_indicies = getattr(kmk_keyboard, 'swap_indicies', {})
|
|
|
|
self.rollover_cols_every_rows = getattr(
|
|
|
|
kmk_keyboard,
|
|
|
|
'rollover_cols_every_rows',
|
|
|
|
self.len_rows,
|
|
|
|
)
|
|
|
|
|
|
|
|
for k, v in self.swap_indicies.items():
|
|
|
|
self.swap_indicies[v] = k
|
|
|
|
|
2018-10-06 10:32:17 +02:00
|
|
|
def scan_for_pressed(self):
|
|
|
|
pressed = []
|
2018-09-03 05:06:53 +02:00
|
|
|
|
2018-10-06 10:32:17 +02:00
|
|
|
for oidx, opin in enumerate(self.outputs):
|
2018-10-11 09:29:59 +02:00
|
|
|
opin.value(True)
|
2018-10-06 10:32:17 +02:00
|
|
|
|
|
|
|
for iidx, ipin in enumerate(self.inputs):
|
2018-10-11 09:29:59 +02:00
|
|
|
if ipin.value():
|
2018-10-16 10:04:36 +02:00
|
|
|
if self.diode_orientation == DiodeOrientation.ROWS:
|
|
|
|
report_tuple = (oidx, iidx)
|
|
|
|
else:
|
|
|
|
new_oidx = oidx + self.len_cols * (iidx // self.rollover_cols_every_rows)
|
|
|
|
new_iidx = iidx - self.rollover_cols_every_rows * (
|
|
|
|
iidx // self.rollover_cols_every_rows
|
|
|
|
)
|
|
|
|
report_tuple = (new_iidx, new_oidx)
|
|
|
|
|
|
|
|
if report_tuple in self.swap_indicies:
|
|
|
|
report_tuple = self.swap_indicies[report_tuple]
|
|
|
|
|
|
|
|
pressed.append(report_tuple)
|
2018-10-06 10:32:17 +02:00
|
|
|
|
2018-10-11 09:29:59 +02:00
|
|
|
opin.value(False)
|
2018-09-03 05:06:53 +02:00
|
|
|
|
2018-10-06 10:32:17 +02:00
|
|
|
if len(pressed) != self.last_pressed_len:
|
|
|
|
self.last_pressed_len = len(pressed)
|
|
|
|
return matrix_changed(pressed)
|
|
|
|
|
|
|
|
return None # The default, but for explicitness
|