matrix.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. #elif (MATRIX_COLS <= 16)
  39. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  40. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  41. #elif (MATRIX_COLS <= 32)
  42. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  43. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  44. #endif
  45. #ifdef MATRIX_MASKED
  46. extern const matrix_row_t matrix_mask[];
  47. #endif
  48. static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  49. static const uint8_t tp_pins[3] = TRACKPOINT_PINS;
  50. /* matrix state(1:on, 0:off) */
  51. static matrix_row_t matrix[MATRIX_ROWS];
  52. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  53. static void init_cols(void);
  54. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
  55. static void unselect_rows(void);
  56. static void select_row(uint8_t row);
  57. static void unselect_row(uint8_t row);
  58. __attribute__ ((weak))
  59. void matrix_init_kb(void) {
  60. matrix_init_user();
  61. }
  62. __attribute__ ((weak))
  63. void matrix_scan_kb(void) {
  64. matrix_scan_user();
  65. }
  66. __attribute__ ((weak))
  67. void matrix_init_user(void) {
  68. }
  69. __attribute__ ((weak))
  70. void matrix_scan_user(void) {
  71. }
  72. inline
  73. uint8_t matrix_rows(void) {
  74. return MATRIX_ROWS;
  75. }
  76. inline
  77. uint8_t matrix_cols(void) {
  78. return MATRIX_COLS;
  79. }
  80. void matrix_init(void) {
  81. // initialize row and col
  82. unselect_rows();
  83. init_cols();
  84. // initialize matrix state: all keys off
  85. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  86. matrix[i] = 0;
  87. matrix_debouncing[i] = 0;
  88. }
  89. matrix_init_kb();
  90. }
  91. uint8_t matrix_scan(void)
  92. {
  93. // Set row, read cols
  94. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  95. # if (DEBOUNCE > 0)
  96. bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
  97. if (matrix_changed) {
  98. debouncing = true;
  99. debouncing_time = timer_read();
  100. }
  101. # else
  102. read_cols_on_row(matrix, current_row);
  103. # endif
  104. }
  105. # if (DEBOUNCE > 0)
  106. if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCE)) {
  107. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  108. matrix[i] = matrix_debouncing[i];
  109. }
  110. debouncing = false;
  111. }
  112. # endif
  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. // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
  125. // switch blocker installed and the switch is always pressed.
  126. #ifdef MATRIX_MASKED
  127. return matrix[row] & matrix_mask[row];
  128. #else
  129. return matrix[row];
  130. #endif
  131. }
  132. void matrix_print(void)
  133. {
  134. print_matrix_header();
  135. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  136. print_hex8(row); print(": ");
  137. print_matrix_row(row);
  138. print("\n");
  139. }
  140. }
  141. #define ROW_MASK 0b11100000
  142. static const uint8_t row_bit[MATRIX_ROWS] = {
  143. // 76543210
  144. 0b00000000,
  145. 0b00100000,
  146. 0b01000000,
  147. 0b01100000,
  148. 0b10000000,
  149. 0b10100000,
  150. 0b11000000,
  151. 0b11100000,
  152. };
  153. static void init_cols(void)
  154. {
  155. // columns
  156. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  157. uint8_t pin = col_pins[x];
  158. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  159. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  160. }
  161. // rows
  162. DDRF |= ROW_MASK;
  163. PORTF &= ~ROW_MASK;
  164. // trackpoint
  165. for(uint8_t x = 0; x < 3; x++) {
  166. uint8_t pin = tp_pins[x];
  167. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  168. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  169. }
  170. }
  171. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
  172. {
  173. // Store last value of row prior to reading
  174. matrix_row_t last_row_value = current_matrix[current_row];
  175. // Clear data in matrix row
  176. current_matrix[current_row] = 0;
  177. // special case for trackpoint
  178. if (current_row == 8) {
  179. for(uint8_t tp_index = 0; tp_index < 3; tp_index++) {
  180. // Select the TP pin to read (active low)
  181. uint8_t pin = tp_pins[tp_index];
  182. uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
  183. // Populate the matrix row with the state of the col pin
  184. current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << tp_index);
  185. }
  186. return (last_row_value != current_matrix[current_row]);
  187. }
  188. // Select row and wait for row selecton to stabilize
  189. select_row(current_row);
  190. _delay_us(5); // without this wait it won't read stable value.
  191. // wait_us(50);
  192. // For each col...
  193. for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  194. // Select the col pin to read (active low)
  195. uint8_t pin = col_pins[col_index];
  196. uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
  197. // Populate the matrix row with the state of the col pin
  198. current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  199. }
  200. // Unselect row
  201. unselect_row(current_row);
  202. return (last_row_value != current_matrix[current_row]);
  203. }
  204. static void select_row(uint8_t row)
  205. {
  206. PORTF = row_bit[row] | (PORTF & ~ROW_MASK);
  207. }
  208. static void unselect_row(uint8_t row)
  209. {
  210. }
  211. static void unselect_rows(void)
  212. {
  213. }