matrix.c 7.6 KB

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