2 Copyright 2012 Jun Wako
3 Copyright 2014 Jack Humbert
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.
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.
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/>.
30 #include "honeycomb.h"
31 #include "pointing_device.h"
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)
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;
56 __attribute__ ((weak))
57 void matrix_init_quantum(void) {
61 __attribute__ ((weak))
62 void matrix_scan_quantum(void) {
66 __attribute__ ((weak))
67 void matrix_init_kb(void) {
71 __attribute__ ((weak))
72 void matrix_scan_kb(void) {
76 __attribute__ ((weak))
77 void matrix_init_user(void) {
80 __attribute__ ((weak))
81 void matrix_scan_user(void) {
85 uint8_t matrix_rows(void) {
90 uint8_t matrix_cols(void) {
94 void matrix_init(void) {
96 matrix_init_quantum();
99 uint8_t matrix_scan(void)
103 uint32_t timeout = 0;
105 // The 's' character requests the RF slave to send the matrix
106 SERIAL_UART_DATA = 's';
108 // Trust the external keystates entirely, erase the last data
109 uint8_t uart_data[4] = {0};
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){
118 if (timeout > 10000){
119 xprintf("\r\nTime out in keyboard.");
123 uart_data[i] = SERIAL_UART_DATA;
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]);
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.
143 // A mouse report for scrolling would go here, but I don't plan on doing scrolling with the encoder. So.
145 report_mouse_t currentReport = {};
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];
161 currentReport.h = 0;*/
163 pointing_device_set_report(currentReport);
165 xprintf("\r\nRequested packet, data 3 was %d",uart_data[3]);
168 matrix_scan_quantum();
173 bool matrix_is_on(uint8_t row, uint8_t col)
175 return (matrix[row] & ((matrix_row_t)1<col));
179 matrix_row_t matrix_get_row(uint8_t row)
184 void matrix_print(void)
186 print_matrix_header();
188 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
189 phex(row); print(": ");
190 print_matrix_row(row);
195 uint8_t matrix_key_count(void)
198 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
199 count += matrix_bitpop(i);