matrix.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. /*
  15. * scan matrix
  16. */
  17. #include <stdint.h>
  18. #include <stdbool.h>
  19. #include "wait.h"
  20. #include "util.h"
  21. #include "matrix.h"
  22. #include "split_util.h"
  23. #include "config.h"
  24. #include "quantum.h"
  25. #include "debounce.h"
  26. #include "transport.h"
  27. #if (MATRIX_COLS <= 8)
  28. # define print_matrix_header() print("\nr/c 01234567\n")
  29. # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  30. # define matrix_bitpop(i) bitpop(matrix[i])
  31. # define ROW_SHIFTER ((uint8_t)1)
  32. #elif (MATRIX_COLS <= 16)
  33. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  34. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  35. # define matrix_bitpop(i) bitpop16(matrix[i])
  36. # define ROW_SHIFTER ((uint16_t)1)
  37. #elif (MATRIX_COLS <= 32)
  38. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  39. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  40. # define matrix_bitpop(i) bitpop32(matrix[i])
  41. # define ROW_SHIFTER ((uint32_t)1)
  42. #endif
  43. #define ERROR_DISCONNECT_COUNT 5
  44. #define ROWS_PER_HAND (MATRIX_ROWS / 2)
  45. #ifdef DIRECT_PINS
  46. static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS;
  47. #else
  48. static pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  49. static pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  50. #endif
  51. /* matrix state(1:on, 0:off) */
  52. static matrix_row_t matrix[MATRIX_ROWS];
  53. static matrix_row_t raw_matrix[ROWS_PER_HAND];
  54. // row offsets for each hand
  55. uint8_t thisHand, thatHand;
  56. // user-defined overridable functions
  57. __attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); }
  58. __attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); }
  59. __attribute__((weak)) void matrix_init_user(void) {}
  60. __attribute__((weak)) void matrix_scan_user(void) {}
  61. __attribute__((weak)) void matrix_slave_scan_user(void) {}
  62. // helper functions
  63. inline uint8_t matrix_rows(void) { return MATRIX_ROWS; }
  64. inline uint8_t matrix_cols(void) { return MATRIX_COLS; }
  65. bool matrix_is_modified(void) {
  66. if (debounce_active()) return false;
  67. return true;
  68. }
  69. inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); }
  70. inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
  71. void matrix_print(void) {
  72. print_matrix_header();
  73. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  74. phex(row);
  75. print(": ");
  76. print_matrix_row(row);
  77. print("\n");
  78. }
  79. }
  80. uint8_t matrix_key_count(void) {
  81. uint8_t count = 0;
  82. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  83. count += matrix_bitpop(i);
  84. }
  85. return count;
  86. }
  87. // matrix code
  88. #ifdef DIRECT_PINS
  89. static void init_pins(void) {
  90. for (int row = 0; row < MATRIX_ROWS; row++) {
  91. for (int col = 0; col < MATRIX_COLS; col++) {
  92. pin_t pin = direct_pins[row][col];
  93. if (pin != NO_PIN) {
  94. setPinInputHigh(pin);
  95. }
  96. }
  97. }
  98. }
  99. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  100. matrix_row_t last_row_value = current_matrix[current_row];
  101. current_matrix[current_row] = 0;
  102. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  103. pin_t pin = direct_pins[current_row][col_index];
  104. if (pin != NO_PIN) {
  105. current_matrix[current_row] |= readPin(pin) ? 0 : (ROW_SHIFTER << col_index);
  106. }
  107. }
  108. return (last_row_value != current_matrix[current_row]);
  109. }
  110. #elif (DIODE_DIRECTION == COL2ROW)
  111. static void select_row(uint8_t row) {
  112. setPinOutput(row_pins[row]);
  113. writePinLow(row_pins[row]);
  114. }
  115. static void unselect_row(uint8_t row) { setPinInputHigh(row_pins[row]); }
  116. static void unselect_rows(void) {
  117. for (uint8_t x = 0; x < ROWS_PER_HAND; x++) {
  118. setPinInputHigh(row_pins[x]);
  119. }
  120. }
  121. static void init_pins(void) {
  122. unselect_rows();
  123. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  124. setPinInputHigh(col_pins[x]);
  125. }
  126. }
  127. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  128. // Store last value of row prior to reading
  129. matrix_row_t last_row_value = current_matrix[current_row];
  130. // Clear data in matrix row
  131. current_matrix[current_row] = 0;
  132. // Select row and wait for row selecton to stabilize
  133. select_row(current_row);
  134. wait_us(30);
  135. // For each col...
  136. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  137. // Populate the matrix row with the state of the col pin
  138. current_matrix[current_row] |= readPin(col_pins[col_index]) ? 0 : (ROW_SHIFTER << col_index);
  139. }
  140. // Unselect row
  141. unselect_row(current_row);
  142. return (last_row_value != current_matrix[current_row]);
  143. }
  144. #elif (DIODE_DIRECTION == ROW2COL)
  145. static void select_col(uint8_t col) {
  146. setPinOutput(col_pins[col]);
  147. writePinLow(col_pins[col]);
  148. }
  149. static void unselect_col(uint8_t col) { setPinInputHigh(col_pins[col]); }
  150. static void unselect_cols(void) {
  151. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  152. setPinInputHigh(col_pins[x]);
  153. }
  154. }
  155. static void init_pins(void) {
  156. unselect_cols();
  157. for (uint8_t x = 0; x < ROWS_PER_HAND; x++) {
  158. setPinInputHigh(row_pins[x]);
  159. }
  160. }
  161. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  162. bool matrix_changed = false;
  163. // Select col and wait for col selecton to stabilize
  164. select_col(current_col);
  165. wait_us(30);
  166. // For each row...
  167. for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) {
  168. // Store last value of row prior to reading
  169. matrix_row_t last_row_value = current_matrix[row_index];
  170. // Check row pin state
  171. if (readPin(row_pins[row_index])) {
  172. // Pin HI, clear col bit
  173. current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
  174. } else {
  175. // Pin LO, set col bit
  176. current_matrix[row_index] |= (ROW_SHIFTER << current_col);
  177. }
  178. // Determine if the matrix changed state
  179. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
  180. matrix_changed = true;
  181. }
  182. }
  183. // Unselect col
  184. unselect_col(current_col);
  185. return matrix_changed;
  186. }
  187. #endif
  188. void matrix_init(void) {
  189. debug_enable = true;
  190. debug_matrix = true;
  191. debug_mouse = true;
  192. // Set pinout for right half if pinout for that half is defined
  193. if (!isLeftHand) {
  194. #ifdef MATRIX_ROW_PINS_RIGHT
  195. const uint8_t row_pins_right[MATRIX_ROWS] = MATRIX_ROW_PINS_RIGHT;
  196. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  197. row_pins[i] = row_pins_right[i];
  198. }
  199. #endif
  200. #ifdef MATRIX_COL_PINS_RIGHT
  201. const uint8_t col_pins_right[MATRIX_COLS] = MATRIX_COL_PINS_RIGHT;
  202. for (uint8_t i = 0; i < MATRIX_COLS; i++) {
  203. col_pins[i] = col_pins_right[i];
  204. }
  205. #endif
  206. }
  207. thisHand = isLeftHand ? 0 : (ROWS_PER_HAND);
  208. thatHand = ROWS_PER_HAND - thisHand;
  209. // initialize key pins
  210. init_pins();
  211. // initialize matrix state: all keys off
  212. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  213. matrix[i] = 0;
  214. }
  215. debounce_init(ROWS_PER_HAND);
  216. matrix_init_quantum();
  217. }
  218. uint8_t _matrix_scan(void) {
  219. bool changed = false;
  220. #if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
  221. // Set row, read cols
  222. for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
  223. changed |= read_cols_on_row(raw_matrix, current_row);
  224. }
  225. #elif (DIODE_DIRECTION == ROW2COL)
  226. // Set col, read rows
  227. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  228. changed |= read_rows_on_col(raw_matrix, current_col);
  229. }
  230. #endif
  231. debounce(raw_matrix, matrix + thisHand, ROWS_PER_HAND, changed);
  232. return 1;
  233. }
  234. uint8_t matrix_scan(void) {
  235. uint8_t ret = _matrix_scan();
  236. if (is_keyboard_master()) {
  237. static uint8_t error_count;
  238. if (!transport_master(matrix + thatHand)) {
  239. error_count++;
  240. if (error_count > ERROR_DISCONNECT_COUNT) {
  241. // reset other half if disconnected
  242. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  243. matrix[thatHand + i] = 0;
  244. }
  245. }
  246. } else {
  247. error_count = 0;
  248. }
  249. matrix_scan_quantum();
  250. } else {
  251. transport_slave(matrix + thisHand);
  252. matrix_slave_scan_user();
  253. }
  254. return ret;
  255. }