logo

qmk_firmware

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

matrix.c (6353B)


  1. /*
  2. Copyright 2012 Jun Wako <wakojun@gmail.com>
  3. Copyright 2017 Cole Markham <cole@ccmcomputing.net>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /*
  16. * scan matrix
  17. */
  18. #include "matrix.h"
  19. #include "meira.h"
  20. #include "wait.h"
  21. #include "print.h"
  22. #include "debug.h"
  23. #include "timer.h"
  24. #ifndef DEBOUNCE
  25. # define DEBOUNCE 5
  26. #endif
  27. #if (DEBOUNCE > 0)
  28. static uint16_t debouncing_time;
  29. static bool debouncing = false;
  30. #endif
  31. #if (MATRIX_COLS <= 8)
  32. # define print_matrix_header() print("\nr/c 01234567\n")
  33. # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  34. # define ROW_SHIFTER ((uint8_t)1)
  35. #elif (MATRIX_COLS <= 16)
  36. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  37. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  38. # define ROW_SHIFTER ((uint16_t)1)
  39. #elif (MATRIX_COLS <= 32)
  40. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  41. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  42. # define ROW_SHIFTER ((uint32_t)1)
  43. #endif
  44. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  45. static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  46. static const uint8_t col_pins[4] = MATRIX_COL_PINS_SCANNED;
  47. //static const uint8_t lrow_pins[MATRIX_ROWS] = LED_ROW_PINS;
  48. //static const uint8_t lcol_pins[4] = LED_COL_PINS;
  49. /* matrix state(1:on, 0:off) */
  50. static matrix_row_t matrix[MATRIX_ROWS];
  51. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  52. static void init_rows(void);
  53. //static void init_lcols(void);
  54. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
  55. static void unselect_cols(void);
  56. static void select_col(uint8_t col);
  57. __attribute__ ((weak))
  58. void matrix_init_kb(void) {
  59. matrix_init_user();
  60. }
  61. __attribute__ ((weak))
  62. void matrix_scan_kb(void) {
  63. matrix_scan_user();
  64. }
  65. __attribute__ ((weak))
  66. void matrix_init_user(void) {
  67. }
  68. __attribute__ ((weak))
  69. void matrix_scan_user(void) {
  70. }
  71. inline
  72. uint8_t matrix_rows(void)
  73. {
  74. return MATRIX_ROWS;
  75. }
  76. inline
  77. uint8_t matrix_cols(void)
  78. {
  79. return MATRIX_COLS;
  80. }
  81. void matrix_init(void)
  82. {
  83. debug_enable = true;
  84. debug_matrix = true;
  85. debug_mouse = true;
  86. // initialize row and col
  87. unselect_cols();
  88. init_rows();
  89. // init_lcols();
  90. // initialize matrix state: all keys off
  91. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  92. matrix[i] = 0;
  93. matrix_debouncing[i] = 0;
  94. }
  95. matrix_init_kb();
  96. }
  97. uint8_t _matrix_scan(void)
  98. {
  99. // Set col, read rows
  100. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  101. # if (DEBOUNCE > 0)
  102. bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
  103. if (matrix_changed) {
  104. debouncing = true;
  105. debouncing_time = timer_read();
  106. }
  107. # else
  108. read_rows_on_col(matrix, current_col);
  109. # endif
  110. }
  111. # if (DEBOUNCE > 0)
  112. if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCE)) {
  113. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  114. matrix[i] = matrix_debouncing[i];
  115. }
  116. debouncing = false;
  117. }
  118. # endif
  119. return 1;
  120. }
  121. uint8_t matrix_scan(void)
  122. {
  123. uint8_t ret = _matrix_scan();
  124. matrix_scan_kb();
  125. return ret;
  126. }
  127. inline
  128. bool matrix_is_on(uint8_t row, uint8_t col)
  129. {
  130. return (matrix[row] & ((matrix_row_t)1<<col));
  131. }
  132. inline
  133. matrix_row_t matrix_get_row(uint8_t row)
  134. {
  135. return matrix[row];
  136. }
  137. void matrix_print(void)
  138. {
  139. print("\nr/c 0123456789ABCDEF\n");
  140. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  141. print_hex8(row); print(": ");
  142. print_bin_reverse16(matrix_get_row(row));
  143. print("\n");
  144. }
  145. }
  146. static void init_rows(void)
  147. {
  148. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  149. uint8_t pin = row_pins[x];
  150. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  151. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  152. }
  153. }
  154. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  155. {
  156. bool matrix_changed = false;
  157. // Select col and wait for col selection to stabilize
  158. select_col(current_col);
  159. wait_us(30);
  160. // For each row...
  161. for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
  162. {
  163. // Store last value of row prior to reading
  164. matrix_row_t last_row_value = current_matrix[row_index];
  165. // Check row pin state
  166. if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
  167. {
  168. // Pin LO, set col bit
  169. current_matrix[row_index] |= (ROW_SHIFTER << current_col);
  170. }
  171. else
  172. {
  173. // Pin HI, clear col bit
  174. current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
  175. }
  176. // Determine if the matrix changed state
  177. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
  178. {
  179. matrix_changed = true;
  180. }
  181. }
  182. // Unselect col
  183. unselect_cols();
  184. return matrix_changed;
  185. }
  186. static void select_col(uint8_t col)
  187. {
  188. #ifdef FLIPPED_BOARD
  189. col = MATRIX_COLS - col - 1;
  190. #endif
  191. for(uint8_t x = 0; x < 4; x++) {
  192. uint8_t pin = col_pins[x];
  193. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  194. if (((col >> x) & 0x1) == 1){
  195. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HIGH
  196. } else {
  197. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  198. }
  199. }
  200. }
  201. static void unselect_cols(void)
  202. {
  203. // FIXME This really needs to use the global enable on the decoder, because currently this sets the value to col1
  204. for(uint8_t x = 0; x < 4; x++) {
  205. uint8_t pin = col_pins[x];
  206. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  207. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  208. }
  209. }