logo

qmk_firmware

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

matrix.c (4472B)


  1. /*
  2. Copyright 2012-2018 Jun Wako, Jack Humbert, Yiancar
  3. Copyright 2022 jun10000
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "matrix.h"
  16. #include "wait.h"
  17. #if (MATRIX_COLS <= 8)
  18. # define ROW_SHIFTER ((uint8_t)1)
  19. #elif (MATRIX_COLS <= 16)
  20. # define ROW_SHIFTER ((uint16_t)1)
  21. #elif (MATRIX_COLS <= 32)
  22. # define ROW_SHIFTER ((uint32_t)1)
  23. #endif
  24. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  25. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  26. static void select_row(uint8_t row)
  27. {
  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. {
  33. gpio_set_pin_input_high(row_pins[row]);
  34. }
  35. static void unselect_rows(void)
  36. {
  37. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  38. gpio_set_pin_input_high(row_pins[x]);
  39. }
  40. }
  41. static void select_col(uint8_t col)
  42. {
  43. gpio_set_pin_output(col_pins[col]);
  44. gpio_write_pin_low(col_pins[col]);
  45. }
  46. static void unselect_col(uint8_t col)
  47. {
  48. gpio_set_pin_input_high(col_pins[col]);
  49. }
  50. static void unselect_cols(void)
  51. {
  52. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  53. gpio_set_pin_input_high(col_pins[x]);
  54. }
  55. }
  56. static void init_pins(void) {
  57. unselect_rows();
  58. unselect_cols();
  59. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  60. gpio_set_pin_input_high(col_pins[x]);
  61. }
  62. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  63. gpio_set_pin_input_high(row_pins[x]);
  64. }
  65. }
  66. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
  67. {
  68. // Store last value of row prior to reading
  69. matrix_row_t last_row_value = current_matrix[current_row];
  70. // Clear data in matrix row
  71. current_matrix[current_row] = 0;
  72. // Select row and wait for row selecton to stabilize
  73. select_row(current_row);
  74. wait_us(30);
  75. // For each col...
  76. for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  77. // Select the col pin to read (active low)
  78. uint8_t pin_state = gpio_read_pin(col_pins[col_index]);
  79. // Populate the matrix row with the state of the col pin
  80. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
  81. }
  82. // Unselect row
  83. unselect_row(current_row);
  84. return (last_row_value != current_matrix[current_row]);
  85. }
  86. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  87. {
  88. bool matrix_changed = false;
  89. // Select col and wait for col selecton to stabilize
  90. select_col(current_col);
  91. wait_us(30);
  92. // For each row...
  93. for(uint8_t row_index = 0; row_index < MATRIX_ROWS/2; row_index++)
  94. {
  95. uint8_t tmp = row_index + MATRIX_ROWS/2;
  96. // Store last value of row prior to reading
  97. matrix_row_t last_row_value = current_matrix[tmp];
  98. // Check row pin state
  99. if (gpio_read_pin(row_pins[row_index]) == 0)
  100. {
  101. // Pin LO, set col bit
  102. current_matrix[tmp] |= (ROW_SHIFTER << current_col);
  103. }
  104. else
  105. {
  106. // Pin HI, clear col bit
  107. current_matrix[tmp] &= ~(ROW_SHIFTER << current_col);
  108. }
  109. // Determine if the matrix changed state
  110. if ((last_row_value != current_matrix[tmp]) && !(matrix_changed))
  111. {
  112. matrix_changed = true;
  113. }
  114. }
  115. // Unselect col
  116. unselect_col(current_col);
  117. return matrix_changed;
  118. }
  119. void matrix_init_custom(void) {
  120. // initialize key pins
  121. init_pins();
  122. }
  123. bool matrix_scan_custom(matrix_row_t current_matrix[])
  124. {
  125. bool changed = false;
  126. // Set row, read cols
  127. for (uint8_t current_row = 0; current_row < MATRIX_ROWS / 2; current_row++) {
  128. changed |= read_cols_on_row(current_matrix, current_row);
  129. }
  130. //else
  131. // Set col, read rows
  132. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  133. changed |= read_rows_on_col(current_matrix, current_col);
  134. }
  135. return (uint8_t)changed;
  136. }