matrix.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. Copyright 2012 Jun Wako
  3. Copyright 2014 Jack Humbert
  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. #include <stdint.h>
  16. #include <stdbool.h>
  17. #if defined(__AVR__)
  18. #include <avr/io.h>
  19. #endif
  20. #include "wait.h"
  21. #include "print.h"
  22. #include "debug.h"
  23. #include "util.h"
  24. #include "matrix.h"
  25. #ifdef MATRIX_MASKED
  26. extern const matrix_row_t matrix_mask[];
  27. #endif
  28. /* Set 0 if debouncing isn't needed */
  29. #ifndef DEBOUNCING_DELAY
  30. # define DEBOUNCING_DELAY 5
  31. #endif
  32. static uint8_t debouncing = DEBOUNCING_DELAY;
  33. static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  34. static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  35. /* matrix state(1:on, 0:off) */
  36. static matrix_row_t matrix[MATRIX_ROWS];
  37. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  38. #if DIODE_DIRECTION == ROW2COL
  39. static matrix_row_t matrix_reversed[MATRIX_COLS];
  40. static matrix_row_t matrix_reversed_debouncing[MATRIX_COLS];
  41. #endif
  42. #if MATRIX_COLS > 16
  43. #define SHIFTER 1UL
  44. #else
  45. #define SHIFTER 1
  46. #endif
  47. static matrix_row_t read_cols(void);
  48. static void init_cols(void);
  49. static void unselect_rows(void);
  50. static void select_row(uint8_t row);
  51. __attribute__ ((weak))
  52. void matrix_init_quantum(void) {
  53. matrix_init_kb();
  54. }
  55. __attribute__ ((weak))
  56. void matrix_scan_quantum(void) {
  57. matrix_scan_kb();
  58. }
  59. __attribute__ ((weak))
  60. void matrix_init_kb(void) {
  61. matrix_init_user();
  62. }
  63. __attribute__ ((weak))
  64. void matrix_scan_kb(void) {
  65. matrix_scan_user();
  66. }
  67. __attribute__ ((weak))
  68. void matrix_init_user(void) {
  69. }
  70. __attribute__ ((weak))
  71. void matrix_scan_user(void) {
  72. }
  73. inline
  74. uint8_t matrix_rows(void) {
  75. return MATRIX_ROWS;
  76. }
  77. inline
  78. uint8_t matrix_cols(void) {
  79. return MATRIX_COLS;
  80. }
  81. // void matrix_power_up(void) {
  82. // #if DIODE_DIRECTION == COL2ROW
  83. // for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
  84. // /* DDRxn */
  85. // _SFR_IO8((row_pins[r] >> 4) + 1) |= _BV(row_pins[r] & 0xF);
  86. // toggle_row(r);
  87. // }
  88. // for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
  89. // /* PORTxn */
  90. // _SFR_IO8((col_pins[c] >> 4) + 2) |= _BV(col_pins[c] & 0xF);
  91. // }
  92. // #else
  93. // for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
  94. // /* DDRxn */
  95. // _SFR_IO8((col_pins[c] >> 4) + 1) |= _BV(col_pins[c] & 0xF);
  96. // toggle_col(c);
  97. // }
  98. // for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
  99. // /* PORTxn */
  100. // _SFR_IO8((row_pins[r] >> 4) + 2) |= _BV(row_pins[r] & 0xF);
  101. // }
  102. // #endif
  103. // }
  104. void matrix_init(void) {
  105. // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
  106. #ifdef __AVR_ATmega32U4__
  107. MCUCR |= _BV(JTD);
  108. MCUCR |= _BV(JTD);
  109. #endif
  110. // initialize row and col
  111. unselect_rows();
  112. init_cols();
  113. // initialize matrix state: all keys off
  114. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  115. matrix[i] = 0;
  116. matrix_debouncing[i] = 0;
  117. }
  118. matrix_init_quantum();
  119. }
  120. uint8_t matrix_scan(void)
  121. {
  122. #if DIODE_DIRECTION == COL2ROW
  123. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  124. select_row(i);
  125. wait_us(30); // without this wait read unstable value.
  126. matrix_row_t cols = read_cols();
  127. if (matrix_debouncing[i] != cols) {
  128. matrix_debouncing[i] = cols;
  129. if (debouncing) {
  130. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  131. }
  132. debouncing = DEBOUNCING_DELAY;
  133. }
  134. unselect_rows();
  135. }
  136. if (debouncing) {
  137. if (--debouncing) {
  138. wait_ms(1);
  139. } else {
  140. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  141. matrix[i] = matrix_debouncing[i];
  142. }
  143. }
  144. }
  145. #else
  146. for (uint8_t i = 0; i < MATRIX_COLS; i++) {
  147. select_row(i);
  148. wait_us(30); // without this wait read unstable value.
  149. matrix_row_t rows = read_cols();
  150. if (matrix_reversed_debouncing[i] != rows) {
  151. matrix_reversed_debouncing[i] = rows;
  152. if (debouncing) {
  153. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  154. }
  155. debouncing = DEBOUNCING_DELAY;
  156. }
  157. unselect_rows();
  158. }
  159. if (debouncing) {
  160. if (--debouncing) {
  161. wait_ms(1);
  162. } else {
  163. for (uint8_t i = 0; i < MATRIX_COLS; i++) {
  164. matrix_reversed[i] = matrix_reversed_debouncing[i];
  165. }
  166. }
  167. }
  168. for (uint8_t y = 0; y < MATRIX_ROWS; y++) {
  169. matrix_row_t row = 0;
  170. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  171. row |= ((matrix_reversed[x] & (1<<y)) >> y) << x;
  172. }
  173. matrix[y] = row;
  174. }
  175. #endif
  176. matrix_scan_quantum();
  177. return 1;
  178. }
  179. bool matrix_is_modified(void)
  180. {
  181. if (debouncing) return false;
  182. return true;
  183. }
  184. inline
  185. bool matrix_is_on(uint8_t row, uint8_t col)
  186. {
  187. return (matrix[row] & ((matrix_row_t)1<col));
  188. }
  189. inline
  190. matrix_row_t matrix_get_row(uint8_t row)
  191. {
  192. // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
  193. // switch blocker installed and the switch is always pressed.
  194. #ifdef MATRIX_MASKED
  195. return matrix[row] & matrix_mask[row];
  196. #else
  197. return matrix[row];
  198. #endif
  199. }
  200. void matrix_print(void)
  201. {
  202. #if (MATRIX_COLS <= 8)
  203. print("\nr/c 01234567\n");
  204. #elif (MATRIX_COLS <= 16)
  205. print("\nr/c 0123456789ABCDEF\n");
  206. #elif (MATRIX_COLS <= 32)
  207. print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n");
  208. #endif
  209. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  210. phex(row); print(": ");
  211. #if (MATRIX_COLS <= 8)
  212. print_bin_reverse8(matrix_get_row(row));
  213. #elif (MATRIX_COLS <= 16)
  214. print_bin_reverse16(matrix_get_row(row));
  215. #elif (MATRIX_COLS <= 32)
  216. print_bin_reverse32(matrix_get_row(row));
  217. #endif
  218. print("\n");
  219. }
  220. }
  221. uint8_t matrix_key_count(void)
  222. {
  223. uint8_t count = 0;
  224. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  225. #if (MATRIX_COLS <= 8)
  226. count += bitpop(matrix[i]);
  227. #elif (MATRIX_COLS <= 16)
  228. count += bitpop16(matrix[i]);
  229. #elif (MATRIX_COLS <= 32)
  230. count += bitpop32(matrix[i]);
  231. #endif
  232. }
  233. return count;
  234. }
  235. static void init_cols(void)
  236. {
  237. #if DIODE_DIRECTION == COL2ROW
  238. for(int x = 0; x < MATRIX_COLS; x++) {
  239. int pin = col_pins[x];
  240. #else
  241. for(int x = 0; x < MATRIX_ROWS; x++) {
  242. int pin = row_pins[x];
  243. #endif
  244. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF);
  245. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF);
  246. }
  247. }
  248. static matrix_row_t read_cols(void)
  249. {
  250. matrix_row_t result = 0;
  251. #if DIODE_DIRECTION == COL2ROW
  252. for(int x = 0; x < MATRIX_COLS; x++) {
  253. int pin = col_pins[x];
  254. #else
  255. for(int x = 0; x < MATRIX_ROWS; x++) {
  256. int pin = row_pins[x];
  257. #endif
  258. result |= (_SFR_IO8(pin >> 4) & _BV(pin & 0xF)) ? 0 : (SHIFTER << x);
  259. }
  260. return result;
  261. }
  262. static void unselect_rows(void)
  263. {
  264. #if DIODE_DIRECTION == COL2ROW
  265. for(int x = 0; x < MATRIX_ROWS; x++) {
  266. int pin = row_pins[x];
  267. #else
  268. for(int x = 0; x < MATRIX_COLS; x++) {
  269. int pin = col_pins[x];
  270. #endif
  271. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF);
  272. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF);
  273. }
  274. }
  275. static void select_row(uint8_t row)
  276. {
  277. #if DIODE_DIRECTION == COL2ROW
  278. int pin = row_pins[row];
  279. #else
  280. int pin = col_pins[row];
  281. #endif
  282. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF);
  283. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF);
  284. }