app_usbd_hid_mouse.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**
  2. * Copyright (c) 2017 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef APP_USBD_HID_MOUSE_H__
  41. #define APP_USBD_HID_MOUSE_H__
  42. #include <stdint.h>
  43. #include <stdbool.h>
  44. #include "nrf_drv_usbd.h"
  45. #include "app_usbd_class_base.h"
  46. #include "app_usbd_hid_types.h"
  47. #include "app_usbd_hid.h"
  48. #include "app_usbd.h"
  49. #include "app_usbd_core.h"
  50. #include "app_usbd_descriptor.h"
  51. #include "app_usbd_hid_mouse_desc.h"
  52. #include "app_usbd_hid_mouse_internal.h"
  53. /**
  54. * @defgroup app_usbd_hid_mouse USB HID mouse
  55. * @ingroup app_usbd_hid
  56. *
  57. * @brief @tagAPI52840 Module with types, definitions, and API used by the HID mouse class.
  58. * @{
  59. */
  60. #ifdef DOXYGEN
  61. /**
  62. * @brief HID mouse class instance type.
  63. *
  64. * @ref APP_USBD_CLASS_TYPEDEF
  65. */
  66. typedef struct { } app_usbd_hid_mouse_t;
  67. #else
  68. /*lint -save -e10 -e26 -e123 -e505 */
  69. APP_USBD_CLASS_TYPEDEF(app_usbd_hid_mouse, \
  70. APP_USBD_HID_MOUSE_CONFIG(0, NRF_DRV_USBD_EPIN1), \
  71. APP_USBD_HID_MOUSE_INSTANCE_SPECIFIC_DEC, \
  72. APP_USBD_HID_MOUSE_DATA_SPECIFIC_DEC \
  73. );
  74. /*lint -restore*/
  75. #endif
  76. /**
  77. * @brief Global definition macro of app_usbd_hid_mouse_t class.
  78. *
  79. * @param instance_name Name of global instance.
  80. * @param interface_number Unique interface number.
  81. * @param endpoint Input endpoint (@ref nrf_drv_usbd_ep_t).
  82. * @param bcnt Mouse button count (from 1 to 8).
  83. * @param user_ev_handler User event handler (optional).
  84. * @param subclass_boot Subclass boot (@ref app_usbd_hid_subclass_t).
  85. *
  86. * @note This macro is just simplified version of @ref APP_USBD_HID_MOUSE_GLOBAL_DEF_INTERNAL.
  87. *
  88. * @code
  89. APP_USBD_HID_MOUSE_GLOBAL_DEF(my_awesome_mouse, 0, NRF_DRV_USBD_EPIN1, 3, NULL);
  90. * @endcode
  91. */
  92. #define APP_USBD_HID_MOUSE_GLOBAL_DEF(instance_name, \
  93. interface_number, \
  94. endpoint, \
  95. bcnt, \
  96. user_ev_handler, \
  97. subclass_boot) \
  98. APP_USBD_HID_GENERIC_SUBCLASS_REPORT_DESC(mouse_desc, APP_USBD_HID_MOUSE_REPORT_DSC_BUTTON(bcnt)); \
  99. static const app_usbd_hid_subclass_desc_t * mouse_descs[] = {&mouse_desc}; \
  100. APP_USBD_HID_MOUSE_GLOBAL_DEF_INTERNAL(instance_name, \
  101. interface_number, \
  102. endpoint, \
  103. bcnt, \
  104. user_ev_handler, \
  105. subclass_boot)
  106. /**
  107. * @brief Helper function to get class instance from HID mouse internals.
  108. *
  109. * @param[in] p_mouse Mouse instance (declared by @ref APP_USBD_HID_MOUSE_GLOBAL_DEF).
  110. *
  111. * @return Base class instance.
  112. */
  113. static inline app_usbd_class_inst_t const *
  114. app_usbd_hid_mouse_class_inst_get(app_usbd_hid_mouse_t const * p_mouse)
  115. {
  116. return &p_mouse->base;
  117. }
  118. /**
  119. * @brief Helper function to get HID mouse from base class instance.
  120. *
  121. * @param[in] p_inst Base class instance.
  122. *
  123. * @return HID mouse class handle.
  124. */
  125. static inline app_usbd_hid_mouse_t const *
  126. app_usbd_hid_mouse_class_get(app_usbd_class_inst_t const * p_inst)
  127. {
  128. return (app_usbd_hid_mouse_t const *)p_inst;
  129. }
  130. /**
  131. * @brief Move mouse X axis.
  132. *
  133. * @param[in] p_mouse Mouse instance (declared by @ref APP_USBD_HID_MOUSE_GLOBAL_DEF).
  134. * @param[in] offset Relative mouse position: allowed full int8_t range.
  135. *
  136. * @return Standard error code.
  137. */
  138. ret_code_t app_usbd_hid_mouse_x_move(app_usbd_hid_mouse_t const * p_mouse, int8_t offset);
  139. /**
  140. * @brief Move mouse Y axis.
  141. *
  142. * @param[in] p_mouse Mouse instance (declared by @ref APP_USBD_HID_MOUSE_GLOBAL_DEF).
  143. * @param[in] offset Relative mouse position: allowed full int8_t range.
  144. *
  145. * @return Standard error code.
  146. */
  147. ret_code_t app_usbd_hid_mouse_y_move(app_usbd_hid_mouse_t const * p_mouse, int8_t offset);
  148. /**
  149. * @brief Move mouse scroll.
  150. *
  151. * @param[in] p_mouse Mouse instance (declared by @ref APP_USBD_HID_MOUSE_GLOBAL_DEF).
  152. * @param[in] offset Relative mouse position: allowed full int8_t range.
  153. *
  154. * @return Standard error code.
  155. */
  156. ret_code_t app_usbd_hid_mouse_scroll_move(app_usbd_hid_mouse_t const * p_mouse, int8_t offset);
  157. /**
  158. * @brief Set mouse button state.
  159. *
  160. * @param[in] p_mouse Mouse instance (declared by @ref APP_USBD_HID_MOUSE_GLOBAL_DEF).
  161. * @param[in] button_id Button number (0...7).
  162. * @param[in] state Button state: true -> PRESSED, false -> RELEASED.
  163. *
  164. * @return Standard error code.
  165. */
  166. ret_code_t app_usbd_hid_mouse_button_state(app_usbd_hid_mouse_t const * p_mouse,
  167. uint8_t button_id,
  168. bool state);
  169. /**
  170. * @brief Function handling SET_PROTOCOL command.
  171. *
  172. *
  173. * @param[in] p_mouse Mouse instance.
  174. * @param[in] ev User event.
  175. *
  176. * @return Standard error code.
  177. */
  178. ret_code_t hid_mouse_on_set_protocol(app_usbd_hid_mouse_t const * p_mouse,
  179. app_usbd_hid_user_event_t ev);
  180. /**
  181. * @brief Function that clears HID mouse buffers and sends an empty report.
  182. *
  183. * @param[in] p_inst Base class instance.
  184. *
  185. * @return Standard error code.
  186. */
  187. ret_code_t hid_mouse_clear_buffer(app_usbd_class_inst_t const * p_inst);
  188. /** @} */
  189. #endif /* APP_USBD_HID_MOUSE_H__ */