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.

USBMouseKeyboard.h 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 USBMOUSEKEYBOARD_H
  19. #define USBMOUSEKEYBOARD_H
  20. #define REPORT_ID_KEYBOARD 1
  21. #define REPORT_ID_MOUSE 2
  22. #define REPORT_ID_VOLUME 3
  23. #include "USBMouse.h"
  24. #include "USBKeyboard.h"
  25. #include "Stream.h"
  26. #include "USBHID.h"
  27. /**
  28. * USBMouseKeyboard example
  29. * @code
  30. *
  31. * #include "mbed.h"
  32. * #include "USBMouseKeyboard.h"
  33. *
  34. * USBMouseKeyboard key_mouse;
  35. *
  36. * int main(void)
  37. * {
  38. * while(1)
  39. * {
  40. * key_mouse.move(20, 0);
  41. * key_mouse.printf("Hello From MBED\r\n");
  42. * wait(1);
  43. * }
  44. * }
  45. * @endcode
  46. *
  47. *
  48. * @code
  49. *
  50. * #include "mbed.h"
  51. * #include "USBMouseKeyboard.h"
  52. *
  53. * USBMouseKeyboard key_mouse(ABS_MOUSE);
  54. *
  55. * int main(void)
  56. * {
  57. * while(1)
  58. * {
  59. * key_mouse.move(X_MAX_ABS/2, Y_MAX_ABS/2);
  60. * key_mouse.printf("Hello from MBED\r\n");
  61. * wait(1);
  62. * }
  63. * }
  64. * @endcode
  65. */
  66. class USBMouseKeyboard: public USBHID, public Stream
  67. {
  68. public:
  69. /**
  70. * Constructor
  71. *
  72. * @param mouse_type Mouse type: ABS_MOUSE (absolute mouse) or REL_MOUSE (relative mouse) (default: REL_MOUSE)
  73. * @param leds Leds bus: first: NUM_LOCK, second: CAPS_LOCK, third: SCROLL_LOCK
  74. * @param vendor_id Your vendor_id (default: 0x1234)
  75. * @param product_id Your product_id (default: 0x0001)
  76. * @param product_release Your preoduct_release (default: 0x0001)
  77. *
  78. */
  79. USBMouseKeyboard(MOUSE_TYPE mouse_type = REL_MOUSE, uint16_t vendor_id = 0x0021, uint16_t product_id = 0x0011, uint16_t product_release = 0x0001):
  80. USBHID(0, 0, vendor_id, product_id, product_release, false)
  81. {
  82. lock_status = 0;
  83. button = 0;
  84. this->mouse_type = mouse_type;
  85. connect();
  86. };
  87. /**
  88. * Write a state of the mouse
  89. *
  90. * @param x x-axis position
  91. * @param y y-axis position
  92. * @param buttons buttons state (first bit represents MOUSE_LEFT, second bit MOUSE_RIGHT and third bit MOUSE_MIDDLE)
  93. * @param z wheel state (>0 to scroll down, <0 to scroll up)
  94. * @returns true if there is no error, false otherwise
  95. */
  96. bool update(int16_t x, int16_t y, uint8_t buttons, int8_t z);
  97. /**
  98. * Move the cursor to (x, y)
  99. *
  100. * @param x x-axis position
  101. * @param y y-axis position
  102. * @returns true if there is no error, false otherwise
  103. */
  104. bool move(int16_t x, int16_t y);
  105. /**
  106. * Press one or several buttons
  107. *
  108. * @param button button state (ex: press(MOUSE_LEFT))
  109. * @returns true if there is no error, false otherwise
  110. */
  111. bool press(uint8_t button);
  112. /**
  113. * Release one or several buttons
  114. *
  115. * @param button button state (ex: release(MOUSE_LEFT))
  116. * @returns true if there is no error, false otherwise
  117. */
  118. bool release(uint8_t button);
  119. /**
  120. * Double click (MOUSE_LEFT)
  121. *
  122. * @returns true if there is no error, false otherwise
  123. */
  124. bool doubleClick();
  125. /**
  126. * Click
  127. *
  128. * @param button state of the buttons ( ex: clic(MOUSE_LEFT))
  129. * @returns true if there is no error, false otherwise
  130. */
  131. bool click(uint8_t button);
  132. /**
  133. * Scrolling
  134. *
  135. * @param z value of the wheel (>0 to go down, <0 to go up)
  136. * @returns true if there is no error, false otherwise
  137. */
  138. bool scroll(int8_t z);
  139. /**
  140. * To send a character defined by a modifier(CTRL, SHIFT, ALT) and the key
  141. *
  142. * @code
  143. * //To send CTRL + s (save)
  144. * keyboard.keyCode('s', KEY_CTRL);
  145. * @endcode
  146. *
  147. * @param modifier bit 0: KEY_CTRL, bit 1: KEY_SHIFT, bit 2: KEY_ALT (default: 0)
  148. * @param key character to send
  149. * @returns true if there is no error, false otherwise
  150. */
  151. bool keyCode(uint8_t key, uint8_t modifier = 0);
  152. /**
  153. * Send a character
  154. *
  155. * @param c character to be sent
  156. * @returns true if there is no error, false otherwise
  157. */
  158. virtual int _putc(int c);
  159. /**
  160. * Control media keys
  161. *
  162. * @param key media key pressed (KEY_NEXT_TRACK, KEY_PREVIOUS_TRACK, KEY_STOP, KEY_PLAY_PAUSE, KEY_MUTE, KEY_VOLUME_UP, KEY_VOLUME_DOWN)
  163. * @returns true if there is no error, false otherwise
  164. */
  165. bool mediaControl(MEDIA_KEY key);
  166. /**
  167. * 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:
  168. * - First bit: NUM_LOCK
  169. * - Second bit: CAPS_LOCK
  170. * - Third bit: SCROLL_LOCK
  171. *
  172. * @returns status of lock keys
  173. */
  174. uint8_t lockStatus();
  175. /*
  176. * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
  177. *
  178. * @returns pointer to the report descriptor
  179. */
  180. virtual uint8_t * reportDesc();
  181. /*
  182. * Called when a data is received on the OUT endpoint. Useful to switch on LED of LOCK keys
  183. *
  184. * @returns if handle by subclass, return true
  185. */
  186. virtual bool EPINT_OUT_callback();
  187. private:
  188. bool mouseWrite(int8_t x, int8_t y, uint8_t buttons, int8_t z);
  189. MOUSE_TYPE mouse_type;
  190. uint8_t button;
  191. bool mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z);
  192. uint8_t lock_status;
  193. //dummy otherwise it doesn't compile (we must define all methods of an abstract class)
  194. virtual int _getc() { return -1;}
  195. };
  196. #endif