matrix.c 8.8 KB

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