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

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