You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

328 lines
8.4 KiB

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