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.

269 lines
7.6 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 "debounce.h"
  20. #include "quantum.h"
  21. // Encoder things
  22. #define SWITCH_1 F7
  23. #define SWITCH_2 D7
  24. static bool read_encoder_values(matrix_row_t current_matrix[], uint8_t current_row);
  25. #ifdef MATRIX_MASKED
  26. extern const matrix_row_t matrix_mask[];
  27. #endif
  28. #ifdef DIRECT_PINS
  29. static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS;
  30. #elif (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
  31. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  32. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  33. #endif
  34. /* matrix state(1:on, 0:off) */
  35. static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
  36. static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
  37. // helper functions
  38. inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); }
  39. inline matrix_row_t matrix_get_row(uint8_t row) {
  40. // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
  41. // switch blocker installed and the switch is always pressed.
  42. #ifdef MATRIX_MASKED
  43. return matrix[row] & matrix_mask[row];
  44. #else
  45. return matrix[row];
  46. #endif
  47. }
  48. // matrix code
  49. #ifdef DIRECT_PINS
  50. static void init_pins(void) {
  51. for (int row = 0; row < MATRIX_ROWS; row++) {
  52. for (int col = 0; col < MATRIX_COLS; col++) {
  53. pin_t pin = direct_pins[row][col];
  54. if (pin != NO_PIN) {
  55. setPinInputHigh(pin);
  56. }
  57. }
  58. }
  59. }
  60. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  61. matrix_row_t last_row_value = current_matrix[current_row];
  62. current_matrix[current_row] = 0;
  63. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  64. pin_t pin = direct_pins[current_row][col_index];
  65. if (pin != NO_PIN) {
  66. current_matrix[current_row] |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  67. }
  68. }
  69. return (last_row_value != current_matrix[current_row]);
  70. }
  71. #elif (DIODE_DIRECTION == COL2ROW)
  72. static void select_row(uint8_t row) {
  73. setPinOutput(row_pins[row]);
  74. writePinLow(row_pins[row]);
  75. }
  76. static void unselect_row(uint8_t row) { setPinInputHigh(row_pins[row]); }
  77. static void unselect_rows(void) {
  78. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  79. setPinInputHigh(row_pins[x]);
  80. }
  81. }
  82. static void init_pins(void) {
  83. unselect_rows();
  84. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  85. setPinInputHigh(col_pins[x]);
  86. }
  87. }
  88. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  89. // Store last value of row prior to reading
  90. matrix_row_t last_row_value = current_matrix[current_row];
  91. // Clear data in matrix row
  92. current_matrix[current_row] = 0;
  93. // Select row and wait for row selecton to stabilize
  94. select_row(current_row);
  95. wait_us(30);
  96. // For each col...
  97. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  98. // Select the col pin to read (active low)
  99. uint8_t pin_state = readPin(col_pins[col_index]);
  100. // Populate the matrix row with the state of the col pin
  101. current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  102. }
  103. // Unselect row
  104. unselect_row(current_row);
  105. return (last_row_value != current_matrix[current_row]);
  106. }
  107. #elif (DIODE_DIRECTION == ROW2COL)
  108. static void select_col(uint8_t col) {
  109. setPinOutput(col_pins[col]);
  110. writePinLow(col_pins[col]);
  111. }
  112. static void unselect_col(uint8_t col) { setPinInputHigh(col_pins[col]); }
  113. static void unselect_cols(void) {
  114. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  115. setPinInputHigh(col_pins[x]);
  116. }
  117. }
  118. static void init_pins(void) {
  119. unselect_cols();
  120. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  121. setPinInputHigh(row_pins[x]);
  122. }
  123. }
  124. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  125. bool matrix_changed = false;
  126. // Select col and wait for col selecton to stabilize
  127. select_col(current_col);
  128. wait_us(30);
  129. // For each row...
  130. for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
  131. // Store last value of row prior to reading
  132. matrix_row_t last_row_value = current_matrix[row_index];
  133. // Check row pin state
  134. if (readPin(row_pins[row_index]) == 0) {
  135. // Pin LO, set col bit
  136. current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col);
  137. } else {
  138. // Pin HI, clear col bit
  139. current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col);
  140. }
  141. // Determine if the matrix changed state
  142. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
  143. matrix_changed = true;
  144. }
  145. }
  146. // Unselect col
  147. unselect_col(current_col);
  148. return matrix_changed;
  149. }
  150. #endif
  151. void matrix_init(void) {
  152. // initialize key pins
  153. init_pins();
  154. // initialize matrix state: all keys off
  155. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  156. raw_matrix[i] = 0;
  157. matrix[i] = 0;
  158. }
  159. debounce_init(MATRIX_ROWS);
  160. matrix_init_quantum();
  161. }
  162. uint8_t matrix_scan(void) {
  163. bool changed = false;
  164. #if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
  165. // Set row, read cols
  166. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  167. changed |= read_cols_on_row(raw_matrix, current_row);
  168. }
  169. #elif (DIODE_DIRECTION == ROW2COL)
  170. // Set col, read rows
  171. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  172. changed |= read_rows_on_col(raw_matrix, current_col);
  173. }
  174. #endif
  175. debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
  176. // Read encoder switches, already debounced
  177. changed |= read_encoder_values(matrix, 4);
  178. matrix_scan_quantum();
  179. return (uint8_t)changed;
  180. }
  181. // Customisations for the encoders
  182. void matrix_init_kb(void){
  183. setPinInput(SWITCH_1);
  184. setPinInput(SWITCH_2);
  185. }
  186. void matrix_scan_kb(void){
  187. }
  188. void matrix_print(void){
  189. }
  190. static bool read_encoder_values(matrix_row_t current_matrix[], uint8_t current_row) {
  191. // Store last value of row prior to reading
  192. matrix_row_t last_row_value = current_matrix[current_row];
  193. // Clear data in matrix row
  194. current_matrix[current_row] = 0;
  195. // Debounce the encoder buttons using a shift register
  196. static uint8_t btn_1_array;
  197. static uint8_t btn_2_array;
  198. bool btn_1_rising = 0;
  199. bool btn_2_rising = 0;
  200. btn_1_array <<= 1;
  201. btn_2_array <<= 1;
  202. btn_1_array |= readPin(SWITCH_1);
  203. btn_2_array |= readPin(SWITCH_2);
  204. (btn_1_array == 0b01111111) ? (btn_1_rising = 1) : (btn_1_rising = 0);
  205. (btn_2_array == 0b01111111) ? (btn_2_rising = 1) : (btn_2_rising = 0);
  206. // Populate the matrix row with the state of the encoder
  207. current_matrix[current_row] |= btn_1_rising ? (1 << 0) : 0;
  208. current_matrix[current_row] |= btn_2_rising ? (1 << 1) : 0;
  209. return (last_row_value != current_matrix[current_row]);
  210. }