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.

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