]>
git.gir.st - tmk_keyboard.git/blob - macway/matrix.c
7 #include <util/delay.h>
12 // matrix is active low. (key on: 0/key off: 1)
13 // row: Hi-Z(unselected)/low output(selected)
14 // PD0, PC7, PD7, PF6, PD6, PD1, PD2, PC6, PF7
15 // col: input w/pullup
18 // matrix state buffer
21 static uint8_t _matrix0
[MATRIX_ROWS
];
22 static uint8_t _matrix1
[MATRIX_ROWS
];
24 static bool matrix_has_ghost_in_row(uint8_t row
);
25 static uint8_t read_col(void);
26 static void unselect_rows(void);
27 static void select_row(uint8_t row
);
42 // this must be called once before matrix_scan.
43 void matrix_init(void)
45 // initialize row and col
50 // initialize matrix state: all keys off
51 for (int i
=0; i
< MATRIX_ROWS
; i
++) _matrix0
[i
] = 0x00;
52 for (int i
=0; i
< MATRIX_ROWS
; i
++) _matrix1
[i
] = 0x00;
54 matrix_prev
= _matrix1
;
65 for (int i
= 0; i
< MATRIX_ROWS
; i
++) {
67 _delay_us(30); // without this wait read unstable value.
68 matrix
[i
] = ~read_col();
74 bool matrix_is_modified(void)
76 for (int i
= 0; i
< MATRIX_ROWS
; i
++) {
77 if (matrix
[i
] != matrix_prev
[i
])
83 bool matrix_has_ghost(void)
85 for (int i
= 0; i
< MATRIX_ROWS
; i
++) {
86 if (matrix_has_ghost_in_row(i
))
93 bool matrix_is_on(int row
, int col
)
95 return (matrix
[row
] & (1<<col
));
99 uint16_t matrix_get_row(int row
)
104 void matrix_print(void)
106 print("\nr/c 01234567\n");
107 for (int row
= 0; row
< matrix_rows(); row
++) {
108 phex(row
); print(": ");
109 pbin_reverse(matrix_get_row(row
));
110 if (matrix_has_ghost_in_row(row
)) {
117 int matrix_key_count(void)
120 for (int i
= 0; i
< MATRIX_ROWS
; i
++) {
121 count
+= bitpop(matrix
[i
]);
126 static bool matrix_has_ghost_in_row(uint8_t row
)
128 // no ghost exists in case less than 2 keys on
129 if (((matrix
[row
] - 1) & matrix
[row
]) == 0)
132 // ghost exists in case same state as other row
133 for (int i
=0; i
< MATRIX_ROWS
; i
++) {
134 if (i
!= row
&& (matrix
[i
] & matrix
[row
]) == matrix
[row
])
140 static uint8_t read_col(void)
145 static void unselect_rows(void)
155 static void select_row(uint8_t row
)