]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/honeycomb/matrix.c
Usbasploader bootloader option addition (#6304)
[qmk_firmware.git] / keyboards / honeycomb / matrix.c
1 /*
2 Copyright 2012 Jun Wako
3 Copyright 2014 Jack Humbert
4 Copyright 2019 @filoxo
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 #include <stdint.h>
20 #include <stdbool.h>
21 #if defined(__AVR__)
22 #include <avr/io.h>
23 #endif
24 #include "wait.h"
25 #include "print.h"
26 #include "debug.h"
27 #include "util.h"
28 #include "matrix.h"
29 #include "timer.h"
30 #include "honeycomb.h"
31 #include "pointing_device.h"
32 #include "report.h"
33
34 #if (MATRIX_COLS <= 8)
35 # define print_matrix_header()  print("\nr/c 01234567\n")
36 # define print_matrix_row(row)  print_bin_reverse8(matrix_get_row(row))
37 # define matrix_bitpop(i)       bitpop(matrix[i])
38 # define ROW_SHIFTER ((uint8_t)1)
39 #elif (MATRIX_COLS <= 16)
40 # define print_matrix_header()  print("\nr/c 0123456789ABCDEF\n")
41 # define print_matrix_row(row)  print_bin_reverse16(matrix_get_row(row))
42 # define matrix_bitpop(i)       bitpop16(matrix[i])
43 # define ROW_SHIFTER ((uint16_t)1)
44 #elif (MATRIX_COLS <= 32)
45 # define print_matrix_header()  print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
46 # define print_matrix_row(row)  print_bin_reverse32(matrix_get_row(row))
47 # define matrix_bitpop(i)       bitpop32(matrix[i])
48 # define ROW_SHIFTER  ((uint32_t)1)
49 #endif
50
51 /* matrix state(1:on, 0:off) */
52 static matrix_row_t matrix[MATRIX_ROWS];
53 //extern int8_t encoderValue;
54 int8_t encoderValue = 0;
55
56 __attribute__ ((weak))
57 void matrix_init_quantum(void) {
58     matrix_init_kb();
59 }
60
61 __attribute__ ((weak))
62 void matrix_scan_quantum(void) {
63     matrix_scan_kb();
64 }
65
66 __attribute__ ((weak))
67 void matrix_init_kb(void) {
68     matrix_init_user();
69 }
70
71 __attribute__ ((weak))
72 void matrix_scan_kb(void) {
73     matrix_scan_user();
74 }
75
76 __attribute__ ((weak))
77 void matrix_init_user(void) {
78 }
79
80 __attribute__ ((weak))
81 void matrix_scan_user(void) {
82 }
83
84 inline
85 uint8_t matrix_rows(void) {
86     return MATRIX_ROWS;
87 }
88
89 inline
90 uint8_t matrix_cols(void) {
91     return MATRIX_COLS;
92 }
93
94 void matrix_init(void) {
95
96     matrix_init_quantum();
97 }
98
99 uint8_t matrix_scan(void)
100 {
101     SERIAL_UART_INIT();
102
103     uint32_t timeout = 0;
104
105     // The 's' character requests the RF slave to send the matrix
106     SERIAL_UART_DATA = 's';
107
108     // Trust the external keystates entirely, erase the last data
109     uint8_t uart_data[4] = {0};
110
111     // There are 3 bytes corresponding to the data, and a checksum
112     for (uint8_t i = 0; i < 4; i++) {
113         // Wait for the serial data, timeout if it's been too long
114         // This only happened in testing with a loose wire, but does no
115         // harm to leave it in here
116         while(!SERIAL_UART_RXD_PRESENT){
117             timeout++;
118             if (timeout > 10000){
119                 xprintf("\r\nTime out in keyboard.");
120                 break;
121             }
122         }
123         uart_data[i] = SERIAL_UART_DATA;
124     }
125
126     // Check for the end packet, it's our checksum.
127     // Will only be a match if the correct bytes were recieved
128     if (uart_data[3] == (uart_data[0] ^ uart_data[1] ^ uart_data[2])) { // This is an arbitrary checksum calculated by XORing all the data.
129         // Transferring the keystates to the QMK matrix variable
130                 /* ASSUMING MSB FIRST */
131                 matrix[0] = ((uint16_t) uart_data[0] << 8) | ((uint16_t) uart_data[1]);
132                 encoderValue += (int8_t) uart_data[2];
133                 if ((uart_data[0] | uart_data[1] | uart_data[2])!=0){
134                         xprintf("\r\n0x%0X%02X%02X",uart_data[0],uart_data[1], uart_data[2]);
135                 }
136                 /* OK, TURNS OUT THAT WAS A BAD ASSUMPTION */
137         for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
138                         // I've unpacked these into the mirror image of what QMK expects them to be, so...
139                         matrix[i] = bitrev16(matrix[i]);
140                         // So I'll reverse it, and this should be fine now.
141         }
142
143         // A mouse report for scrolling would go here, but I don't plan on doing scrolling with the encoder. So.
144
145         report_mouse_t currentReport = {};
146 /*
147         currentReport = pointing_device_get_report();
148             //mouseReport.x = 127 max -127 min
149         currentReport.x = (int8_t) uart_data[6];
150             //mouseReport.y = 127 max -127 min
151         currentReport.y = (int8_t) uart_data[7];
152             //mouseReport.v = 127 max -127 min (scroll vertical)
153         currentReport.v = (int8_t) uart_data[8];
154             //mouseReport.h = 127 max -127 min (scroll horizontal)
155         currentReport.h = (int8_t) uart_data[9];
156         */
157         /*
158         currentReport.x = 0;
159         currentReport.y = 0;
160         currentReport.v = 0;
161         currentReport.h = 0;*/
162
163         pointing_device_set_report(currentReport);
164     } else {
165         xprintf("\r\nRequested packet, data 3 was %d",uart_data[3]);
166     }
167
168     matrix_scan_quantum();
169     return 1;
170 }
171
172 inline
173 bool matrix_is_on(uint8_t row, uint8_t col)
174 {
175     return (matrix[row] & ((matrix_row_t)1<col));
176 }
177
178 inline
179 matrix_row_t matrix_get_row(uint8_t row)
180 {
181     return matrix[row];
182 }
183
184 void matrix_print(void)
185 {
186     print_matrix_header();
187
188     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
189         phex(row); print(": ");
190         print_matrix_row(row);
191         print("\n");
192     }
193 }
194
195 uint8_t matrix_key_count(void)
196 {
197     uint8_t count = 0;
198     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
199         count += matrix_bitpop(i);
200     }
201     return count;
202 }