matrix.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. Copyright 2023 QVEX Tech
  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. gpio_set_pin_input_high(PIN_JU);
  35. gpio_set_pin_input_high(PIN_JD);
  36. gpio_set_pin_input_high(PIN_JL);
  37. gpio_set_pin_input_high(PIN_JR);
  38. gpio_set_pin_input_high(PIN_JC);
  39. gpio_set_pin_input_high(PIN_TC);
  40. }
  41. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  42. if (current_row > 2) return false;
  43. matrix_row_t last_row_value = current_matrix[current_row];
  44. current_matrix[current_row] = 0;
  45. select_row(current_row);
  46. matrix_io_delay();
  47. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  48. uint8_t pin_state = gpio_read_pin(col_pins[col_index]);
  49. current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  50. }
  51. unselect_row(current_row);
  52. return (last_row_value != current_matrix[current_row]);
  53. }
  54. static bool read_encoder_switches(matrix_row_t current_matrix[]) {
  55. matrix_row_t last_fourth_row = current_matrix[3];
  56. matrix_row_t last_fifth_row = current_matrix[4];
  57. current_matrix[3] = 0;
  58. current_matrix[4] = 0;
  59. current_matrix[4] |= !gpio_read_pin(PIN_TC) ? (1 << 1) : 0;
  60. if (!gpio_read_pin(PIN_JC)) {
  61. if (!gpio_read_pin(PIN_JU)) {
  62. current_matrix[3] |= (1 << 0);
  63. } else if (!gpio_read_pin(PIN_JD)) {
  64. current_matrix[3] |= (1 << 1);
  65. } else if (!gpio_read_pin(PIN_JL)) {
  66. current_matrix[3] |= (1 << 2);
  67. } else if (!gpio_read_pin(PIN_JR)) {
  68. current_matrix[3] |= (1 << 3);
  69. } else {
  70. current_matrix[4] |= (1 << 0);
  71. }
  72. }
  73. return last_fourth_row != current_matrix[3] || last_fifth_row != current_matrix[4];
  74. }
  75. void matrix_init_custom(void) {
  76. init_pins();
  77. }
  78. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  79. bool changed = false;
  80. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  81. changed |= read_cols_on_row(current_matrix, current_row);
  82. }
  83. changed |= read_encoder_switches(current_matrix);
  84. return changed;
  85. }