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.

USBKeyboard.h 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* Copyright (c) 2010-2011 mbed.org, MIT License
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  4. * and associated documentation files (the "Software"), to deal in the Software without
  5. * restriction, including without limitation the rights to use, copy, modify, merge, publish,
  6. * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
  7. * Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or
  10. * substantial portions of the Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  13. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  14. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  15. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. */
  18. #ifndef USBKEYBOARD_H
  19. #define USBKEYBOARD_H
  20. #include "USBHID.h"
  21. #include "Stream.h"
  22. /* Modifiers */
  23. enum MODIFIER_KEY {
  24. KEY_CTRL = 1,
  25. KEY_SHIFT = 2,
  26. KEY_ALT = 4,
  27. };
  28. enum MEDIA_KEY {
  29. KEY_NEXT_TRACK, /*!< next Track Button */
  30. KEY_PREVIOUS_TRACK, /*!< Previous track Button */
  31. KEY_STOP, /*!< Stop Button */
  32. KEY_PLAY_PAUSE, /*!< Play/Pause Button */
  33. KEY_MUTE, /*!< Mute Button */
  34. KEY_VOLUME_UP, /*!< Volume Up Button */
  35. KEY_VOLUME_DOWN, /*!< Volume Down Button */
  36. };
  37. enum FUNCTION_KEY {
  38. KEY_F1 = 128, /* F1 key */
  39. KEY_F2, /* F2 key */
  40. KEY_F3, /* F3 key */
  41. KEY_F4, /* F4 key */
  42. KEY_F5, /* F5 key */
  43. KEY_F6, /* F6 key */
  44. KEY_F7, /* F7 key */
  45. KEY_F8, /* F8 key */
  46. KEY_F9, /* F9 key */
  47. KEY_F10, /* F10 key */
  48. KEY_F11, /* F11 key */
  49. KEY_F12, /* F12 key */
  50. KEY_PRINT_SCREEN, /* Print Screen key */
  51. KEY_SCROLL_LOCK, /* Scroll lock */
  52. KEY_CAPS_LOCK, /* caps lock */
  53. KEY_NUM_LOCK, /* num lock */
  54. KEY_INSERT, /* Insert key */
  55. KEY_HOME, /* Home key */
  56. KEY_PAGE_UP, /* Page Up key */
  57. KEY_PAGE_DOWN, /* Page Down key */
  58. RIGHT_ARROW, /* Right arrow */
  59. LEFT_ARROW, /* Left arrow */
  60. DOWN_ARROW, /* Down arrow */
  61. UP_ARROW, /* Up arrow */
  62. };
  63. /**
  64. * USBKeyboard example
  65. * @code
  66. *
  67. * #include "mbed.h"
  68. * #include "USBKeyboard.h"
  69. *
  70. * USBKeyboard key;
  71. *
  72. * int main(void)
  73. * {
  74. * while (1)
  75. * {
  76. * key.printf("Hello World\r\n");
  77. * wait(1);
  78. * }
  79. * }
  80. *
  81. * @endcode
  82. */
  83. class USBKeyboard: public USBHID, public Stream {
  84. public:
  85. /**
  86. * Constructor
  87. *
  88. *
  89. * @param leds Leds bus: first: NUM_LOCK, second: CAPS_LOCK, third: SCROLL_LOCK
  90. * @param vendor_id Your vendor_id (default: 0x1235)
  91. * @param product_id Your product_id (default: 0x0050)
  92. * @param product_release Your preoduct_release (default: 0x0001)
  93. *
  94. */
  95. USBKeyboard(uint16_t vendor_id = 0x1235, uint16_t product_id = 0x0050, uint16_t product_release = 0x0001):
  96. USBHID(0, 0, vendor_id, product_id, product_release, false) {
  97. lock_status = 0;
  98. connect();
  99. };
  100. /**
  101. * To send a character defined by a modifier(CTRL, SHIFT, ALT) and the key
  102. *
  103. * @code
  104. * //To send CTRL + s (save)
  105. * keyboard.keyCode('s', KEY_CTRL);
  106. * @endcode
  107. *
  108. * @param modifier bit 0: KEY_CTRL, bit 1: KEY_SHIFT, bit 2: KEY_ALT (default: 0)
  109. * @param key character to send
  110. * @returns true if there is no error, false otherwise
  111. */
  112. bool keyCode(uint8_t key, uint8_t modifier = 0);
  113. /**
  114. * Send a character
  115. *
  116. * @param c character to be sent
  117. * @returns true if there is no error, false otherwise
  118. */
  119. virtual int _putc(int c);
  120. /**
  121. * Control media keys
  122. *
  123. * @param key media key pressed (KEY_NEXT_TRACK, KEY_PREVIOUS_TRACK, KEY_STOP, KEY_PLAY_PAUSE, KEY_MUTE, KEY_VOLUME_UP, KEY_VOLUME_DOWN)
  124. * @returns true if there is no error, false otherwise
  125. */
  126. bool mediaControl(MEDIA_KEY key);
  127. /*
  128. * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
  129. *
  130. * @returns pointer to the report descriptor
  131. */
  132. virtual uint8_t * reportDesc();
  133. /*
  134. * Called when a data is received on the OUT endpoint. Useful to switch on LED of LOCK keys
  135. *
  136. * @returns if handle by subclass, return true
  137. */
  138. virtual bool EPINT_OUT_callback();
  139. /**
  140. * Read status of lock keys. Useful to switch-on/off leds according to key pressed. Only the first three bits of the result is important:
  141. * - First bit: NUM_LOCK
  142. * - Second bit: CAPS_LOCK
  143. * - Third bit: SCROLL_LOCK
  144. *
  145. * @returns status of lock keys
  146. */
  147. uint8_t lockStatus();
  148. protected:
  149. /*
  150. * Get configuration descriptor
  151. *
  152. * @returns pointer to the configuration descriptor
  153. */
  154. virtual uint8_t * configurationDesc();
  155. private:
  156. //dummy otherwise it doesn,t compile (we must define all methods of an abstract class)
  157. virtual int _getc() {
  158. return -1;
  159. };
  160. uint8_t lock_status;
  161. };
  162. #endif