logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

matrix.c (8300B)


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