logo

qmk_firmware

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

matrix.c (4468B)


  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. #include "atomic_util.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. static inline void gpio_atomic_set_pin_output_low(pin_t pin) {
  19. ATOMIC_BLOCK_FORCEON {
  20. gpio_set_pin_output(pin);
  21. gpio_write_pin_low(pin);
  22. }
  23. }
  24. static inline void gpio_atomic_set_pin_input_high(pin_t pin) {
  25. ATOMIC_BLOCK_FORCEON { gpio_set_pin_input_high(pin); }
  26. }
  27. static void select_row(uint8_t row) {
  28. gpio_atomic_set_pin_output_low(row_pins[row]);
  29. }
  30. static void unselect_row(uint8_t row) {
  31. gpio_atomic_set_pin_input_high(row_pins[row]);
  32. }
  33. static void unselect_rows(void) {
  34. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  35. gpio_atomic_set_pin_input_high(row_pins[x]);
  36. }
  37. }
  38. static void select_col(uint8_t col) {
  39. gpio_atomic_set_pin_output_low(col_pins[col]);
  40. }
  41. static void unselect_col(uint8_t col) {
  42. gpio_atomic_set_pin_input_high(col_pins[col]);
  43. }
  44. static void unselect_cols(void) {
  45. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  46. gpio_atomic_set_pin_input_high(col_pins[x]);
  47. }
  48. }
  49. static void init_pins(void) {
  50. unselect_rows();
  51. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  52. gpio_atomic_set_pin_input_high(col_pins[x]);
  53. }
  54. unselect_cols();
  55. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  56. gpio_atomic_set_pin_input_high(row_pins[x]);
  57. }
  58. }
  59. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  60. // Store last value of row prior to reading
  61. matrix_row_t last_row_value = current_matrix[current_row];
  62. // Select row
  63. select_row(current_row);
  64. matrix_io_delay();
  65. // For each col...
  66. for (uint8_t col_index = 0; col_index < MATRIX_COLS / 2; col_index++) {
  67. // Check row pin state
  68. if (gpio_read_pin(col_pins[col_index])) {
  69. // Pin HI, clear col bit
  70. current_matrix[current_row] &= ~(MATRIX_ROW_SHIFTER << col_index);
  71. } else {
  72. // Pin LO, set col bit
  73. current_matrix[current_row] |= (MATRIX_ROW_SHIFTER << col_index);
  74. }
  75. }
  76. // Unselect row
  77. unselect_row(current_row);
  78. return (last_row_value != current_matrix[current_row]);
  79. }
  80. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  81. bool matrix_changed = false;
  82. // Select col
  83. select_col(current_col);
  84. matrix_io_delay();
  85. // For each row...
  86. for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
  87. // Store last value of row prior to reading
  88. matrix_row_t last_row_value = current_matrix[row_index];
  89. // Check row pin state
  90. if (gpio_read_pin(row_pins[row_index])) {
  91. // Pin HI, clear col bit
  92. current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << ( current_col + MATRIX_COLS/2));
  93. } else {
  94. // Pin LO, set col bit
  95. current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << ( current_col + MATRIX_COLS/2));
  96. }
  97. // Determine if the matrix changed state
  98. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
  99. matrix_changed = true;
  100. }
  101. }
  102. // Unselect col
  103. unselect_col(current_col);
  104. return matrix_changed;
  105. }
  106. void matrix_init_custom(void) {
  107. init_pins();
  108. }
  109. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  110. bool changed = false;
  111. // Set row, read cols
  112. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  113. changed |= read_cols_on_row(current_matrix, current_row);
  114. }
  115. // Set col, read rows
  116. for (uint8_t current_col = 0; current_col < (MATRIX_COLS/2); current_col++) {
  117. changed |= read_rows_on_col(current_matrix, current_col);
  118. }
  119. return (uint8_t)changed;
  120. }