matrix.c 7.0 KB

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