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.

289 lines
7.2 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 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 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_kb(void) {
  66. matrix_init_user();
  67. }
  68. __attribute__ ((weak))
  69. void matrix_scan_kb(void) {
  70. matrix_scan_user();
  71. }
  72. __attribute__ ((weak))
  73. void matrix_init_user(void) {
  74. }
  75. __attribute__ ((weak))
  76. void matrix_scan_user(void) {
  77. }
  78. inline
  79. uint8_t matrix_rows(void) {
  80. return MATRIX_ROWS;
  81. }
  82. inline
  83. uint8_t matrix_cols(void) {
  84. return MATRIX_COLS;
  85. }
  86. void matrix_init(void) {
  87. // initialize row and col
  88. unselect_rows();
  89. init_cols();
  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_quantum();
  96. }
  97. uint8_t matrix_scan(void)
  98. {
  99. // Set row, read cols
  100. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  101. # if (DEBOUNCE > 0)
  102. bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
  103. if (matrix_changed) {
  104. debouncing = true;
  105. debouncing_time = timer_read();
  106. }
  107. # else
  108. read_cols_on_row(matrix, current_row);
  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. matrix_scan_quantum();
  120. return 1;
  121. }
  122. bool matrix_is_modified(void)
  123. {
  124. #if (DEBOUNCE > 0)
  125. if (debouncing) return false;
  126. #endif
  127. return true;
  128. }
  129. inline
  130. bool matrix_is_on(uint8_t row, uint8_t col)
  131. {
  132. return (matrix[row] & ((matrix_row_t)1<<col));
  133. }
  134. inline
  135. matrix_row_t matrix_get_row(uint8_t row)
  136. {
  137. // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
  138. // switch blocker installed and the switch is always pressed.
  139. #ifdef MATRIX_MASKED
  140. return matrix[row] & matrix_mask[row];
  141. #else
  142. return matrix[row];
  143. #endif
  144. }
  145. void matrix_print(void)
  146. {
  147. print_matrix_header();
  148. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  149. print_hex8(row); print(": ");
  150. print_matrix_row(row);
  151. print("\n");
  152. }
  153. }
  154. uint8_t matrix_key_count(void)
  155. {
  156. uint8_t count = 0;
  157. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  158. count += matrix_bitpop(i);
  159. }
  160. return count;
  161. }
  162. #define ROW_MASK 0b11100000
  163. static const uint8_t row_bit[MATRIX_ROWS] = {
  164. // 76543210
  165. 0b00000000,
  166. 0b00100000,
  167. 0b01000000,
  168. 0b01100000,
  169. 0b10000000,
  170. 0b10100000,
  171. 0b11000000,
  172. 0b11100000,
  173. };
  174. static void init_cols(void)
  175. {
  176. // columns
  177. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  178. uint8_t pin = col_pins[x];
  179. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  180. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  181. }
  182. // rows
  183. DDRF |= ROW_MASK;
  184. PORTF &= ~ROW_MASK;
  185. // trackpoint
  186. for(uint8_t x = 0; x < 3; x++) {
  187. uint8_t pin = tp_pins[x];
  188. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  189. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  190. }
  191. }
  192. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
  193. {
  194. // Store last value of row prior to reading
  195. matrix_row_t last_row_value = current_matrix[current_row];
  196. // Clear data in matrix row
  197. current_matrix[current_row] = 0;
  198. // special case for trackpoint
  199. if (current_row == 8) {
  200. for(uint8_t tp_index = 0; tp_index < 3; tp_index++) {
  201. // Select the TP pin to read (active low)
  202. uint8_t pin = tp_pins[tp_index];
  203. uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
  204. // Populate the matrix row with the state of the col pin
  205. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << tp_index);
  206. }
  207. return (last_row_value != current_matrix[current_row]);
  208. }
  209. // Select row and wait for row selecton to stabilize
  210. select_row(current_row);
  211. _delay_us(5); // without this wait it won't read stable value.
  212. // wait_us(50);
  213. // For each col...
  214. for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  215. // Select the col pin to read (active low)
  216. uint8_t pin = col_pins[col_index];
  217. uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
  218. // Populate the matrix row with the state of the col pin
  219. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
  220. }
  221. // Unselect row
  222. unselect_row(current_row);
  223. return (last_row_value != current_matrix[current_row]);
  224. }
  225. static void select_row(uint8_t row)
  226. {
  227. PORTF = row_bit[row] | (PORTF & ~ROW_MASK);
  228. }
  229. static void unselect_row(uint8_t row)
  230. {
  231. }
  232. static void unselect_rows(void)
  233. {
  234. }