logo

qmk_firmware

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

matrix.c (3471B)


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