aboutsummaryrefslogtreecommitdiff
path: root/keyboards/chimera_ls/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/chimera_ls/matrix.c')
-rw-r--r--keyboards/chimera_ls/matrix.c168
1 files changed, 168 insertions, 0 deletions
diff --git a/keyboards/chimera_ls/matrix.c b/keyboards/chimera_ls/matrix.c
new file mode 100644
index 000000000..9edd91818
--- /dev/null
+++ b/keyboards/chimera_ls/matrix.c
@@ -0,0 +1,168 @@
1/*
2Copyright 2012 Jun Wako
3Copyright 2014 Jack Humbert
4
5This program is free software: you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation, either version 2 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along 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#elif (MATRIX_COLS <= 64)
47# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF\n")
48# define print_matrix_row(row) print_bin_reverse64(matrix_get_row(row))
49# define matrix_bitpop(i) bitpop64(matrix[i])
50# define ROW_SHIFTER ((uint64_t)1)
51#endif
52
53/* matrix state(1:on, 0:off) */
54static matrix_row_t matrix[MATRIX_ROWS];
55
56__attribute__ ((weak))
57void matrix_init_quantum(void) {
58 matrix_init_kb();
59}
60
61__attribute__ ((weak))
62void matrix_scan_quantum(void) {
63 matrix_scan_kb();
64}
65
66__attribute__ ((weak))
67void matrix_init_kb(void) {
68 matrix_init_user();
69}
70
71__attribute__ ((weak))
72void matrix_scan_kb(void) {
73 matrix_scan_user();
74}
75
76__attribute__ ((weak))
77void matrix_init_user(void) {
78}
79
80__attribute__ ((weak))
81void matrix_scan_user(void) {
82}
83
84inline
85uint8_t matrix_rows(void) {
86 return MATRIX_ROWS;
87}
88
89inline
90uint8_t matrix_cols(void) {
91 return MATRIX_COLS;
92}
93
94void matrix_init(void) {
95 matrix_init_quantum();
96 serial_init();
97}
98
99uint8_t matrix_scan(void)
100{
101 uint32_t timeout = 0;
102
103 //the s character requests the RF slave to send the matrix
104 SERIAL_UART_DATA = 's';
105
106 //trust the external keystates entirely, erase the last data
107 uint8_t uart_data[11] = {0};
108
109 //there are 10 bytes corresponding to 10 columns, and an end byte
110 for (uint8_t i = 0; i < 11; i++) {
111 //wait for the serial data, timeout if it's been too long
112 //this only happened in testing with a loose wire, but does no
113 //harm to leave it in here
114 while(!SERIAL_UART_RXD_PRESENT){
115 timeout++;
116 if (timeout > 10000){
117 break;
118 }
119 }
120 uart_data[i] = SERIAL_UART_DATA;
121 }
122
123 //check for the end packet, the key state bytes use the LSBs, so 0xE0
124 //will only show up here if the correct bytes were recieved
125 if (uart_data[10] == 0xE0)
126 {
127 //shifting and transferring the keystates to the QMK matrix variable
128 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
129 matrix[i] = (uint16_t) uart_data[i*2] | (uint16_t) uart_data[i*2+1] << 5;
130 }
131 }
132
133
134 matrix_scan_quantum();
135 return 1;
136}
137
138inline
139bool matrix_is_on(uint8_t row, uint8_t col)
140{
141 return (matrix[row] & ((matrix_row_t)1<<col));
142}
143
144inline
145matrix_row_t matrix_get_row(uint8_t row)
146{
147 return matrix[row];
148}
149
150void matrix_print(void)
151{
152 print_matrix_header();
153
154 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
155 print_hex8(row); print(": ");
156 print_matrix_row(row);
157 print("\n");
158 }
159}
160
161uint8_t matrix_key_count(void)
162{
163 uint8_t count = 0;
164 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
165 count += matrix_bitpop(i);
166 }
167 return count;
168}