logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

matrix.c (3422B)


  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. #include "wait.h"
  16. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  17. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  18. extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
  19. extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values
  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. gpio_set_pin_input_high(PIN_JU);
  38. gpio_set_pin_input_high(PIN_JD);
  39. gpio_set_pin_input_high(PIN_JL);
  40. gpio_set_pin_input_high(PIN_JR);
  41. gpio_set_pin_input_high(PIN_JC);
  42. gpio_set_pin_input_high(PIN_TC);
  43. }
  44. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  45. if (current_row > 2) return false;
  46. matrix_row_t last_row_value = current_matrix[current_row];
  47. current_matrix[current_row] = 0;
  48. select_row(current_row);
  49. wait_us(30);
  50. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  51. uint8_t pin_state = gpio_read_pin(col_pins[col_index]);
  52. current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  53. }
  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[]) {
  58. matrix_row_t last_fourth_row = current_matrix[3];
  59. matrix_row_t last_fifth_row = current_matrix[4];
  60. current_matrix[3] = 0;
  61. current_matrix[4] = 0;
  62. current_matrix[4] |= !gpio_read_pin(PIN_TC) ? (1 << 1) : 0;
  63. if (!gpio_read_pin(PIN_JC)) {
  64. if (!gpio_read_pin(PIN_JU)) {
  65. current_matrix[3] |= (1 << 0);
  66. } else if (!gpio_read_pin(PIN_JD)) {
  67. current_matrix[3] |= (1 << 1);
  68. } else if (!gpio_read_pin(PIN_JL)) {
  69. current_matrix[3] |= (1 << 2);
  70. } else if (!gpio_read_pin(PIN_JR)) {
  71. current_matrix[3] |= (1 << 3);
  72. } else {
  73. current_matrix[4] |= (1 << 0);
  74. }
  75. }
  76. return last_fourth_row != current_matrix[3] || last_fifth_row != current_matrix[4];
  77. }
  78. void matrix_init_custom(void) {
  79. init_pins();
  80. }
  81. bool matrix_scan_custom(void) {
  82. bool changed = false;
  83. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  84. changed |= read_cols_on_row(raw_matrix, current_row);
  85. }
  86. changed |= read_encoder_switches(raw_matrix);
  87. return changed;
  88. }