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.

host.c 2.6KB

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