You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

306 lines
7.6 KiB

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