logo

qmk_firmware

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

matrix.c (5283B)


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