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.9KB

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