1 /* Copyright 2017 Mattia Dal Ben
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #if (MATRIX_COLS <= 8)
29 # define print_matrix_header() print("\nr/c 01234567\n")
30 # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
31 # define matrix_bitpop(i) bitpop(matrix[i])
32 # define ROW_SHIFTER ((uint8_t)1)
33 #elif (MATRIX_COLS <= 16)
34 # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
35 # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
36 # define matrix_bitpop(i) bitpop16(matrix[i])
37 # define ROW_SHIFTER ((uint16_t)1)
38 #elif (MATRIX_COLS <= 32)
39 # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
40 # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
41 # define matrix_bitpop(i) bitpop32(matrix[i])
42 # define ROW_SHIFTER ((uint32_t)1)
45 /* matrix state(1:on, 0:off) */
46 static matrix_row_t matrix[MATRIX_ROWS];
48 __attribute__ ((weak))
49 void matrix_init_quantum(void) {
53 __attribute__ ((weak))
54 void matrix_scan_quantum(void) {
58 __attribute__ ((weak))
59 void matrix_init_kb(void) {
63 __attribute__ ((weak))
64 void matrix_scan_kb(void) {
68 __attribute__ ((weak))
69 void matrix_init_user(void) {
72 __attribute__ ((weak))
73 void matrix_scan_user(void) {
77 uint8_t matrix_rows(void) {
82 uint8_t matrix_cols(void) {
86 void matrix_init(void) {
88 matrix_init_quantum();
91 uint8_t matrix_scan(void)
97 //the s character requests the RF slave to send the matrix
98 SERIAL_UART_DATA = 's';
100 //trust the external keystates entirely, erase the last data
101 uint8_t uart_data[11] = {0};
103 //there are 14 bytes corresponding to 14 columns, and an end byte
104 for (uint8_t i = 0; i < 11; i++) {
105 //wait for the serial data, timeout if it's been too long
106 //this only happened in testing with a loose wire, but does no
107 //harm to leave it in here
108 while(!SERIAL_UART_RXD_PRESENT){
110 if (timeout > 10000){
114 uart_data[i] = SERIAL_UART_DATA;
117 //check for the end packet, the key state bytes use the LSBs, so 0xE0
118 //will only show up here if the correct bytes were recieved
119 if (uart_data[10] == 0xE0)
121 //shifting and transferring the keystates to the QMK matrix variable
122 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
123 matrix[i] = (uint16_t) uart_data[i*2] | (uint16_t) uart_data[i*2+1] << 7;
128 matrix_scan_quantum();
133 bool matrix_is_on(uint8_t row, uint8_t col)
135 return (matrix[row] & ((matrix_row_t)1<<col));
139 matrix_row_t matrix_get_row(uint8_t row)
144 void matrix_print(void)
146 print_matrix_header();
148 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
149 phex(row); print(": ");
150 print_matrix_row(row);
155 uint8_t matrix_key_count(void)
158 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
159 count += matrix_bitpop(i);