logo

qmk_firmware

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

matrix.c (4439B)


  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. #if (MATRIX_COLS <= 8)
  16. # define ROW_SHIFTER ((uint8_t)1)
  17. #elif (MATRIX_COLS <= 16)
  18. # define ROW_SHIFTER ((uint16_t)1)
  19. #elif (MATRIX_COLS <= 32)
  20. # define ROW_SHIFTER ((uint32_t)1)
  21. #endif
  22. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  23. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  24. static void select_row(uint8_t row) {
  25. gpio_set_pin_output(row_pins[row]);
  26. gpio_write_pin_low(row_pins[row]);
  27. }
  28. static void unselect_row(uint8_t row) {
  29. gpio_set_pin_input_high(row_pins[row]);
  30. }
  31. static void unselect_rows(void) {
  32. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  33. gpio_set_pin_input_high(row_pins[x]);
  34. }
  35. }
  36. static void select_col(uint8_t col) {
  37. gpio_set_pin_output(col_pins[col]);
  38. gpio_write_pin_low(col_pins[col]);
  39. }
  40. static void unselect_col(uint8_t col) {
  41. gpio_set_pin_input_high(col_pins[col]);
  42. }
  43. static void unselect_cols(void) {
  44. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  45. gpio_set_pin_input_high(col_pins[x]);
  46. }
  47. }
  48. static void init_pins(void) {
  49. unselect_rows();
  50. unselect_cols();
  51. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  52. gpio_set_pin_input_high(col_pins[x]);
  53. }
  54. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  55. gpio_set_pin_input_high(row_pins[x]);
  56. }
  57. }
  58. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  59. // Store last value of row prior to reading
  60. matrix_row_t last_row_value = current_matrix[current_row];
  61. // Clear data in matrix row
  62. current_matrix[current_row] = 0;
  63. // Select row and wait for row selecton to stabilize
  64. select_row(current_row);
  65. matrix_io_delay();
  66. // For each col...
  67. for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  68. // Select the col pin to read (active low)
  69. uint8_t pin_state = gpio_read_pin(col_pins[col_index]);
  70. // Populate the matrix row with the state of the col pin
  71. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
  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 selecton to stabilize
  80. select_col(current_col);
  81. matrix_io_delay();
  82. // For each row...
  83. for(uint8_t row_index = 0; row_index < MATRIX_ROWS / 2; row_index++) {
  84. uint8_t tmp = row_index + MATRIX_ROWS / 2;
  85. // Store last value of row prior to reading
  86. matrix_row_t last_row_value = current_matrix[tmp];
  87. // Check row pin state
  88. if (gpio_read_pin(row_pins[row_index]) == 0) {
  89. // Pin LO, set col bit
  90. current_matrix[tmp] |= (ROW_SHIFTER << current_col);
  91. } else {
  92. // Pin HI, clear col bit
  93. current_matrix[tmp] &= ~(ROW_SHIFTER << current_col);
  94. }
  95. // Determine if the matrix changed state
  96. if ((last_row_value != current_matrix[tmp]) && !(matrix_changed)) {
  97. matrix_changed = true;
  98. }
  99. }
  100. // Unselect col
  101. unselect_col(current_col);
  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 / 2; 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; current_col++) {
  116. changed |= read_rows_on_col(current_matrix, current_col);
  117. }
  118. return changed;
  119. }