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.

USBMouse.h 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 USBMOUSE_H
  19. #define USBMOUSE_H
  20. #include "USBHID.h"
  21. #define REPORT_ID_MOUSE 2
  22. /* Common usage */
  23. enum MOUSE_BUTTON
  24. {
  25. MOUSE_LEFT = 1,
  26. MOUSE_RIGHT = 2,
  27. MOUSE_MIDDLE = 4,
  28. };
  29. /* X and Y limits */
  30. /* These values do not directly map to screen pixels */
  31. /* Zero may be interpreted as meaning 'no movement' */
  32. #define X_MIN_ABS (1) /*!< Minimum value on x-axis */
  33. #define Y_MIN_ABS (1) /*!< Minimum value on y-axis */
  34. #define X_MAX_ABS (0x7fff) /*!< Maximum value on x-axis */
  35. #define Y_MAX_ABS (0x7fff) /*!< Maximum value on y-axis */
  36. #define X_MIN_REL (-127) /*!< The maximum value that we can move to the left on the x-axis */
  37. #define Y_MIN_REL (-127) /*!< The maximum value that we can move up on the y-axis */
  38. #define X_MAX_REL (127) /*!< The maximum value that we can move to the right on the x-axis */
  39. #define Y_MAX_REL (127) /*!< The maximum value that we can move down on the y-axis */
  40. enum MOUSE_TYPE
  41. {
  42. ABS_MOUSE,
  43. REL_MOUSE,
  44. };
  45. /**
  46. *
  47. * USBMouse example
  48. * @code
  49. * #include "mbed.h"
  50. * #include "USBMouse.h"
  51. *
  52. * USBMouse mouse;
  53. *
  54. * int main(void)
  55. * {
  56. * while (1)
  57. * {
  58. * mouse.move(20, 0);
  59. * wait(0.5);
  60. * }
  61. * }
  62. *
  63. * @endcode
  64. *
  65. *
  66. * @code
  67. * #include "mbed.h"
  68. * #include "USBMouse.h"
  69. * #include <math.h>
  70. *
  71. * USBMouse mouse(ABS_MOUSE);
  72. *
  73. * int main(void)
  74. * {
  75. * uint16_t x_center = (X_MAX_ABS - X_MIN_ABS)/2;
  76. * uint16_t y_center = (Y_MAX_ABS - Y_MIN_ABS)/2;
  77. * uint16_t x_screen = 0;
  78. * uint16_t y_screen = 0;
  79. *
  80. * uint32_t x_origin = x_center;
  81. * uint32_t y_origin = y_center;
  82. * uint32_t radius = 5000;
  83. * uint32_t angle = 0;
  84. *
  85. * while (1)
  86. * {
  87. * x_screen = x_origin + cos((double)angle*3.14/180.0)*radius;
  88. * y_screen = y_origin + sin((double)angle*3.14/180.0)*radius;
  89. *
  90. * mouse.move(x_screen, y_screen);
  91. * angle += 3;
  92. * wait(0.01);
  93. * }
  94. * }
  95. *
  96. * @endcode
  97. */
  98. class USBMouse: public USBHID
  99. {
  100. public:
  101. /**
  102. * Constructor
  103. *
  104. * @param mouse_type Mouse type: ABS_MOUSE (absolute mouse) or REL_MOUSE (relative mouse) (default: REL_MOUSE)
  105. * @param vendor_id Your vendor_id (default: 0x1234)
  106. * @param product_id Your product_id (default: 0x0001)
  107. * @param product_release Your preoduct_release (default: 0x0001)
  108. *
  109. */
  110. USBMouse(MOUSE_TYPE mouse_type = REL_MOUSE, uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0001, uint16_t product_release = 0x0001):
  111. USBHID(0, 0, vendor_id, product_id, product_release, false)
  112. {
  113. button = 0;
  114. this->mouse_type = mouse_type;
  115. connect();
  116. };
  117. /**
  118. * Write a state of the mouse
  119. *
  120. * @param x x-axis position
  121. * @param y y-axis position
  122. * @param buttons buttons state (first bit represents MOUSE_LEFT, second bit MOUSE_RIGHT and third bit MOUSE_MIDDLE)
  123. * @param z wheel state (>0 to scroll down, <0 to scroll up)
  124. * @returns true if there is no error, false otherwise
  125. */
  126. bool update(int16_t x, int16_t y, uint8_t buttons, int8_t z);
  127. /**
  128. * Move the cursor to (x, y)
  129. *
  130. * @param x-axis position
  131. * @param y-axis position
  132. * @returns true if there is no error, false otherwise
  133. */
  134. bool move(int16_t x, int16_t y);
  135. /**
  136. * Press one or several buttons
  137. *
  138. * @param button button state (ex: press(MOUSE_LEFT))
  139. * @returns true if there is no error, false otherwise
  140. */
  141. bool press(uint8_t button);
  142. /**
  143. * Release one or several buttons
  144. *
  145. * @param button button state (ex: release(MOUSE_LEFT))
  146. * @returns true if there is no error, false otherwise
  147. */
  148. bool release(uint8_t button);
  149. /**
  150. * Double click (MOUSE_LEFT)
  151. *
  152. * @returns true if there is no error, false otherwise
  153. */
  154. bool doubleClick();
  155. /**
  156. * Click
  157. *
  158. * @param button state of the buttons ( ex: clic(MOUSE_LEFT))
  159. * @returns true if there is no error, false otherwise
  160. */
  161. bool click(uint8_t button);
  162. /**
  163. * Scrolling
  164. *
  165. * @param z value of the wheel (>0 to go down, <0 to go up)
  166. * @returns true if there is no error, false otherwise
  167. */
  168. bool scroll(int8_t z);
  169. /*
  170. * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
  171. *
  172. * @returns pointer to the report descriptor
  173. */
  174. virtual uint8_t * reportDesc();
  175. protected:
  176. /*
  177. * Get configuration descriptor
  178. *
  179. * @returns pointer to the configuration descriptor
  180. */
  181. virtual uint8_t * configurationDesc();
  182. private:
  183. MOUSE_TYPE mouse_type;
  184. uint8_t button;
  185. bool mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z);
  186. };
  187. #endif