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.

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