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.

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