matrix.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. Copyright 2012-2020 Jun Wako, Jack Humbert, Yiancar, Takeshi Nishio
  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 init_pins(void) {
  30. unselect_rows();
  31. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  32. gpio_set_pin_input_high(col_pins[x]);
  33. }
  34. }
  35. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  36. // Store last value of row prior to reading
  37. matrix_row_t last_row_value = current_matrix[current_row];
  38. // Clear data in matrix row
  39. current_matrix[current_row] = 0;
  40. // Select row and wait for row selecton to stabilize
  41. select_row(current_row);
  42. matrix_io_delay();
  43. // For each col...
  44. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  45. // skip reading when index equals (= pin itself)
  46. if (col_index != current_row) {
  47. // Check col pin pin_state
  48. if (gpio_read_pin(col_pins[col_index]) == 0) {
  49. // Pin LO, set col bit
  50. current_matrix[current_row] |= (MATRIX_ROW_SHIFTER << col_index);
  51. } else {
  52. // Pin HI, clear col bit
  53. current_matrix[current_row] &= ~(MATRIX_ROW_SHIFTER << col_index);
  54. }
  55. }
  56. }
  57. // Unselect row
  58. unselect_row(current_row);
  59. return (last_row_value != current_matrix[current_row]);
  60. }
  61. void matrix_init_custom(void) {
  62. // initialize key pins
  63. init_pins();
  64. }
  65. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  66. bool changed = false;
  67. // Set row, read cols
  68. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  69. changed |= read_cols_on_row(current_matrix, current_row);
  70. }
  71. return changed;
  72. }