matrix.c 8.3 KB

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