matrix.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* Copyright 2021 xyzz
  2. *
  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. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <stdint.h>
  17. #include <stdbool.h>
  18. #include "util.h"
  19. #include "matrix.h"
  20. #include "debounce.h"
  21. #include "quantum.h"
  22. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  23. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  24. static void select_col(uint8_t col) {
  25. setPinOutput(col_pins[col]);
  26. writePinHigh(col_pins[col]);
  27. }
  28. static void unselect_col(uint8_t col) { setPinInputLow(col_pins[col]); }
  29. static void unselect_cols(void) {
  30. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  31. setPinInputLow(col_pins[x]);
  32. }
  33. }
  34. static void init_pins(void) {
  35. unselect_cols();
  36. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  37. setPinInputLow(row_pins[x]);
  38. }
  39. }
  40. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  41. bool matrix_changed = false;
  42. // Select col and wait for col selecton to stabilize
  43. select_col(current_col);
  44. matrix_io_delay();
  45. // For each row...
  46. for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
  47. // Store last value of row prior to reading
  48. matrix_row_t last_row_value = current_matrix[row_index];
  49. matrix_row_t current_row_value = last_row_value;
  50. // Check row pin state
  51. if (readPin(row_pins[row_index]) != 0) {
  52. // Pin LO, set col bit
  53. current_row_value |= (MATRIX_ROW_SHIFTER << current_col);
  54. } else {
  55. // Pin HI, clear col bit
  56. current_row_value &= ~(MATRIX_ROW_SHIFTER << current_col);
  57. }
  58. // Determine if the matrix changed state
  59. if ((last_row_value != current_row_value)) {
  60. matrix_changed |= true;
  61. current_matrix[row_index] = current_row_value;
  62. }
  63. }
  64. // Unselect col
  65. unselect_col(current_col);
  66. return matrix_changed;
  67. }
  68. void matrix_init_custom(void) {
  69. // initialize key pins
  70. init_pins();
  71. }
  72. uint8_t matrix_scan_custom(matrix_row_t current_matrix[]) {
  73. bool changed = false;
  74. // Set col, read rows
  75. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  76. changed |= read_rows_on_col(current_matrix, current_col);
  77. }
  78. return (uint8_t)changed;
  79. }