matrix.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. Copyright 2022 somepin
  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 col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  20. /* All rows use a 74HC138 3 to 8 bit demultiplexer.
  21. *
  22. * A2 A1 A0
  23. * D0 D1 D2
  24. * 0: 0 0 0
  25. * 1: 0 0 1
  26. * 2: 0 1 0
  27. * 3: 0 1 1
  28. * 4: 1 0 0
  29. * 5: 1 0 1
  30. * 6: 1 1 0
  31. */
  32. static void select_row(uint8_t row) {
  33. sn74x138_set_addr(row);
  34. }
  35. static void init_pins(void) {
  36. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  37. setPinInputHigh(col_pins[x]);
  38. }
  39. }
  40. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  41. bool matrix_changed = false;
  42. // Store last value of row prior to reading
  43. matrix_row_t last_row_value = current_matrix[current_row];
  44. // Start with a clear matrix row
  45. current_matrix[current_row] = 0;
  46. // Select row and wait for row selection to stabilize
  47. select_row(current_row);
  48. matrix_io_delay();
  49. // For each col...
  50. matrix_row_t row_shifter = MATRIX_ROW_SHIFTER;
  51. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  52. // Select the col pin to read (active low)
  53. uint8_t pin_state = readPin(col_pins[col_index]);
  54. // Populate the matrix row with the state of the col pin
  55. current_matrix[current_row] |= pin_state ? 0 : (row_shifter << col_index);
  56. }
  57. // Determine if matrix changed state
  58. if ((last_row_value != current_matrix[current_row]) && !(matrix_changed)) {
  59. matrix_changed = true;
  60. }
  61. return matrix_changed;
  62. }
  63. void matrix_init_custom(void) {
  64. // initialize demultiplexer
  65. sn74x138_init();
  66. // initialize key pins
  67. init_pins();
  68. }
  69. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  70. bool changed = false;
  71. // Set row, read cols
  72. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  73. changed |= read_cols_on_row(current_matrix, current_row);
  74. }
  75. return changed;
  76. }