matrix.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. Copyright 2012-2018 Jun Wako, Jack Humbert, Yiancar
  3. Copyright 2022 jun10000
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "matrix.h"
  16. #include "wait.h"
  17. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  18. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  19. static void select_row(uint8_t row)
  20. {
  21. gpio_set_pin_output(row_pins[row]);
  22. gpio_write_pin_low(row_pins[row]);
  23. }
  24. static void unselect_row(uint8_t row)
  25. {
  26. gpio_set_pin_input_high(row_pins[row]);
  27. }
  28. static void unselect_rows(void)
  29. {
  30. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  31. gpio_set_pin_input_high(row_pins[x]);
  32. }
  33. }
  34. static void select_col(uint8_t col)
  35. {
  36. gpio_set_pin_output(col_pins[col]);
  37. gpio_write_pin_low(col_pins[col]);
  38. }
  39. static void unselect_col(uint8_t col)
  40. {
  41. gpio_set_pin_input_high(col_pins[col]);
  42. }
  43. static void unselect_cols(void)
  44. {
  45. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  46. gpio_set_pin_input_high(col_pins[x]);
  47. }
  48. }
  49. static void init_pins(void) {
  50. unselect_rows();
  51. unselect_cols();
  52. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  53. gpio_set_pin_input_high(col_pins[x]);
  54. }
  55. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  56. gpio_set_pin_input_high(row_pins[x]);
  57. }
  58. }
  59. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
  60. {
  61. // Store last value of row prior to reading
  62. matrix_row_t last_row_value = current_matrix[current_row];
  63. // Clear data in matrix row
  64. current_matrix[current_row] = 0;
  65. // Select row and wait for row selecton to stabilize
  66. select_row(current_row);
  67. wait_us(30);
  68. // For each col...
  69. for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  70. // Select the col pin to read (active low)
  71. uint8_t pin_state = gpio_read_pin(col_pins[col_index]);
  72. // Populate the matrix row with the state of the col pin
  73. current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  74. }
  75. // Unselect row
  76. unselect_row(current_row);
  77. return (last_row_value != current_matrix[current_row]);
  78. }
  79. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  80. {
  81. bool matrix_changed = false;
  82. // Select col and wait for col selecton to stabilize
  83. select_col(current_col);
  84. wait_us(30);
  85. // For each row...
  86. for(uint8_t row_index = 0; row_index < MATRIX_ROWS/2; row_index++)
  87. {
  88. uint8_t tmp = row_index + MATRIX_ROWS/2;
  89. // Store last value of row prior to reading
  90. matrix_row_t last_row_value = current_matrix[tmp];
  91. // Check row pin state
  92. if (gpio_read_pin(row_pins[row_index]) == 0)
  93. {
  94. // Pin LO, set col bit
  95. current_matrix[tmp] |= (MATRIX_ROW_SHIFTER << current_col);
  96. }
  97. else
  98. {
  99. // Pin HI, clear col bit
  100. current_matrix[tmp] &= ~(MATRIX_ROW_SHIFTER << current_col);
  101. }
  102. // Determine if the matrix changed state
  103. if ((last_row_value != current_matrix[tmp]) && !(matrix_changed))
  104. {
  105. matrix_changed = true;
  106. }
  107. }
  108. // Unselect col
  109. unselect_col(current_col);
  110. return matrix_changed;
  111. }
  112. void matrix_init_custom(void) {
  113. // initialize key pins
  114. init_pins();
  115. }
  116. bool matrix_scan_custom(matrix_row_t current_matrix[])
  117. {
  118. bool changed = false;
  119. // Set row, read cols
  120. for (uint8_t current_row = 0; current_row < MATRIX_ROWS / 2; current_row++) {
  121. changed |= read_cols_on_row(current_matrix, current_row);
  122. }
  123. //else
  124. // Set col, read rows
  125. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  126. changed |= read_rows_on_col(current_matrix, current_col);
  127. }
  128. return (uint8_t)changed;
  129. }