app_usbd_hid_kbd.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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_KBD_H__
  41. #define APP_USBD_HID_KBD_H__
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. #include <stdint.h>
  46. #include <stdbool.h>
  47. #include "nrf_drv_usbd.h"
  48. #include "app_usbd_class_base.h"
  49. #include "app_usbd_hid_types.h"
  50. #include "app_usbd_hid.h"
  51. #include "app_usbd.h"
  52. #include "app_usbd_core.h"
  53. #include "app_usbd_descriptor.h"
  54. #include "app_usbd_hid_kbd_desc.h"
  55. #include "app_usbd_hid_kbd_internal.h"
  56. /**
  57. * @defgroup app_usbd_hid_kbd USB HID keyboard
  58. * @ingroup app_usbd_hid
  59. *
  60. * @brief @tagAPI52840 Module with types, definitions, and API used by the HID keyboard class.
  61. * @{
  62. */
  63. /**
  64. * @brief HID keyboard codes.
  65. */
  66. typedef enum {
  67. APP_USBD_HID_KBD_A = 4, /**<KBD_A code*/
  68. APP_USBD_HID_KBD_B = 5, /**<KBD_B code*/
  69. APP_USBD_HID_KBD_C = 6, /**<KBD_C code*/
  70. APP_USBD_HID_KBD_D = 7, /**<KBD_D code*/
  71. APP_USBD_HID_KBD_E = 8, /**<KBD_E code*/
  72. APP_USBD_HID_KBD_F = 9, /**<KBD_F code*/
  73. APP_USBD_HID_KBD_G = 10, /**<KBD_G code*/
  74. APP_USBD_HID_KBD_H = 11, /**<KBD_H code*/
  75. APP_USBD_HID_KBD_I = 12, /**<KBD_I code*/
  76. APP_USBD_HID_KBD_J = 13, /**<KBD_J code*/
  77. APP_USBD_HID_KBD_K = 14, /**<KBD_K code*/
  78. APP_USBD_HID_KBD_L = 15, /**<KBD_L code*/
  79. APP_USBD_HID_KBD_M = 16, /**<KBD_M code*/
  80. APP_USBD_HID_KBD_N = 17, /**<KBD_N code*/
  81. APP_USBD_HID_KBD_O = 18, /**<KBD_O code*/
  82. APP_USBD_HID_KBD_P = 19, /**<KBD_P code*/
  83. APP_USBD_HID_KBD_Q = 20, /**<KBD_Q code*/
  84. APP_USBD_HID_KBD_R = 21, /**<KBD_R code*/
  85. APP_USBD_HID_KBD_S = 22, /**<KBD_S code*/
  86. APP_USBD_HID_KBD_T = 23, /**<KBD_T code*/
  87. APP_USBD_HID_KBD_U = 24, /**<KBD_U code*/
  88. APP_USBD_HID_KBD_V = 25, /**<KBD_V code*/
  89. APP_USBD_HID_KBD_W = 26, /**<KBD_W code*/
  90. APP_USBD_HID_KBD_X = 27, /**<KBD_X code*/
  91. APP_USBD_HID_KBD_Y = 28, /**<KBD_Y code*/
  92. APP_USBD_HID_KBD_Z = 29, /**<KBD_Z code*/
  93. APP_USBD_HID_KBD_1 = 30, /**<KBD_1 code*/
  94. APP_USBD_HID_KBD_2 = 31, /**<KBD_2 code*/
  95. APP_USBD_HID_KBD_3 = 32, /**<KBD_3 code*/
  96. APP_USBD_HID_KBD_4 = 33, /**<KBD_4 code*/
  97. APP_USBD_HID_KBD_5 = 34, /**<KBD_5 code*/
  98. APP_USBD_HID_KBD_6 = 35, /**<KBD_6 code*/
  99. APP_USBD_HID_KBD_7 = 36, /**<KBD_7 code*/
  100. APP_USBD_HID_KBD_8 = 37, /**<KBD_8 code*/
  101. APP_USBD_HID_KBD_9 = 38, /**<KBD_9 code*/
  102. APP_USBD_HID_KBD_0 = 39, /**<KBD_0 code*/
  103. APP_USBD_HID_KBD_ENTER = 40, /**<KBD_ENTER code*/
  104. APP_USBD_HID_KBD_ESCAPE = 41, /**<KBD_ESCAPE code*/
  105. APP_USBD_HID_KBD_BACKSPACE = 42, /**<KBD_BACKSPACE code*/
  106. APP_USBD_HID_KBD_TAB = 43, /**<KBD_TAB code*/
  107. APP_USBD_HID_KBD_SPACEBAR = 44, /**<KBD_SPACEBAR code*/
  108. APP_USBD_HID_KBD_UNDERSCORE = 45, /**<KBD_UNDERSCORE code*/
  109. APP_USBD_HID_KBD_PLUS = 46, /**<KBD_PLUS code*/
  110. APP_USBD_HID_KBD_OPEN_BRACKET = 47, /**<KBD_OPEN_BRACKET code*/
  111. APP_USBD_HID_KBD_CLOSE_BRACKET = 48, /**<KBD_CLOSE_BRACKET code*/
  112. APP_USBD_HID_KBD_BACKSLASH = 49, /**<KBD_BACKSLASH code*/
  113. APP_USBD_HID_KBD_ASH = 50, /**<KBD_ASH code*/
  114. APP_USBD_HID_KBD_COLON = 51, /**<KBD_COLON code*/
  115. APP_USBD_HID_KBD_QUOTE = 52, /**<KBD_QUOTE code*/
  116. APP_USBD_HID_KBD_TILDE = 53, /**<KBD_TILDE code*/
  117. APP_USBD_HID_KBD_COMMA = 54, /**<KBD_COMMA code*/
  118. APP_USBD_HID_KBD_DOT = 55, /**<KBD_DOT code*/
  119. APP_USBD_HID_KBD_SLASH = 56, /**<KBD_SLASH code*/
  120. APP_USBD_HID_KBD_CAPS_LOCK = 57, /**<KBD_CAPS_LOCK code*/
  121. APP_USBD_HID_KBD_F1 = 58, /**<KBD_F1 code*/
  122. APP_USBD_HID_KBD_F2 = 59, /**<KBD_F2 code*/
  123. APP_USBD_HID_KBD_F3 = 60, /**<KBD_F3 code*/
  124. APP_USBD_HID_KBD_F4 = 61, /**<KBD_F4 code*/
  125. APP_USBD_HID_KBD_F5 = 62, /**<KBD_F5 code*/
  126. APP_USBD_HID_KBD_F6 = 63, /**<KBD_F6 code*/
  127. APP_USBD_HID_KBD_F7 = 64, /**<KBD_F7 code*/
  128. APP_USBD_HID_KBD_F8 = 65, /**<KBD_F8 code*/
  129. APP_USBD_HID_KBD_F9 = 66, /**<KBD_F9 code*/
  130. APP_USBD_HID_KBD_F10 = 67, /**<KBD_F10 code*/
  131. APP_USBD_HID_KBD_F11 = 68, /**<KBD_F11 code*/
  132. APP_USBD_HID_KBD_F12 = 69, /**<KBD_F12 code*/
  133. APP_USBD_HID_KBD_PRINTSCREEN = 70, /**<KBD_PRINTSCREEN code*/
  134. APP_USBD_HID_KBD_SCROLL_LOCK = 71, /**<KBD_SCROLL_LOCK code*/
  135. APP_USBD_HID_KBD_PAUSE = 72, /**<KBD_PAUSE code*/
  136. APP_USBD_HID_KBD_INSERT = 73, /**<KBD_INSERT code*/
  137. APP_USBD_HID_KBD_HOME = 74, /**<KBD_HOME code*/
  138. APP_USBD_HID_KBD_PAGEUP = 75, /**<KBD_PAGEUP code*/
  139. APP_USBD_HID_KBD_DELETE = 76, /**<KBD_DELETE code*/
  140. APP_USBD_HID_KBD_END = 77, /**<KBD_END code*/
  141. APP_USBD_HID_KBD_PAGEDOWN = 78, /**<KBD_PAGEDOWN code*/
  142. APP_USBD_HID_KBD_RIGHT = 79, /**<KBD_RIGHT code*/
  143. APP_USBD_HID_KBD_LEFT = 80, /**<KBD_LEFT code*/
  144. APP_USBD_HID_KBD_DOWN = 81, /**<KBD_DOWN code*/
  145. APP_USBD_HID_KBD_UP = 82, /**<KBD_UP code*/
  146. APP_USBD_HID_KBD_KEYPAD_NUM_LOCK = 83, /**<KBD_KEYPAD_NUM_LOCK code*/
  147. APP_USBD_HID_KBD_KEYPAD_DIVIDE = 84, /**<KBD_KEYPAD_DIVIDE code*/
  148. APP_USBD_HID_KBD_KEYPAD_AT = 85, /**<KBD_KEYPAD_AT code*/
  149. APP_USBD_HID_KBD_KEYPAD_MULTIPLY = 85, /**<KBD_KEYPAD_MULTIPLY code*/
  150. APP_USBD_HID_KBD_KEYPAD_MINUS = 86, /**<KBD_KEYPAD_MINUS code*/
  151. APP_USBD_HID_KBD_KEYPAD_PLUS = 87, /**<KBD_KEYPAD_PLUS code*/
  152. APP_USBD_HID_KBD_KEYPAD_ENTER = 88, /**<KBD_KEYPAD_ENTER code*/
  153. APP_USBD_HID_KBD_KEYPAD_1 = 89, /**<KBD_KEYPAD_1 code*/
  154. APP_USBD_HID_KBD_KEYPAD_2 = 90, /**<KBD_KEYPAD_2 code*/
  155. APP_USBD_HID_KBD_KEYPAD_3 = 91, /**<KBD_KEYPAD_3 code*/
  156. APP_USBD_HID_KBD_KEYPAD_4 = 92, /**<KBD_KEYPAD_4 code*/
  157. APP_USBD_HID_KBD_KEYPAD_5 = 93, /**<KBD_KEYPAD_5 code*/
  158. APP_USBD_HID_KBD_KEYPAD_6 = 94, /**<KBD_KEYPAD_6 code*/
  159. APP_USBD_HID_KBD_KEYPAD_7 = 95, /**<KBD_KEYPAD_7 code*/
  160. APP_USBD_HID_KBD_KEYPAD_8 = 96, /**<KBD_KEYPAD_8 code*/
  161. APP_USBD_HID_KBD_KEYPAD_9 = 97, /**<KBD_KEYPAD_9 code*/
  162. APP_USBD_HID_KBD_KEYPAD_0 = 98, /**<KBD_KEYPAD_0 code*/
  163. } app_usbd_hid_kbd_codes_t;
  164. /**
  165. * @brief HID keyboard modifier.
  166. */
  167. typedef enum {
  168. APP_USBD_HID_KBD_MODIFIER_NONE = 0x00, /**< MODIFIER_NONE bit*/
  169. APP_USBD_HID_KBD_MODIFIER_LEFT_CTRL = 0x01, /**< MODIFIER_LEFT_CTRL bit*/
  170. APP_USBD_HID_KBD_MODIFIER_LEFT_SHIFT = 0x02, /**< MODIFIER_LEFT_SHIFT bit*/
  171. APP_USBD_HID_KBD_MODIFIER_LEFT_ALT = 0x04, /**< MODIFIER_LEFT_ALT bit*/
  172. APP_USBD_HID_KBD_MODIFIER_LEFT_UI = 0x08, /**< MODIFIER_LEFT_UI bit*/
  173. APP_USBD_HID_KBD_MODIFIER_RIGHT_CTRL = 0x10, /**< MODIFIER_RIGHT_CTRL bit*/
  174. APP_USBD_HID_KBD_MODIFIER_RIGHT_SHIFT = 0x20, /**< MODIFIER_RIGHT_SHIFT bit*/
  175. APP_USBD_HID_KBD_MODIFIER_RIGHT_ALT = 0x40, /**< MODIFIER_RIGHT_ALT bit*/
  176. APP_USBD_HID_KBD_MODIFIER_RIGHT_UI = 0x80, /**< MODIFIER_RIGHT_UI bit*/
  177. } app_usbd_hid_kbd_modifier_t;
  178. /**
  179. * @brief HID keyboard LEDs.
  180. */
  181. typedef enum {
  182. APP_USBD_HID_KBD_LED_NUM_LOCK = 0x01, /**< LED_NUM_LOCK id*/
  183. APP_USBD_HID_KBD_LED_CAPS_LOCK = 0x02, /**< LED_CAPS_LOCK id*/
  184. APP_USBD_HID_KBD_LED_SCROLL_LOCK = 0x04, /**< LED_SCROLL_LOCK id*/
  185. APP_USBD_HID_KBD_LED_COMPOSE = 0x08, /**< LED_COMPOSE id*/
  186. APP_USBD_HID_KBD_LED_KANA = 0x10, /**< LED_KANA id*/
  187. } app_usbd_hid_kbd_led_t;
  188. #ifdef DOXYGEN
  189. /**
  190. * @brief HID keyboard class instance type.
  191. *
  192. * @ref APP_USBD_CLASS_TYPEDEF
  193. */
  194. typedef struct { } app_usbd_hid_kbd_t;
  195. #else
  196. /*lint -save -e10 -e26 -e123 -e505 */
  197. APP_USBD_CLASS_TYPEDEF(app_usbd_hid_kbd, \
  198. APP_USBD_HID_KBD_CONFIG(0, NRF_DRV_USBD_EPIN1), \
  199. APP_USBD_HID_KBD_INSTANCE_SPECIFIC_DEC, \
  200. APP_USBD_HID_KBD_DATA_SPECIFIC_DEC \
  201. );
  202. /*lint -restore*/
  203. #endif
  204. /**
  205. * @brief Global definition of app_usbd_hid_kbd_t class.
  206. *
  207. * @param instance_name Name of global instance.
  208. * @param interface_number Unique interface index.
  209. * @param endpoint Input endpoint (@ref nrf_drv_usbd_ep_t).
  210. * @param user_ev_handler User event handler (optional parameter: NULL might be passed here).
  211. * @param subclass_boot Subclass boot (@ref app_usbd_hid_subclass_t).
  212. *
  213. * Example class definition:
  214. * @code
  215. APP_USBD_HID_KBD_GLOBAL_DEF(my_awesome_kbd, 0, NRF_DRV_USBD_EPIN1, NULL, APP_USBD_HID_SUBCLASS_BOOT);
  216. * @endcode
  217. */
  218. #define APP_USBD_HID_KBD_GLOBAL_DEF(instance_name, interface_number, endpoint, user_ev_handler, subclass_boot) \
  219. APP_USBD_HID_GENERIC_SUBCLASS_REPORT_DESC(keyboard_desc, APP_USBD_HID_KBD_REPORT_DSC()); \
  220. static const app_usbd_hid_subclass_desc_t * keyboard_descs[] = {&keyboard_desc}; \
  221. APP_USBD_HID_KBD_GLOBAL_DEF_INTERNAL(instance_name, \
  222. interface_number, \
  223. endpoint, \
  224. user_ev_handler, \
  225. subclass_boot)
  226. /**
  227. * @brief Helper function to get class instance from HID keyboard internals.
  228. *
  229. * @param[in] p_kbd Keyboard instance (declared by @ref APP_USBD_HID_KBD_GLOBAL_DEF).
  230. *
  231. * @return Base class instance.
  232. */
  233. static inline app_usbd_class_inst_t const *
  234. app_usbd_hid_kbd_class_inst_get(app_usbd_hid_kbd_t const * p_kbd)
  235. {
  236. return &p_kbd->base;
  237. }
  238. /**
  239. * @brief Helper function to get HID keyboard from base class instance.
  240. *
  241. * @param[in] p_inst Base class instance.
  242. *
  243. * @return HID keyboard class handle.
  244. */
  245. static inline app_usbd_hid_kbd_t const *
  246. app_usbd_hid_kbd_class_get(app_usbd_class_inst_t const * p_inst)
  247. {
  248. return (app_usbd_hid_kbd_t const *)p_inst;
  249. }
  250. /**
  251. * @brief Set HID keyboard modifier state.
  252. *
  253. * @param[in] p_kbd Keyboard instance (declared by @ref APP_USBD_HID_KBD_GLOBAL_DEF).
  254. * @param[in] modifier Type of modifier.
  255. * @param[in] state State, true active, false inactive.
  256. *
  257. * @return Standard error code.
  258. */
  259. ret_code_t app_usbd_hid_kbd_modifier_state_set(app_usbd_hid_kbd_t const * p_kbd,
  260. app_usbd_hid_kbd_modifier_t modifier,
  261. bool state);
  262. /**
  263. * @brief Press/release HID keyboard key.
  264. *
  265. * @param[in] p_kbd Keyboard instance (declared by @ref APP_USBD_HID_KBD_GLOBAL_DEF).
  266. * @param[in] key Keyboard key code.
  267. * @param[in] press True -> key press, false -> release.
  268. *
  269. * @return Standard error code.
  270. */
  271. ret_code_t app_usbd_hid_kbd_key_control(app_usbd_hid_kbd_t const * p_kbd,
  272. app_usbd_hid_kbd_codes_t key,
  273. bool press);
  274. /**
  275. * @brief HID Keyboard LEDs state get.
  276. *
  277. * @param[in] p_kbd Keyboard instance (declared by @ref APP_USBD_HID_KBD_GLOBAL_DEF).
  278. * @param[in] led LED code.
  279. *
  280. * @retval true LED is set.
  281. * @retval false LED is not set.
  282. */
  283. bool app_usbd_hid_kbd_led_state_get(app_usbd_hid_kbd_t const * p_kbd,
  284. app_usbd_hid_kbd_led_t led);
  285. /**
  286. * @brief Function handling SET_PROTOCOL command.
  287. *
  288. *
  289. * @param[in] p_kbd Keyboard instance.
  290. * @param[in] ev User event.
  291. *
  292. * @return Standard error code.
  293. */
  294. ret_code_t hid_kbd_on_set_protocol(app_usbd_hid_kbd_t const * p_kbd,
  295. app_usbd_hid_user_event_t ev);
  296. /**
  297. * @brief Function that clears HID keyboard buffers and sends an empty report.
  298. *
  299. * @param[in] p_inst Base class instance.
  300. *
  301. * @return Standard error code.
  302. */
  303. ret_code_t hid_kbd_clear_buffer(app_usbd_class_inst_t const * p_inst);
  304. /** @} */
  305. #ifdef __cplusplus
  306. }
  307. #endif
  308. #endif /* APP_USBD_HID_KBD_H__ */