matrix.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. Copyright 2012 Jun Wako
  3. Generated by planckkeyboard.com (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. /*
  16. * scan matrix
  17. */
  18. #include <stdint.h>
  19. #include <stdbool.h>
  20. #include <avr/io.h>
  21. #include <util/delay.h>
  22. #include "print.h"
  23. #include "debug.h"
  24. #include "util.h"
  25. #include "matrix.h"
  26. #ifndef DEBOUNCE
  27. # define DEBOUNCE 10
  28. #endif
  29. static uint8_t debouncing = DEBOUNCE;
  30. /* matrix state(1:on, 0:off) */
  31. static matrix_row_t matrix[MATRIX_ROWS];
  32. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  33. #if DIODE_DIRECTION == ROW2COL
  34. static matrix_row_t matrix_reversed[MATRIX_COLS];
  35. static matrix_row_t matrix_reversed_debouncing[MATRIX_COLS];
  36. #endif
  37. static matrix_row_t read_cols(void);
  38. static void init_cols(void);
  39. static void unselect_rows(void);
  40. static void select_row(uint8_t row);
  41. __attribute__ ((weak))
  42. void matrix_init_kb(void) {
  43. }
  44. __attribute__ ((weak))
  45. void matrix_scan_kb(void) {
  46. }
  47. inline
  48. uint8_t matrix_rows(void)
  49. {
  50. return MATRIX_ROWS;
  51. }
  52. inline
  53. uint8_t matrix_cols(void)
  54. {
  55. return MATRIX_COLS;
  56. }
  57. void matrix_init(void)
  58. {
  59. // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
  60. MCUCR |= (1<<JTD);
  61. MCUCR |= (1<<JTD);
  62. // initialize row and col
  63. unselect_rows();
  64. init_cols();
  65. // initialize matrix state: all keys off
  66. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  67. matrix[i] = 0;
  68. matrix_debouncing[i] = 0;
  69. }
  70. matrix_init_kb();
  71. }
  72. uint8_t matrix_scan(void)
  73. {
  74. #if DIODE_DIRECTION == COL2ROW
  75. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  76. select_row(i);
  77. _delay_us(30); // without this wait read unstable value.
  78. matrix_row_t cols = read_cols();
  79. if (matrix_debouncing[i] != cols) {
  80. matrix_debouncing[i] = cols;
  81. if (debouncing) {
  82. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  83. }
  84. debouncing = DEBOUNCE;
  85. }
  86. unselect_rows();
  87. }
  88. if (debouncing) {
  89. if (--debouncing) {
  90. _delay_ms(1);
  91. } else {
  92. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  93. matrix[i] = matrix_debouncing[i];
  94. }
  95. }
  96. }
  97. #else
  98. for (uint8_t i = 0; i < MATRIX_COLS; i++) {
  99. select_row(i);
  100. _delay_us(30); // without this wait read unstable value.
  101. matrix_row_t rows = read_cols();
  102. if (matrix_reversed_debouncing[i] != rows) {
  103. matrix_reversed_debouncing[i] = rows;
  104. if (debouncing) {
  105. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  106. }
  107. debouncing = DEBOUNCE;
  108. }
  109. unselect_rows();
  110. }
  111. if (debouncing) {
  112. if (--debouncing) {
  113. _delay_ms(1);
  114. } else {
  115. for (uint8_t i = 0; i < MATRIX_COLS; i++) {
  116. matrix_reversed[i] = matrix_reversed_debouncing[i];
  117. }
  118. }
  119. }
  120. for (uint8_t y = 0; y < MATRIX_ROWS; y++) {
  121. matrix_row_t row = 0;
  122. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  123. row |= ((matrix_reversed[x] & (1<<y)) >> y) << x;
  124. }
  125. matrix[y] = row;
  126. }
  127. #endif
  128. matrix_scan_kb();
  129. return 1;
  130. }
  131. bool matrix_is_modified(void)
  132. {
  133. if (debouncing) return false;
  134. return true;
  135. }
  136. inline
  137. bool matrix_is_on(uint8_t row, uint8_t col)
  138. {
  139. return (matrix[row] & ((matrix_row_t)1<col));
  140. }
  141. inline
  142. matrix_row_t matrix_get_row(uint8_t row)
  143. {
  144. return matrix[row];
  145. }
  146. void matrix_print(void)
  147. {
  148. print("\nr/c 0123456789ABCDEF\n");
  149. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  150. phex(row); print(": ");
  151. pbin_reverse16(matrix_get_row(row));
  152. print("\n");
  153. }
  154. }
  155. uint8_t matrix_key_count(void)
  156. {
  157. uint8_t count = 0;
  158. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  159. count += bitpop16(matrix[i]);
  160. }
  161. return count;
  162. }
  163. static void init_cols(void)
  164. {
  165. int B = 0, C = 0, D = 0, E = 0, F = 0;
  166. #if DIODE_DIRECTION == COL2ROW
  167. for(int x = 0; x < MATRIX_COLS; x++) {
  168. int col = COLS[x];
  169. #else
  170. for(int x = 0; x < MATRIX_ROWS; x++) {
  171. int col = ROWS[x];
  172. #endif
  173. if ((col & 0xF0) == 0x20) {
  174. B |= (1<<(col & 0x0F));
  175. } else if ((col & 0xF0) == 0x30) {
  176. C |= (1<<(col & 0x0F));
  177. } else if ((col & 0xF0) == 0x40) {
  178. D |= (1<<(col & 0x0F));
  179. } else if ((col & 0xF0) == 0x50) {
  180. E |= (1<<(col & 0x0F));
  181. } else if ((col & 0xF0) == 0x60) {
  182. F |= (1<<(col & 0x0F));
  183. }
  184. }
  185. DDRB &= ~(B); PORTB |= (B);
  186. DDRC &= ~(C); PORTC |= (C);
  187. DDRD &= ~(D); PORTD |= (D);
  188. DDRE &= ~(E); PORTE |= (E);
  189. DDRF &= ~(F); PORTF |= (F);
  190. }
  191. static matrix_row_t read_cols(void)
  192. {
  193. matrix_row_t result = 0;
  194. #if DIODE_DIRECTION == COL2ROW
  195. for(int x = 0; x < MATRIX_COLS; x++) {
  196. int col = COLS[x];
  197. #else
  198. for(int x = 0; x < MATRIX_ROWS; x++) {
  199. int col = ROWS[x];
  200. #endif
  201. if ((col & 0xF0) == 0x20) {
  202. result |= (PINB&(1<<(col & 0x0F)) ? 0 : (1<<x));
  203. } else if ((col & 0xF0) == 0x30) {
  204. result |= (PINC&(1<<(col & 0x0F)) ? 0 : (1<<x));
  205. } else if ((col & 0xF0) == 0x40) {
  206. result |= (PIND&(1<<(col & 0x0F)) ? 0 : (1<<x));
  207. } else if ((col & 0xF0) == 0x50) {
  208. result |= (PINE&(1<<(col & 0x0F)) ? 0 : (1<<x));
  209. } else if ((col & 0xF0) == 0x60) {
  210. result |= (PINF&(1<<(col & 0x0F)) ? 0 : (1<<x));
  211. }
  212. }
  213. return result;
  214. }
  215. static void unselect_rows(void)
  216. {
  217. int B = 0, C = 0, D = 0, E = 0, F = 0;
  218. #if DIODE_DIRECTION == COL2ROW
  219. for(int x = 0; x < MATRIX_ROWS; x++) {
  220. int row = ROWS[x];
  221. #else
  222. for(int x = 0; x < MATRIX_COLS; x++) {
  223. int row = COLS[x];
  224. #endif
  225. if ((row & 0xF0) == 0x20) {
  226. B |= (1<<(row & 0x0F));
  227. } else if ((row & 0xF0) == 0x30) {
  228. C |= (1<<(row & 0x0F));
  229. } else if ((row & 0xF0) == 0x40) {
  230. D |= (1<<(row & 0x0F));
  231. } else if ((row & 0xF0) == 0x50) {
  232. E |= (1<<(row & 0x0F));
  233. } else if ((row & 0xF0) == 0x60) {
  234. F |= (1<<(row & 0x0F));
  235. }
  236. }
  237. DDRB &= ~(B); PORTB |= (B);
  238. DDRC &= ~(C); PORTC |= (C);
  239. DDRD &= ~(D); PORTD |= (D);
  240. DDRE &= ~(E); PORTE |= (E);
  241. DDRF &= ~(F); PORTF |= (F);
  242. }
  243. static void select_row(uint8_t row)
  244. {
  245. #if DIODE_DIRECTION == COL2ROW
  246. int row_pin = ROWS[row];
  247. #else
  248. int row_pin = COLS[row];
  249. #endif
  250. if ((row_pin & 0xF0) == 0x20) {
  251. DDRB |= (1<<(row_pin & 0x0F));
  252. PORTB &= ~(1<<(row_pin & 0x0F));
  253. } else if ((row_pin & 0xF0) == 0x30) {
  254. DDRC |= (1<<(row_pin & 0x0F));
  255. PORTC &= ~(1<<(row_pin & 0x0F));
  256. } else if ((row_pin & 0xF0) == 0x40) {
  257. DDRD |= (1<<(row_pin & 0x0F));
  258. PORTD &= ~(1<<(row_pin & 0x0F));
  259. } else if ((row_pin & 0xF0) == 0x50) {
  260. DDRE |= (1<<(row_pin & 0x0F));
  261. PORTE &= ~(1<<(row_pin & 0x0F));
  262. } else if ((row_pin & 0xF0) == 0x60) {
  263. DDRF |= (1<<(row_pin & 0x0F));
  264. PORTF &= ~(1<<(row_pin & 0x0F));
  265. }
  266. }