matrix.c 5.1 KB

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