matrix.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. Copyright 2012 Jun Wako
  3. Copyright 2014 Jack Humbert
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #if defined(__AVR__)
  18. #include <avr/io.h>
  19. #endif
  20. #include "wait.h"
  21. #include "print.h"
  22. #include "debug.h"
  23. #include "util.h"
  24. #include "matrix.h"
  25. #include "timer.h"
  26. #include "dichotomy.h"
  27. #include "pointing_device.h"
  28. #include "report.h"
  29. #include "uart.h"
  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. #elif (MATRIX_COLS <= 16)
  34. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  35. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  36. #elif (MATRIX_COLS <= 32)
  37. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  38. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  39. #endif
  40. #define MAIN_ROWMASK 0xFFF0;
  41. #define LOWER_ROWMASK 0x3FC0;
  42. #define UART_MATRIX_RESPONSE_TIMEOUT 10000
  43. /* matrix state(1:on, 0:off) */
  44. static matrix_row_t matrix[MATRIX_ROWS];
  45. __attribute__ ((weak))
  46. void matrix_init_kb(void) {
  47. matrix_init_user();
  48. }
  49. __attribute__ ((weak))
  50. void matrix_scan_kb(void) {
  51. matrix_scan_user();
  52. }
  53. __attribute__ ((weak))
  54. void matrix_init_user(void) {
  55. }
  56. __attribute__ ((weak))
  57. void matrix_scan_user(void) {
  58. }
  59. inline
  60. uint8_t matrix_rows(void) {
  61. return MATRIX_ROWS;
  62. }
  63. inline
  64. uint8_t matrix_cols(void) {
  65. return MATRIX_COLS;
  66. }
  67. void matrix_init(void) {
  68. matrix_init_kb();
  69. uart_init(1000000);
  70. }
  71. uint8_t matrix_scan(void)
  72. {
  73. uint32_t timeout = 0;
  74. //the s character requests the RF slave to send the matrix
  75. uart_write('s');
  76. //trust the external keystates entirely, erase the last data
  77. uint8_t uart_data[11] = {0};
  78. //there are 10 bytes corresponding to 10 columns, and an end byte
  79. for (uint8_t i = 0; i < 11; i++) {
  80. //wait for the serial data, timeout if it's been too long
  81. //this only happened in testing with a loose wire, but does no
  82. //harm to leave it in here
  83. while(!uart_available()){
  84. timeout++;
  85. if (timeout > UART_MATRIX_RESPONSE_TIMEOUT) {
  86. break;
  87. }
  88. }
  89. if (timeout < UART_MATRIX_RESPONSE_TIMEOUT) {
  90. uart_data[i] = uart_read();
  91. } else {
  92. uart_data[i] = 0x00;
  93. }
  94. }
  95. //check for the end packet, the key state bytes use the LSBs, so 0xE0
  96. //will only show up here if the correct bytes were recieved
  97. uint8_t checksum = 0x00;
  98. for (uint8_t z = 0; z < 10; z++){
  99. checksum = checksum^uart_data[z];
  100. }
  101. checksum = checksum ^ (uart_data[10] & 0xF0);
  102. // Smash the checksum from 1 byte into 4 bits
  103. checksum = (checksum ^ ((checksum & 0xF0)>>4)) & 0x0F;
  104. //xprintf("\r\nGOT RAW PACKET: \r\n%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d",uart_data[0],uart_data[1],uart_data[2],uart_data[3],uart_data[4],uart_data[5],uart_data[6],uart_data[7],uart_data[8],uart_data[9],uart_data[10],checksum);
  105. if ((uart_data[10] & 0x0F) == checksum) { //this is an arbitrary binary checksum (1001) (that would be 0x9.)
  106. //xprintf("\r\nGOT PACKET: \r\n%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d",uart_data[0],uart_data[1],uart_data[2],uart_data[3],uart_data[4],uart_data[5]);
  107. //shifting and transferring the keystates to the QMK matrix variable
  108. //bits 1-12 are row 1, 13-24 are row 2, 25-36 are row 3,
  109. //bits 37-42 are row 4 (only 6 wide, 1-3 are 0, and 10-12 are 0)
  110. //bits 43-48 are row 5 (same as row 4)
  111. /* ASSUMING MSB FIRST */
  112. matrix[0] = (((uint16_t) uart_data[0] << 8) | ((uint16_t) uart_data[1])) & MAIN_ROWMASK;
  113. matrix[1] = ((uint16_t) uart_data[1] << 12) | ((uint16_t) uart_data[2] << 4);
  114. matrix[2] = (((uint16_t) uart_data[3] << 8) | ((uint16_t) uart_data[4])) & MAIN_ROWMASK;
  115. matrix[3] = (((uint16_t) uart_data[4] << 9) | ((uint16_t) uart_data[5] << 1)) & LOWER_ROWMASK;
  116. matrix[4] = (((uint16_t) uart_data[5] << 7) | ((uart_data[10] & 1<<7) ? 1:0) << 13 | ((uart_data[10] & 1<<6) ? 1:0) << 6) & LOWER_ROWMASK;
  117. /* OK, TURNS OUT THAT WAS A BAD ASSUMPTION */
  118. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  119. //I've unpacked these into the mirror image of what QMK expects them to be, so...
  120. /*uint8_t halfOne = (matrix[i]>>8);
  121. uint8_t halfTwo = (matrix[i] & 0xFF);
  122. halfOne = ((halfOne * 0x0802LU & 0x22110LU) | (halfOne * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16;
  123. halfTwo = ((halfTwo * 0x0802LU & 0x22110LU) | (halfTwo * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16;
  124. matrix[i] = ((halfTwo<<8) & halfOne);*/
  125. //matrix[i] = ((matrix[i] * 0x0802LU & 0x22110LU) | (matrix[i] * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16;
  126. matrix[i] = bitrev16(matrix[i]);
  127. //bithack mirror! Doesn't make any sense, but works - and efficiently.
  128. }
  129. //if (uart_data[6]!=0 || uart_data[7]!=0){
  130. //if (maxCount<101){
  131. // xprintf("\r\nMouse data: x=%d, y=%d",(int8_t)uart_data[6],(int8_t)uart_data[7]);
  132. //}
  133. report_mouse_t currentReport = {};
  134. //check for the end packet, bytes 1-4 are movement and scroll
  135. //but byte 5 has bits 0-3 for the scroll button state
  136. //(1000 if pressed, 0000 if not) and bits 4-7 are always 1
  137. //We can use this to verify the report sent properly.
  138. currentReport = pointing_device_get_report();
  139. //shifting and transferring the info to the mouse report varaible
  140. //mouseReport.x = 127 max -127 min
  141. currentReport.x = (int8_t) uart_data[6];
  142. //mouseReport.y = 127 max -127 min
  143. currentReport.y = (int8_t) uart_data[7];
  144. //mouseReport.v = 127 max -127 min (scroll vertical)
  145. currentReport.v = (int8_t) uart_data[8];
  146. //mouseReport.h = 127 max -127 min (scroll horizontal)
  147. currentReport.h = (int8_t) uart_data[9];
  148. /*
  149. currentReport.x = 0;
  150. currentReport.y = 0;
  151. currentReport.v = 0;
  152. currentReport.h = 0;*/
  153. pointing_device_set_report(currentReport);
  154. } else {
  155. //xprintf("\r\nRequested packet, data 10 was %d but checksum was %d",(uart_data[10] & 0x0F), (checksum & 0x0F));
  156. }
  157. //matrix_print();
  158. matrix_scan_kb();
  159. return 1;
  160. }
  161. inline
  162. bool matrix_is_on(uint8_t row, uint8_t col)
  163. {
  164. return (matrix[row] & ((matrix_row_t)1<<col));
  165. }
  166. inline
  167. matrix_row_t matrix_get_row(uint8_t row)
  168. {
  169. return matrix[row];
  170. }
  171. void matrix_print(void)
  172. {
  173. print_matrix_header();
  174. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  175. print_hex8(row); print(": ");
  176. print_matrix_row(row);
  177. print("\n");
  178. }
  179. }