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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. // initialize row and col
  52. unselect_rows();
  53. init_cols();
  54. // initialize matrix state: all keys off
  55. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  56. matrix[i] = 0;
  57. matrix_debouncing[i] = 0;
  58. }
  59. }
  60. uint8_t matrix_scan(void)
  61. {
  62. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  63. select_row(i);
  64. _delay_us(30); // without this wait read unstable value.
  65. matrix_row_t cols = read_cols();
  66. if (matrix_debouncing[i] != cols) {
  67. matrix_debouncing[i] = cols;
  68. if (debouncing) {
  69. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  70. }
  71. debouncing = DEBOUNCE;
  72. }
  73. unselect_rows();
  74. }
  75. if (debouncing) {
  76. if (--debouncing) {
  77. _delay_ms(1);
  78. } else {
  79. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  80. matrix[i] = matrix_debouncing[i];
  81. }
  82. }
  83. }
  84. return 1;
  85. }
  86. bool matrix_is_modified(void)
  87. {
  88. if (debouncing) return false;
  89. return true;
  90. }
  91. inline
  92. bool matrix_is_on(uint8_t row, uint8_t col)
  93. {
  94. return (matrix[row] & ((matrix_row_t)1<<col));
  95. }
  96. inline
  97. matrix_row_t matrix_get_row(uint8_t row)
  98. {
  99. return matrix[row];
  100. }
  101. void matrix_print(void)
  102. {
  103. print("\nr/c 0123456789ABCDEF\n");
  104. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  105. phex(row); print(": ");
  106. pbin_reverse16(matrix_get_row(row));
  107. print("\n");
  108. }
  109. }
  110. uint8_t matrix_key_count(void)
  111. {
  112. uint8_t count = 0;
  113. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  114. count += bitpop16(matrix[i]);
  115. }
  116. return count;
  117. }
  118. /* Column pin configuration
  119. * col: 0 1 2 3 4 5 6 7 8 9 10 11
  120. * pin: B5, B6, B7, D6, F7, F6, F5, F4, F1, F0, B3, B1
  121. */
  122. static void init_cols(void)
  123. {
  124. // Input with pull-up(DDR:0, PORT:1)
  125. DDRB &= ~(1<<1 | 1<<3 | 1<<5 | 1<<6 | 1<<7);
  126. PORTB |= (1<<1 | 1<<3 | 1<<5 | 1<<6 | 1<<7);
  127. DDRD &= ~(1<<6);
  128. PORTD |= (1<<6);
  129. DDRF &= ~(1<<0 | 1<<1 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
  130. PORTF |= (1<<0 | 1<<1 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
  131. }
  132. static matrix_row_t read_cols(void)
  133. {
  134. return (PINB&(1<<5) ? 0 : (1<<0)) |
  135. (PINB&(1<<6) ? 0 : (1<<1)) |
  136. (PINB&(1<<7) ? 0 : (1<<2)) |
  137. (PIND&(1<<6) ? 0 : (1<<3)) |
  138. (PINF&(1<<7) ? 0 : (1<<4)) |
  139. (PINF&(1<<6) ? 0 : (1<<5)) |
  140. (PINF&(1<<5) ? 0 : (1<<6)) |
  141. (PINF&(1<<4) ? 0 : (1<<7)) |
  142. (PINF&(1<<1) ? 0 : (1<<8)) |
  143. (PINF&(1<<0) ? 0 : (1<<9)) |
  144. (PINB&(1<<3) ? 0 : (1<<10)) |
  145. (PINB&(1<<1) ? 0 : (1<<11));
  146. }
  147. /* Row pin configuration
  148. * row: 0 1 2 3 4 5 6 7 8 9 10 11
  149. * pin: B2, B0, D3, D2, D1, D0, D4, C6, D7, E6, B4, C7
  150. */
  151. static void unselect_rows(void)
  152. {
  153. // Hi-Z(DDR:0, PORT:0) to unselect
  154. DDRB &= ~0b00010101;
  155. PORTB &= ~0b00010101;
  156. DDRC &= ~0b11000000;
  157. PORTC &= ~0b11000000;
  158. DDRD &= ~0b10011111;
  159. PORTD &= ~0b10011111;
  160. DDRE &= ~0b01000000;
  161. PORTE &= ~0b01000000;
  162. }
  163. static void select_row(uint8_t row)
  164. {
  165. // Output low(DDR:1, PORT:0) to select
  166. switch (row) {
  167. case 0:
  168. DDRB |= (1<<2);
  169. PORTB &= ~(1<<2);
  170. break;
  171. case 1:
  172. DDRB |= (1<<0);
  173. PORTB &= ~(1<<0);
  174. break;
  175. case 2:
  176. DDRD |= (1<<3);
  177. PORTD &= ~(1<<3);
  178. break;
  179. case 3:
  180. DDRD |= (1<<2);
  181. PORTD &= ~(1<<2);
  182. break;
  183. case 4:
  184. DDRD |= (1<<1);
  185. PORTD &= ~(1<<1);
  186. break;
  187. case 5:
  188. DDRD |= (1<<0);
  189. PORTD &= ~(1<<0);
  190. break;
  191. case 6:
  192. DDRD |= (1<<4);
  193. PORTD &= ~(1<<4);
  194. break;
  195. case 7:
  196. DDRC |= (1<<6);
  197. PORTC &= ~(1<<6);
  198. break;
  199. case 8:
  200. DDRD |= (1<<7);
  201. PORTD &= ~(1<<7);
  202. break;
  203. case 9:
  204. DDRE |= (1<<6);
  205. PORTE &= ~(1<<6);
  206. break;
  207. case 10:
  208. DDRB |= (1<<4);
  209. PORTB &= ~(1<<4);
  210. break;
  211. case 11:
  212. DDRC |= (1<<7);
  213. PORTC &= ~(1<<7);
  214. break;
  215. }
  216. }