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.

164 lines
4.0 KiB

7 years ago
  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. /* matrix state(1:on, 0:off) */
  43. static matrix_row_t matrix[MATRIX_ROWS];
  44. __attribute__ ((weak))
  45. void matrix_init_quantum(void) {
  46. matrix_init_kb();
  47. }
  48. __attribute__ ((weak))
  49. void matrix_scan_quantum(void) {
  50. matrix_scan_kb();
  51. }
  52. __attribute__ ((weak))
  53. void matrix_init_kb(void) {
  54. matrix_init_user();
  55. }
  56. __attribute__ ((weak))
  57. void matrix_scan_kb(void) {
  58. matrix_scan_user();
  59. }
  60. __attribute__ ((weak))
  61. void matrix_init_user(void) {
  62. }
  63. __attribute__ ((weak))
  64. void matrix_scan_user(void) {
  65. }
  66. inline
  67. uint8_t matrix_rows(void) {
  68. return MATRIX_ROWS;
  69. }
  70. inline
  71. uint8_t matrix_cols(void) {
  72. return MATRIX_COLS;
  73. }
  74. void matrix_init(void) {
  75. matrix_init_quantum();
  76. }
  77. uint8_t matrix_scan(void)
  78. {
  79. SERIAL_UART_INIT();
  80. uint32_t timeout = 0;
  81. //the s character requests the RF slave to send the matrix
  82. SERIAL_UART_DATA = 's';
  83. //trust the external keystates entirely, erase the last data
  84. uint8_t uart_data[11] = {0};
  85. //there are 10 bytes corresponding to 10 columns, and an end byte
  86. for (uint8_t i = 0; i < 11; i++) {
  87. //wait for the serial data, timeout if it's been too long
  88. //this only happened in testing with a loose wire, but does no
  89. //harm to leave it in here
  90. while(!SERIAL_UART_RXD_PRESENT){
  91. timeout++;
  92. if (timeout > 10000){
  93. break;
  94. }
  95. }
  96. uart_data[i] = SERIAL_UART_DATA;
  97. }
  98. //check for the end packet, the key state bytes use the LSBs, so 0xE0
  99. //will only show up here if the correct bytes were recieved
  100. if (uart_data[10] == 0xE0)
  101. {
  102. //shifting and transferring the keystates to the QMK matrix variable
  103. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  104. matrix[i] = (uint16_t) uart_data[i*2] | (uint16_t) uart_data[i*2+1] << 5;
  105. }
  106. }
  107. matrix_scan_quantum();
  108. return 1;
  109. }
  110. inline
  111. bool matrix_is_on(uint8_t row, uint8_t col)
  112. {
  113. return (matrix[row] & ((matrix_row_t)1<col));
  114. }
  115. inline
  116. matrix_row_t matrix_get_row(uint8_t row)
  117. {
  118. return matrix[row];
  119. }
  120. void matrix_print(void)
  121. {
  122. print_matrix_header();
  123. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  124. phex(row); print(": ");
  125. print_matrix_row(row);
  126. print("\n");
  127. }
  128. }
  129. uint8_t matrix_key_count(void)
  130. {
  131. uint8_t count = 0;
  132. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  133. count += matrix_bitpop(i);
  134. }
  135. return count;
  136. }