matrix.c 4.2 KB

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