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.

222 lines
6.2 KiB

8 years ago
8 years ago
8 years ago
Simplify split_common Code significantly (#4772) * Eliminate separate slave loop Both master and slave run the standard keyboard_task main loop now. * Refactor i2c/serial specific code Simplify some of the preprocessor mess by using common function names. * Fix missing #endif * Move direct pin mapping support from miniaxe to split_common For boards with more pins than sense--sorry, switches. * Reordering and reformatting only * Don't run matrix_scan_quantum on slave side * Clean up the offset/slaveOffset calculations * Cut undebounced matrix size in half * Refactor debouncing * Minor fixups * Split split_common transport and debounce code into their own files Can now be replaced with custom versions per keyboard using CUSTOM_TRANSPORT = yes and CUSTOM_DEBOUNCE = yes * Refactor debounce for non-split keyboards too * Update handwired/xealous to build using new split_common * Fix debounce breaking basic test * Dodgy method to allow a split kb to only include one of i2c/serial SPLIT_TRANSPORT = serial or SPLIT_TRANSPORT = i2c will include only that driver code in the binary. SPLIT_TRANSPORT = custom (or anything else) will include neither, the keyboard must supply it's own code if SPLIT_TRANSPORT is not defined then the original behaviour (include both avr i2c and serial code) is maintained. This could be better but it would require explicitly updating all the existing split keyboards. * Enable LTO to get lets_split/sockets under the line * Add docs for SPLIT_TRANSPORT, CUSTOM_MATRIX, CUSTOM_DEBOUNCE * Remove avr-specific sei() from split matrix_setup Not needed now that slave doesn't have a separate main loop. Both sides (on avr) call sei() in lufa's main() after exiting keyboard_setup(). * Fix QUANTUM_LIB_SRC references and simplify SPLIT_TRANSPORT. * Add comments and fix formatting.
5 years ago
8 years ago
8 years ago
  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. #ifdef MATRIX_MASKED
  22. extern const matrix_row_t matrix_mask[];
  23. #endif
  24. #ifdef DIRECT_PINS
  25. static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS;
  26. #elif (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
  27. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  28. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  29. #endif
  30. /* matrix state(1:on, 0:off) */
  31. static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
  32. static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
  33. // helper functions
  34. inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); }
  35. inline matrix_row_t matrix_get_row(uint8_t row) {
  36. // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
  37. // switch blocker installed and the switch is always pressed.
  38. #ifdef MATRIX_MASKED
  39. return matrix[row] & matrix_mask[row];
  40. #else
  41. return matrix[row];
  42. #endif
  43. }
  44. // matrix code
  45. #ifdef DIRECT_PINS
  46. static void init_pins(void) {
  47. for (int row = 0; row < MATRIX_ROWS; row++) {
  48. for (int col = 0; col < MATRIX_COLS; col++) {
  49. pin_t pin = direct_pins[row][col];
  50. if (pin != NO_PIN) {
  51. setPinInputHigh(pin);
  52. }
  53. }
  54. }
  55. }
  56. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  57. matrix_row_t last_row_value = current_matrix[current_row];
  58. current_matrix[current_row] = 0;
  59. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  60. pin_t pin = direct_pins[current_row][col_index];
  61. if (pin != NO_PIN) {
  62. current_matrix[current_row] |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  63. }
  64. }
  65. return (last_row_value != current_matrix[current_row]);
  66. }
  67. #elif (DIODE_DIRECTION == COL2ROW)
  68. static void select_row(uint8_t row) {
  69. setPinOutput(row_pins[row]);
  70. writePinLow(row_pins[row]);
  71. }
  72. static void unselect_row(uint8_t row) { setPinInputHigh(row_pins[row]); }
  73. static void unselect_rows(void) {
  74. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  75. setPinInputHigh(row_pins[x]);
  76. }
  77. }
  78. static void init_pins(void) {
  79. unselect_rows();
  80. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  81. setPinInputHigh(col_pins[x]);
  82. }
  83. }
  84. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  85. // Store last value of row prior to reading
  86. matrix_row_t last_row_value = current_matrix[current_row];
  87. // Clear data in matrix row
  88. current_matrix[current_row] = 0;
  89. // Select row and wait for row selecton to stabilize
  90. select_row(current_row);
  91. wait_us(30);
  92. // For each col...
  93. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  94. // Select the col pin to read (active low)
  95. uint8_t pin_state = readPin(col_pins[col_index]);
  96. // Populate the matrix row with the state of the col pin
  97. current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  98. }
  99. // Unselect row
  100. unselect_row(current_row);
  101. return (last_row_value != current_matrix[current_row]);
  102. }
  103. #elif (DIODE_DIRECTION == ROW2COL)
  104. static void select_col(uint8_t col) {
  105. setPinOutput(col_pins[col]);
  106. writePinLow(col_pins[col]);
  107. }
  108. static void unselect_col(uint8_t col) { setPinInputHigh(col_pins[col]); }
  109. static void unselect_cols(void) {
  110. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  111. setPinInputHigh(col_pins[x]);
  112. }
  113. }
  114. static void init_pins(void) {
  115. unselect_cols();
  116. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  117. setPinInputHigh(row_pins[x]);
  118. }
  119. }
  120. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  121. bool matrix_changed = false;
  122. // Select col and wait for col selecton to stabilize
  123. select_col(current_col);
  124. wait_us(30);
  125. // For each row...
  126. for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
  127. // Store last value of row prior to reading
  128. matrix_row_t last_row_value = current_matrix[row_index];
  129. // Check row pin state
  130. if (readPin(row_pins[row_index]) == 0) {
  131. // Pin LO, set col bit
  132. current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col);
  133. } else {
  134. // Pin HI, clear col bit
  135. current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col);
  136. }
  137. // Determine if the matrix changed state
  138. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
  139. matrix_changed = true;
  140. }
  141. }
  142. // Unselect col
  143. unselect_col(current_col);
  144. return matrix_changed;
  145. }
  146. #endif
  147. void matrix_init(void) {
  148. // initialize key pins
  149. init_pins();
  150. // initialize matrix state: all keys off
  151. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  152. raw_matrix[i] = 0;
  153. matrix[i] = 0;
  154. }
  155. debounce_init(MATRIX_ROWS);
  156. matrix_init_quantum();
  157. }
  158. uint8_t matrix_scan(void) {
  159. bool changed = false;
  160. #if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
  161. // Set row, read cols
  162. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  163. changed |= read_cols_on_row(raw_matrix, current_row);
  164. }
  165. #elif (DIODE_DIRECTION == ROW2COL)
  166. // Set col, read rows
  167. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  168. changed |= read_rows_on_col(raw_matrix, current_col);
  169. }
  170. #endif
  171. debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
  172. matrix_scan_quantum();
  173. return (uint8_t)changed;
  174. }