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.

162 lines
4.1 KiB

  1. /* Copyright 2017 Mattia Dal Ben
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  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. *
  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 "protocol/serial.h"
  28. #if (MATRIX_COLS <= 8)
  29. # define print_matrix_header() print("\nr/c 01234567\n")
  30. # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  31. # define matrix_bitpop(i) bitpop(matrix[i])
  32. # define ROW_SHIFTER ((uint8_t)1)
  33. #elif (MATRIX_COLS <= 16)
  34. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  35. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  36. # define matrix_bitpop(i) bitpop16(matrix[i])
  37. # define ROW_SHIFTER ((uint16_t)1)
  38. #elif (MATRIX_COLS <= 32)
  39. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  40. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  41. # define matrix_bitpop(i) bitpop32(matrix[i])
  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. __attribute__ ((weak))
  47. void matrix_init_quantum(void) {
  48. matrix_init_kb();
  49. }
  50. __attribute__ ((weak))
  51. void matrix_scan_quantum(void) {
  52. matrix_scan_kb();
  53. }
  54. __attribute__ ((weak))
  55. void matrix_init_kb(void) {
  56. matrix_init_user();
  57. }
  58. __attribute__ ((weak))
  59. void matrix_scan_kb(void) {
  60. matrix_scan_user();
  61. }
  62. __attribute__ ((weak))
  63. void matrix_init_user(void) {
  64. }
  65. __attribute__ ((weak))
  66. void matrix_scan_user(void) {
  67. }
  68. inline
  69. uint8_t matrix_rows(void) {
  70. return MATRIX_ROWS;
  71. }
  72. inline
  73. uint8_t matrix_cols(void) {
  74. return MATRIX_COLS;
  75. }
  76. void matrix_init(void) {
  77. matrix_init_quantum();
  78. serial_init();
  79. }
  80. uint8_t matrix_scan(void)
  81. {
  82. uint32_t timeout = 0;
  83. //the s character requests the RF slave to send the matrix
  84. SERIAL_UART_DATA = 's';
  85. //trust the external keystates entirely, erase the last data
  86. uint8_t uart_data[11] = {0};
  87. //there are 14 bytes corresponding to 14 columns, and an end byte
  88. for (uint8_t i = 0; i < 11; i++) {
  89. //wait for the serial data, timeout if it's been too long
  90. //this only happened in testing with a loose wire, but does no
  91. //harm to leave it in here
  92. while(!SERIAL_UART_RXD_PRESENT){
  93. timeout++;
  94. if (timeout > 10000){
  95. break;
  96. }
  97. }
  98. uart_data[i] = SERIAL_UART_DATA;
  99. }
  100. //check for the end packet, the key state bytes use the LSBs, so 0xE0
  101. //will only show up here if the correct bytes were recieved
  102. if (uart_data[10] == 0xE0)
  103. {
  104. //shifting and transferring the keystates to the QMK matrix variable
  105. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  106. matrix[i] = (uint16_t) uart_data[i*2] | (uint16_t) uart_data[i*2+1] << 7;
  107. }
  108. }
  109. matrix_scan_quantum();
  110. return 1;
  111. }
  112. inline
  113. bool matrix_is_on(uint8_t row, uint8_t col)
  114. {
  115. return (matrix[row] & ((matrix_row_t)1<<col));
  116. }
  117. inline
  118. matrix_row_t matrix_get_row(uint8_t row)
  119. {
  120. return matrix[row];
  121. }
  122. void matrix_print(void)
  123. {
  124. print_matrix_header();
  125. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  126. print_hex8(row); print(": ");
  127. print_matrix_row(row);
  128. print("\n");
  129. }
  130. }
  131. uint8_t matrix_key_count(void)
  132. {
  133. uint8_t count = 0;
  134. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  135. count += matrix_bitpop(i);
  136. }
  137. return count;
  138. }