diff options
Diffstat (limited to 'keyboards/centromere/matrix.c')
-rw-r--r-- | keyboards/centromere/matrix.c | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/keyboards/centromere/matrix.c b/keyboards/centromere/matrix.c new file mode 100644 index 000000000..7256cd5cb --- /dev/null +++ b/keyboards/centromere/matrix.c | |||
@@ -0,0 +1,153 @@ | |||
1 | /* | ||
2 | Copyright 2012 Jun Wako | ||
3 | Copyright 2014 Jack Humbert | ||
4 | |||
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. | ||
9 | |||
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. | ||
14 | |||
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/>. | ||
17 | */ | ||
18 | #include <stdint.h> | ||
19 | #include <stdbool.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" | ||
28 | #include "timer.h" | ||
29 | #include "protocol/serial.h" | ||
30 | |||
31 | #if (MATRIX_COLS <= 8) | ||
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) */ | ||
49 | static matrix_row_t matrix[MATRIX_ROWS]; | ||
50 | |||
51 | |||
52 | __attribute__ ((weak)) | ||
53 | void matrix_init_kb(void) { | ||
54 | matrix_init_user(); | ||
55 | } | ||
56 | |||
57 | __attribute__ ((weak)) | ||
58 | void matrix_scan_kb(void) { | ||
59 | matrix_scan_user(); | ||
60 | } | ||
61 | |||
62 | __attribute__ ((weak)) | ||
63 | void matrix_init_user(void) { | ||
64 | } | ||
65 | |||
66 | __attribute__ ((weak)) | ||
67 | void matrix_scan_user(void) { | ||
68 | } | ||
69 | |||
70 | inline | ||
71 | uint8_t matrix_rows(void) { | ||
72 | return MATRIX_ROWS; | ||
73 | } | ||
74 | |||
75 | inline | ||
76 | uint8_t matrix_cols(void) { | ||
77 | return MATRIX_COLS; | ||
78 | } | ||
79 | |||
80 | void matrix_init(void) { | ||
81 | |||
82 | matrix_init_quantum(); | ||
83 | serial_init(); | ||
84 | } | ||
85 | |||
86 | uint8_t matrix_scan(void) | ||
87 | { | ||
88 | uint32_t timeout = 0; | ||
89 | |||
90 | //the s character requests the RF remote slave to send the matrix information | ||
91 | SERIAL_UART_DATA = 's'; | ||
92 | |||
93 | //trust the external keystates, erase the last set of data | ||
94 | uint8_t uart_data[11] = {0}; | ||
95 | |||
96 | //there are 10 bytes corresponding to 1w 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 | while(!SERIAL_UART_RXD_PRESENT){ | ||
100 | timeout++; | ||
101 | if (timeout > 10000){ | ||
102 | break; | ||
103 | } | ||
104 | } | ||
105 | uart_data[i] = SERIAL_UART_DATA; | ||
106 | } | ||
107 | |||
108 | //check for the end packet, the key state bytes use the LSBs, so 0xE0 | ||
109 | //will only show up here if the correct bytes were recieved | ||
110 | if (uart_data[10] == 0xE0) | ||
111 | { | ||
112 | //shifting and transferring the keystates to the QMK matrix variable | ||
113 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
114 | matrix[i] = (uint16_t) uart_data[i*2] | (uint16_t) uart_data[i*2+1] << 5; | ||
115 | } | ||
116 | } | ||
117 | |||
118 | |||
119 | matrix_scan_quantum(); | ||
120 | return 1; | ||
121 | } | ||
122 | |||
123 | inline | ||
124 | bool matrix_is_on(uint8_t row, uint8_t col) | ||
125 | { | ||
126 | return (matrix[row] & ((matrix_row_t)1<<col)); | ||
127 | } | ||
128 | |||
129 | inline | ||
130 | matrix_row_t matrix_get_row(uint8_t row) | ||
131 | { | ||
132 | return matrix[row]; | ||
133 | } | ||
134 | |||
135 | void matrix_print(void) | ||
136 | { | ||
137 | print_matrix_header(); | ||
138 | |||
139 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
140 | print_hex8(row); print(": "); | ||
141 | print_matrix_row(row); | ||
142 | print("\n"); | ||
143 | } | ||
144 | } | ||
145 | |||
146 | uint8_t matrix_key_count(void) | ||
147 | { | ||
148 | uint8_t count = 0; | ||
149 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
150 | count += matrix_bitpop(i); | ||
151 | } | ||
152 | return count; | ||
153 | } | ||