logo

qmk_firmware

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

matrix.c (4257B)


  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. 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. {
  19. gpio_set_pin_output(row_pins[row]);
  20. gpio_write_pin_low(row_pins[row]);
  21. }
  22. static void unselect_row(uint8_t row)
  23. {
  24. gpio_set_pin_input_high(row_pins[row]);
  25. }
  26. static void unselect_rows(void)
  27. {
  28. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  29. gpio_set_pin_input_high(row_pins[x]);
  30. }
  31. }
  32. static void select_col(uint8_t col)
  33. {
  34. gpio_set_pin_output(col_pins[col]);
  35. gpio_write_pin_low(col_pins[col]);
  36. }
  37. static void unselect_col(uint8_t col)
  38. {
  39. gpio_set_pin_input_high(col_pins[col]);
  40. }
  41. static void unselect_cols(void)
  42. {
  43. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  44. gpio_set_pin_input_high(col_pins[x]);
  45. }
  46. }
  47. static void init_pins(void) {
  48. unselect_rows();
  49. unselect_cols();
  50. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  51. gpio_set_pin_input_high(col_pins[x]);
  52. }
  53. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  54. gpio_set_pin_input_high(row_pins[x]);
  55. }
  56. }
  57. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
  58. {
  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 : (MATRIX_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. {
  79. bool matrix_changed = false;
  80. // Select col and wait for col selecton to stabilize
  81. select_col(current_col);
  82. matrix_io_delay();
  83. // For each row...
  84. for(uint8_t row_index = 0; row_index < MATRIX_ROWS/2; row_index++)
  85. {
  86. uint8_t tmp = row_index + MATRIX_ROWS/2;
  87. // Store last value of row prior to reading
  88. matrix_row_t last_row_value = current_matrix[tmp];
  89. // Check row pin state
  90. if (gpio_read_pin(row_pins[row_index]) == 0)
  91. {
  92. // Pin LO, set col bit
  93. current_matrix[tmp] |= (MATRIX_ROW_SHIFTER << current_col);
  94. }
  95. else
  96. {
  97. // Pin HI, clear col bit
  98. current_matrix[tmp] &= ~(MATRIX_ROW_SHIFTER << current_col);
  99. }
  100. // Determine if the matrix changed state
  101. if ((last_row_value != current_matrix[tmp]) && !(matrix_changed))
  102. {
  103. matrix_changed = true;
  104. }
  105. }
  106. // Unselect col
  107. unselect_col(current_col);
  108. return matrix_changed;
  109. }
  110. void matrix_init_custom(void) {
  111. // initialize key pins
  112. init_pins();
  113. }
  114. bool matrix_scan_custom(matrix_row_t current_matrix[])
  115. {
  116. bool changed = false;
  117. // Set row, read cols
  118. for (uint8_t current_row = 0; current_row < MATRIX_ROWS / 2; current_row++) {
  119. changed |= read_cols_on_row(current_matrix, current_row);
  120. }
  121. //else
  122. // Set col, read rows
  123. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  124. changed |= read_rows_on_col(current_matrix, current_col);
  125. }
  126. return changed;
  127. }