logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

matrix.c (7254B)


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