logo

qmk_firmware

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

matrix.c (3610B)


  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 "sn74x138.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. /* Columns 6 and 9-15 use a 74HC138 3-to-8 demultiplexer.
  19. * D4 is the enable pin, must be set high to use it.
  20. *
  21. * 0: F7
  22. * 1: F5
  23. * 2: F6
  24. * 3: F1
  25. * 4: F4
  26. * 5: F0
  27. *
  28. * A2 A1 A0
  29. * D0 D1 D2
  30. * 6: 1 1 1
  31. *
  32. * 7: D5
  33. * 8: D3
  34. *
  35. * 9: 1 1 0
  36. * 10: 1 0 1
  37. * 11: 1 0 0
  38. * 12: 0 1 1
  39. * 13: 0 1 0
  40. * 14: 0 0 1
  41. * 15: 0 0 0
  42. */
  43. static void select_col(uint8_t col) {
  44. if (col_pins[col] != NO_PIN) {
  45. gpio_write_pin_low(col_pins[col]);
  46. } else {
  47. sn74x138_set_addr((col == 6) ? 7 : 15 - col);
  48. sn74x138_set_enabled(true);
  49. }
  50. }
  51. static void unselect_col(uint8_t col) {
  52. if (col_pins[col] != NO_PIN) {
  53. gpio_set_pin_output(col_pins[col]);
  54. gpio_write_pin_high(col_pins[col]);
  55. } else {
  56. sn74x138_set_enabled(false);
  57. }
  58. }
  59. static void unselect_cols(void) {
  60. // Native
  61. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  62. if (col_pins[x] != NO_PIN) {
  63. gpio_set_pin_output(col_pins[x]);
  64. gpio_write_pin_high(col_pins[x]);
  65. }
  66. }
  67. // Demultiplexer
  68. sn74x138_set_enabled(false);
  69. }
  70. static void init_pins(void) {
  71. unselect_cols();
  72. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  73. gpio_set_pin_input_high(row_pins[x]);
  74. }
  75. }
  76. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  77. bool matrix_changed = false;
  78. // Select col and wait for col selection to stabilize
  79. select_col(current_col);
  80. matrix_io_delay();
  81. // For each row...
  82. for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
  83. // Store last value of row prior to reading
  84. matrix_row_t last_row_value = current_matrix[row_index];
  85. // Check row pin state
  86. if (gpio_read_pin(row_pins[row_index]) == 0) {
  87. // Pin LO, set col bit
  88. current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col);
  89. } else {
  90. // Pin HI, clear col bit
  91. current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col);
  92. }
  93. // Determine if the matrix changed state
  94. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
  95. matrix_changed = true;
  96. }
  97. }
  98. // Unselect col
  99. unselect_col(current_col);
  100. return matrix_changed;
  101. }
  102. void matrix_init_custom(void) {
  103. // initialize demultiplexer
  104. sn74x138_init();
  105. // initialize key pins
  106. init_pins();
  107. }
  108. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  109. bool changed = false;
  110. // Set col, read rows
  111. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  112. changed |= read_rows_on_col(current_matrix, current_col);
  113. }
  114. return changed;
  115. }