2 Copyright 2012 Jun Wako
3 Copyright 2014 Jack Humbert
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 #if (MATRIX_COLS <= 8)
31 # define print_matrix_header() print("\nr/c 01234567\n")
32 # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
33 # define matrix_bitpop(i) bitpop(matrix[i])
34 # define ROW_SHIFTER ((uint8_t)1)
35 #elif (MATRIX_COLS <= 16)
36 # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
37 # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
38 # define matrix_bitpop(i) bitpop16(matrix[i])
39 # define ROW_SHIFTER ((uint16_t)1)
40 #elif (MATRIX_COLS <= 32)
41 # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
42 # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
43 # define matrix_bitpop(i) bitpop32(matrix[i])
44 # define ROW_SHIFTER ((uint32_t)1)
47 /* matrix state(1:on, 0:off) */
48 static matrix_row_t matrix[MATRIX_ROWS];
51 __attribute__ ((weak))
52 void matrix_init_kb(void) {
56 __attribute__ ((weak))
57 void matrix_scan_kb(void) {
61 __attribute__ ((weak))
62 void matrix_init_user(void) {
65 __attribute__ ((weak))
66 void matrix_scan_user(void) {
70 uint8_t matrix_rows(void) {
75 uint8_t matrix_cols(void) {
79 void matrix_init(void) {
81 matrix_init_quantum();
84 uint8_t matrix_scan(void)
90 //the s character requests the RF slave to send the matrix
91 SERIAL_UART_DATA = 's';
93 //trust the external keystates entirely, erase the last data
94 uint8_t uart_data[11] = {0};
96 //there are 10 bytes corresponding to 10 columns, and an end byte
97 for (uint8_t i = 0; i < 11; i++) {
98 //wait for the serial data, timeout if it's been too long
99 //this only happened in testing with a loose wire, but does no
100 //harm to leave it in here
101 while(!SERIAL_UART_RXD_PRESENT){
103 if (timeout > 10000){
107 uart_data[i] = SERIAL_UART_DATA;
110 //check for the end packet, the key state bytes use the LSBs, so 0xE0
111 //will only show up here if the correct bytes were recieved
112 if (uart_data[10] == 0xE0)
114 //shifting and transferring the keystates to the QMK matrix variable
115 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
116 matrix[i] = (uint16_t) uart_data[i*2] | (uint16_t) uart_data[i*2+1] << 5;
121 matrix_scan_quantum();
126 bool matrix_is_on(uint8_t row, uint8_t col)
128 return (matrix[row] & ((matrix_row_t)1<col));
132 matrix_row_t matrix_get_row(uint8_t row)
137 void matrix_print(void)
139 print_matrix_header();
141 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
142 phex(row); print(": ");
143 print_matrix_row(row);
148 uint8_t matrix_key_count(void)
151 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
152 count += matrix_bitpop(i);