matrix.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. Copyright 2012 Jun Wako
  3. Copyright 2014 Jack Humbert
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #if defined(__AVR__)
  18. #include <avr/io.h>
  19. #endif
  20. #include "wait.h"
  21. #include "print.h"
  22. #include "debug.h"
  23. #include "util.h"
  24. #include "matrix.h"
  25. #include "timer.h"
  26. #if (MATRIX_COLS <= 8)
  27. # define print_matrix_header() print("\nr/c 01234567\n")
  28. # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  29. # define matrix_bitpop(i) bitpop(matrix[i])
  30. # define ROW_SHIFTER ((uint8_t)1)
  31. #elif (MATRIX_COLS <= 16)
  32. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  33. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  34. # define matrix_bitpop(i) bitpop16(matrix[i])
  35. # define ROW_SHIFTER ((uint16_t)1)
  36. #elif (MATRIX_COLS <= 32)
  37. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  38. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  39. # define matrix_bitpop(i) bitpop32(matrix[i])
  40. # define ROW_SHIFTER ((uint32_t)1)
  41. #endif
  42. #define MAIN_ROWMASK 0xFFF0;
  43. #define LOWER_ROWMASK 0x1F80;
  44. /* matrix state(1:on, 0:off) */
  45. static matrix_row_t matrix[MATRIX_ROWS];
  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_quantum();
  70. }
  71. uint8_t matrix_scan(void)
  72. {
  73. SERIAL_UART_INIT();
  74. uint32_t timeout = 0;
  75. //the s character requests the RF slave to send the matrix
  76. SERIAL_UART_DATA = 's';
  77. //trust the external keystates entirely, erase the last data
  78. uint8_t uart_data[7] = {0};
  79. //there are 10 bytes corresponding to 10 columns, and an end byte
  80. for (uint8_t i = 0; i < 7; 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(!SERIAL_UART_RXD_PRESENT){
  85. timeout++;
  86. if (timeout > 10000){
  87. break;
  88. }
  89. }
  90. uart_data[i] = SERIAL_UART_DATA;
  91. }
  92. //check for the end packet, the key state bytes use the LSBs, so 0xE0
  93. //will only show up here if the correct bytes were recieved
  94. if (uart_data[6] == 0x96) { //this is an arbitrary binary checksum (10010110)
  95. //shifting and transferring the keystates to the QMK matrix variable
  96. //bits 1-12 are row 1, 13-24 are row 2, 25-36 are row 3,
  97. //bits 37-42 are row 4 (only 6 wide, 1-3 are 0, and 10-12 are 0)
  98. //bits 43-48 are row 5 (same as row 4)
  99. /* ASSUMING MSB FIRST */
  100. matrix[0] = (((uint16_t) uart_data[0] << 8) | ((uint16_t) uart_data[1])) & MAIN_ROWMASK;
  101. matrix[1] = ((uint16_t) uart_data[1] << 12) | ((uint16_t) uart_data[2] << 4);
  102. matrix[2] = (((uint16_t) uart_data[3] << 8) | ((uint16_t) uart_data[4])) & MAIN_ROWMASK;
  103. matrix[3] = (((uint16_t) uart_data[4] << 9) | ((uint16_t) uart_data[5] << 1)) & LOWER_ROWMASK;
  104. matrix[4] = ((uint16_t) uart_data[5] << 7) & LOWER_ROWMASK;
  105. /* OK, TURNS OUT THAT WAS A BAD ASSUMPTION */
  106. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  107. //I've unpacked these into the mirror image of what QMK expects them to be, so...
  108. matrix[i] = ((matrix[i] * 0x0802LU & 0x22110LU) | (matrix[i] * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16;
  109. //bithack mirror! Doesn't make any sense, but works - and efficiently.
  110. }
  111. }
  112. matrix_scan_quantum();
  113. return 1;
  114. }
  115. inline
  116. bool matrix_is_on(uint8_t row, uint8_t col)
  117. {
  118. return (matrix[row] & ((matrix_row_t)1<col));
  119. }
  120. inline
  121. matrix_row_t matrix_get_row(uint8_t row)
  122. {
  123. return matrix[row];
  124. }
  125. void matrix_print(void)
  126. {
  127. print_matrix_header();
  128. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  129. phex(row); print(": ");
  130. print_matrix_row(row);
  131. print("\n");
  132. }
  133. }
  134. uint8_t matrix_key_count(void)
  135. {
  136. uint8_t count = 0;
  137. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  138. count += matrix_bitpop(i);
  139. }
  140. return count;
  141. }