ROWS = const(12)
COLS = const(16)
N = const(ROWS*COLS)
def scanner():
    def scan1(state):
        for row in range(ROWS):
            counts_by_row[row] = 0

        for column in range(COLS):
            A0.value = column & 1
            A1.value = column & 2
            A2.value = column & 4
            if column & 8:
                EN_U2.value = False
            else:
                EN_U1.value = False

            n = 0
            for row in range(len(ROW)):
                pin = ROW[row]
                i = row + column * ROWS
                # never fill a matrix position with no associated key
                # this improves rollover for instance, A+F1+W cause a ghost
                # at matrix location that is otherwise unused. By not
                # recording a value at the ghost location, the ghost
                # suppression code below won't activate.
                if kc_unused[i]: continue
                value = not pin.value
                n += value
                counts_by_row[row] += value
                state[i] = value
            counts_by_column[column] = n

            if column & 8:
                EN_U2.value = True
            else:
                EN_U1.value = True