2
0

matrix.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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_COLS; i++) {
  42. setPadMode(matrix_col_pins[i], PAL_MODE_OUTPUT_PUSHPULL);
  43. }
  44. for (int i = 0; i < MATRIX_ROWS; i++) {
  45. setPadMode(matrix_row_pins[i], PAL_MODE_INPUT_PULLDOWN);
  46. }
  47. memset(matrix, 0, MATRIX_ROWS * sizeof(matrix_row_t));
  48. memset(matrix_debouncing, 0, MATRIX_COLS * sizeof(matrix_row_t));
  49. matrix_init_quantum();
  50. }
  51. uint8_t matrix_scan(void) {
  52. // actual matrix
  53. for (int col = 0; col < MATRIX_COLS; col++) {
  54. matrix_row_t data = 0;
  55. setPad(matrix_col_pins[col]);
  56. // need wait to settle pin state
  57. wait_us(20);
  58. for (int row = 0; row < MATRIX_ROWS; row++) {
  59. data |= (readPad(matrix_row_pins[row]) << row);
  60. }
  61. clearPad(matrix_col_pins[col]);
  62. if (matrix_debouncing[col] != data) {
  63. matrix_debouncing[col] = data;
  64. debouncing = true;
  65. debouncing_time = timer_read();
  66. }
  67. }
  68. if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) {
  69. for (int row = 0; row < MATRIX_ROWS; row++) {
  70. matrix[row] = 0;
  71. for (int col = 0; col < MATRIX_COLS; col++) {
  72. matrix[row] |= ((matrix_debouncing[col] & (1 << row) ? 1 : 0) << col);
  73. }
  74. }
  75. debouncing = false;
  76. }
  77. matrix_scan_quantum();
  78. return 1;
  79. }
  80. bool matrix_is_on(uint8_t row, uint8_t col) {
  81. return (matrix[row] & (1<<col));
  82. }
  83. matrix_row_t matrix_get_row(uint8_t row) {
  84. return matrix[row];
  85. }
  86. void matrix_print(void) {
  87. printf("\nr/c 01234567\n");
  88. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  89. printf("%X0: ", row);
  90. matrix_row_t data = matrix_get_row(row);
  91. for (int col = 0; col < MATRIX_COLS; col++) {
  92. if (data & (1<<col))
  93. printf("1");
  94. else
  95. printf("0");
  96. }
  97. printf("\n");
  98. }
  99. }