matrix.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. if (matrix_init_kb) {
  71. (*matrix_init_kb)();
  72. }
  73. }
  74. uint8_t matrix_scan(void)
  75. {
  76. #if DIODE_DIRECTION == COL2ROW
  77. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  78. select_row(i);
  79. _delay_us(30); // without this wait read unstable value.
  80. matrix_row_t cols = read_cols();
  81. if (matrix_debouncing[i] != cols) {
  82. matrix_debouncing[i] = cols;
  83. if (debouncing) {
  84. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  85. }
  86. debouncing = DEBOUNCE;
  87. }
  88. unselect_rows();
  89. }
  90. if (debouncing) {
  91. if (--debouncing) {
  92. _delay_ms(1);
  93. } else {
  94. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  95. matrix[i] = matrix_debouncing[i];
  96. }
  97. }
  98. }
  99. #else
  100. for (uint8_t i = 0; i < MATRIX_COLS; i++) {
  101. select_row(i);
  102. _delay_us(30); // without this wait read unstable value.
  103. matrix_row_t rows = read_cols();
  104. if (matrix_reversed_debouncing[i] != rows) {
  105. matrix_reversed_debouncing[i] = rows;
  106. if (debouncing) {
  107. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  108. }
  109. debouncing = DEBOUNCE;
  110. }
  111. unselect_rows();
  112. }
  113. if (debouncing) {
  114. if (--debouncing) {
  115. _delay_ms(1);
  116. } else {
  117. for (uint8_t i = 0; i < MATRIX_COLS; i++) {
  118. matrix_reversed[i] = matrix_reversed_debouncing[i];
  119. }
  120. }
  121. }
  122. for (uint8_t y = 0; y < MATRIX_ROWS; y++) {
  123. matrix_row_t row = 0;
  124. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  125. row |= ((matrix_reversed[x] & (1<<y)) >> y) << x;
  126. }
  127. matrix[y] = row;
  128. }
  129. #endif
  130. if (matrix_scan_kb) {
  131. (*matrix_scan_kb)();
  132. }
  133. return 1;
  134. }
  135. bool matrix_is_modified(void)
  136. {
  137. if (debouncing) return false;
  138. return true;
  139. }
  140. inline
  141. bool matrix_is_on(uint8_t row, uint8_t col)
  142. {
  143. return (matrix[row] & ((matrix_row_t)1<col));
  144. }
  145. inline
  146. matrix_row_t matrix_get_row(uint8_t row)
  147. {
  148. return matrix[row];
  149. }
  150. void matrix_print(void)
  151. {
  152. print("\nr/c 0123456789ABCDEF\n");
  153. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  154. phex(row); print(": ");
  155. pbin_reverse16(matrix_get_row(row));
  156. print("\n");
  157. }
  158. }
  159. uint8_t matrix_key_count(void)
  160. {
  161. uint8_t count = 0;
  162. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  163. count += bitpop16(matrix[i]);
  164. }
  165. return count;
  166. }
  167. static void init_cols(void)
  168. {
  169. int B = 0, C = 0, D = 0, E = 0, F = 0;
  170. #if DIODE_DIRECTION == COL2ROW
  171. for(int x = 0; x < MATRIX_COLS; x++) {
  172. int col = COLS[x];
  173. #else
  174. for(int x = 0; x < MATRIX_ROWS; x++) {
  175. int col = ROWS[x];
  176. #endif
  177. if ((col & 0xF0) == 0x20) {
  178. B |= (1<<(col & 0x0F));
  179. } else if ((col & 0xF0) == 0x30) {
  180. C |= (1<<(col & 0x0F));
  181. } else if ((col & 0xF0) == 0x40) {
  182. D |= (1<<(col & 0x0F));
  183. } else if ((col & 0xF0) == 0x50) {
  184. E |= (1<<(col & 0x0F));
  185. } else if ((col & 0xF0) == 0x60) {
  186. F |= (1<<(col & 0x0F));
  187. }
  188. }
  189. DDRB &= ~(B); PORTB |= (B);
  190. DDRC &= ~(C); PORTC |= (C);
  191. DDRD &= ~(D); PORTD |= (D);
  192. DDRE &= ~(E); PORTE |= (E);
  193. DDRF &= ~(F); PORTF |= (F);
  194. }
  195. static matrix_row_t read_cols(void)
  196. {
  197. matrix_row_t result = 0;
  198. #if DIODE_DIRECTION == COL2ROW
  199. for(int x = 0; x < MATRIX_COLS; x++) {
  200. int col = COLS[x];
  201. #else
  202. for(int x = 0; x < MATRIX_ROWS; x++) {
  203. int col = ROWS[x];
  204. #endif
  205. if ((col & 0xF0) == 0x20) {
  206. result |= (PINB&(1<<(col & 0x0F)) ? 0 : (1<<x));
  207. } else if ((col & 0xF0) == 0x30) {
  208. result |= (PINC&(1<<(col & 0x0F)) ? 0 : (1<<x));
  209. } else if ((col & 0xF0) == 0x40) {
  210. result |= (PIND&(1<<(col & 0x0F)) ? 0 : (1<<x));
  211. } else if ((col & 0xF0) == 0x50) {
  212. result |= (PINE&(1<<(col & 0x0F)) ? 0 : (1<<x));
  213. } else if ((col & 0xF0) == 0x60) {
  214. result |= (PINF&(1<<(col & 0x0F)) ? 0 : (1<<x));
  215. }
  216. }
  217. return result;
  218. }
  219. static void unselect_rows(void)
  220. {
  221. int B = 0, C = 0, D = 0, E = 0, F = 0;
  222. #if DIODE_DIRECTION == COL2ROW
  223. for(int x = 0; x < MATRIX_ROWS; x++) {
  224. int row = ROWS[x];
  225. #else
  226. for(int x = 0; x < MATRIX_COLS; x++) {
  227. int row = COLS[x];
  228. #endif
  229. if ((row & 0xF0) == 0x20) {
  230. B |= (1<<(row & 0x0F));
  231. } else if ((row & 0xF0) == 0x30) {
  232. C |= (1<<(row & 0x0F));
  233. } else if ((row & 0xF0) == 0x40) {
  234. D |= (1<<(row & 0x0F));
  235. } else if ((row & 0xF0) == 0x50) {
  236. E |= (1<<(row & 0x0F));
  237. } else if ((row & 0xF0) == 0x60) {
  238. F |= (1<<(row & 0x0F));
  239. }
  240. }
  241. DDRB &= ~(B); PORTB |= (B);
  242. DDRC &= ~(C); PORTC |= (C);
  243. DDRD &= ~(D); PORTD |= (D);
  244. DDRE &= ~(E); PORTE |= (E);
  245. DDRF &= ~(F); PORTF |= (F);
  246. }
  247. static void select_row(uint8_t row)
  248. {
  249. #if DIODE_DIRECTION == COL2ROW
  250. int row_pin = ROWS[row];
  251. #else
  252. int row_pin = COLS[row];
  253. #endif
  254. if ((row_pin & 0xF0) == 0x20) {
  255. DDRB |= (1<<(row_pin & 0x0F));
  256. PORTB &= ~(1<<(row_pin & 0x0F));
  257. } else if ((row_pin & 0xF0) == 0x30) {
  258. DDRC |= (1<<(row_pin & 0x0F));
  259. PORTC &= ~(1<<(row_pin & 0x0F));
  260. } else if ((row_pin & 0xF0) == 0x40) {
  261. DDRD |= (1<<(row_pin & 0x0F));
  262. PORTD &= ~(1<<(row_pin & 0x0F));
  263. } else if ((row_pin & 0xF0) == 0x50) {
  264. DDRE |= (1<<(row_pin & 0x0F));
  265. PORTE &= ~(1<<(row_pin & 0x0F));
  266. } else if ((row_pin & 0xF0) == 0x60) {
  267. DDRF |= (1<<(row_pin & 0x0F));
  268. PORTF &= ~(1<<(row_pin & 0x0F));
  269. }
  270. }