aboutsummaryrefslogtreecommitdiff
path: root/keyboards/telophase/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/telophase/matrix.c')
-rw-r--r--keyboards/telophase/matrix.c127
1 files changed, 14 insertions, 113 deletions
diff --git a/keyboards/telophase/matrix.c b/keyboards/telophase/matrix.c
index bc7a125e0..a153dd4bf 100644
--- a/keyboards/telophase/matrix.c
+++ b/keyboards/telophase/matrix.c
@@ -15,86 +15,18 @@ GNU General Public License for more details.
15You should have received a copy of the GNU General Public License 15You should have received a copy of the GNU General Public License
16along with this program. If not, see <http://www.gnu.org/licenses/>. 16along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/ 17*/
18#include <stdint.h> 18
19#include <stdbool.h> 19#include "quantum.h"
20#if defined(__AVR__)
21#include <avr/io.h>
22#endif
23#include "wait.h"
24#include "print.h"
25#include "debug.h"
26#include "util.h"
27#include "matrix.h" 20#include "matrix.h"
28#include "timer.h"
29#include "protocol/serial.h" 21#include "protocol/serial.h"
30 22
31#if (MATRIX_COLS <= 8) 23void matrix_init_custom(void) {
32# define print_matrix_header() print("\nr/c 01234567\n")
33# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
34# define matrix_bitpop(i) bitpop(matrix[i])
35# define ROW_SHIFTER ((uint8_t)1)
36#elif (MATRIX_COLS <= 16)
37# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
38# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
39# define matrix_bitpop(i) bitpop16(matrix[i])
40# define ROW_SHIFTER ((uint16_t)1)
41#elif (MATRIX_COLS <= 32)
42# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
43# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
44# define matrix_bitpop(i) bitpop32(matrix[i])
45# define ROW_SHIFTER ((uint32_t)1)
46#endif
47
48/* matrix state(1:on, 0:off) */
49static matrix_row_t matrix[MATRIX_ROWS];
50
51__attribute__ ((weak))
52void matrix_init_quantum(void) {
53 matrix_init_kb();
54}
55
56__attribute__ ((weak))
57void matrix_scan_quantum(void) {
58 matrix_scan_kb();
59}
60
61__attribute__ ((weak))
62void matrix_init_kb(void) {
63 matrix_init_user();
64}
65
66__attribute__ ((weak))
67void matrix_scan_kb(void) {
68 matrix_scan_user();
69}
70
71__attribute__ ((weak))
72void matrix_init_user(void) {
73}
74
75__attribute__ ((weak))
76void matrix_scan_user(void) {
77}
78
79inline
80uint8_t matrix_rows(void) {
81 return MATRIX_ROWS;
82}
83
84inline
85uint8_t matrix_cols(void) {
86 return MATRIX_COLS;
87}
88
89void matrix_init(void) {
90
91 matrix_init_quantum();
92 serial_init(); 24 serial_init();
93} 25}
94 26
95uint8_t matrix_scan(void) 27bool matrix_scan_custom(matrix_row_t current_matrix[]) {
96{
97 uint32_t timeout = 0; 28 uint32_t timeout = 0;
29 bool changed = false;
98 30
99 //the s character requests the RF slave to send the matrix 31 //the s character requests the RF slave to send the matrix
100 SERIAL_UART_DATA = 's'; 32 SERIAL_UART_DATA = 's';
@@ -107,9 +39,9 @@ uint8_t matrix_scan(void)
107 //wait for the serial data, timeout if it's been too long 39 //wait for the serial data, timeout if it's been too long
108 //this only happened in testing with a loose wire, but does no 40 //this only happened in testing with a loose wire, but does no
109 //harm to leave it in here 41 //harm to leave it in here
110 while(!SERIAL_UART_RXD_PRESENT){ 42 while (!SERIAL_UART_RXD_PRESENT) {
111 timeout++; 43 timeout++;
112 if (timeout > 10000){ 44 if (timeout > 10000) {
113 break; 45 break;
114 } 46 }
115 } 47 }
@@ -118,47 +50,16 @@ uint8_t matrix_scan(void)
118 50
119 //check for the end packet, the key state bytes use the LSBs, so 0xE0 51 //check for the end packet, the key state bytes use the LSBs, so 0xE0
120 //will only show up here if the correct bytes were recieved 52 //will only show up here if the correct bytes were recieved
121 if (uart_data[11] == 0xE0) 53 if (uart_data[11] == 0xE0) {
122 {
123 //shifting and transferring the keystates to the QMK matrix variable 54 //shifting and transferring the keystates to the QMK matrix variable
124 for (uint8_t i = 0; i < MATRIX_ROWS; i++) { 55 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
125 matrix[i] = (uint16_t) uart_data[i*2] | (uint16_t) uart_data[i*2+1] << 6; 56 matrix_row_t current_row = (uint16_t) uart_data[i * 2] | (uint16_t) uart_data[i * 2 + 1] << 6;
57 if (current_matrix[i] != current_row) {
58 changed = true;
59 }
60 current_matrix[i] = current_row;
126 } 61 }
127 } 62 }
128 63
129 64 return changed;
130 matrix_scan_quantum();
131 return 1;
132}
133
134inline
135bool matrix_is_on(uint8_t row, uint8_t col)
136{
137 return (matrix[row] & ((matrix_row_t)1<<col));
138}
139
140inline
141matrix_row_t matrix_get_row(uint8_t row)
142{
143 return matrix[row];
144}
145
146void matrix_print(void)
147{
148 print_matrix_header();
149
150 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
151 print_hex8(row); print(": ");
152 print_matrix_row(row);
153 print("\n");
154 }
155}
156
157uint8_t matrix_key_count(void)
158{
159 uint8_t count = 0;
160 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
161 count += matrix_bitpop(i);
162 }
163 return count;
164} 65}