You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 lines
5.5 KiB

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