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.

127 lines
3.5 KiB

  1. /* Copyright 2019
  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. #include "wait.h"
  19. #include "print.h"
  20. #include "debug.h"
  21. #include "util.h"
  22. #include "matrix.h"
  23. #include "debounce.h"
  24. #include "quantum.h"
  25. //_____COMMON__________________________________________________________________
  26. // user-defined overridable functions
  27. __attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); }
  28. __attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); }
  29. __attribute__((weak)) void matrix_init_user(void) {}
  30. __attribute__((weak)) void matrix_scan_user(void) {}
  31. //_____COULD BE COMMON_________________________________________________________
  32. /* matrix state(1:on, 0:off) */
  33. /*static*/ matrix_row_t raw_matrix[MATRIX_ROWS];
  34. /*static*/ matrix_row_t matrix[MATRIX_ROWS];
  35. #if (MATRIX_COLS <= 8)
  36. # define print_matrix_header() print("\nr/c 01234567\n")
  37. # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  38. # define matrix_bitpop(i) bitpop(matrix[i])
  39. # define ROW_SHIFTER ((uint8_t)1)
  40. #elif (MATRIX_COLS <= 16)
  41. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  42. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  43. # define matrix_bitpop(i) bitpop16(matrix[i])
  44. # define ROW_SHIFTER ((uint16_t)1)
  45. #elif (MATRIX_COLS <= 32)
  46. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  47. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  48. # define matrix_bitpop(i) bitpop32(matrix[i])
  49. # define ROW_SHIFTER ((uint32_t)1)
  50. #endif
  51. __attribute__ ((weak))
  52. uint8_t matrix_rows(void) {
  53. return MATRIX_ROWS;
  54. }
  55. __attribute__ ((weak))
  56. uint8_t matrix_cols(void) {
  57. return MATRIX_COLS;
  58. }
  59. __attribute__ ((weak))
  60. matrix_row_t matrix_get_row(uint8_t row) {
  61. return matrix[row];
  62. }
  63. __attribute__ ((weak))
  64. uint8_t matrix_key_count(void) {
  65. uint8_t count = 0;
  66. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  67. count += matrix_bitpop(i);
  68. }
  69. return count;
  70. }
  71. __attribute__ ((weak))
  72. void matrix_print(void) {
  73. print_matrix_header();
  74. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  75. phex(row); print(": ");
  76. print_matrix_row(row);
  77. print("\n");
  78. }
  79. }
  80. //_____CUSTOM MATRIX 'LITE'____________________________________________________
  81. __attribute__ ((weak))
  82. void custom_matrix_init(void) {
  83. }
  84. __attribute__ ((weak))
  85. bool custom_matrix_scan(matrix_row_t current_matrix[]) {
  86. bool changed = true;
  87. return changed;
  88. }
  89. __attribute__ ((weak))
  90. void matrix_init(void) {
  91. custom_matrix_init();
  92. // initialize matrix state: all keys off
  93. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  94. raw_matrix[i] = 0;
  95. matrix[i] = 0;
  96. }
  97. debounce_init(MATRIX_ROWS);
  98. matrix_init_quantum();
  99. }
  100. __attribute__ ((weak))
  101. uint8_t matrix_scan(void) {
  102. bool changed = custom_matrix_scan(raw_matrix);
  103. debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
  104. matrix_scan_quantum();
  105. return 1;
  106. }