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. #include "protocol/serial.h"
  31. #if (MATRIX_COLS <= 8)
  32. # define print_matrix_header() print("\nr/c 01234567\n")
  33. # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  34. # define matrix_bitpop(i) bitpop(matrix[i])
  35. # define ROW_SHIFTER ((uint8_t)1)
  36. #elif (MATRIX_COLS <= 16)
  37. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  38. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  39. # define matrix_bitpop(i) bitpop16(matrix[i])
  40. # define ROW_SHIFTER ((uint16_t)1)
  41. #elif (MATRIX_COLS <= 32)
  42. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  43. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  44. # define matrix_bitpop(i) bitpop32(matrix[i])
  45. # define ROW_SHIFTER ((uint32_t)1)
  46. #endif
  47. /* matrix state(1:on, 0:off) */
  48. static matrix_row_t matrix[MATRIX_ROWS];
  49. //extern int8_t encoderValue;
  50. int8_t encoderValue = 0;
  51. __attribute__ ((weak))
  52. void matrix_init_quantum(void) {
  53. matrix_init_kb();
  54. }
  55. __attribute__ ((weak))
  56. void matrix_scan_quantum(void) {
  57. matrix_scan_kb();
  58. }
  59. __attribute__ ((weak))
  60. void matrix_init_kb(void) {
  61. matrix_init_user();
  62. }
  63. __attribute__ ((weak))
  64. void matrix_scan_kb(void) {
  65. matrix_scan_user();
  66. }
  67. __attribute__ ((weak))
  68. void matrix_init_user(void) {
  69. }
  70. __attribute__ ((weak))
  71. void matrix_scan_user(void) {
  72. }
  73. inline
  74. uint8_t matrix_rows(void) {
  75. return MATRIX_ROWS;
  76. }
  77. inline
  78. uint8_t matrix_cols(void) {
  79. return MATRIX_COLS;
  80. }
  81. void matrix_init(void) {
  82. matrix_init_quantum();
  83. serial_init();
  84. }
  85. uint8_t matrix_scan(void)
  86. {
  87. uint32_t timeout = 0;
  88. // The 's' character requests the RF slave to send the matrix
  89. SERIAL_UART_DATA = 's';
  90. // Trust the external keystates entirely, erase the last data
  91. uint8_t uart_data[4] = {0};
  92. // There are 3 bytes corresponding to the data, and a checksum
  93. for (uint8_t i = 0; i < 4; i++) {
  94. // Wait for the serial data, timeout if it's been too long
  95. // This only happened in testing with a loose wire, but does no
  96. // harm to leave it in here
  97. while(!SERIAL_UART_RXD_PRESENT){
  98. timeout++;
  99. if (timeout > 10000){
  100. xprintf("\r\nTime out in keyboard.");
  101. break;
  102. }
  103. }
  104. uart_data[i] = SERIAL_UART_DATA;
  105. }
  106. // Check for the end packet, it's our checksum.
  107. // Will only be a match if the correct bytes were recieved
  108. if (uart_data[3] == (uart_data[0] ^ uart_data[1] ^ uart_data[2])) { // This is an arbitrary checksum calculated by XORing all the data.
  109. // Transferring the keystates to the QMK matrix variable
  110. /* ASSUMING MSB FIRST */
  111. matrix[0] = ((uint16_t) uart_data[0] << 8) | ((uint16_t) uart_data[1]);
  112. encoderValue += (int8_t) uart_data[2];
  113. if ((uart_data[0] | uart_data[1] | uart_data[2])!=0){
  114. xprintf("\r\n0x%0X%02X%02X",uart_data[0],uart_data[1], uart_data[2]);
  115. }
  116. /* OK, TURNS OUT THAT WAS A BAD ASSUMPTION */
  117. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  118. // I've unpacked these into the mirror image of what QMK expects them to be, so...
  119. matrix[i] = bitrev16(matrix[i]);
  120. // So I'll reverse it, and this should be fine now.
  121. }
  122. // A mouse report for scrolling would go here, but I don't plan on doing scrolling with the encoder. So.
  123. report_mouse_t currentReport = {};
  124. /*
  125. currentReport = pointing_device_get_report();
  126. //mouseReport.x = 127 max -127 min
  127. currentReport.x = (int8_t) uart_data[6];
  128. //mouseReport.y = 127 max -127 min
  129. currentReport.y = (int8_t) uart_data[7];
  130. //mouseReport.v = 127 max -127 min (scroll vertical)
  131. currentReport.v = (int8_t) uart_data[8];
  132. //mouseReport.h = 127 max -127 min (scroll horizontal)
  133. currentReport.h = (int8_t) uart_data[9];
  134. */
  135. /*
  136. currentReport.x = 0;
  137. currentReport.y = 0;
  138. currentReport.v = 0;
  139. currentReport.h = 0;*/
  140. pointing_device_set_report(currentReport);
  141. } else {
  142. xprintf("\r\nRequested packet, data 3 was %d",uart_data[3]);
  143. }
  144. matrix_scan_quantum();
  145. return 1;
  146. }
  147. inline
  148. bool matrix_is_on(uint8_t row, uint8_t col)
  149. {
  150. return (matrix[row] & ((matrix_row_t)1<col));
  151. }
  152. inline
  153. matrix_row_t matrix_get_row(uint8_t row)
  154. {
  155. return matrix[row];
  156. }
  157. void matrix_print(void)
  158. {
  159. print_matrix_header();
  160. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  161. print_hex8(row); print(": ");
  162. print_matrix_row(row);
  163. print("\n");
  164. }
  165. }
  166. uint8_t matrix_key_count(void)
  167. {
  168. uint8_t count = 0;
  169. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  170. count += matrix_bitpop(i);
  171. }
  172. return count;
  173. }