logo

qmk_firmware

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

matrix.c (4418B)


  1. /*
  2. Copyright 2012-2023 Jun Wako <wakojun@gmail.com> Kyrremann <kyrremann@gmail.com>
  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. Copied from here: https://github.com/e3w2q/qmk_firmware/blob/762fe3e0a7cbea768245a75520f06ff5a2f00b9f/keyboards/2x3test/matrix.c
  14. */
  15. /*
  16. * scan matrix
  17. */
  18. #include <stdint.h>
  19. #include <stdbool.h>
  20. #include "wait.h"
  21. #include "util.h"
  22. #include "matrix.h"
  23. #include "quantum.h"
  24. #define ROW_SHIFTER ((uint16_t)1)
  25. static const pin_t row_pins[] = MATRIX_ROW_PINS;
  26. static const pin_t col_pins[] = MATRIX_COL_PINS;
  27. static void select_row(uint8_t row) {
  28. gpio_set_pin_output(row_pins[row]);
  29. gpio_write_pin_low(row_pins[row]);
  30. }
  31. static void unselect_row(uint8_t row) {
  32. gpio_set_pin_input_high(row_pins[row]);
  33. }
  34. static void unselect_rows(void) {
  35. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  36. gpio_set_pin_input_high(row_pins[x]);
  37. }
  38. }
  39. static void select_col(uint8_t col) {
  40. gpio_set_pin_output(col_pins[col]);
  41. gpio_write_pin_low(col_pins[col]);
  42. }
  43. static void unselect_col(uint8_t col) {
  44. gpio_set_pin_input_high(col_pins[col]);
  45. }
  46. static void unselect_cols(void) {
  47. for (uint8_t x = 0; x < MATRIX_COLS/2; x++) {
  48. gpio_set_pin_input_high(col_pins[x*2]);
  49. }
  50. }
  51. static void init_pins(void) {
  52. unselect_cols();
  53. unselect_rows();
  54. }
  55. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  56. // Store last value of row prior to reading
  57. matrix_row_t last_row_value = current_matrix[current_row];
  58. // Select row and wait for row selection to stabilize
  59. select_row(current_row);
  60. wait_us(30);
  61. // For each col...
  62. for (uint8_t col_index = 0; col_index < MATRIX_COLS / 2; col_index++) {
  63. uint16_t column_index_bitmask = ROW_SHIFTER << ((col_index * 2) + 1);
  64. // Check row pin state
  65. if (gpio_read_pin(col_pins[col_index*2])) {
  66. // Pin HI, clear col bit
  67. current_matrix[current_row] &= ~column_index_bitmask;
  68. } else {
  69. // Pin LO, set col bit
  70. current_matrix[current_row] |= column_index_bitmask;
  71. }
  72. }
  73. // Unselect row
  74. unselect_row(current_row);
  75. return (last_row_value != current_matrix[current_row]);
  76. }
  77. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  78. bool matrix_changed = false;
  79. // Select col and wait for col selection to stabilize
  80. select_col(current_col*2);
  81. wait_us(30);
  82. // For each row...
  83. for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
  84. // Store last value of row prior to reading
  85. matrix_row_t last_row_value = current_matrix[row_index];
  86. uint16_t column_index_bitmask = ROW_SHIFTER << (current_col * 2);
  87. // Check row pin state
  88. if (gpio_read_pin(row_pins[row_index])) {
  89. // Pin HI, clear col bit
  90. current_matrix[row_index] &= ~column_index_bitmask;
  91. } else {
  92. // Pin LO, set col bit
  93. current_matrix[row_index] |= column_index_bitmask;
  94. }
  95. // Determine if the matrix changed state
  96. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
  97. matrix_changed = true;
  98. }
  99. }
  100. // Unselect col
  101. unselect_col(current_col*2);
  102. return matrix_changed;
  103. }
  104. void matrix_init_custom(void) {
  105. // initialize key pins
  106. init_pins();
  107. }
  108. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  109. bool changed = false;
  110. // Set row, read cols
  111. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  112. changed |= read_cols_on_row(current_matrix, current_row);
  113. }
  114. // Set col, read rows
  115. for (uint8_t current_col = 0; current_col < MATRIX_COLS/2; current_col++) {
  116. changed |= read_rows_on_col(current_matrix, current_col);
  117. }
  118. return changed;
  119. }