matrix.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. #define ERROR_DISCONNECT_COUNT 5
  24. #define ROWS_PER_HAND (MATRIX_ROWS / 2)
  25. #ifdef DIRECT_PINS
  26. static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS;
  27. #elif (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
  28. static pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  29. static pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  30. #endif
  31. /* matrix state(1:on, 0:off) */
  32. extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
  33. extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values
  34. // row offsets for each hand
  35. uint8_t thisHand, thatHand;
  36. // user-defined overridable functions
  37. __attribute__((weak)) void matrix_slave_scan_kb(void) { matrix_slave_scan_user(); }
  38. __attribute__((weak)) void matrix_slave_scan_user(void) {}
  39. static inline void setPinOutput_writeLow(pin_t pin) {
  40. ATOMIC_BLOCK_FORCEON {
  41. setPinOutput(pin);
  42. writePinLow(pin);
  43. }
  44. }
  45. static inline void setPinInputHigh_atomic(pin_t pin) {
  46. ATOMIC_BLOCK_FORCEON { setPinInputHigh(pin); }
  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. // Start with a clear matrix row
  62. matrix_row_t current_row_value = 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_row_value |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  67. }
  68. }
  69. // If the row has changed, store the row and return the changed flag.
  70. if (current_matrix[current_row] != current_row_value) {
  71. current_matrix[current_row] = current_row_value;
  72. return true;
  73. }
  74. return false;
  75. }
  76. #elif defined(DIODE_DIRECTION)
  77. # if (DIODE_DIRECTION == COL2ROW)
  78. static void select_row(uint8_t row) { setPinOutput_writeLow(row_pins[row]); }
  79. static void unselect_row(uint8_t row) { setPinInputHigh_atomic(row_pins[row]); }
  80. static void unselect_rows(void) {
  81. for (uint8_t x = 0; x < ROWS_PER_HAND; x++) {
  82. setPinInputHigh_atomic(row_pins[x]);
  83. }
  84. }
  85. static void init_pins(void) {
  86. unselect_rows();
  87. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  88. setPinInputHigh_atomic(col_pins[x]);
  89. }
  90. }
  91. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  92. // Start with a clear matrix row
  93. matrix_row_t current_row_value = 0;
  94. // Select row
  95. select_row(current_row);
  96. matrix_output_select_delay();
  97. // For each col...
  98. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  99. // Select the col pin to read (active low)
  100. uint8_t pin_state = readPin(col_pins[col_index]);
  101. // Populate the matrix row with the state of the col pin
  102. current_row_value |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  103. }
  104. // Unselect row
  105. unselect_row(current_row);
  106. if (current_row + 1 < MATRIX_ROWS) {
  107. matrix_output_unselect_delay(); // wait for row signal to go HIGH
  108. }
  109. // If the row has changed, store the row and return the changed flag.
  110. if (current_matrix[current_row] != current_row_value) {
  111. current_matrix[current_row] = current_row_value;
  112. return true;
  113. }
  114. return false;
  115. }
  116. # elif (DIODE_DIRECTION == ROW2COL)
  117. static void select_col(uint8_t col) { setPinOutput_writeLow(col_pins[col]); }
  118. static void unselect_col(uint8_t col) { setPinInputHigh_atomic(col_pins[col]); }
  119. static void unselect_cols(void) {
  120. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  121. setPinInputHigh_atomic(col_pins[x]);
  122. }
  123. }
  124. static void init_pins(void) {
  125. unselect_cols();
  126. for (uint8_t x = 0; x < ROWS_PER_HAND; x++) {
  127. setPinInputHigh_atomic(row_pins[x]);
  128. }
  129. }
  130. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  131. bool matrix_changed = false;
  132. // Select col
  133. select_col(current_col);
  134. matrix_output_select_delay();
  135. // For each row...
  136. for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) {
  137. // Store last value of row prior to reading
  138. matrix_row_t last_row_value = current_matrix[row_index];
  139. matrix_row_t current_row_value = last_row_value;
  140. // Check row pin state
  141. if (readPin(row_pins[row_index]) == 0) {
  142. // Pin LO, set col bit
  143. current_row_value |= (MATRIX_ROW_SHIFTER << current_col);
  144. } else {
  145. // Pin HI, clear col bit
  146. current_row_value &= ~(MATRIX_ROW_SHIFTER << current_col);
  147. }
  148. // Determine if the matrix changed state
  149. if ((last_row_value != current_row_value)) {
  150. matrix_changed |= true;
  151. current_matrix[row_index] = current_row_value;
  152. }
  153. }
  154. // Unselect col
  155. unselect_col(current_col);
  156. if (current_col + 1 < MATRIX_COLS) {
  157. matrix_output_unselect_delay(); // wait for col signal to go HIGH
  158. }
  159. return matrix_changed;
  160. }
  161. # else
  162. # error DIODE_DIRECTION must be one of COL2ROW or ROW2COL!
  163. # endif
  164. #else
  165. # error DIODE_DIRECTION is not defined!
  166. #endif
  167. void matrix_init(void) {
  168. split_pre_init();
  169. // Set pinout for right half if pinout for that half is defined
  170. if (!isLeftHand) {
  171. #ifdef DIRECT_PINS_RIGHT
  172. const pin_t direct_pins_right[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS_RIGHT;
  173. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  174. for (uint8_t j = 0; j < MATRIX_COLS; j++) {
  175. direct_pins[i][j] = direct_pins_right[i][j];
  176. }
  177. }
  178. #endif
  179. #ifdef MATRIX_ROW_PINS_RIGHT
  180. const pin_t row_pins_right[MATRIX_ROWS] = MATRIX_ROW_PINS_RIGHT;
  181. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  182. row_pins[i] = row_pins_right[i];
  183. }
  184. #endif
  185. #ifdef MATRIX_COL_PINS_RIGHT
  186. const pin_t col_pins_right[MATRIX_COLS] = MATRIX_COL_PINS_RIGHT;
  187. for (uint8_t i = 0; i < MATRIX_COLS; i++) {
  188. col_pins[i] = col_pins_right[i];
  189. }
  190. #endif
  191. }
  192. thisHand = isLeftHand ? 0 : (ROWS_PER_HAND);
  193. thatHand = ROWS_PER_HAND - thisHand;
  194. // initialize key pins
  195. init_pins();
  196. // initialize matrix state: all keys off
  197. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  198. raw_matrix[i] = 0;
  199. matrix[i] = 0;
  200. }
  201. debounce_init(ROWS_PER_HAND);
  202. matrix_init_quantum();
  203. split_post_init();
  204. }
  205. bool matrix_post_scan(void) {
  206. bool changed = false;
  207. if (is_keyboard_master()) {
  208. static uint8_t error_count;
  209. matrix_row_t slave_matrix[ROWS_PER_HAND] = {0};
  210. if (!transport_master(matrix + thisHand, slave_matrix)) {
  211. error_count++;
  212. if (error_count > ERROR_DISCONNECT_COUNT) {
  213. // reset other half if disconnected
  214. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  215. matrix[thatHand + i] = 0;
  216. slave_matrix[i] = 0;
  217. }
  218. changed = true;
  219. }
  220. } else {
  221. error_count = 0;
  222. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  223. if (matrix[thatHand + i] != slave_matrix[i]) {
  224. matrix[thatHand + i] = slave_matrix[i];
  225. changed = true;
  226. }
  227. }
  228. }
  229. matrix_scan_quantum();
  230. } else {
  231. transport_slave(matrix + thatHand, matrix + thisHand);
  232. matrix_slave_scan_kb();
  233. }
  234. return changed;
  235. }
  236. uint8_t matrix_scan(void) {
  237. bool local_changed = false;
  238. #if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
  239. // Set row, read cols
  240. for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
  241. local_changed |= read_cols_on_row(raw_matrix, current_row);
  242. }
  243. #elif (DIODE_DIRECTION == ROW2COL)
  244. // Set col, read rows
  245. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  246. local_changed |= read_rows_on_col(raw_matrix, current_col);
  247. }
  248. #endif
  249. debounce(raw_matrix, matrix + thisHand, ROWS_PER_HAND, local_changed);
  250. bool remote_changed = matrix_post_scan();
  251. return (uint8_t)(local_changed || remote_changed);
  252. }