matrix.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2022 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "matrix.h"
  4. #include <string.h>
  5. #include "atomic_util.h"
  6. #include "wait.h"
  7. #include "debug.h"
  8. #include "util.h"
  9. #include "debounce.h"
  10. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  11. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  12. static inline void gpio_atomic_set_pin_output_low(pin_t pin) {
  13. ATOMIC_BLOCK_FORCEON {
  14. gpio_set_pin_output(pin);
  15. gpio_write_pin_low(pin);
  16. }
  17. }
  18. static inline void gpio_atomic_set_pin_output_high(pin_t pin) {
  19. ATOMIC_BLOCK_FORCEON {
  20. gpio_set_pin_output(pin);
  21. gpio_write_pin_high(pin);
  22. }
  23. }
  24. static inline void gpio_atomic_set_pin_input_high(pin_t pin) {
  25. ATOMIC_BLOCK_FORCEON {
  26. gpio_set_pin_input_high(pin);
  27. }
  28. }
  29. static inline uint8_t readMatrixPin(pin_t pin) {
  30. if (pin != NO_PIN) {
  31. return gpio_read_pin(pin);
  32. } else {
  33. return 1;
  34. }
  35. }
  36. static bool select_row(uint8_t row) {
  37. pin_t pin = row_pins[row];
  38. if (pin != NO_PIN) {
  39. gpio_atomic_set_pin_output_low(pin);
  40. return true;
  41. }
  42. return false;
  43. }
  44. static void unselect_row(uint8_t row) {
  45. pin_t pin = row_pins[row];
  46. if (pin != NO_PIN) {
  47. gpio_atomic_set_pin_input_high(pin);
  48. }
  49. }
  50. static void unselect_rows(void) {
  51. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  52. unselect_row(x);
  53. }
  54. }
  55. __attribute__((weak)) void matrix_init_custom(void) {
  56. unselect_rows();
  57. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  58. if (col_pins[x] != NO_PIN) {
  59. gpio_atomic_set_pin_input_high(col_pins[x]);
  60. }
  61. }
  62. gpio_atomic_set_pin_input_high(F7);
  63. gpio_atomic_set_pin_input_high(F0);
  64. }
  65. void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  66. // Start with a clear matrix row
  67. matrix_row_t current_row_value = 0;
  68. if (!select_row(current_row)) { // Select row
  69. return; // skip NO_PIN row
  70. }
  71. matrix_output_select_delay();
  72. // For each col...
  73. matrix_row_t row_shifter = MATRIX_ROW_SHIFTER;
  74. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++, row_shifter <<= 1) {
  75. uint8_t pin_state = 0;
  76. if (current_row == 3 && col_index == 0) {
  77. pin_state = !readMatrixPin(F7);
  78. } else if (current_row == 3 && col_index == 3) {
  79. pin_state = !readMatrixPin(F0);
  80. } else {
  81. pin_state = readMatrixPin(col_pins[col_index]);
  82. }
  83. // Populate the matrix row with the state of the col pin
  84. current_row_value |= pin_state ? 0 : row_shifter;
  85. }
  86. // Unselect row
  87. unselect_row(current_row);
  88. matrix_output_unselect_delay(current_row, current_row_value != 0); // wait for all Col signals to go HIGH
  89. // Update the matrix
  90. current_matrix[current_row] = current_row_value;
  91. }
  92. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  93. static matrix_row_t temp_matrix[MATRIX_ROWS] = {0};
  94. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  95. matrix_read_cols_on_row(temp_matrix, current_row);
  96. }
  97. bool changed = memcmp(current_matrix, temp_matrix, sizeof(temp_matrix)) != 0;
  98. if (changed) {
  99. memcpy(current_matrix, temp_matrix, sizeof(temp_matrix));
  100. }
  101. return changed;
  102. }