matrix.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. Copyright 2012-2020 Jun Wako, Jack Humbert, Yiancar, Takeshi Nishio
  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. #define ROW_SHIFTER ((uint16_t)1)
  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. static void select_row(uint8_t row) {
  22. setPinOutput(row_pins[row]);
  23. writePinLow(row_pins[row]);
  24. }
  25. static void unselect_row(uint8_t row) {
  26. setPinInputHigh(row_pins[row]);
  27. }
  28. static void unselect_rows(void) {
  29. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  30. setPinInputHigh(row_pins[x]);
  31. }
  32. }
  33. static void init_pins(void) {
  34. unselect_rows();
  35. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  36. setPinInputHigh(col_pins[x]);
  37. }
  38. }
  39. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  40. // Store last value of row prior to reading
  41. matrix_row_t last_row_value = current_matrix[current_row];
  42. // Clear data in matrix row
  43. current_matrix[current_row] = 0;
  44. // Select row and wait for row selecton to stabilize
  45. select_row(current_row);
  46. matrix_io_delay();
  47. // For each col...
  48. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  49. // skip reading when index equals (= pin itself)
  50. if (col_index != current_row) {
  51. // Check col pin pin_state
  52. if (readPin(col_pins[col_index]) == 0) {
  53. // Pin LO, set col bit
  54. current_matrix[current_row] |= (ROW_SHIFTER << col_index);
  55. } else {
  56. // Pin HI, clear col bit
  57. current_matrix[current_row] &= ~(ROW_SHIFTER << col_index);
  58. }
  59. }
  60. }
  61. // Unselect row
  62. unselect_row(current_row);
  63. return (last_row_value != current_matrix[current_row]);
  64. }
  65. void matrix_init_custom(void) {
  66. // initialize key pins
  67. init_pins();
  68. matrix_init_quantum();
  69. }
  70. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  71. bool changed = false;
  72. // Set row, read cols
  73. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  74. changed |= read_cols_on_row(current_matrix, current_row);
  75. }
  76. matrix_scan_quantum();
  77. return changed;
  78. }