2 Copyright 2012 Jun Wako <wakojun@gmail.com>
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "split_util.h"
28 #include "split_flags.h"
31 #include "transport.h"
33 #if (MATRIX_COLS <= 8)
34 # define print_matrix_header() print("\nr/c 01234567\n")
35 # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
36 # define matrix_bitpop(i) bitpop(matrix[i])
37 # define ROW_SHIFTER ((uint8_t)1)
38 #elif (MATRIX_COLS <= 16)
39 # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
40 # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
41 # define matrix_bitpop(i) bitpop16(matrix[i])
42 # define ROW_SHIFTER ((uint16_t)1)
43 #elif (MATRIX_COLS <= 32)
44 # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
45 # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
46 # define matrix_bitpop(i) bitpop32(matrix[i])
47 # define ROW_SHIFTER ((uint32_t)1)
50 #define ERROR_DISCONNECT_COUNT 5
52 #define ROWS_PER_HAND (MATRIX_ROWS / 2)
55 static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS;
57 static pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
58 static pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
61 /* matrix state(1:on, 0:off) */
62 static matrix_row_t matrix[MATRIX_ROWS];
63 static matrix_row_t raw_matrix[ROWS_PER_HAND];
65 // row offsets for each hand
66 uint8_t thisHand, thatHand;
68 // user-defined overridable functions
70 __attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); }
72 __attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); }
74 __attribute__((weak)) void matrix_init_user(void) {}
76 __attribute__((weak)) void matrix_scan_user(void) {}
78 __attribute__((weak)) void matrix_slave_scan_user(void) {}
82 inline uint8_t matrix_rows(void) { return MATRIX_ROWS; }
84 inline uint8_t matrix_cols(void) { return MATRIX_COLS; }
86 bool matrix_is_modified(void) {
87 if (debounce_active()) return false;
91 inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); }
93 inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
95 void matrix_print(void) {
96 print_matrix_header();
98 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
101 print_matrix_row(row);
106 uint8_t matrix_key_count(void) {
108 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
109 count += matrix_bitpop(i);
118 static void init_pins(void) {
119 for (int row = 0; row < MATRIX_ROWS; row++) {
120 for (int col = 0; col < MATRIX_COLS; col++) {
121 pin_t pin = direct_pins[row][col];
123 setPinInputHigh(pin);
129 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
130 matrix_row_t last_row_value = current_matrix[current_row];
131 current_matrix[current_row] = 0;
133 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
134 pin_t pin = direct_pins[current_row][col_index];
136 current_matrix[current_row] |= readPin(pin) ? 0 : (ROW_SHIFTER << col_index);
140 return (last_row_value != current_matrix[current_row]);
143 #elif (DIODE_DIRECTION == COL2ROW)
145 static void select_row(uint8_t row) {
146 setPinOutput(row_pins[row]);
147 writePinLow(row_pins[row]);
150 static void unselect_row(uint8_t row) { setPinInputHigh(row_pins[row]); }
152 static void unselect_rows(void) {
153 for (uint8_t x = 0; x < ROWS_PER_HAND; x++) {
154 setPinInputHigh(row_pins[x]);
158 static void init_pins(void) {
160 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
161 setPinInputHigh(col_pins[x]);
165 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
166 // Store last value of row prior to reading
167 matrix_row_t last_row_value = current_matrix[current_row];
169 // Clear data in matrix row
170 current_matrix[current_row] = 0;
172 // Select row and wait for row selecton to stabilize
173 select_row(current_row);
177 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
178 // Populate the matrix row with the state of the col pin
179 current_matrix[current_row] |= readPin(col_pins[col_index]) ? 0 : (ROW_SHIFTER << col_index);
183 unselect_row(current_row);
185 return (last_row_value != current_matrix[current_row]);
188 #elif (DIODE_DIRECTION == ROW2COL)
190 static void select_col(uint8_t col) {
191 setPinOutput(col_pins[col]);
192 writePinLow(col_pins[col]);
195 static void unselect_col(uint8_t col) { setPinInputHigh(col_pins[col]); }
197 static void unselect_cols(void) {
198 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
199 setPinInputHigh(col_pins[x]);
203 static void init_pins(void) {
205 for (uint8_t x = 0; x < ROWS_PER_HAND; x++) {
206 setPinInputHigh(row_pins[x]);
210 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
211 bool matrix_changed = false;
213 // Select col and wait for col selecton to stabilize
214 select_col(current_col);
218 for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) {
219 // Store last value of row prior to reading
220 matrix_row_t last_row_value = current_matrix[row_index];
222 // Check row pin state
223 if (readPin(row_pins[row_index])) {
224 // Pin HI, clear col bit
225 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
227 // Pin LO, set col bit
228 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
231 // Determine if the matrix changed state
232 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
233 matrix_changed = true;
238 unselect_col(current_col);
240 return matrix_changed;
245 void matrix_init(void) {
250 // Set pinout for right half if pinout for that half is defined
252 #ifdef MATRIX_ROW_PINS_RIGHT
253 const uint8_t row_pins_right[MATRIX_ROWS] = MATRIX_ROW_PINS_RIGHT;
254 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
255 row_pins[i] = row_pins_right[i];
258 #ifdef MATRIX_COL_PINS_RIGHT
259 const uint8_t col_pins_right[MATRIX_COLS] = MATRIX_COL_PINS_RIGHT;
260 for (uint8_t i = 0; i < MATRIX_COLS; i++) {
261 col_pins[i] = col_pins_right[i];
266 thisHand = isLeftHand ? 0 : (ROWS_PER_HAND);
267 thatHand = ROWS_PER_HAND - thisHand;
269 // initialize key pins
272 // initialize matrix state: all keys off
273 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
277 debounce_init(ROWS_PER_HAND);
279 matrix_init_quantum();
282 uint8_t _matrix_scan(void) {
283 bool changed = false;
285 #if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
286 // Set row, read cols
287 for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
288 changed |= read_cols_on_row(raw_matrix, current_row);
290 #elif (DIODE_DIRECTION == ROW2COL)
291 // Set col, read rows
292 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
293 changed |= read_rows_on_col(raw_matrix, current_col);
297 debounce(raw_matrix, matrix + thisHand, ROWS_PER_HAND, changed);
302 uint8_t matrix_scan(void) {
303 uint8_t ret = _matrix_scan();
305 if (is_keyboard_master()) {
306 static uint8_t error_count;
308 if (!transport_master(matrix + thatHand)) {
311 if (error_count > ERROR_DISCONNECT_COUNT) {
312 // reset other half if disconnected
313 for (int i = 0; i < ROWS_PER_HAND; ++i) {
314 matrix[thatHand + i] = 0;
321 matrix_scan_quantum();
323 transport_slave(matrix + thisHand);
324 matrix_slave_scan_user();