matrix.c 7.5 KB

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