matrix.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 "wait.h"
  17. #include "util.h"
  18. #include "matrix.h"
  19. #include "quantum.h"
  20. // Encoder things
  21. #define ENC_SW F7
  22. static bool read_encoder_switches(matrix_row_t current_matrix[], uint8_t current_row);
  23. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  24. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  25. /* matrix state(1:on, 0:off) */
  26. extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
  27. extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values
  28. static void select_row(uint8_t row) {
  29. setPinOutput(row_pins[row]);
  30. writePinLow(row_pins[row]);
  31. }
  32. static void unselect_row(uint8_t row) { setPinInputHigh(row_pins[row]); }
  33. static void unselect_rows(void) {
  34. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  35. setPinInputHigh(row_pins[x]);
  36. }
  37. }
  38. static void init_pins(void) {
  39. unselect_rows();
  40. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  41. setPinInputHigh(col_pins[x]);
  42. }
  43. }
  44. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  45. // Store last value of row prior to reading
  46. matrix_row_t last_row_value = current_matrix[current_row];
  47. // Clear data in matrix row
  48. current_matrix[current_row] = 0;
  49. // Select row and wait for row selecton to stabilize
  50. select_row(current_row);
  51. wait_us(30);
  52. // For each col...
  53. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  54. // Select the col pin to read (active low)
  55. uint8_t pin_state = readPin(col_pins[col_index]);
  56. // Populate the matrix row with the state of the col pin
  57. current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  58. }
  59. // Unselect row
  60. unselect_row(current_row);
  61. return (last_row_value != current_matrix[current_row]);
  62. }
  63. void matrix_init_custom(void) {
  64. // initialize key pins
  65. setPinInput(ENC_SW);
  66. init_pins();
  67. }
  68. bool matrix_scan_custom(void) {
  69. bool changed = false;
  70. // Set row, read cols
  71. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  72. changed |= read_cols_on_row(raw_matrix, current_row);
  73. }
  74. // Read encoder switches, already debounced
  75. changed |= read_encoder_switches(matrix, 2);
  76. return changed;
  77. }
  78. static bool read_encoder_switches(matrix_row_t current_matrix[], uint8_t current_row) {
  79. // Store last value of row prior to reading
  80. matrix_row_t last_row_value = current_matrix[current_row];
  81. // Clear data in matrix row
  82. current_matrix[current_row] = 0;
  83. // Debounce the encoder buttons using a shift register
  84. static uint8_t btn_1_array;
  85. bool btn_1_pressed = 0;
  86. btn_1_array <<= 1;
  87. btn_1_array |= readPin(ENC_SW);
  88. (btn_1_array == 0b11111111) ? (btn_1_pressed = 1) : (btn_1_pressed = 0);
  89. // Populate the matrix row with the state of the encoder
  90. current_matrix[current_row] |= btn_1_pressed ? (1 << 0) : 0;
  91. return (last_row_value != current_matrix[current_row]);
  92. }