matrix.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. Copyright 2012 Jun Wako <wakojun@gmail.com>
  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 "util.h"
  17. #include "matrix.h"
  18. #include "debounce.h"
  19. #include "quantum.h"
  20. #include "split_util.h"
  21. #include "config.h"
  22. #include "transport.h"
  23. #ifdef ENCODER_ENABLE
  24. # include "encoder.h"
  25. #endif
  26. #define ERROR_DISCONNECT_COUNT 5
  27. #define ROWS_PER_HAND (MATRIX_ROWS / 2)
  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 pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  32. static pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  33. #endif
  34. /* matrix state(1:on, 0:off) */
  35. extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
  36. extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values
  37. // row offsets for each hand
  38. uint8_t thisHand, thatHand;
  39. // user-defined overridable functions
  40. __attribute__((weak)) void matrix_slave_scan_user(void) {}
  41. // matrix code
  42. #ifdef DIRECT_PINS
  43. static void init_pins(void) {
  44. for (int row = 0; row < MATRIX_ROWS; row++) {
  45. for (int col = 0; col < MATRIX_COLS; col++) {
  46. pin_t pin = direct_pins[row][col];
  47. if (pin != NO_PIN) {
  48. setPinInputHigh(pin);
  49. }
  50. }
  51. }
  52. }
  53. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  54. matrix_row_t last_row_value = current_matrix[current_row];
  55. current_matrix[current_row] = 0;
  56. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  57. pin_t pin = direct_pins[current_row][col_index];
  58. if (pin != NO_PIN) {
  59. current_matrix[current_row] |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  60. }
  61. }
  62. return (last_row_value != current_matrix[current_row]);
  63. }
  64. #elif defined(DIODE_DIRECTION)
  65. # if (DIODE_DIRECTION == COL2ROW)
  66. static void select_row(uint8_t row) {
  67. setPinOutput(row_pins[row]);
  68. writePinLow(row_pins[row]);
  69. }
  70. static void unselect_row(uint8_t row) { setPinInputHigh(row_pins[row]); }
  71. static void unselect_rows(void) {
  72. for (uint8_t x = 0; x < ROWS_PER_HAND; x++) {
  73. setPinInputHigh(row_pins[x]);
  74. }
  75. }
  76. static void init_pins(void) {
  77. unselect_rows();
  78. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  79. setPinInputHigh(col_pins[x]);
  80. }
  81. }
  82. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  83. // Store last value of row prior to reading
  84. matrix_row_t last_row_value = current_matrix[current_row];
  85. // Clear data in matrix row
  86. current_matrix[current_row] = 0;
  87. // Select row and wait for row selecton to stabilize
  88. select_row(current_row);
  89. matrix_io_delay();
  90. // For each col...
  91. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  92. // Select the col pin to read (active low)
  93. uint8_t pin_state = readPin(col_pins[col_index]);
  94. // Populate the matrix row with the state of the col pin
  95. current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  96. }
  97. // Unselect row
  98. unselect_row(current_row);
  99. return (last_row_value != current_matrix[current_row]);
  100. }
  101. # elif (DIODE_DIRECTION == ROW2COL)
  102. static void select_col(uint8_t col) {
  103. setPinOutput(col_pins[col]);
  104. writePinLow(col_pins[col]);
  105. }
  106. static void unselect_col(uint8_t col) { setPinInputHigh(col_pins[col]); }
  107. static void unselect_cols(void) {
  108. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  109. setPinInputHigh(col_pins[x]);
  110. }
  111. }
  112. static void init_pins(void) {
  113. unselect_cols();
  114. for (uint8_t x = 0; x < ROWS_PER_HAND; x++) {
  115. setPinInputHigh(row_pins[x]);
  116. }
  117. }
  118. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  119. bool matrix_changed = false;
  120. // Select col and wait for col selecton to stabilize
  121. select_col(current_col);
  122. matrix_io_delay();
  123. // For each row...
  124. for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) {
  125. // Store last value of row prior to reading
  126. matrix_row_t last_row_value = current_matrix[row_index];
  127. // Check row pin state
  128. if (readPin(row_pins[row_index]) == 0) {
  129. // Pin LO, set col bit
  130. current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col);
  131. } else {
  132. // Pin HI, clear col bit
  133. current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col);
  134. }
  135. // Determine if the matrix changed state
  136. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
  137. matrix_changed = true;
  138. }
  139. }
  140. // Unselect col
  141. unselect_col(current_col);
  142. return matrix_changed;
  143. }
  144. # else
  145. # error DIODE_DIRECTION must be one of COL2ROW or ROW2COL!
  146. # endif
  147. #else
  148. # error DIODE_DIRECTION is not defined!
  149. #endif
  150. void matrix_init(void) {
  151. keyboard_split_setup();
  152. // Set pinout for right half if pinout for that half is defined
  153. if (!isLeftHand) {
  154. #ifdef DIRECT_PINS_RIGHT
  155. const pin_t direct_pins_right[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS_RIGHT;
  156. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  157. for (uint8_t j = 0; j < MATRIX_COLS; j++) {
  158. direct_pins[i][j] = direct_pins_right[i][j];
  159. }
  160. }
  161. #endif
  162. #ifdef MATRIX_ROW_PINS_RIGHT
  163. const pin_t row_pins_right[MATRIX_ROWS] = MATRIX_ROW_PINS_RIGHT;
  164. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  165. row_pins[i] = row_pins_right[i];
  166. }
  167. #endif
  168. #ifdef MATRIX_COL_PINS_RIGHT
  169. const pin_t col_pins_right[MATRIX_COLS] = MATRIX_COL_PINS_RIGHT;
  170. for (uint8_t i = 0; i < MATRIX_COLS; i++) {
  171. col_pins[i] = col_pins_right[i];
  172. }
  173. #endif
  174. }
  175. thisHand = isLeftHand ? 0 : (ROWS_PER_HAND);
  176. thatHand = ROWS_PER_HAND - thisHand;
  177. // initialize key pins
  178. init_pins();
  179. // initialize matrix state: all keys off
  180. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  181. raw_matrix[i] = 0;
  182. matrix[i] = 0;
  183. }
  184. debounce_init(ROWS_PER_HAND);
  185. matrix_init_quantum();
  186. }
  187. void matrix_post_scan(void) {
  188. if (is_keyboard_master()) {
  189. static uint8_t error_count;
  190. if (!transport_master(matrix + thatHand)) {
  191. error_count++;
  192. if (error_count > ERROR_DISCONNECT_COUNT) {
  193. // reset other half if disconnected
  194. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  195. matrix[thatHand + i] = 0;
  196. }
  197. }
  198. } else {
  199. error_count = 0;
  200. }
  201. matrix_scan_quantum();
  202. } else {
  203. transport_slave(matrix + thisHand);
  204. #ifdef ENCODER_ENABLE
  205. encoder_read();
  206. #endif
  207. matrix_slave_scan_user();
  208. }
  209. }
  210. uint8_t matrix_scan(void) {
  211. bool changed = false;
  212. #if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
  213. // Set row, read cols
  214. for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
  215. changed |= read_cols_on_row(raw_matrix, current_row);
  216. }
  217. #elif (DIODE_DIRECTION == ROW2COL)
  218. // Set col, read rows
  219. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  220. changed |= read_rows_on_col(raw_matrix, current_col);
  221. }
  222. #endif
  223. debounce(raw_matrix, matrix + thisHand, ROWS_PER_HAND, changed);
  224. matrix_post_scan();
  225. return (uint8_t)changed;
  226. }