]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/redox_w/matrix.c
Remove more commented out MCUs
[qmk_firmware.git] / keyboards / redox_w / matrix.c
1 /* Copyright 2017 Mattia Dal Ben
2  *
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.
7  *
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.
12  *
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/>.
15  */
16 #include <stdint.h>
17 #include <stdbool.h>
18 #if defined(__AVR__)
19 #include <avr/io.h>
20 #endif
21 #include "wait.h"
22 #include "print.h"
23 #include "debug.h"
24 #include "util.h"
25 #include "matrix.h"
26 #include "timer.h"
27
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)
43 #endif
44
45 /* matrix state(1:on, 0:off) */
46 static matrix_row_t matrix[MATRIX_ROWS];
47
48 __attribute__ ((weak))
49 void matrix_init_quantum(void) {
50     matrix_init_kb();
51 }
52
53 __attribute__ ((weak))
54 void matrix_scan_quantum(void) {
55     matrix_scan_kb();
56 }
57
58 __attribute__ ((weak))
59 void matrix_init_kb(void) {
60     matrix_init_user();
61 }
62
63 __attribute__ ((weak))
64 void matrix_scan_kb(void) {
65     matrix_scan_user();
66 }
67
68 __attribute__ ((weak))
69 void matrix_init_user(void) {
70 }
71
72 __attribute__ ((weak))
73 void matrix_scan_user(void) {
74 }
75
76 inline
77 uint8_t matrix_rows(void) {
78     return MATRIX_ROWS;
79 }
80
81 inline
82 uint8_t matrix_cols(void) {
83     return MATRIX_COLS;
84 }
85
86 void matrix_init(void) {
87
88     matrix_init_quantum();
89 }
90
91 uint8_t matrix_scan(void)
92 {
93     SERIAL_UART_INIT();
94
95     uint32_t timeout = 0;
96
97     //the s character requests the RF slave to send the matrix
98     SERIAL_UART_DATA = 's';
99
100     //trust the external keystates entirely, erase the last data
101     uint8_t uart_data[11] = {0};
102
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){
109             timeout++;
110             if (timeout > 10000){
111                 break;
112             }
113         }
114         uart_data[i] = SERIAL_UART_DATA;
115     }
116
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)
120     {
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;
124         }
125     }
126
127
128     matrix_scan_quantum();
129     return 1;
130 }
131
132 inline
133 bool matrix_is_on(uint8_t row, uint8_t col)
134 {
135     return (matrix[row] & ((matrix_row_t)1<<col));
136 }
137
138 inline
139 matrix_row_t matrix_get_row(uint8_t row)
140 {
141     return matrix[row];
142 }
143
144 void matrix_print(void)
145 {
146     print_matrix_header();
147
148     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
149         phex(row); print(": ");
150         print_matrix_row(row);
151         print("\n");
152     }
153 }
154
155 uint8_t matrix_key_count(void)
156 {
157     uint8_t count = 0;
158     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
159         count += matrix_bitpop(i);
160     }
161     return count;
162 }