Keyboard firmwares for Atmel AVR and Cortex-M
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

matrix.c 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. Copyright 2011 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. #if (MATRIX_COLS > 16)
  26. # error "MATRIX_COLS must not exceed 16"
  27. #endif
  28. #if (MATRIX_ROWS > 255)
  29. # error "MATRIX_ROWS must not exceed 255"
  30. #endif
  31. #ifndef DEBOUNCE
  32. # define DEBOUNCE 0
  33. #endif
  34. static uint8_t debouncing = DEBOUNCE;
  35. // matrix state buffer(1:on, 0:off)
  36. #if (MATRIX_COLS <= 8)
  37. static uint8_t *matrix;
  38. static uint8_t *matrix_prev;
  39. static uint8_t _matrix0[MATRIX_ROWS];
  40. static uint8_t _matrix1[MATRIX_ROWS];
  41. #else
  42. static uint16_t *matrix;
  43. static uint16_t *matrix_prev;
  44. static uint16_t _matrix0[MATRIX_ROWS];
  45. static uint16_t _matrix1[MATRIX_ROWS];
  46. #endif
  47. #ifdef MATRIX_HAS_GHOST
  48. static bool matrix_has_ghost_in_row(uint8_t row);
  49. #endif
  50. static uint8_t read_col(uint8_t row);
  51. static void unselect_rows(void);
  52. static void select_row(uint8_t row);
  53. inline
  54. uint8_t matrix_rows(void)
  55. {
  56. return MATRIX_ROWS;
  57. }
  58. inline
  59. uint8_t matrix_cols(void)
  60. {
  61. return MATRIX_COLS;
  62. }
  63. void matrix_init(void)
  64. {
  65. // initialize row and col
  66. unselect_rows();
  67. // Input with pull-up(DDR:0, PORT:1)
  68. // Column C1 ~ C7 (PortC0-6)
  69. // Column C0(Port E1)
  70. DDRC &= ~0b01111111;
  71. PORTC |= 0b01111111;
  72. DDRE &= ~0b00000010;
  73. PORTE |= 0b00000010;
  74. // modifier B3/4,F4/5,E4 always input
  75. DDRB |= 0b00011000;
  76. PORTB &= 0b00011000;
  77. DDRF |= ~0b00110000;
  78. PORTF &= 0b00110000;
  79. //DDRB &= ~0b00011000;
  80. //PORTB |= 0b00011000;
  81. //DDRF &= ~0b00110000;
  82. //PORTF |= 0b00110000;
  83. //DDRE &= ~0b00010000;
  84. //PORTE |= 0b00010000;
  85. // initialize matrix state: all keys off
  86. for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
  87. for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
  88. matrix = _matrix0;
  89. matrix_prev = _matrix1;
  90. }
  91. uint8_t matrix_scan(void)
  92. {
  93. if (!debouncing) {
  94. uint8_t *tmp = matrix_prev;
  95. matrix_prev = matrix;
  96. matrix = tmp;
  97. }
  98. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  99. unselect_rows();
  100. select_row(i);
  101. _delay_us(30); // without this wait read unstable value.
  102. if (matrix[i] != (uint8_t)~read_col(i)) {
  103. matrix[i] = (uint8_t)~read_col(i);
  104. if (debouncing) {
  105. debug("bounce!: "); debug_hex(debouncing); print("\n");
  106. }
  107. debouncing = DEBOUNCE;
  108. }
  109. }
  110. unselect_rows();
  111. if (debouncing) {
  112. debouncing--;
  113. }
  114. return 1;
  115. }
  116. bool matrix_is_modified(void)
  117. {
  118. if (debouncing) return false;
  119. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  120. if (matrix[i] != matrix_prev[i]) {
  121. return true;
  122. }
  123. }
  124. return false;
  125. }
  126. inline
  127. bool matrix_has_ghost(void)
  128. {
  129. #ifdef MATRIX_HAS_GHOST
  130. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  131. if (matrix_has_ghost_in_row(i))
  132. return true;
  133. }
  134. #endif
  135. return false;
  136. }
  137. inline
  138. bool matrix_is_on(uint8_t row, uint8_t col)
  139. {
  140. return (matrix[row] & (1<<col));
  141. }
  142. inline
  143. #if (MATRIX_COLS <= 8)
  144. uint8_t matrix_get_row(uint8_t row)
  145. #else
  146. uint16_t matrix_get_row(uint8_t row)
  147. #endif
  148. {
  149. return matrix[row];
  150. }
  151. void matrix_print(void)
  152. {
  153. print("\nr/c 01234567\n");
  154. for (uint8_t row = 0; row < matrix_rows(); row++) {
  155. phex(row); print(": ");
  156. #if (MATRIX_COLS <= 8)
  157. pbin_reverse(matrix_get_row(row));
  158. #else
  159. pbin_reverse16(matrix_get_row(row));
  160. #endif
  161. #ifdef MATRIX_HAS_GHOST
  162. if (matrix_has_ghost_in_row(row)) {
  163. print(" <ghost");
  164. }
  165. #endif
  166. print("\n");
  167. }
  168. }
  169. uint8_t matrix_key_count(void)
  170. {
  171. uint8_t count = 0;
  172. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  173. #if (MATRIX_COLS <= 8)
  174. count += bitpop(matrix[i]);
  175. #else
  176. count += bitpop16(matrix[i]);
  177. #endif
  178. }
  179. return count;
  180. }
  181. #ifdef MATRIX_HAS_GHOST
  182. inline
  183. static bool matrix_has_ghost_in_row(uint8_t row)
  184. {
  185. // no ghost exists in case less than 2 keys on
  186. if (((matrix[row] - 1) & matrix[row]) == 0)
  187. return false;
  188. // ghost exists in case same state as other row
  189. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  190. if (i != row && (matrix[i] & matrix[row]) == matrix[row])
  191. return true;
  192. }
  193. return false;
  194. }
  195. #endif
  196. inline
  197. static uint8_t read_col(uint8_t row)
  198. {
  199. // For normal : Column C1 ~ C7 (PortC0-6), C0(Port E1)
  200. // For modifier : B3(CNTRL)/4(SHIFT),F4(CMD/GUI)/5(OPTION,ALT)
  201. // Modifier would be copied to report->mods except E4(CAPSLOCK)
  202. uint8_t tmp;
  203. if ( row == 10 ) {
  204. tmp = 0xF0;
  205. tmp |= (PINB >> 3 ) & 0b00000011; // LEFT CTRL is 0bit in modifier (HID Spec)
  206. // LEFT SHIFT is 1bit in modifier (HID Spec)
  207. tmp |= (PINF >> 3 ) & 0b00000100; // LEFT ALT is 2bit in modifier (HID Spec)
  208. tmp |= (PINF >> 1 ) & 0b00001000; // LEFT GUI is 3bit in modifier (HID Spec)
  209. //tmp |= (PINE << 1 ) & 0b00010000; // Caps Lock(Should not be in modifier
  210. } else {
  211. tmp = 0x00;
  212. tmp = (PINE >> 1)&0b00000001;
  213. tmp |= PINC << 1 ;
  214. }
  215. return tmp;
  216. }
  217. inline
  218. static void unselect_rows(void)
  219. {
  220. // Hi-Z(DDR:0, PORT:0) to unselect
  221. // DDR : 1, output 0, input
  222. DDRB &= ~0b00000011; // PB: 1,0
  223. PORTB &= ~0b00000011;
  224. DDRD &= ~0b00010000; // PD: 4
  225. PORTD &= ~0b00010000;
  226. DDRE &= ~0b11000000; // PE: 7,6
  227. PORTE &= ~0b11000000;
  228. DDRF &= ~0b11000111; // PF: 7,6,2,1,0
  229. PORTF &= ~0b11000111;
  230. // to unselect virtual row(modifier), set port to output with low
  231. DDRB |= 0b00011000; // PB: 3,4 for modifier(row10)
  232. PORTB &= ~0b00011000;
  233. DDRF |= 0b00110000; // PF: 4,5 for modifier
  234. PORTF &= ~0b00110000;
  235. }
  236. inline
  237. static void select_row(uint8_t row)
  238. {
  239. // Output low(DDR:1, PORT:0) to select
  240. // with row enable, column could send low to AVR when pressed
  241. // row: 0 1 2 3 4 5 6 7 8 9
  242. // pin: PB1, PB0, PE7, PE6, PD4, PF2, PF0, PF1, PF6 PF7
  243. switch (row) {
  244. case 0:
  245. DDRB |= (1<<1);
  246. PORTB &= ~(1<<1);
  247. break;
  248. case 1:
  249. DDRB |= (1<<0);
  250. PORTB &= ~(1<<0);
  251. break;
  252. case 2:
  253. DDRE |= (1<<7);
  254. PORTE &= ~(1<<7);
  255. break;
  256. case 3:
  257. DDRE |= (1<<6);
  258. PORTE &= ~(1<<6);
  259. break;
  260. case 4:
  261. DDRD |= (1<<4);
  262. PORTD &= ~(1<<4);
  263. break;
  264. case 5:
  265. DDRF |= (1<<2);
  266. PORTF &= ~(1<<2);
  267. break;
  268. case 6:
  269. DDRF |= (1<<0);
  270. PORTF &= ~(1<<0);
  271. break;
  272. case 7:
  273. DDRF |= (1<<1);
  274. PORTF &= ~(1<<1);
  275. break;
  276. case 8:
  277. DDRF |= (1<<6);
  278. PORTF &= ~(1<<6);
  279. break;
  280. case 9:
  281. DDRF |= (1<<7);
  282. PORTF &= ~(1<<7);
  283. break;
  284. case 10:
  285. // modifier has no row enable
  286. // to select virtual row, set port as input
  287. DDRB &= ~0b00011000;
  288. PORTB |= 0b00011000;
  289. DDRF &= ~0b00110000;
  290. PORTF |= 0b00110000;
  291. break;
  292. }
  293. }