matrix.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 <stdint.h>
  15. #include <stdbool.h>
  16. #include "util.h"
  17. #include "matrix.h"
  18. #include "debounce.h"
  19. #include "quantum.h"
  20. static const pin_t row_pins[] = MATRIX_ROW_PINS;
  21. static const pin_t col_pins[] = MATRIX_COL_PINS;
  22. /* matrix state(1:on, 0:off) */
  23. static matrix_row_t last_matrix[MATRIX_ROWS]; // raw values of last scan
  24. // matrix code
  25. void select_row(uint8_t row) {
  26. setPinOutput(row_pins[row]);
  27. writePinLow(row_pins[row]);
  28. }
  29. void unselect_row(uint8_t row) { setPinInputHigh(row_pins[row]); }
  30. bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  31. // Store last value of row prior to reading
  32. matrix_row_t last_row_value = current_matrix[current_row];
  33. // Clear data in matrix row
  34. current_matrix[current_row] = 0;
  35. // Select row and wait for row selecton to stabilize
  36. select_row(current_row);
  37. matrix_io_delay();
  38. // For each col...
  39. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  40. // Select the col pin to read (active low)
  41. uint8_t pin_state = readPin(col_pins[col_index]);
  42. // Populate the matrix row with the state of the col pin
  43. current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  44. }
  45. // Unselect row
  46. unselect_row(current_row);
  47. return (last_row_value != current_matrix[current_row]);
  48. }
  49. void select_col(uint8_t col) {
  50. setPinOutput(col_pins[col]);
  51. writePinLow(col_pins[col]);
  52. }
  53. void unselect_col(uint8_t col) { setPinInputHigh(col_pins[col]); }
  54. bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  55. bool matrix_changed = false;
  56. // Select col and wait for col selecton to stabilize
  57. select_col(current_col);
  58. matrix_io_delay();
  59. // For each row...
  60. for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
  61. // Store last value of row prior to reading
  62. matrix_row_t last_row_value = current_matrix[row_index];
  63. // Check row pin state
  64. if (readPin(row_pins[row_index]) == 0) {
  65. // Pin LO, set col bit
  66. current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col);
  67. }
  68. // Determine if the matrix changed state
  69. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
  70. matrix_changed = true;
  71. }
  72. }
  73. // Unselect col
  74. unselect_col(current_col);
  75. return matrix_changed;
  76. }
  77. void unselect_rows(void) {
  78. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  79. setPinInputHigh(row_pins[x]);
  80. }
  81. }
  82. void unselect_cols(void) {
  83. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  84. setPinInputHigh(col_pins[x]);
  85. }
  86. }
  87. void init_pins(void) {
  88. unselect_rows();
  89. unselect_cols();
  90. }
  91. void matrix_init_custom(void) {
  92. // initialize key pins
  93. init_pins();
  94. }
  95. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  96. bool changed = false;
  97. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  98. last_matrix[current_row] = current_matrix[current_row];
  99. read_cols_on_row(current_matrix, current_row);
  100. }
  101. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  102. read_rows_on_col(current_matrix, current_col);
  103. }
  104. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  105. if (last_matrix[current_row] != current_matrix[current_row]) {
  106. changed = true;
  107. }
  108. }
  109. return (uint8_t)changed;
  110. }