You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.3 KiB

  1. /*
  2. Copyright 2012-2018 Jun Wako, Jack Humbert, Yiancar
  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 "matrix.h"
  17. #include "quantum.h"
  18. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  19. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  20. static inline void setPinOutput_writeLow(pin_t pin) {
  21. ATOMIC_BLOCK_FORCEON {
  22. setPinOutput(pin);
  23. writePinLow(pin);
  24. }
  25. }
  26. static inline void setPinInputHigh_atomic(pin_t pin) {
  27. ATOMIC_BLOCK_FORCEON { setPinInputHigh(pin); }
  28. }
  29. static void select_row(uint8_t row) {
  30. setPinOutput_writeLow(row_pins[row]);
  31. }
  32. static void unselect_row(uint8_t row) {
  33. setPinInputHigh_atomic(row_pins[row]);
  34. }
  35. static void unselect_rows(void) {
  36. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  37. setPinInputHigh_atomic(row_pins[x]);
  38. }
  39. }
  40. static void select_col(uint8_t col) {
  41. setPinOutput_writeLow(col_pins[col]);
  42. }
  43. static void unselect_col(uint8_t col) {
  44. setPinInputHigh_atomic(col_pins[col]);
  45. }
  46. static void unselect_cols(void) {
  47. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  48. setPinInputHigh_atomic(col_pins[x]);
  49. }
  50. }
  51. static void init_pins(void) {
  52. unselect_rows();
  53. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  54. setPinInputHigh_atomic(col_pins[x]);
  55. }
  56. unselect_cols();
  57. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  58. setPinInputHigh_atomic(row_pins[x]);
  59. }
  60. }
  61. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  62. // Store last value of row prior to reading
  63. matrix_row_t last_row_value = current_matrix[current_row];
  64. // Select row
  65. select_row(current_row);
  66. matrix_io_delay();
  67. // For each col...
  68. for (uint8_t col_index = 0; col_index < MATRIX_COLS / 2; col_index++) {
  69. // Check row pin state
  70. if (readPin(col_pins[col_index])) {
  71. // Pin HI, clear col bit
  72. current_matrix[current_row] &= ~(MATRIX_ROW_SHIFTER << col_index);
  73. } else {
  74. // Pin LO, set col bit
  75. current_matrix[current_row] |= (MATRIX_ROW_SHIFTER << col_index);
  76. }
  77. }
  78. // Unselect row
  79. unselect_row(current_row);
  80. return (last_row_value != current_matrix[current_row]);
  81. }
  82. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  83. bool matrix_changed = false;
  84. // Select col
  85. select_col(current_col);
  86. matrix_io_delay();
  87. // For each row...
  88. for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
  89. // Store last value of row prior to reading
  90. matrix_row_t last_row_value = current_matrix[row_index];
  91. // Check row pin state
  92. if (readPin(row_pins[row_index])) {
  93. // Pin HI, clear col bit
  94. current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << ( current_col + MATRIX_COLS/2));
  95. } else {
  96. // Pin LO, set col bit
  97. current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << ( current_col + MATRIX_COLS/2));
  98. }
  99. // Determine if the matrix changed state
  100. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
  101. matrix_changed = true;
  102. }
  103. }
  104. // Unselect col
  105. unselect_col(current_col);
  106. return matrix_changed;
  107. }
  108. void matrix_init_custom(void) {
  109. init_pins();
  110. }
  111. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  112. bool changed = false;
  113. // Set row, read cols
  114. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  115. changed |= read_cols_on_row(current_matrix, current_row);
  116. }
  117. // Set col, read rows
  118. for (uint8_t current_col = 0; current_col < (MATRIX_COLS/2); current_col++) {
  119. changed |= read_rows_on_col(current_matrix, current_col);
  120. }
  121. return (uint8_t)changed;
  122. }