2 Copyright 2012 Jun Wako
3 Copyright 2014 Jack Humbert
4 Copyright 2017 Priyadi Iman Nurcahyo
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
32 /* Set 0 if debouncing isn't needed */
39 static uint16_t debouncing_time;
40 static bool debouncing = false;
43 #if (MATRIX_COLS <= 8)
44 # define print_matrix_header() print("\nr/c 01234567\n")
45 # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
46 # define matrix_bitpop(i) bitpop(matrix[i])
47 # define ROW_SHIFTER ((uint8_t)1)
48 #elif (MATRIX_COLS <= 16)
49 # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
50 # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
51 # define matrix_bitpop(i) bitpop16(matrix[i])
52 # define ROW_SHIFTER ((uint16_t)1)
53 #elif (MATRIX_COLS <= 32)
54 # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
55 # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
56 # define matrix_bitpop(i) bitpop32(matrix[i])
57 # define ROW_SHIFTER ((uint32_t)1)
61 extern const matrix_row_t matrix_mask[];
64 static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
65 static const uint8_t tp_pins[3] = TRACKPOINT_PINS;
67 /* matrix state(1:on, 0:off) */
68 static matrix_row_t matrix[MATRIX_ROWS];
69 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
71 static void init_cols(void);
72 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
73 static void unselect_rows(void);
74 static void select_row(uint8_t row);
75 static void unselect_row(uint8_t row);
78 __attribute__ ((weak))
79 void matrix_init_kb(void) {
83 __attribute__ ((weak))
84 void matrix_scan_kb(void) {
88 __attribute__ ((weak))
89 void matrix_init_user(void) {
92 __attribute__ ((weak))
93 void matrix_scan_user(void) {
97 uint8_t matrix_rows(void) {
102 uint8_t matrix_cols(void) {
106 void matrix_init(void) {
108 // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
109 #if (defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega32U4__))
114 // initialize row and col
118 // initialize matrix state: all keys off
119 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
121 matrix_debouncing[i] = 0;
124 matrix_init_quantum();
127 uint8_t matrix_scan(void)
129 // Set row, read cols
130 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
132 bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
134 if (matrix_changed) {
136 debouncing_time = timer_read();
140 read_cols_on_row(matrix, current_row);
146 if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCE)) {
147 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
148 matrix[i] = matrix_debouncing[i];
154 matrix_scan_quantum();
158 bool matrix_is_modified(void)
161 if (debouncing) return false;
167 bool matrix_is_on(uint8_t row, uint8_t col)
170 return (matrix[row] & ((matrix_row_t)1<<col));
174 matrix_row_t matrix_get_row(uint8_t row)
176 // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
177 // switch blocker installed and the switch is always pressed.
179 return matrix[row] & matrix_mask[row];
185 void matrix_print(void)
187 print_matrix_header();
189 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
190 phex(row); print(": ");
191 print_matrix_row(row);
196 uint8_t matrix_key_count(void)
199 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
200 count += matrix_bitpop(i);
206 #define ROW_MASK 0b11100000
208 static const uint8_t row_bit[MATRIX_ROWS] = {
220 static void init_cols(void)
223 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
224 uint8_t pin = col_pins[x];
225 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
226 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
234 for(uint8_t x = 0; x < 3; x++) {
235 uint8_t pin = tp_pins[x];
236 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
237 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
241 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
243 // Store last value of row prior to reading
244 matrix_row_t last_row_value = current_matrix[current_row];
246 // Clear data in matrix row
247 current_matrix[current_row] = 0;
249 // special case for trackpoint
250 if (current_row == 8) {
251 for(uint8_t tp_index = 0; tp_index < 3; tp_index++) {
253 // Select the TP pin to read (active low)
254 uint8_t pin = tp_pins[tp_index];
255 uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
257 // Populate the matrix row with the state of the col pin
258 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << tp_index);
260 return (last_row_value != current_matrix[current_row]);
263 // Select row and wait for row selecton to stabilize
264 select_row(current_row);
265 _delay_us(5); // without this wait it won't read stable value.
269 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
271 // Select the col pin to read (active low)
272 uint8_t pin = col_pins[col_index];
273 uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
275 // Populate the matrix row with the state of the col pin
276 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
280 unselect_row(current_row);
282 return (last_row_value != current_matrix[current_row]);
285 static void select_row(uint8_t row)
287 PORTF = row_bit[row] | (PORTF & ~ROW_MASK);
290 static void unselect_row(uint8_t row)
294 static void unselect_rows(void)