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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include <stdint.h>
  2. #include "usb_keycodes.h"
  3. #include "usb_keyboard.h"
  4. #include "usb_mouse.h"
  5. #include "debug.h"
  6. #include "host.h"
  7. #include "util.h"
  8. #ifdef USB_NKRO_ENABLE
  9. bool keyboard_nkro = false;
  10. #endif
  11. static report_keyboard_t report0;
  12. static report_keyboard_t report1;
  13. report_keyboard_t *keyboard_report = &report0;
  14. report_keyboard_t *keyboard_report_prev = &report1;
  15. static inline void add_key_byte(uint8_t code);
  16. static inline void add_key_bit(uint8_t code);
  17. uint8_t host_keyboard_leds(void)
  18. {
  19. return usb_keyboard_leds;
  20. }
  21. /* keyboard report operations */
  22. void host_add_key(uint8_t key)
  23. {
  24. #ifdef USB_NKRO_ENABLE
  25. if (keyboard_nkro) {
  26. add_key_bit(key);
  27. return;
  28. }
  29. #endif
  30. add_key_byte(key);
  31. }
  32. void host_add_mod_bit(uint8_t mod)
  33. {
  34. keyboard_report->mods |= mod;
  35. }
  36. void host_set_mods(uint8_t mods)
  37. {
  38. keyboard_report->mods = mods;
  39. }
  40. void host_add_code(uint8_t code)
  41. {
  42. if (IS_MOD(code)) {
  43. host_add_mod_bit(MOD_BIT(code));
  44. } else {
  45. host_add_key(code);
  46. }
  47. }
  48. void host_swap_keyboard_report(void)
  49. {
  50. report_keyboard_t *tmp = keyboard_report_prev;
  51. keyboard_report_prev = keyboard_report;
  52. keyboard_report = tmp;
  53. }
  54. void host_clear_keyboard_report(void)
  55. {
  56. keyboard_report->mods = 0;
  57. for (int8_t i = 0; i < REPORT_KEYS; i++) {
  58. keyboard_report->keys[i] = 0;
  59. }
  60. }
  61. uint8_t host_has_anykey(void)
  62. {
  63. uint8_t cnt = 0;
  64. for (int i = 0; i < REPORT_KEYS; i++) {
  65. if (keyboard_report->keys[i])
  66. cnt++;
  67. }
  68. return cnt;
  69. }
  70. uint8_t host_get_first_key(void)
  71. {
  72. #ifdef USB_NKRO_ENABLE
  73. if (keyboard_nkro) {
  74. uint8_t i = 0;
  75. for (; i < REPORT_KEYS && !keyboard_report->keys[i]; i++)
  76. ;
  77. return i<<3 | biton(keyboard_report->keys[i]);
  78. }
  79. #endif
  80. return keyboard_report->keys[0];
  81. }
  82. void host_send_keyboard_report(void)
  83. {
  84. usb_keyboard_send_report(keyboard_report);
  85. }
  86. void host_mouse_send(report_mouse_t *report)
  87. {
  88. usb_mouse_send(report->x, report->y, report->v, report->h, report->buttons);
  89. }
  90. static inline void add_key_byte(uint8_t code)
  91. {
  92. // TODO: fix ugly code
  93. int8_t i = 0;
  94. int8_t empty = -1;
  95. for (; i < REPORT_KEYS; i++) {
  96. if (keyboard_report_prev->keys[i] == code) {
  97. keyboard_report->keys[i] = code;
  98. break;
  99. }
  100. if (empty == -1 &&
  101. keyboard_report_prev->keys[i] == 0 &&
  102. keyboard_report->keys[i] == 0) {
  103. empty = i;
  104. }
  105. }
  106. if (i == REPORT_KEYS) {
  107. if (empty != -1) {
  108. keyboard_report->keys[empty] = code;
  109. }
  110. }
  111. }
  112. static inline void add_key_bit(uint8_t code)
  113. {
  114. if ((code>>3) < REPORT_KEYS) {
  115. keyboard_report->keys[code>>3] |= 1<<(code&7);
  116. } else {
  117. debug("add_key_bit: can't add: "); phex(code); debug("\n");
  118. }
  119. }