logo

qmk_firmware

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

matrix.c (5136B)


  1. #include <stdint.h>
  2. #include <stdbool.h>
  3. #include <avr/io.h>
  4. #include "wait.h"
  5. #include "action_layer.h"
  6. #include "print.h"
  7. #include "debug.h"
  8. #include "util.h"
  9. #include "matrix.h"
  10. #include "hotdox.h"
  11. #include "left.h"
  12. /*
  13. * This constant define not debouncing time in msecs, but amount of matrix
  14. * scan loops which should be made to get stable debounced results.
  15. *
  16. * On Ergodox matrix scan rate is relatively low, because of slow I2C.
  17. * Now it's only 317 scans/second, or about 3.15 msec/scan.
  18. * According to Cherry specs, debouncing time is 5 msec.
  19. *
  20. * And so, there is no sense to have DEBOUNCE higher than 2.
  21. */
  22. #ifndef DEBOUNCE
  23. # define DEBOUNCE 5
  24. #endif
  25. /* matrix state(1:on, 0:off) */
  26. static matrix_row_t matrix[MATRIX_ROWS];
  27. // Debouncing: store for each key the number of scans until it's eligible to
  28. // change. When scanning the matrix, ignore any changes in keys that have
  29. // already changed in the last DEBOUNCE scans.
  30. static uint8_t debounce_matrix[MATRIX_ROWS * MATRIX_COLS];
  31. static matrix_row_t read_cols(uint8_t row);
  32. static void init_cols(void);
  33. static void unselect_rows(void);
  34. static void select_row(uint8_t row);
  35. __attribute__ ((weak))
  36. void matrix_init_user(void) {}
  37. __attribute__ ((weak))
  38. void matrix_scan_user(void) {}
  39. __attribute__ ((weak))
  40. void matrix_init_kb(void) {
  41. matrix_init_user();
  42. }
  43. __attribute__ ((weak))
  44. void matrix_scan_kb(void) {
  45. matrix_scan_user();
  46. }
  47. inline
  48. uint8_t matrix_rows(void)
  49. {
  50. return MATRIX_ROWS;
  51. }
  52. inline
  53. uint8_t matrix_cols(void)
  54. {
  55. return MATRIX_COLS;
  56. }
  57. void matrix_init(void)
  58. {
  59. unselect_rows();
  60. init_cols();
  61. //eeprom_update_word(EECONFIG_MAGIC, 0x0000);
  62. // initialize matrix state: all keys off
  63. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  64. matrix[i] = 0;
  65. for (uint8_t j=0; j < MATRIX_COLS; ++j) {
  66. debounce_matrix[i * MATRIX_COLS + j] = 0;
  67. }
  68. }
  69. matrix_init_kb();
  70. }
  71. void matrix_power_up(void) {
  72. unselect_rows();
  73. init_cols();
  74. // initialize matrix state: all keys off
  75. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  76. matrix[i] = 0;
  77. }
  78. }
  79. // Returns a matrix_row_t whose bits are set if the corresponding key should be
  80. // eligible to change in this scan.
  81. matrix_row_t debounce_mask(uint8_t row) {
  82. matrix_row_t result = 0;
  83. for (uint8_t j=0; j < MATRIX_COLS; ++j) {
  84. if (debounce_matrix[row * MATRIX_COLS + j]) {
  85. --debounce_matrix[row * MATRIX_COLS + j];
  86. } else {
  87. result |= (1 << j);
  88. }
  89. }
  90. return result;
  91. }
  92. // Report changed keys in the given row. Resets the debounce countdowns
  93. // corresponding to each set bit in 'change' to DEBOUNCE.
  94. void debounce_report(matrix_row_t change, uint8_t row) {
  95. for (uint8_t i = 0; i < MATRIX_COLS; ++i) {
  96. if (change & (1 << i)) {
  97. debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE;
  98. }
  99. }
  100. }
  101. uint8_t matrix_scan(void)
  102. {
  103. left_scan();
  104. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  105. select_row(i);
  106. wait_us(30); // without this wait read unstable value.
  107. matrix_row_t mask = debounce_mask(i);
  108. matrix_row_t cols = (read_cols(i) & mask) | (matrix[i] & ~mask);
  109. debounce_report(cols ^ matrix[i], i);
  110. matrix[i] = cols;
  111. unselect_rows();
  112. }
  113. matrix_scan_kb();
  114. return 1;
  115. }
  116. inline
  117. bool matrix_is_on(uint8_t row, uint8_t col)
  118. {
  119. return (matrix[row] & ((matrix_row_t)1<<col));
  120. }
  121. inline
  122. matrix_row_t matrix_get_row(uint8_t row)
  123. {
  124. return matrix[row];
  125. }
  126. void matrix_print(void)
  127. {
  128. print("\nr/c 0123456789ABCDEF\n");
  129. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  130. print_hex8(row); print(": ");
  131. print_bin_reverse16(matrix_get_row(row));
  132. print("\n");
  133. }
  134. }
  135. static void init_cols(void)
  136. {
  137. // Pro Micro
  138. gpio_set_pin_input_high(B0);
  139. gpio_set_pin_input_high(B1);
  140. gpio_set_pin_input_high(B2);
  141. gpio_set_pin_input_high(B3);
  142. gpio_set_pin_input_high(D2);
  143. gpio_set_pin_input_high(D3);
  144. gpio_set_pin_input_high(C6);
  145. left_init();
  146. }
  147. static matrix_row_t read_cols(uint8_t row)
  148. {
  149. matrix_row_t cols0 = 0x00, cols1 = 0x00;
  150. cols0 = left_read_cols();
  151. cols1 = (PINC&(1<<PC6) ? 0 : (1<<(0+7))) |
  152. (PIND&(1<<PD3) ? 0 : (1<<(1+7))) |
  153. (PIND&(1<<PD2) ? 0 : (1<<(2+7))) |
  154. (PINB&(1<<PB3) ? 0 : (1<<(3+7))) |
  155. (PINB&(1<<PB2) ? 0 : (1<<(4+7))) |
  156. (PINB&(1<<PB1) ? 0 : (1<<(5+7))) |
  157. (PINB&(1<<PB0) ? 0 : (1<<(6+7))) ;
  158. return (cols0 | cols1);
  159. }
  160. static void unselect_rows(void)
  161. {
  162. // Pro Micro
  163. gpio_set_pin_input(F0);
  164. gpio_set_pin_input(F1);
  165. gpio_set_pin_input(F4);
  166. gpio_set_pin_input(F5);
  167. gpio_set_pin_input(F6);
  168. gpio_set_pin_input(F7);
  169. left_unselect_rows();
  170. }
  171. static void select_row(uint8_t row)
  172. {
  173. // Pro Micro
  174. switch (row) {
  175. case 5:
  176. gpio_set_pin_output(F0);
  177. gpio_write_pin_low(F0);
  178. break;
  179. case 4:
  180. gpio_set_pin_output(F1);
  181. gpio_write_pin_low(F1);
  182. break;
  183. case 3:
  184. gpio_set_pin_output(F4);
  185. gpio_write_pin_low(F4);
  186. break;
  187. case 2:
  188. gpio_set_pin_output(F5);
  189. gpio_write_pin_low(F5);
  190. break;
  191. case 1:
  192. gpio_set_pin_output(F6);
  193. gpio_write_pin_low(F6);
  194. break;
  195. case 0:
  196. gpio_set_pin_output(F7);
  197. gpio_write_pin_low(F7);
  198. break;
  199. }
  200. left_select_row(row);
  201. }