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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

matrix.c 8.8KB

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