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.

matrix.c 3.7KB

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