matrix.c 8.2 KB

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