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.

156 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. #if (MATRIX_COLS <= 8)
  19. # define ROW_SHIFTER ((uint8_t)1)
  20. #elif (MATRIX_COLS <= 16)
  21. # define ROW_SHIFTER ((uint16_t)1)
  22. #elif (MATRIX_COLS <= 32)
  23. # define ROW_SHIFTER ((uint32_t)1)
  24. #endif
  25. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  26. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  27. static void select_row(uint8_t row) {
  28. setPinOutput(row_pins[row]);
  29. writePinLow(row_pins[row]);
  30. }
  31. static void unselect_row(uint8_t row) {
  32. setPinInputHigh(row_pins[row]);
  33. }
  34. static void unselect_rows(void) {
  35. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  36. setPinInputHigh(row_pins[x]);
  37. }
  38. }
  39. static void select_col(uint8_t col) {
  40. setPinOutput(col_pins[col]);
  41. writePinLow(col_pins[col]);
  42. }
  43. static void unselect_col(uint8_t col) {
  44. setPinInputHigh(col_pins[col]);
  45. }
  46. static void unselect_cols(void) {
  47. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  48. setPinInputHigh(col_pins[x]);
  49. }
  50. }
  51. static void init_pins(void) {
  52. unselect_rows();
  53. unselect_cols();
  54. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  55. setPinInputHigh(col_pins[x]);
  56. }
  57. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  58. setPinInputHigh(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. // Clear data in matrix row
  65. current_matrix[current_row] = 0;
  66. // Select row and wait for row selecton to stabilize
  67. select_row(current_row);
  68. matrix_io_delay();
  69. // For each col...
  70. for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  71. // Select the col pin to read (active low)
  72. uint8_t pin_state = gpio_read_pin(col_pins[col_index]);
  73. // Populate the matrix row with the state of the col pin
  74. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
  75. }
  76. // Unselect row
  77. unselect_row(current_row);
  78. return (last_row_value != current_matrix[current_row]);
  79. }
  80. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  81. bool matrix_changed = false;
  82. // Select col and wait for col selecton to stabilize
  83. select_col(current_col);
  84. matrix_io_delay();
  85. // For each row...
  86. for(uint8_t row_index = 0; row_index < MATRIX_ROWS / 2; row_index++) {
  87. uint8_t tmp = row_index + MATRIX_ROWS / 2;
  88. // Store last value of row prior to reading
  89. matrix_row_t last_row_value = current_matrix[tmp];
  90. // Check row pin state
  91. if (gpio_read_pin(row_pins[row_index]) == 0) {
  92. // Pin LO, set col bit
  93. current_matrix[tmp] |= (ROW_SHIFTER << current_col);
  94. } else {
  95. // Pin HI, clear col bit
  96. current_matrix[tmp] &= ~(ROW_SHIFTER << current_col);
  97. }
  98. // Determine if the matrix changed state
  99. if ((last_row_value != current_matrix[tmp]) && !(matrix_changed)) {
  100. matrix_changed = true;
  101. }
  102. }
  103. // Unselect col
  104. unselect_col(current_col);
  105. return matrix_changed;
  106. }
  107. void matrix_init_custom(void) {
  108. // initialize key pins
  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 / 2; 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; current_col++) {
  119. changed |= read_rows_on_col(current_matrix, current_col);
  120. }
  121. return changed;
  122. }