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 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include <avr/interrupt.h>
  2. #include <avr/pgmspace.h>
  3. #include "usb_keyboard.h"
  4. #include "print.h"
  5. #include "debug.h"
  6. static bool is_sent = false;
  7. // which modifier keys are currently pressed
  8. // 1=left ctrl, 2=left shift, 4=left alt, 8=left gui
  9. // 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
  10. uint8_t keyboard_modifier_keys=0;
  11. // which keys are currently pressed, up to 6 keys may be down at once
  12. uint8_t keyboard_keys[6]={0,0,0,0,0,0};
  13. // protocol setting from the host. We use exactly the same report
  14. // either way, so this variable only stores the setting since we
  15. // are required to be able to report which setting is in use.
  16. uint8_t keyboard_protocol=1;
  17. // the idle configuration, how often we send the report to the
  18. // host (ms * 4) even when it hasn't changed
  19. uint8_t keyboard_idle_config=125;
  20. // count until idle timeout
  21. uint8_t keyboard_idle_count=0;
  22. // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
  23. volatile uint8_t keyboard_leds=0;
  24. // perform a single keystroke
  25. int8_t usb_keyboard_press(uint8_t key, uint8_t modifier)
  26. {
  27. int8_t r;
  28. keyboard_modifier_keys = modifier;
  29. keyboard_keys[0] = key;
  30. r = usb_keyboard_send();
  31. if (r) return r;
  32. keyboard_modifier_keys = 0;
  33. keyboard_keys[0] = 0;
  34. return usb_keyboard_send();
  35. }
  36. // send the contents of keyboard_keys and keyboard_modifier_keys
  37. int8_t usb_keyboard_send(void)
  38. {
  39. uint8_t i, intr_state, timeout;
  40. if (!usb_configured()) return -1;
  41. intr_state = SREG;
  42. cli();
  43. UENUM = KEYBOARD_ENDPOINT;
  44. timeout = UDFNUML + 50;
  45. while (1) {
  46. // are we ready to transmit?
  47. if (UEINTX & (1<<RWAL)) break;
  48. SREG = intr_state;
  49. // has the USB gone offline?
  50. if (!usb_configured()) return -1;
  51. // have we waited too long?
  52. if (UDFNUML == timeout) return -1;
  53. // get ready to try checking again
  54. intr_state = SREG;
  55. cli();
  56. UENUM = KEYBOARD_ENDPOINT;
  57. }
  58. UEDATX = keyboard_modifier_keys;
  59. UEDATX = 0;
  60. for (i=0; i<6; i++) {
  61. UEDATX = keyboard_keys[i];
  62. }
  63. UEINTX = 0x3A;
  64. keyboard_idle_count = 0;
  65. SREG = intr_state;
  66. is_sent = true;
  67. return 0;
  68. }
  69. void usb_keyboard_init(void) {
  70. usb_keyboard_clear();
  71. is_sent = false;
  72. }
  73. void usb_keyboard_clear(void) {
  74. usb_keyboard_clear_key();
  75. usb_keyboard_clear_mod();
  76. }
  77. void usb_keyboard_clear_key(void) {
  78. for (int i = 0; i < 6; i++) keyboard_keys[i] = 0;
  79. }
  80. void usb_keyboard_clear_mod(void) {
  81. keyboard_modifier_keys = 0;
  82. }
  83. bool usb_keyboard_is_sent(void) {
  84. return is_sent;
  85. }
  86. bool usb_keyboard_has_key(void) {
  87. uint8_t keys = 0;
  88. for (int i = 0; i < 6; i++) keys |= keyboard_keys[i];
  89. return keys ? true : false;
  90. }
  91. bool usb_keyboard_has_mod(void) {
  92. return keyboard_modifier_keys ? true : false;
  93. }
  94. void usb_keyboard_print(void) {
  95. if (!debug_keyboard) return;
  96. print("\nkeys: ");
  97. for (int i = 0; i < 6; i++) { phex(keyboard_keys[i]); print(" "); }
  98. print("\n");
  99. print("mods: "); phex(keyboard_modifier_keys); print("\n");
  100. }