matrix.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. // Encoder things
  16. #define SWITCH_1 F7
  17. #define SWITCH_2 D7
  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 void select_row(uint8_t row) {
  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. gpio_set_pin_input_high(row_pins[row]);
  26. }
  27. static void unselect_rows(void) {
  28. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  29. gpio_set_pin_input_high(row_pins[x]);
  30. }
  31. }
  32. static void init_pins(void) {
  33. unselect_rows();
  34. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  35. gpio_set_pin_input_high(col_pins[x]);
  36. }
  37. }
  38. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  39. // Store last value of row prior to reading
  40. matrix_row_t last_row_value = current_matrix[current_row];
  41. // Clear data in matrix row
  42. current_matrix[current_row] = 0;
  43. // Select row and wait for row selection to stabilize
  44. select_row(current_row);
  45. matrix_io_delay();
  46. // For each col...
  47. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  48. // Select the col pin to read (active low)
  49. uint8_t pin_state = gpio_read_pin(col_pins[col_index]);
  50. // Populate the matrix row with the state of the col pin
  51. current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  52. }
  53. // Unselect row
  54. unselect_row(current_row);
  55. return (last_row_value != current_matrix[current_row]);
  56. }
  57. static bool read_encoder_switches(matrix_row_t current_matrix[], uint8_t current_row) {
  58. // Store last value of row prior to reading
  59. matrix_row_t last_row_value = current_matrix[current_row];
  60. // Clear data in matrix row
  61. current_matrix[current_row] = 0;
  62. // Populate the matrix row with the state of the encoder
  63. current_matrix[current_row] |= gpio_read_pin(SWITCH_1) ? (1 << 0) : 0;
  64. current_matrix[current_row] |= gpio_read_pin(SWITCH_2) ? (1 << 1) : 0;
  65. return (last_row_value != current_matrix[current_row]);
  66. }
  67. void matrix_init_custom(void) {
  68. // initialize key pins
  69. gpio_set_pin_input(SWITCH_1);
  70. gpio_set_pin_input(SWITCH_2);
  71. init_pins();
  72. }
  73. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  74. bool changed = false;
  75. // Set row, read cols
  76. for (uint8_t current_row = 0; current_row < MATRIX_ROWS - 1; current_row++) {
  77. changed |= read_cols_on_row(current_matrix, current_row);
  78. }
  79. // Read encoder switches, already debounced
  80. changed |= read_encoder_switches(current_matrix, 4);
  81. return changed;
  82. }