You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
3.4 KiB

  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 <stdint.h>
  15. #include <stdbool.h>
  16. #include "matrix.h"
  17. #include "quantum.h"
  18. #include "sn74x138.h"
  19. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  20. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  21. /* Columns 6-12 use a 74HC138 3-to-8 demultiplexer.
  22. *
  23. * 0: C7
  24. * 1: B6
  25. * 2: C6
  26. * 3: B4
  27. * 4: B5
  28. * 5: D7
  29. *
  30. * A2 A1 A0
  31. * D0 D1 D2
  32. * 6: 1 1 1
  33. * 7: 1 1 0
  34. * 8: 1 0 1
  35. * 9: 1 0 0
  36. * 10: 0 1 1
  37. * 11: 0 1 0
  38. * 12: 0 0 1
  39. *
  40. * 13: D3
  41. * 14: B7
  42. * 15: B3
  43. */
  44. static void select_col(uint8_t col) {
  45. if (col_pins[col] != NO_PIN) {
  46. gpio_write_pin_low(col_pins[col]);
  47. } else {
  48. sn74x138_set_addr(13 - col);
  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_addr(0);
  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_addr(0);
  69. }
  70. static void init_pins(void) {
  71. unselect_cols();
  72. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  73. setPinInputHigh(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. }