]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/dichotemy/matrix.c
Do some cleanup for the API
[qmk_firmware.git] / keyboards / dichotemy / matrix.c
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
30 #if (MATRIX_COLS <= 8)
31 #    define print_matrix_header()  print("\nr/c 01234567\n")
32 #    define print_matrix_row(row)  print_bin_reverse8(matrix_get_row(row))
33 #    define matrix_bitpop(i)       bitpop(matrix[i])
34 #    define ROW_SHIFTER ((uint8_t)1)
35 #elif (MATRIX_COLS <= 16)
36 #    define print_matrix_header()  print("\nr/c 0123456789ABCDEF\n")
37 #    define print_matrix_row(row)  print_bin_reverse16(matrix_get_row(row))
38 #    define matrix_bitpop(i)       bitpop16(matrix[i])
39 #    define ROW_SHIFTER ((uint16_t)1)
40 #elif (MATRIX_COLS <= 32)
41 #    define print_matrix_header()  print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
42 #    define print_matrix_row(row)  print_bin_reverse32(matrix_get_row(row))
43 #    define matrix_bitpop(i)       bitpop32(matrix[i])
44 #    define ROW_SHIFTER  ((uint32_t)1)
45 #endif
46
47 #define MAIN_ROWMASK 0xFFF0;
48 #define LOWER_ROWMASK 0x1F80;
49
50 /* matrix state(1:on, 0:off) */
51 static matrix_row_t matrix[MATRIX_ROWS];
52
53 __attribute__ ((weak))
54 void matrix_init_quantum(void) {
55     matrix_init_kb();
56 }
57
58 __attribute__ ((weak))
59 void matrix_scan_quantum(void) {
60     matrix_scan_kb();
61 }
62
63 __attribute__ ((weak))
64 void matrix_init_kb(void) {
65     matrix_init_user();
66 }
67
68 __attribute__ ((weak))
69 void matrix_scan_kb(void) {
70     matrix_scan_user();
71 }
72
73 __attribute__ ((weak))
74 void matrix_init_user(void) {
75 }
76
77 __attribute__ ((weak))
78 void matrix_scan_user(void) {
79 }
80
81 inline
82 uint8_t matrix_rows(void) {
83     return MATRIX_ROWS;
84 }
85
86 inline
87 uint8_t matrix_cols(void) {
88     return MATRIX_COLS;
89 }
90
91 void matrix_init(void) {
92
93     matrix_init_quantum();
94 }
95
96 uint8_t matrix_scan(void)
97 {
98     SERIAL_UART_INIT();
99
100     uint32_t timeout = 0;
101
102     //the s character requests the RF slave to send the matrix
103     SERIAL_UART_DATA = 's';
104
105     //trust the external keystates entirely, erase the last data
106     uint8_t uart_data[7] = {0};
107
108     //there are 10 bytes corresponding to 10 columns, and an end byte
109     for (uint8_t i = 0; i < 7; i++) {
110         //wait for the serial data, timeout if it's been too long
111         //this only happened in testing with a loose wire, but does no
112         //harm to leave it in here
113         while(!SERIAL_UART_RXD_PRESENT){
114             timeout++;
115             if (timeout > 10000){
116                 break;
117             }
118         } 
119         uart_data[i] = SERIAL_UART_DATA;
120     }
121
122     //check for the end packet, the key state bytes use the LSBs, so 0xE0
123     //will only show up here if the correct bytes were recieved
124     if (uart_data[6] == 0x96) { //this is an arbitrary binary checksum (10010110)
125         //shifting and transferring the keystates to the QMK matrix variable
126                 //bits 1-12 are row 1, 13-24 are row 2, 25-36 are row 3,
127                 //bits 37-42 are row 4 (only 6 wide, 1-3 are 0, and 10-12 are 0)
128                 //bits 43-48 are row 5 (same as row 4)
129                 /* ASSUMING MSB FIRST */
130                 matrix[0] = (((uint16_t) uart_data[0] << 8) | ((uint16_t) uart_data[1])) & MAIN_ROWMASK;
131                 matrix[1] = ((uint16_t) uart_data[1] << 12) | ((uint16_t) uart_data[2] << 4);
132                 matrix[2] = (((uint16_t) uart_data[3] << 8) | ((uint16_t) uart_data[4])) & MAIN_ROWMASK;
133                 matrix[3] = (((uint16_t) uart_data[4] << 9) | ((uint16_t) uart_data[5] << 1)) & LOWER_ROWMASK;
134                 matrix[4] = ((uint16_t) uart_data[5] << 7) & LOWER_ROWMASK;
135                 /* OK, TURNS OUT THAT WAS A BAD ASSUMPTION */
136         for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
137                         //I've unpacked these into the mirror image of what QMK expects them to be, so...
138                         matrix[i] = ((matrix[i] * 0x0802LU & 0x22110LU) | (matrix[i] * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16;
139                         //bithack mirror!  Doesn't make any sense, but works - and efficiently.
140         }
141     }
142
143
144     matrix_scan_quantum();
145     return 1;
146 }
147
148 inline
149 bool matrix_is_on(uint8_t row, uint8_t col)
150 {
151     return (matrix[row] & ((matrix_row_t)1<col));
152 }
153
154 inline
155 matrix_row_t matrix_get_row(uint8_t row)
156 {
157     return matrix[row];
158 }
159
160 void matrix_print(void)
161 {
162     print_matrix_header();
163
164     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
165         phex(row); print(": ");
166         print_matrix_row(row);
167         print("\n");
168     }
169 }
170
171 uint8_t matrix_key_count(void)
172 {
173     uint8_t count = 0;
174     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
175         count += matrix_bitpop(i);
176     }
177     return count;
178 }