Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

matrix.c 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. Copyright 2012 Jun Wako <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /*
  15. * scan matrix
  16. */
  17. #include <stdint.h>
  18. #include <stdbool.h>
  19. #include <avr/io.h>
  20. #include <util/delay.h>
  21. #include "print.h"
  22. #include "debug.h"
  23. #include "util.h"
  24. #include "matrix.h"
  25. #ifndef DEBOUNCE
  26. # define DEBOUNCE 5
  27. #endif
  28. static uint8_t debouncing = DEBOUNCE;
  29. /* matrix state(1:on, 0:off) */
  30. static matrix_row_t matrix[MATRIX_ROWS];
  31. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  32. static matrix_row_t read_cols(void);
  33. static void init_cols(void);
  34. static void unselect_rows(void);
  35. static void select_row(uint8_t row);
  36. inline
  37. uint8_t matrix_rows(void)
  38. {
  39. return MATRIX_ROWS;
  40. }
  41. inline
  42. uint8_t matrix_cols(void)
  43. {
  44. return MATRIX_COLS;
  45. }
  46. void matrix_init(void)
  47. {
  48. // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
  49. MCUCR |= (1<<JTD);
  50. MCUCR |= (1<<JTD);
  51. backlight_init_ports();
  52. // initialize row and col
  53. unselect_rows();
  54. init_cols();
  55. // initialize matrix state: all keys off
  56. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  57. matrix[i] = 0;
  58. matrix_debouncing[i] = 0;
  59. }
  60. }
  61. uint8_t matrix_scan(void)
  62. {
  63. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  64. select_row(i);
  65. _delay_us(30); // without this wait read unstable value.
  66. matrix_row_t cols = read_cols();
  67. if (matrix_debouncing[i] != cols) {
  68. matrix_debouncing[i] = cols;
  69. if (debouncing) {
  70. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  71. }
  72. debouncing = DEBOUNCE;
  73. }
  74. unselect_rows();
  75. }
  76. if (debouncing) {
  77. if (--debouncing) {
  78. _delay_ms(1);
  79. } else {
  80. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  81. matrix[i] = matrix_debouncing[i];
  82. }
  83. }
  84. }
  85. return 1;
  86. }
  87. bool matrix_is_modified(void)
  88. {
  89. if (debouncing) return false;
  90. return true;
  91. }
  92. inline
  93. bool matrix_is_on(uint8_t row, uint8_t col)
  94. {
  95. return (matrix[row] & ((matrix_row_t)1<<col));
  96. }
  97. inline
  98. matrix_row_t matrix_get_row(uint8_t row)
  99. {
  100. return matrix[row];
  101. }
  102. void matrix_print(void)
  103. {
  104. print("\nr/c 0123456789ABCDEF\n");
  105. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  106. phex(row); print(": ");
  107. pbin_reverse16(matrix_get_row(row));
  108. print("\n");
  109. }
  110. }
  111. uint8_t matrix_key_count(void)
  112. {
  113. uint8_t count = 0;
  114. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  115. count += bitpop16(matrix[i]);
  116. }
  117. return count;
  118. }
  119. /* Column pin configuration
  120. * col: 0 1 2 3 4 5
  121. * pin: D0, D4, C6, D7, E6, B4
  122. */
  123. static void init_cols(void)
  124. {
  125. // Input with pull-up(DDR:0, PORT:1)
  126. DDRD &= ~(1<<0 | 1<<4 | 1<<7 );
  127. PORTD |= (1<<0 | 1<<4 | 1<<7 );
  128. DDRB &= ~(1<<4);
  129. PORTB |= (1<<4);
  130. DDRC &= ~(1<<6);
  131. PORTC |= (1<<6);
  132. DDRE &= ~(1<<6);
  133. PORTE |= (1<<6);
  134. }
  135. static matrix_row_t read_cols(void)
  136. {
  137. /* //LED on Bottom of switch
  138. return (PIND&(1<<0) ? 0 : (1<<0)) |
  139. (PIND&(1<<4) ? 0 : (1<<1)) |
  140. (PINC&(1<<6) ? 0 : (1<<2)) |
  141. (PIND&(1<<7) ? 0 : (1<<3)) |
  142. (PINE&(1<<6) ? 0 : (1<<4)) |
  143. (PINB&(1<<4) ? 0 : (1<<5));
  144. */
  145. //LED on Top of switch
  146. return (PIND&(1<<0) ? 0 : (1<<5)) |
  147. (PIND&(1<<4) ? 0 : (1<<4)) |
  148. (PINC&(1<<6) ? 0 : (1<<3)) |
  149. (PIND&(1<<7) ? 0 : (1<<2)) |
  150. (PINE&(1<<6) ? 0 : (1<<1)) |
  151. (PINB&(1<<4) ? 0 : (1<<0));
  152. }
  153. /* Row pin configuration
  154. * row: 0 1 2 3 4 5 6 7 8 9
  155. * pin: B6, B2, B3, B1, F7, D3, D2, F4, F5, F6
  156. */
  157. static void unselect_rows(void)
  158. {
  159. // Hi-Z(DDR:0, PORT:0) to unselect
  160. DDRF &= ~0b11110000;
  161. PORTF &= ~0b11110000;
  162. DDRB &= ~0b01001110;
  163. PORTB &= ~0b01001110;
  164. DDRD &= ~0b00001100;
  165. PORTD &= ~0b00001100;
  166. }
  167. static void select_row(uint8_t row)
  168. {
  169. // Output low(DDR:1, PORT:0) to select
  170. /* //LED on Bottom of switch
  171. switch (row) {
  172. case 0:
  173. DDRD |= (1<<3);
  174. PORTD &= ~(1<<3);
  175. break;
  176. case 1:
  177. DDRD |= (1<<2);
  178. PORTD &= ~(1<<2);
  179. break;
  180. case 2:
  181. DDRF |= (1<<4);
  182. PORTF &= ~(1<<4);
  183. break;
  184. case 3:
  185. DDRF |= (1<<5);
  186. PORTF &= ~(1<<5);
  187. break;
  188. case 4:
  189. DDRF |= (1<<6);
  190. PORTF &= ~(1<<6);
  191. break;
  192. case 5:
  193. DDRB |= (1<<6);
  194. PORTB &= ~(1<<6);
  195. break;
  196. case 6:
  197. DDRB |= (1<<2);
  198. PORTB &= ~(1<<2);
  199. break;
  200. case 7:
  201. DDRB |= (1<<3);
  202. PORTB &= ~(1<<3);
  203. break;
  204. case 8:
  205. DDRB |= (1<<1);
  206. PORTB &= ~(1<<1);
  207. break;
  208. case 9:
  209. DDRF |= (1<<7);
  210. PORTF &= ~(1<<7);
  211. break;
  212. }
  213. */
  214. //LED on Top of switch
  215. switch (row) {
  216. case 9:
  217. DDRD |= (1<<3);
  218. PORTD &= ~(1<<3);
  219. break;
  220. case 8:
  221. DDRD |= (1<<2);
  222. PORTD &= ~(1<<2);
  223. break;
  224. case 7:
  225. DDRF |= (1<<4);
  226. PORTF &= ~(1<<4);
  227. break;
  228. case 6:
  229. DDRF |= (1<<5);
  230. PORTF &= ~(1<<5);
  231. break;
  232. case 5:
  233. DDRF |= (1<<6);
  234. PORTF &= ~(1<<6);
  235. break;
  236. case 4:
  237. DDRB |= (1<<6);
  238. PORTB &= ~(1<<6);
  239. break;
  240. case 3:
  241. DDRB |= (1<<2);
  242. PORTB &= ~(1<<2);
  243. break;
  244. case 2:
  245. DDRB |= (1<<3);
  246. PORTB &= ~(1<<3);
  247. break;
  248. case 1:
  249. DDRB |= (1<<1);
  250. PORTB &= ~(1<<1);
  251. break;
  252. case 0:
  253. DDRF |= (1<<7);
  254. PORTF &= ~(1<<7);
  255. break;
  256. }
  257. }