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 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * scan matrix
  3. */
  4. #include <stdint.h>
  5. #include <stdbool.h>
  6. #include <avr/io.h>
  7. #include <util/delay.h>
  8. #include "print.h"
  9. #include "util.h"
  10. #include "debug.h"
  11. #include "adb.h"
  12. #include "usb_keyboard.h"
  13. #include "matrix_skel.h"
  14. #if (MATRIX_COLS > 16)
  15. # error "MATRIX_COLS must not exceed 16"
  16. #endif
  17. #if (MATRIX_ROWS > 255)
  18. # error "MATRIX_ROWS must not exceed 255"
  19. #endif
  20. static bool _matrix_is_modified = false;
  21. // matrix state buffer(1:on, 0:off)
  22. #if (MATRIX_COLS <= 8)
  23. static uint8_t *matrix;
  24. static uint8_t _matrix0[MATRIX_ROWS];
  25. #else
  26. static uint16_t *matrix;
  27. static uint16_t _matrix0[MATRIX_ROWS];
  28. #endif
  29. #ifdef MATRIX_HAS_GHOST
  30. static bool matrix_has_ghost_in_row(uint8_t row);
  31. #endif
  32. static void _register_key(uint8_t key);
  33. inline
  34. uint8_t matrix_rows(void)
  35. {
  36. return MATRIX_ROWS;
  37. }
  38. inline
  39. uint8_t matrix_cols(void)
  40. {
  41. return MATRIX_COLS;
  42. }
  43. void matrix_init(void)
  44. {
  45. adb_host_init();
  46. // initialize matrix state: all keys off
  47. for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
  48. matrix = _matrix0;
  49. print_enable = true;
  50. debug_enable = true;
  51. debug_matrix = true;
  52. debug_keyboard = true;
  53. debug_mouse = true;
  54. print("debug enabled.\n");
  55. return;
  56. }
  57. uint8_t matrix_scan(void)
  58. {
  59. uint16_t codes;
  60. uint8_t key0, key1;
  61. static uint8_t prev_led = 0;
  62. if (prev_led != usb_keyboard_leds) {
  63. adb_host_kbd_led(~usb_keyboard_leds);
  64. prev_led = usb_keyboard_leds;
  65. }
  66. _matrix_is_modified = false;
  67. codes = adb_host_kbd_recv();
  68. key0 = codes>>8;
  69. key1 = codes&0xFF;
  70. if (codes == 0) { // no keys
  71. return 0;
  72. } else if (key0 == 0xFF && key1 != 0xFF) { // error
  73. return codes&0xFF;
  74. } else {
  75. _matrix_is_modified = true;
  76. _register_key(key0);
  77. if (key1 != 0xFF) // key1 is 0xFF when no second key.
  78. _register_key(key1);
  79. }
  80. if (debug_matrix && matrix_is_modified()) {
  81. print("adb_host_kbd_recv: "); phex16(codes); print("\n");
  82. }
  83. return 1;
  84. }
  85. bool matrix_is_modified(void)
  86. {
  87. return _matrix_is_modified;
  88. }
  89. inline
  90. bool matrix_has_ghost(void)
  91. {
  92. #ifdef MATRIX_HAS_GHOST
  93. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  94. if (matrix_has_ghost_in_row(i))
  95. return true;
  96. }
  97. #endif
  98. return false;
  99. }
  100. inline
  101. bool matrix_is_on(uint8_t row, uint8_t col)
  102. {
  103. return (matrix[row] & (1<<col));
  104. }
  105. inline
  106. #if (MATRIX_COLS <= 8)
  107. uint8_t matrix_get_row(uint8_t row)
  108. #else
  109. uint16_t matrix_get_row(uint8_t row)
  110. #endif
  111. {
  112. return matrix[row];
  113. }
  114. void matrix_print(void)
  115. {
  116. #if (MATRIX_COLS <= 8)
  117. print("\nr/c 01234567\n");
  118. #else
  119. print("\nr/c 0123456789ABCDEF\n");
  120. #endif
  121. for (uint8_t row = 0; row < matrix_rows(); row++) {
  122. phex(row); print(": ");
  123. #if (MATRIX_COLS <= 8)
  124. pbin_reverse(matrix_get_row(row));
  125. #else
  126. pbin_reverse16(matrix_get_row(row));
  127. #endif
  128. #ifdef MATRIX_HAS_GHOST
  129. if (matrix_has_ghost_in_row(row)) {
  130. print(" <ghost");
  131. }
  132. #endif
  133. print("\n");
  134. }
  135. }
  136. uint8_t matrix_key_count(void)
  137. {
  138. uint8_t count = 0;
  139. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  140. #if (MATRIX_COLS <= 8)
  141. count += bitpop(matrix[i]);
  142. #else
  143. count += bitpop16(matrix[i]);
  144. #endif
  145. }
  146. return count;
  147. }
  148. #ifdef MATRIX_HAS_GHOST
  149. inline
  150. static bool matrix_has_ghost_in_row(uint8_t row)
  151. {
  152. // no ghost exists in case less than 2 keys on
  153. if (((matrix[row] - 1) & matrix[row]) == 0)
  154. return false;
  155. // ghost exists in case same state as other row
  156. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  157. if (i != row && (matrix[i] & matrix[row]) == matrix[row])
  158. return true;
  159. }
  160. return false;
  161. }
  162. #endif
  163. inline
  164. static void _register_key(uint8_t key)
  165. {
  166. uint8_t col, row;
  167. col = key&0x07;
  168. row = (key>>3)&0x0F;
  169. if (key&0x80) {
  170. matrix[row] &= ~(1<<col);
  171. } else {
  172. matrix[row] |= (1<<col);
  173. }
  174. }