USBKeyboardMiku.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**********************************************************************************************
  2. *
  3. * This library for the Arduino IDE adds HID USB keyboard functionality to your projects, it
  4. * allows Arduinos and other AVRs to act as an HID USB Device. This way no drivers have to be
  5. * installed, the keyboard will work with every PC and OS.
  6. * It is based the V-USB code by Objective Developement (https://www.obdev.at/products/vusb/)
  7. * and under the same license (GNU GPLv3, see LICENSE.txt).
  8. *
  9. *********************************************************************************************/
  10. #ifndef __USBKeyboard_h__
  11. #define __USBKeyboard_h__
  12. #include <avr/pgmspace.h>
  13. #include <avr/interrupt.h>
  14. #include <string.h>
  15. #include <usbdrv.h>
  16. #include <Arduino.h>
  17. #include <keycodes.h>
  18. /* constants */
  19. #define MODIFIER_CONTROL (1<<0)
  20. #define MODIFIER_SHIFT (1<<1)
  21. #define MODIFIER_ALT (1<<2)
  22. #define MODIFIER_GUI (1<<3)
  23. #ifndef LAYOUT
  24. #define LAYOUT LAYOUT_US
  25. #endif
  26. /* makros */
  27. #define conc(a, b) (a ## b)
  28. #define concat(a, b) conc(a, b)
  29. /* global variables */
  30. static uint8_t idle_rate = 500 / 4; /* see HID1_11.pdf sect 7.2.4 */
  31. static uint8_t protocol_version = 0; /* see HID1_11.pdf sect 7.2.6 */
  32. volatile static uint8_t LED_states = 0; /* see HID1_11.pdf appendix B section 1 */
  33. volatile static uint8_t toggle_counter = 0; /* keep track of how many times caps lock have toggled */
  34. static uint8_t keyboard_layout = 0; /* keyboard layout, US layout by standard */
  35. class USBKeyboard : public Print {
  36. public: /*#################### PUBLIC FUNCTIONS ####################*/
  37. /*******************************************************
  38. * Constructor, call it when initializing the library
  39. ******************************************************/
  40. USBKeyboard();
  41. USBKeyboard(uint8_t layout);
  42. /*******************************************************
  43. * Call this function at least every 20ms,
  44. * otherwise an USB timeout will occur
  45. ******************************************************/
  46. void update();
  47. /*******************************************************
  48. * Type a single char to the USB host
  49. ******************************************************/
  50. virtual size_t write(uint8_t ascii);
  51. /*******************************************************
  52. * Send a single key to the USB host
  53. ******************************************************/
  54. void sendKey(uint8_t keycode) {sendKey(keycode, 0);}
  55. void sendKey(uint8_t keycode, uint8_t modifiers);
  56. /*******************************************************
  57. * Send up to 6 keys to the USB host
  58. ******************************************************/
  59. void sendKeys(uint8_t keycode1, uint8_t keycode2, uint8_t keycode3, uint8_t keycode4, uint8_t keycode5, uint8_t keycode6) {sendKeys(keycode1, keycode2, keycode3, keycode4, keycode5, keycode6, 0);}
  60. void sendKeys(uint8_t keycode1, uint8_t keycode2, uint8_t keycode3, uint8_t keycode4, uint8_t keycode5, uint8_t keycode6, uint8_t modifiers);
  61. /*******************************************************
  62. * Translate ASCII char to keycode
  63. ******************************************************/
  64. uint8_t asciiToKeycode(char ascii);
  65. /*******************************************************
  66. * Translate ASCII char to Shift state, taking into
  67. * consideration the status of Caps Lock
  68. ******************************************************/
  69. uint8_t asciiToShiftState(char ascii);
  70. /*******************************************************
  71. * Get the state of Caps Lock
  72. ******************************************************/
  73. bool isCapsLockActivated();
  74. /*******************************************************
  75. * Get how often Caps Lock was toggled
  76. ******************************************************/
  77. uint8_t getCapsLockToggleCount();
  78. /*******************************************************
  79. * Reset the Caps Lock toggle counter
  80. ******************************************************/
  81. void resetCapsLockToggleCount();
  82. /*******************************************************
  83. * Send the keyboard report
  84. ******************************************************/
  85. void sendReport(uint8_t modifiers, uint8_t keycode1, uint8_t keycode2, uint8_t keycode3, uint8_t keycode4, uint8_t keycode5, uint8_t keycode6);
  86. };
  87. #endif /* __USBKeyboard_h__ */