2
0

matrix.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include <stdint.h>
  2. #include <stdbool.h>
  3. #include <string.h>
  4. #include "hal.h"
  5. #include "timer.h"
  6. #include "wait.h"
  7. #include "printf.h"
  8. #include "backlight.h"
  9. #include "matrix.h"
  10. #include "action.h"
  11. #include "keycode.h"
  12. #include <string.h>
  13. #include "quantum.h"
  14. /*
  15. * col: { A10, B2, A15, A0, A1, A2, B0, B1, C13, A6, A7, A3 }
  16. * row: { B5, B10, A9, A8 }
  17. */
  18. /* matrix state(1:on, 0:off) */
  19. static matrix_row_t matrix[MATRIX_ROWS];
  20. static matrix_row_t matrix_debouncing[MATRIX_COLS];
  21. static bool debouncing = false;
  22. static uint16_t debouncing_time = 0;
  23. //static LINE_TYPE matrix_col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  24. static LINE_TYPE matrix_row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  25. __attribute__ ((weak))
  26. void matrix_init_user(void) {}
  27. __attribute__ ((weak))
  28. void matrix_scan_user(void) {}
  29. __attribute__ ((weak))
  30. void matrix_init_kb(void) {
  31. matrix_init_user();
  32. }
  33. __attribute__ ((weak))
  34. void matrix_scan_kb(void) {
  35. matrix_scan_user();
  36. }
  37. void matrix_init(void) {
  38. printf("matrix init\n");
  39. //debug_matrix = true;
  40. // actual matrix setup
  41. for (int i = 0; i < MATRIX_ROWS; i++) {
  42. setPadMode(matrix_row_pins[i], PAL_MODE_INPUT_PULLUP);
  43. }
  44. memset(matrix, 0, MATRIX_ROWS * sizeof(matrix_row_t));
  45. memset(matrix_debouncing, 0, MATRIX_COLS * sizeof(matrix_row_t));
  46. matrix_init_quantum();
  47. }
  48. uint8_t matrix_scan(void) {
  49. // actual matrix
  50. for (int col = 0; col < MATRIX_COLS; col++) {
  51. matrix_row_t data = 0;
  52. for (int row = 0; row < MATRIX_ROWS; row++) {
  53. data |= ((readPad(matrix_row_pins[row]) == 0) << row);
  54. }
  55. if (matrix_debouncing[col] != data) {
  56. matrix_debouncing[col] = data;
  57. debouncing = true;
  58. debouncing_time = timer_read();
  59. }
  60. }
  61. if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) {
  62. for (int row = 0; row < MATRIX_ROWS; row++) {
  63. matrix[row] = 0;
  64. for (int col = 0; col < MATRIX_COLS; col++) {
  65. matrix[row] |= ((matrix_debouncing[col] & (1 << row) ? 1 : 0) << col);
  66. }
  67. }
  68. debouncing = false;
  69. }
  70. matrix_scan_quantum();
  71. return 1;
  72. }
  73. bool matrix_is_on(uint8_t row, uint8_t col) {
  74. return (matrix[row] & (1<<col));
  75. }
  76. matrix_row_t matrix_get_row(uint8_t row) {
  77. return matrix[row];
  78. }
  79. void matrix_print(void) {
  80. printf("\nr/c 01234567\n");
  81. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  82. printf("%X0: ", row);
  83. matrix_row_t data = matrix_get_row(row);
  84. for (int col = 0; col < MATRIX_COLS; col++) {
  85. if (data & (1<<col))
  86. printf("1");
  87. else
  88. printf("0");
  89. }
  90. printf("\n");
  91. }
  92. }