Keyboard firmwares for Atmel AVR and Cortex-M
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

usb_keyboard.c 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include <avr/interrupt.h>
  2. #include <avr/pgmspace.h>
  3. #include "usb_keycodes.h"
  4. #include "usb_keyboard.h"
  5. #include "print.h"
  6. #include "debug.h"
  7. // keyboard report.
  8. static usb_keyboard_report_t _report0 = { {0}, 0 };
  9. static usb_keyboard_report_t _report1 = { {0}, 0 };
  10. usb_keyboard_report_t *usb_keyboard_report = &_report0;
  11. usb_keyboard_report_t *usb_keyboard_report_prev = &_report1;
  12. // protocol setting from the host. We use exactly the same report
  13. // either way, so this variable only stores the setting since we
  14. // are required to be able to report which setting is in use.
  15. uint8_t usb_keyboard_protocol=1;
  16. // the idle configuration, how often we send the report to the
  17. // host (ms * 4) even when it hasn't changed
  18. uint8_t usb_keyboard_idle_config=125;
  19. // count until idle timeout
  20. uint8_t usb_keyboard_idle_count=0;
  21. // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
  22. volatile uint8_t usb_keyboard_leds=0;
  23. int8_t usb_keyboard_send(void)
  24. {
  25. return usb_keyboard_send_report(usb_keyboard_report);
  26. }
  27. int8_t usb_keyboard_send_report(usb_keyboard_report_t *report)
  28. {
  29. uint8_t i, intr_state, timeout;
  30. if (!usb_configured()) return -1;
  31. intr_state = SREG;
  32. cli();
  33. UENUM = KEYBOARD_ENDPOINT;
  34. timeout = UDFNUML + 50;
  35. while (1) {
  36. // are we ready to transmit?
  37. if (UEINTX & (1<<RWAL)) break;
  38. SREG = intr_state;
  39. // has the USB gone offline?
  40. if (!usb_configured()) return -1;
  41. // have we waited too long?
  42. if (UDFNUML == timeout) return -1;
  43. // get ready to try checking again
  44. intr_state = SREG;
  45. cli();
  46. UENUM = KEYBOARD_ENDPOINT;
  47. }
  48. UEDATX = report->mods;
  49. UEDATX = 0;
  50. for (i = 0; i < 6; i++) {
  51. UEDATX = report->keys[i];
  52. }
  53. UEINTX = 0x3A;
  54. SREG = intr_state;
  55. if (!usb_configured()) return -1;
  56. intr_state = SREG;
  57. cli();
  58. UENUM = KEYBOARD_ENDPOINT2;
  59. timeout = UDFNUML + 50;
  60. while (1) {
  61. // are we ready to transmit?
  62. if (UEINTX & (1<<RWAL)) break;
  63. SREG = intr_state;
  64. // has the USB gone offline?
  65. if (!usb_configured()) return -1;
  66. // have we waited too long?
  67. if (UDFNUML == timeout) return -1;
  68. // get ready to try checking again
  69. intr_state = SREG;
  70. cli();
  71. UENUM = KEYBOARD_ENDPOINT2;
  72. }
  73. UEDATX = 0;
  74. UEDATX = 0;
  75. for (i = 6; i < 12; i++) {
  76. UEDATX = report->keys[i];
  77. }
  78. UEINTX = 0x3A;
  79. SREG = intr_state;
  80. usb_keyboard_idle_count = 0;
  81. report->is_sent =true;
  82. usb_keyboard_print_report(report);
  83. return 0;
  84. }
  85. void usb_keyboard_swap_report(void) {
  86. usb_keyboard_report_t *tmp = usb_keyboard_report_prev;
  87. usb_keyboard_report_prev = usb_keyboard_report;
  88. usb_keyboard_report = tmp;
  89. }
  90. void usb_keyboard_clear_report(void) {
  91. usb_keyboard_clear_keys();
  92. usb_keyboard_clear_mods();
  93. usb_keyboard_report->is_sent = false;
  94. }
  95. void usb_keyboard_clear_keys(void) {
  96. for (int i = 0; i < KEYBOARD_REPORT_MAX; i++) usb_keyboard_report->keys[i] = 0;
  97. }
  98. void usb_keyboard_clear_mods(void)
  99. {
  100. usb_keyboard_report->mods = 0;
  101. }
  102. void usb_keyboard_add_code(uint8_t code)
  103. {
  104. if (IS_MOD(code)) {
  105. usb_keyboard_add_mod(code);
  106. } else {
  107. usb_keyboard_add_key(code);
  108. }
  109. }
  110. void usb_keyboard_add_key(uint8_t code)
  111. {
  112. for (int i = 0; i < KEYBOARD_REPORT_MAX; i++) {
  113. if (!usb_keyboard_report->keys[i]) {
  114. usb_keyboard_report->keys[i] = code;
  115. return;
  116. }
  117. }
  118. }
  119. void usb_keyboard_set_keys(uint8_t *keys)
  120. {
  121. for (int i = 0; i < KEYBOARD_REPORT_MAX; i++)
  122. usb_keyboard_report->keys[i] = keys[i];
  123. }
  124. void usb_keyboard_set_mods(uint8_t mods)
  125. {
  126. usb_keyboard_report->mods = mods;
  127. }
  128. void usb_keyboard_add_mod(uint8_t code)
  129. {
  130. usb_keyboard_report->mods |= MOD_BIT(code);
  131. }
  132. void usb_keyboard_del_code(uint8_t code)
  133. {
  134. if (IS_MOD(code)) {
  135. usb_keyboard_del_mod(code);
  136. } else {
  137. usb_keyboard_del_key(code);
  138. }
  139. }
  140. void usb_keyboard_del_key(uint8_t code)
  141. {
  142. for (int i = 0; i < KEYBOARD_REPORT_MAX; i++) {
  143. if (usb_keyboard_report->keys[i] == code) {
  144. usb_keyboard_report->keys[i] = KB_NO;
  145. return;
  146. }
  147. }
  148. }
  149. void usb_keyboard_del_mod(uint8_t code)
  150. {
  151. usb_keyboard_report->mods &= ~MOD_BIT(code);
  152. }
  153. bool usb_keyboard_is_sent(void)
  154. {
  155. return usb_keyboard_report->is_sent;
  156. }
  157. bool usb_keyboard_has_key(void)
  158. {
  159. uint8_t keys = 0;
  160. for (int i = 0; i < KEYBOARD_REPORT_MAX; i++) keys |= usb_keyboard_report->keys[i];
  161. return keys ? true : false;
  162. }
  163. bool usb_keyboard_has_mod(void)
  164. {
  165. return usb_keyboard_report->mods ? true : false;
  166. }
  167. void usb_keyboard_print_report(usb_keyboard_report_t *report)
  168. {
  169. if (!debug_keyboard) return;
  170. print("keys: ");
  171. for (int i = 0; i < KEYBOARD_REPORT_MAX; i++) { phex(report->keys[i]); print(" "); }
  172. print(" mods: "); phex(report->mods); print("\n");
  173. }