logo

qmk_firmware

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

matrix.c (4201B)


  1. /*
  2. Copyright 2012-2020 Jun Wako, Jack Humbert, Yiancar, Ein Terakawa, Drashna, Josh Hinnebusch
  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. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "util.h"
  15. #include "matrix.h"
  16. #include "debounce.h"
  17. static const pin_t row_pins[] = MATRIX_ROW_PINS;
  18. static const pin_t col_pins[] = MATRIX_COL_PINS;
  19. /* matrix state(1:on, 0:off) */
  20. static matrix_row_t last_matrix[MATRIX_ROWS]; // raw values of last scan
  21. // matrix code
  22. void select_row(uint8_t row) {
  23. gpio_set_pin_output(row_pins[row]);
  24. gpio_write_pin_low(row_pins[row]);
  25. }
  26. void unselect_row(uint8_t row) { gpio_set_pin_input_high(row_pins[row]); }
  27. bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  28. // Store last value of row prior to reading
  29. matrix_row_t last_row_value = current_matrix[current_row];
  30. // Clear data in matrix row
  31. current_matrix[current_row] = 0;
  32. // Select row and wait for row selecton to stabilize
  33. select_row(current_row);
  34. matrix_io_delay();
  35. // For each col...
  36. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  37. // Select the col pin to read (active low)
  38. uint8_t pin_state = gpio_read_pin(col_pins[col_index]);
  39. // Populate the matrix row with the state of the col pin
  40. current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  41. }
  42. // Unselect row
  43. unselect_row(current_row);
  44. return (last_row_value != current_matrix[current_row]);
  45. }
  46. void select_col(uint8_t col) {
  47. gpio_set_pin_output(col_pins[col]);
  48. gpio_write_pin_low(col_pins[col]);
  49. }
  50. void unselect_col(uint8_t col) { gpio_set_pin_input_high(col_pins[col]); }
  51. bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  52. bool matrix_changed = false;
  53. // Select col and wait for col selecton to stabilize
  54. select_col(current_col);
  55. matrix_io_delay();
  56. // For each row...
  57. for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
  58. // Store last value of row prior to reading
  59. matrix_row_t last_row_value = current_matrix[row_index];
  60. // Check row pin state
  61. if (gpio_read_pin(row_pins[row_index]) == 0) {
  62. // Pin LO, set col bit
  63. current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col);
  64. }
  65. // Determine if the matrix changed state
  66. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
  67. matrix_changed = true;
  68. }
  69. }
  70. // Unselect col
  71. unselect_col(current_col);
  72. return matrix_changed;
  73. }
  74. void unselect_rows(void) {
  75. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  76. gpio_set_pin_input_high(row_pins[x]);
  77. }
  78. }
  79. void unselect_cols(void) {
  80. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  81. gpio_set_pin_input_high(col_pins[x]);
  82. }
  83. }
  84. void init_pins(void) {
  85. unselect_rows();
  86. unselect_cols();
  87. }
  88. void matrix_init_custom(void) {
  89. // initialize key pins
  90. init_pins();
  91. }
  92. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  93. bool changed = false;
  94. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  95. last_matrix[current_row] = current_matrix[current_row];
  96. read_cols_on_row(current_matrix, current_row);
  97. }
  98. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  99. read_rows_on_col(current_matrix, current_col);
  100. }
  101. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  102. if (last_matrix[current_row] != current_matrix[current_row]) {
  103. changed = true;
  104. }
  105. }
  106. return (uint8_t)changed;
  107. }