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.

пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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(void);
  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. DDRB = 0x00;
  69. PORTB = 0xFF;
  70. // initialize matrix state: all keys off
  71. for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
  72. for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
  73. matrix = _matrix0;
  74. matrix_prev = _matrix1;
  75. }
  76. uint8_t matrix_scan(void)
  77. {
  78. if (!debouncing) {
  79. uint8_t *tmp = matrix_prev;
  80. matrix_prev = matrix;
  81. matrix = tmp;
  82. }
  83. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  84. unselect_rows();
  85. select_row(i);
  86. _delay_us(30); // without this wait read unstable value.
  87. if (matrix[i] != (uint8_t)~read_col()) {
  88. matrix[i] = (uint8_t)~read_col();
  89. if (debouncing) {
  90. debug("bounce!: "); debug_hex(debouncing); print("\n");
  91. }
  92. debouncing = DEBOUNCE;
  93. }
  94. }
  95. unselect_rows();
  96. if (debouncing) {
  97. debouncing--;
  98. }
  99. return 1;
  100. }
  101. bool matrix_is_modified(void)
  102. {
  103. if (debouncing) return false;
  104. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  105. if (matrix[i] != matrix_prev[i]) {
  106. return true;
  107. }
  108. }
  109. return false;
  110. }
  111. inline
  112. bool matrix_has_ghost(void)
  113. {
  114. #ifdef MATRIX_HAS_GHOST
  115. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  116. if (matrix_has_ghost_in_row(i))
  117. return true;
  118. }
  119. #endif
  120. return false;
  121. }
  122. inline
  123. bool matrix_is_on(uint8_t row, uint8_t col)
  124. {
  125. return (matrix[row] & (1<<col));
  126. }
  127. inline
  128. #if (MATRIX_COLS <= 8)
  129. uint8_t matrix_get_row(uint8_t row)
  130. #else
  131. uint16_t matrix_get_row(uint8_t row)
  132. #endif
  133. {
  134. return matrix[row];
  135. }
  136. void matrix_print(void)
  137. {
  138. print("\nr/c 01234567\n");
  139. for (uint8_t row = 0; row < matrix_rows(); row++) {
  140. phex(row); print(": ");
  141. #if (MATRIX_COLS <= 8)
  142. pbin_reverse(matrix_get_row(row));
  143. #else
  144. pbin_reverse16(matrix_get_row(row));
  145. #endif
  146. #ifdef MATRIX_HAS_GHOST
  147. if (matrix_has_ghost_in_row(row)) {
  148. print(" <ghost");
  149. }
  150. #endif
  151. print("\n");
  152. }
  153. }
  154. uint8_t matrix_key_count(void)
  155. {
  156. uint8_t count = 0;
  157. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  158. #if (MATRIX_COLS <= 8)
  159. count += bitpop(matrix[i]);
  160. #else
  161. count += bitpop16(matrix[i]);
  162. #endif
  163. }
  164. return count;
  165. }
  166. #ifdef MATRIX_HAS_GHOST
  167. inline
  168. static bool matrix_has_ghost_in_row(uint8_t row)
  169. {
  170. // no ghost exists in case less than 2 keys on
  171. if (((matrix[row] - 1) & matrix[row]) == 0)
  172. return false;
  173. // ghost exists in case same state as other row
  174. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  175. if (i != row && (matrix[i] & matrix[row]) == matrix[row])
  176. return true;
  177. }
  178. return false;
  179. }
  180. #endif
  181. inline
  182. static uint8_t read_col(void)
  183. {
  184. return PINB;
  185. }
  186. inline
  187. static void unselect_rows(void)
  188. {
  189. // Hi-Z(DDR:0, PORT:0) to unselect
  190. DDRC &= ~0b11000000; // PC: 7,6
  191. PORTC &= ~0b11000000;
  192. DDRD &= ~0b11000111; // PD: 7,6,2,1,0
  193. PORTD &= ~0b11000111;
  194. DDRF &= ~0b11000000; // PF: 7,6
  195. PORTF &= ~0b11000000;
  196. }
  197. inline
  198. static void select_row(uint8_t row)
  199. {
  200. // Output low(DDR:1, PORT:0) to select
  201. // row: 0 1 2 3 4 5 6 7 8
  202. // pin: PD0, PC7, PD7, PF6, PD6, PD1, PD2, PC6, PF7
  203. switch (row) {
  204. case 0:
  205. DDRD |= (1<<0);
  206. PORTD &= ~(1<<0);
  207. break;
  208. case 1:
  209. DDRC |= (1<<7);
  210. PORTC &= ~(1<<7);
  211. break;
  212. case 2:
  213. DDRD |= (1<<7);
  214. PORTD &= ~(1<<7);
  215. break;
  216. case 3:
  217. DDRF |= (1<<6);
  218. PORTF &= ~(1<<6);
  219. break;
  220. case 4:
  221. DDRD |= (1<<6);
  222. PORTD &= ~(1<<6);
  223. break;
  224. case 5:
  225. DDRD |= (1<<1);
  226. PORTD &= ~(1<<1);
  227. break;
  228. case 6:
  229. DDRD |= (1<<2);
  230. PORTD &= ~(1<<2);
  231. break;
  232. case 7:
  233. DDRC |= (1<<6);
  234. PORTC &= ~(1<<6);
  235. break;
  236. case 8:
  237. DDRF |= (1<<7);
  238. PORTF &= ~(1<<7);
  239. break;
  240. }
  241. }