Keyboard firmwares for Atmel AVR and Cortex-M
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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