logo

qmk_firmware

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

matrix.c (6894B)


  1. /*
  2. Copyright 2012 Jun Wako
  3. Copyright 2014 Jack Humbert
  4. Copyright 2017 Priyadi Iman Nurcahyo
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <stdint.h>
  17. #include <stdbool.h>
  18. #if defined(__AVR__)
  19. #include <avr/io.h>
  20. #endif
  21. #include "wait.h"
  22. #include "print.h"
  23. #include "debug.h"
  24. #include "util.h"
  25. #include "matrix.h"
  26. #include "timer.h"
  27. /* Set 0 if debouncing isn't needed */
  28. #ifndef DEBOUNCE
  29. # define DEBOUNCE 5
  30. #endif
  31. #if (DEBOUNCE > 0)
  32. static uint16_t debouncing_time;
  33. static bool debouncing = false;
  34. #endif
  35. #if (MATRIX_COLS <= 8)
  36. # define print_matrix_header() print("\nr/c 01234567\n")
  37. # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  38. # define ROW_SHIFTER ((uint8_t)1)
  39. #elif (MATRIX_COLS <= 16)
  40. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  41. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  42. # define ROW_SHIFTER ((uint16_t)1)
  43. #elif (MATRIX_COLS <= 32)
  44. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  45. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  46. # define ROW_SHIFTER ((uint32_t)1)
  47. #endif
  48. #ifdef MATRIX_MASKED
  49. extern const matrix_row_t matrix_mask[];
  50. #endif
  51. static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  52. static const uint8_t tp_pins[3] = TRACKPOINT_PINS;
  53. /* matrix state(1:on, 0:off) */
  54. static matrix_row_t matrix[MATRIX_ROWS];
  55. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  56. static void init_cols(void);
  57. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
  58. static void unselect_rows(void);
  59. static void select_row(uint8_t row);
  60. static void unselect_row(uint8_t row);
  61. __attribute__ ((weak))
  62. void matrix_init_kb(void) {
  63. matrix_init_user();
  64. }
  65. __attribute__ ((weak))
  66. void matrix_scan_kb(void) {
  67. matrix_scan_user();
  68. }
  69. __attribute__ ((weak))
  70. void matrix_init_user(void) {
  71. }
  72. __attribute__ ((weak))
  73. void matrix_scan_user(void) {
  74. }
  75. inline
  76. uint8_t matrix_rows(void) {
  77. return MATRIX_ROWS;
  78. }
  79. inline
  80. uint8_t matrix_cols(void) {
  81. return MATRIX_COLS;
  82. }
  83. void matrix_init(void) {
  84. // initialize row and col
  85. unselect_rows();
  86. init_cols();
  87. // initialize matrix state: all keys off
  88. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  89. matrix[i] = 0;
  90. matrix_debouncing[i] = 0;
  91. }
  92. matrix_init_kb();
  93. }
  94. uint8_t matrix_scan(void)
  95. {
  96. // Set row, read cols
  97. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  98. # if (DEBOUNCE > 0)
  99. bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
  100. if (matrix_changed) {
  101. debouncing = true;
  102. debouncing_time = timer_read();
  103. }
  104. # else
  105. read_cols_on_row(matrix, current_row);
  106. # endif
  107. }
  108. # if (DEBOUNCE > 0)
  109. if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCE)) {
  110. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  111. matrix[i] = matrix_debouncing[i];
  112. }
  113. debouncing = false;
  114. }
  115. # endif
  116. matrix_scan_kb();
  117. return 1;
  118. }
  119. inline
  120. bool matrix_is_on(uint8_t row, uint8_t col)
  121. {
  122. return (matrix[row] & ((matrix_row_t)1<<col));
  123. }
  124. inline
  125. matrix_row_t matrix_get_row(uint8_t row)
  126. {
  127. // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
  128. // switch blocker installed and the switch is always pressed.
  129. #ifdef MATRIX_MASKED
  130. return matrix[row] & matrix_mask[row];
  131. #else
  132. return matrix[row];
  133. #endif
  134. }
  135. void matrix_print(void)
  136. {
  137. print_matrix_header();
  138. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  139. print_hex8(row); print(": ");
  140. print_matrix_row(row);
  141. print("\n");
  142. }
  143. }
  144. #define ROW_MASK 0b11100000
  145. static const uint8_t row_bit[MATRIX_ROWS] = {
  146. // 76543210
  147. 0b00000000,
  148. 0b00100000,
  149. 0b01000000,
  150. 0b01100000,
  151. 0b10000000,
  152. 0b10100000,
  153. 0b11000000,
  154. 0b11100000,
  155. };
  156. static void init_cols(void)
  157. {
  158. // columns
  159. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  160. uint8_t pin = col_pins[x];
  161. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  162. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  163. }
  164. // rows
  165. DDRF |= ROW_MASK;
  166. PORTF &= ~ROW_MASK;
  167. // trackpoint
  168. for(uint8_t x = 0; x < 3; x++) {
  169. uint8_t pin = tp_pins[x];
  170. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  171. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  172. }
  173. }
  174. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
  175. {
  176. // Store last value of row prior to reading
  177. matrix_row_t last_row_value = current_matrix[current_row];
  178. // Clear data in matrix row
  179. current_matrix[current_row] = 0;
  180. // special case for trackpoint
  181. if (current_row == 8) {
  182. for(uint8_t tp_index = 0; tp_index < 3; tp_index++) {
  183. // Select the TP pin to read (active low)
  184. uint8_t pin = tp_pins[tp_index];
  185. uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
  186. // Populate the matrix row with the state of the col pin
  187. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << tp_index);
  188. }
  189. return (last_row_value != current_matrix[current_row]);
  190. }
  191. // Select row and wait for row selecton to stabilize
  192. select_row(current_row);
  193. _delay_us(5); // without this wait it won't read stable value.
  194. // wait_us(50);
  195. // For each col...
  196. for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  197. // Select the col pin to read (active low)
  198. uint8_t pin = col_pins[col_index];
  199. uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
  200. // Populate the matrix row with the state of the col pin
  201. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
  202. }
  203. // Unselect row
  204. unselect_row(current_row);
  205. return (last_row_value != current_matrix[current_row]);
  206. }
  207. static void select_row(uint8_t row)
  208. {
  209. PORTF = row_bit[row] | (PORTF & ~ROW_MASK);
  210. }
  211. static void unselect_row(uint8_t row)
  212. {
  213. }
  214. static void unselect_rows(void)
  215. {
  216. }