diff options
Diffstat (limited to 'keyboards/sirius/uni660/matrix.c')
-rw-r--r-- | keyboards/sirius/uni660/matrix.c | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/keyboards/sirius/uni660/matrix.c b/keyboards/sirius/uni660/matrix.c new file mode 100644 index 000000000..3e231b33b --- /dev/null +++ b/keyboards/sirius/uni660/matrix.c | |||
@@ -0,0 +1,160 @@ | |||
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 "debounce.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 | debounce_init(MATRIX_ROWS); | ||
82 | matrix_init_quantum(); | ||
83 | } | ||
84 | |||
85 | uint8_t matrix_scan(void) | ||
86 | { | ||
87 | bool matrix_has_changed = false; | ||
88 | |||
89 | SERIAL_UART_INIT(); | ||
90 | |||
91 | uint32_t timeout = 0; | ||
92 | |||
93 | //the s character requests the RF slave to send the matrix | ||
94 | SERIAL_UART_DATA = 's'; | ||
95 | |||
96 | //trust the external keystates entirely, erase the last data | ||
97 | uint8_t uart_data[17] = {0}; | ||
98 | |||
99 | //there are 16 bytes corresponding to 16 columns, and an end byte | ||
100 | for (uint8_t i = 0; i < 17; i++) { | ||
101 | //wait for the serial data, timeout if it's been too long | ||
102 | //this only happened in testing with a loose wire, but does no | ||
103 | //harm to leave it in here | ||
104 | while(!SERIAL_UART_RXD_PRESENT){ | ||
105 | timeout++; | ||
106 | if (timeout > 10000){ | ||
107 | break; | ||
108 | } | ||
109 | } | ||
110 | uart_data[i] = SERIAL_UART_DATA; | ||
111 | } | ||
112 | |||
113 | //check for the end packet, the key state bytes use the LSBs, so 0xE0 | ||
114 | //will only show up here if the correct bytes were recieved | ||
115 | if (uart_data[10] == 0xE0) | ||
116 | { | ||
117 | //shifting and transferring the keystates to the QMK matrix variable | ||
118 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
119 | matrix[i] = (uint16_t) uart_data[i*2] | (uint16_t) uart_data[i*2+1] << 8; | ||
120 | } | ||
121 | } | ||
122 | |||
123 | debounce(matrix, matrix, MATRIX_ROWS, matrix_has_changed); | ||
124 | |||
125 | matrix_scan_quantum(); | ||
126 | |||
127 | return matrix_has_changed; | ||
128 | } | ||
129 | |||
130 | inline | ||
131 | bool matrix_is_on(uint8_t row, uint8_t col) | ||
132 | { | ||
133 | return (matrix[row] & ((matrix_row_t)1<<col)); | ||
134 | } | ||
135 | |||
136 | inline | ||
137 | matrix_row_t matrix_get_row(uint8_t row) | ||
138 | { | ||
139 | return matrix[row]; | ||
140 | } | ||
141 | |||
142 | void matrix_print(void) | ||
143 | { | ||
144 | print_matrix_header(); | ||
145 | |||
146 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
147 | phex(row); print(": "); | ||
148 | print_matrix_row(row); | ||
149 | print("\n"); | ||
150 | } | ||
151 | } | ||
152 | |||
153 | uint8_t matrix_key_count(void) | ||
154 | { | ||
155 | uint8_t count = 0; | ||
156 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
157 | count += matrix_bitpop(i); | ||
158 | } | ||
159 | return count; | ||
160 | } | ||