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.

277 lines
8.1 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. #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. static pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  26. static pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  27. /* matrix state(1:on, 0:off) */
  28. static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
  29. static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
  30. // row offsets for each hand
  31. uint8_t thisHand, thatHand;
  32. // user-defined overridable functions
  33. __attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); }
  34. __attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); }
  35. __attribute__((weak)) void matrix_init_user(void) {}
  36. __attribute__((weak)) void matrix_scan_user(void) {}
  37. __attribute__((weak)) void matrix_slave_scan_user(void) {}
  38. matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
  39. void matrix_print(void) {}
  40. static inline void setPinOutput_writeLow(pin_t pin) {
  41. ATOMIC_BLOCK_FORCEON {
  42. setPinOutput(pin);
  43. writePinLow(pin);
  44. }
  45. }
  46. static inline void setPinInputHigh_atomic(pin_t pin) {
  47. ATOMIC_BLOCK_FORCEON { setPinInputHigh(pin); }
  48. }
  49. // matrix code
  50. static void select_row(uint8_t row) { setPinOutput_writeLow(row_pins[row]); }
  51. static void unselect_row(uint8_t row) { setPinInputHigh_atomic(row_pins[row]); }
  52. static void unselect_rows(void) {
  53. for (uint8_t x = 0; x < ROWS_PER_HAND; x++) {
  54. setPinInputHigh_atomic(row_pins[x]);
  55. }
  56. }
  57. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  58. // Start with a clear matrix row
  59. matrix_row_t current_row_value = 0;
  60. // Select row
  61. select_row(current_row);
  62. wait_us(30);
  63. // For each col...
  64. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  65. // Select the col pin to read (active low)
  66. uint8_t pin_state = readPin(col_pins[col_index]);
  67. // Populate the matrix row with the state of the col pin
  68. current_row_value |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  69. }
  70. // Unselect row
  71. unselect_row(current_row);
  72. if (current_row + 1 < MATRIX_ROWS) {
  73. wait_us(30); // wait for row signal to go HIGH
  74. }
  75. // If the row has changed, store the row and return the changed flag.
  76. if (current_matrix[current_row] != current_row_value) {
  77. current_matrix[current_row] = current_row_value;
  78. return true;
  79. }
  80. return false;
  81. }
  82. static void select_col(uint8_t col) { setPinOutput_writeLow(col_pins[col]); }
  83. static void unselect_col(uint8_t col) { setPinInputHigh_atomic(col_pins[col]); }
  84. static void unselect_cols(void) {
  85. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  86. setPinInputHigh_atomic(col_pins[x]);
  87. }
  88. }
  89. static void init_pins(void) {
  90. unselect_rows();
  91. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  92. setPinInputHigh_atomic(col_pins[x]);
  93. }
  94. unselect_cols();
  95. for (uint8_t x = 0; x < ROWS_PER_HAND; x++) {
  96. setPinInputHigh_atomic(row_pins[x]);
  97. }
  98. }
  99. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  100. bool matrix_changed = false;
  101. // Select col
  102. select_col(current_col);
  103. wait_us(30);
  104. // For each row...
  105. for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) {
  106. // Store last value of row prior to reading
  107. matrix_row_t last_row_value = current_matrix[row_index];
  108. matrix_row_t current_row_value = last_row_value;
  109. // Check row pin state
  110. if (readPin(row_pins[row_index]) == 0) {
  111. // Pin LO, set col bit
  112. current_row_value |= (MATRIX_ROW_SHIFTER << current_col);
  113. } else {
  114. // Pin HI, clear col bit
  115. current_row_value &= ~(MATRIX_ROW_SHIFTER << current_col);
  116. }
  117. // Determine if the matrix changed state
  118. if ((last_row_value != current_row_value)) {
  119. matrix_changed |= true;
  120. current_matrix[row_index] = current_row_value;
  121. }
  122. }
  123. // Unselect col
  124. unselect_col(current_col);
  125. if (current_col + 1 < MATRIX_COLS) {
  126. wait_us(30); // wait for col signal to go HIGH
  127. }
  128. return matrix_changed;
  129. }
  130. void matrix_init(void) {
  131. split_pre_init();
  132. // Set pinout for right half if pinout for that half is defined
  133. if (!isLeftHand) {
  134. #ifdef DIRECT_PINS_RIGHT
  135. const pin_t direct_pins_right[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS_RIGHT;
  136. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  137. for (uint8_t j = 0; j < MATRIX_COLS; j++) {
  138. direct_pins[i][j] = direct_pins_right[i][j];
  139. }
  140. }
  141. #endif
  142. #ifdef MATRIX_ROW_PINS_RIGHT
  143. const pin_t row_pins_right[MATRIX_ROWS] = MATRIX_ROW_PINS_RIGHT;
  144. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  145. row_pins[i] = row_pins_right[i];
  146. }
  147. #endif
  148. #ifdef MATRIX_COL_PINS_RIGHT
  149. const pin_t col_pins_right[MATRIX_COLS] = MATRIX_COL_PINS_RIGHT;
  150. for (uint8_t i = 0; i < MATRIX_COLS; i++) {
  151. col_pins[i] = col_pins_right[i];
  152. }
  153. #endif
  154. }
  155. thisHand = isLeftHand ? 0 : (ROWS_PER_HAND);
  156. thatHand = ROWS_PER_HAND - thisHand;
  157. // initialize key pins
  158. init_pins();
  159. // initialize matrix state: all keys off
  160. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  161. raw_matrix[i] = 0;
  162. matrix[i] = 0;
  163. }
  164. debounce_init(ROWS_PER_HAND);
  165. matrix_init_quantum();
  166. split_post_init();
  167. }
  168. bool matrix_post_scan(void) {
  169. bool changed = false;
  170. if (is_keyboard_master()) {
  171. static uint8_t error_count;
  172. matrix_row_t slave_matrix[ROWS_PER_HAND] = {0};
  173. if (!transport_master(matrix + thisHand, slave_matrix)) {
  174. error_count++;
  175. if (error_count > ERROR_DISCONNECT_COUNT) {
  176. // reset other half if disconnected
  177. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  178. matrix[thatHand + i] = 0;
  179. slave_matrix[i] = 0;
  180. }
  181. changed = true;
  182. }
  183. } else {
  184. error_count = 0;
  185. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  186. if (matrix[thatHand + i] != slave_matrix[i]) {
  187. matrix[thatHand + i] = slave_matrix[i];
  188. changed = true;
  189. }
  190. }
  191. }
  192. matrix_scan_quantum();
  193. } else {
  194. transport_slave(matrix + thatHand, matrix + thisHand);
  195. matrix_slave_scan_user();
  196. }
  197. return changed;
  198. }
  199. uint8_t matrix_scan(void) {
  200. bool local_changed = false;
  201. static matrix_row_t temp_raw_matrix[MATRIX_ROWS]; // temp raw values
  202. // Set row, read cols
  203. for (uint8_t current_row = 0; current_row < ROWS_PER_HAND/2; current_row++) {
  204. local_changed |= read_cols_on_row(raw_matrix, current_row);
  205. }
  206. // Set col, read rows
  207. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  208. local_changed |= read_rows_on_col(temp_raw_matrix, current_col);
  209. //Updated key matrix on lines 6-10 (or lines 16-20)
  210. if(local_changed) {
  211. for (uint8_t i = ROWS_PER_HAND/2; i < ROWS_PER_HAND; i++) {
  212. raw_matrix[i] = temp_raw_matrix[i];
  213. }
  214. }
  215. }
  216. debounce(raw_matrix, matrix + thisHand, ROWS_PER_HAND, local_changed);
  217. bool remote_changed = matrix_post_scan();
  218. return (uint8_t)(local_changed || remote_changed);
  219. }