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.

usb_keyboard.c.bak 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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_back = &_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. timeout = UDFNUML + 50;
  32. do {
  33. intr_state = SREG;
  34. cli();
  35. UENUM = KEYBOARD_ENDPOINT;
  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. } while (1);
  44. UEDATX = report->mods;
  45. UEDATX = 0;
  46. for (i = 0; i < 6; i++) {
  47. UEDATX = report->keys[i];
  48. }
  49. UEINTX = 0x3A;
  50. /*
  51. timeout = UDFNUML + 50;
  52. do {
  53. intr_state = SREG;
  54. cli();
  55. UENUM = KEYBOARD_ENDPOINT2;
  56. // are we ready to transmit?
  57. if (UEINTX & (1<<RWAL)) break;
  58. SREG = intr_state;
  59. // has the USB gone offline?
  60. if (!usb_configured()) return -1;
  61. // have we waited too long?
  62. if (UDFNUML == timeout) return -1;
  63. } while (1);
  64. UEDATX = 0;
  65. UEDATX = 0;
  66. for (i = 6; i < 12; i++) {
  67. UEDATX = report->keys[i];
  68. }
  69. UEINTX = 0x3A;
  70. */
  71. usb_keyboard_idle_count = 0;
  72. report->is_sent =true;
  73. usb_keyboard_print_report(report);
  74. return 0;
  75. }
  76. void usb_keyboard_swap_report(void) {
  77. usb_keyboard_report_t *tmp = usb_keyboard_report_back;
  78. usb_keyboard_report_back = usb_keyboard_report;
  79. usb_keyboard_report = tmp;
  80. }
  81. void usb_keyboard_clear_report(void) {
  82. usb_keyboard_clear_keys();
  83. usb_keyboard_clear_mods();
  84. usb_keyboard_report->is_sent = false;
  85. }
  86. void usb_keyboard_clear_keys(void) {
  87. for (int i = 0; i < KEYBOARD_REPORT_MAX; i++) usb_keyboard_report->keys[i] = 0;
  88. }
  89. void usb_keyboard_clear_mods(void)
  90. {
  91. usb_keyboard_report->mods = 0;
  92. }
  93. void usb_keyboard_add_code(uint8_t code)
  94. {
  95. if (IS_MOD(code)) {
  96. usb_keyboard_add_mod(code);
  97. } else {
  98. usb_keyboard_add_key(code);
  99. }
  100. }
  101. void usb_keyboard_add_key(uint8_t code)
  102. {
  103. for (int i = 0; i < KEYBOARD_REPORT_MAX; i++) {
  104. if (!usb_keyboard_report->keys[i]) {
  105. usb_keyboard_report->keys[i] = code;
  106. return;
  107. }
  108. }
  109. }
  110. void usb_keyboard_set_keys(uint8_t *keys)
  111. {
  112. for (int i = 0; i < KEYBOARD_REPORT_MAX; i++)
  113. usb_keyboard_report->keys[i] = keys[i];
  114. }
  115. void usb_keyboard_set_mods(uint8_t mods)
  116. {
  117. usb_keyboard_report->mods = mods;
  118. }
  119. void usb_keyboard_add_mod(uint8_t code)
  120. {
  121. usb_keyboard_report->mods |= MOD_BIT(code);
  122. }
  123. void usb_keyboard_del_code(uint8_t code)
  124. {
  125. if (IS_MOD(code)) {
  126. usb_keyboard_del_mod(code);
  127. } else {
  128. usb_keyboard_del_key(code);
  129. }
  130. }
  131. void usb_keyboard_del_key(uint8_t code)
  132. {
  133. for (int i = 0; i < KEYBOARD_REPORT_MAX; i++) {
  134. if (usb_keyboard_report->keys[i] == code) {
  135. usb_keyboard_report->keys[i] = KB_NO;
  136. return;
  137. }
  138. }
  139. }
  140. void usb_keyboard_del_mod(uint8_t code)
  141. {
  142. usb_keyboard_report->mods &= ~MOD_BIT(code);
  143. }
  144. bool usb_keyboard_is_sent(void)
  145. {
  146. return usb_keyboard_report->is_sent;
  147. }
  148. bool usb_keyboard_has_key(void)
  149. {
  150. uint8_t keys = 0;
  151. for (int i = 0; i < KEYBOARD_REPORT_MAX; i++) keys |= usb_keyboard_report->keys[i];
  152. return keys ? true : false;
  153. }
  154. bool usb_keyboard_has_mod(void)
  155. {
  156. return usb_keyboard_report->mods ? true : false;
  157. }
  158. void usb_keyboard_print_report(usb_keyboard_report_t *report)
  159. {
  160. if (!debug_keyboard) return;
  161. print("keys: ");
  162. for (int i = 0; i < KEYBOARD_REPORT_MAX; i++) { phex(report->keys[i]); print(" "); }
  163. print(" mods: "); phex(report->mods); print("\n");
  164. }