logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

matrix.c (3625B)


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