1 /**
2 ******************************************************************************
3 * @file usbh_hid_mouse.c
4 * @author MCD Application Team
5 * @brief This file is the application layer for USB Host HID Mouse Handling.
6 ******************************************************************************
7 * @attention
8 *
9 * <h2><center>© Copyright (c) 2015 STMicroelectronics.
10 * All rights reserved.</center></h2>
11 *
12 * This software component is licensed by ST under Ultimate Liberty license
13 * SLA0044, the "License"; You may not use this file except in compliance with
14 * the License. You may obtain a copy of the License at:
15 * www.st.com/SLA0044
16 *
17 ******************************************************************************
18 */
19
20 /* BSPDependencies
21 - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c"
22 - "stm32xxxxx_{eval}{discovery}_io.c"
23 - "stm32xxxxx_{eval}{discovery}{adafruit}_lcd.c"
24 - "stm32xxxxx_{eval}{discovery}_sdram.c"
25 EndBSPDependencies */
26
27 /* Includes ------------------------------------------------------------------*/
28 #include "usbh_hid_mouse.h"
29 #include "usbh_hid_parser.h"
30
31
32 /** @addtogroup USBH_LIB
33 * @{
34 */
35
36 /** @addtogroup USBH_CLASS
37 * @{
38 */
39
40 /** @addtogroup USBH_HID_CLASS
41 * @{
42 */
43
44 /** @defgroup USBH_HID_MOUSE
45 * @brief This file includes HID Layer Handlers for USB Host HID class.
46 * @{
47 */
48
49 /** @defgroup USBH_HID_MOUSE_Private_TypesDefinitions
50 * @{
51 */
52 /**
53 * @}
54 */
55
56
57 /** @defgroup USBH_HID_MOUSE_Private_Defines
58 * @{
59 */
60 /**
61 * @}
62 */
63
64
65 /** @defgroup USBH_HID_MOUSE_Private_Macros
66 * @{
67 */
68 /**
69 * @}
70 */
71
72 /** @defgroup USBH_HID_MOUSE_Private_FunctionPrototypes
73 * @{
74 */
75 static USBH_StatusTypeDef USBH_HID_MouseDecode(USBH_HandleTypeDef *phost);
76
77 /**
78 * @}
79 */
80
81
82 /** @defgroup USBH_HID_MOUSE_Private_Variables
83 * @{
84 */
85 HID_MOUSE_Info_TypeDef mouse_info;
86 uint32_t mouse_report_data[2];
87 uint32_t mouse_rx_report_buf[2];
88
89 /* Structures defining how to access items in a HID mouse report */
90 /* Access button 1 state. */
91 static const HID_Report_ItemTypedef prop_b1 =
92 {
93 (uint8_t *)(void *)mouse_report_data + 0, /*data*/
94 1, /*size*/
95 0, /*shift*/
96 0, /*count (only for array items)*/
97 0, /*signed?*/
98 0, /*min value read can return*/
99 1, /*max value read can return*/
100 0, /*min value device can report*/
101 1, /*max value device can report*/
102 1 /*resolution*/
103 };
104
105 /* Access button 2 state. */
106 static const HID_Report_ItemTypedef prop_b2 =
107 {
108 (uint8_t *)(void *)mouse_report_data + 0, /*data*/
109 1, /*size*/
110 1, /*shift*/
111 0, /*count (only for array items)*/
112 0, /*signed?*/
113 0, /*min value read can return*/
114 1, /*max value read can return*/
115 0, /*min value device can report*/
116 1, /*max value device can report*/
117 1 /*resolution*/
118 };
119
120 /* Access button 3 state. */
121 static const HID_Report_ItemTypedef prop_b3 =
122 {
123 (uint8_t *)(void *)mouse_report_data + 0, /*data*/
124 1, /*size*/
125 2, /*shift*/
126 0, /*count (only for array items)*/
127 0, /*signed?*/
128 0, /*min value read can return*/
129 1, /*max value read can return*/
130 0, /*min vale device can report*/
131 1, /*max value device can report*/
132 1 /*resolution*/
133 };
134
135 /* Access x coordinate change. */
136 static const HID_Report_ItemTypedef prop_x =
137 {
138 (uint8_t *)(void *)mouse_report_data + 1, /*data*/
139 8, /*size*/
140 0, /*shift*/
141 0, /*count (only for array items)*/
142 1, /*signed?*/
143 0, /*min value read can return*/
144 0xFFFF,/*max value read can return*/
145 0, /*min vale device can report*/
146 0xFFFF,/*max value device can report*/
147 1 /*resolution*/
148 };
149
150 /* Access y coordinate change. */
151 static const HID_Report_ItemTypedef prop_y =
152 {
153 (uint8_t *)(void *)mouse_report_data + 2, /*data*/
154 8, /*size*/
155 0, /*shift*/
156 0, /*count (only for array items)*/
157 1, /*signed?*/
158 0, /*min value read can return*/
159 0xFFFF,/*max value read can return*/
160 0, /*min vale device can report*/
161 0xFFFF,/*max value device can report*/
162 1 /*resolution*/
163 };
164
165
166 /**
167 * @}
168 */
169
170
171 /** @defgroup USBH_HID_MOUSE_Private_Functions
172 * @{
173 */
174
175 /**
176 * @brief USBH_HID_MouseInit
177 * The function init the HID mouse.
178 * @param phost: Host handle
179 * @retval USBH Status
180 */
USBH_HID_MouseInit(USBH_HandleTypeDef * phost)181 USBH_StatusTypeDef USBH_HID_MouseInit(USBH_HandleTypeDef *phost)
182 {
183 uint32_t i;
184 HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData;
185
186 mouse_info.x = 0U;
187 mouse_info.y = 0U;
188 mouse_info.buttons[0] = 0U;
189 mouse_info.buttons[1] = 0U;
190 mouse_info.buttons[2] = 0U;
191
192 for (i = 0U; i < (sizeof(mouse_report_data) / sizeof(uint32_t)); i++)
193 {
194 mouse_report_data[i] = 0U;
195 mouse_rx_report_buf[i] = 0U;
196 }
197
198 if (HID_Handle->length > sizeof(mouse_report_data))
199 {
200 HID_Handle->length = sizeof(mouse_report_data);
201 }
202 HID_Handle->pData = (uint8_t *)(void *)mouse_rx_report_buf;
203 USBH_HID_FifoInit(&HID_Handle->fifo, phost->device.Data, HID_QUEUE_SIZE * sizeof(mouse_report_data));
204
205 return USBH_OK;
206 }
207
208 /**
209 * @brief USBH_HID_GetMouseInfo
210 * The function return mouse information.
211 * @param phost: Host handle
212 * @retval mouse information
213 */
USBH_HID_GetMouseInfo(USBH_HandleTypeDef * phost)214 HID_MOUSE_Info_TypeDef *USBH_HID_GetMouseInfo(USBH_HandleTypeDef *phost)
215 {
216 if (USBH_HID_MouseDecode(phost) == USBH_OK)
217 {
218 return &mouse_info;
219 }
220 else
221 {
222 return NULL;
223 }
224 }
225
226 /**
227 * @brief USBH_HID_MouseDecode
228 * The function decode mouse data.
229 * @param phost: Host handle
230 * @retval USBH Status
231 */
USBH_HID_MouseDecode(USBH_HandleTypeDef * phost)232 static USBH_StatusTypeDef USBH_HID_MouseDecode(USBH_HandleTypeDef *phost)
233 {
234 HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData;
235
236 if (HID_Handle->length == 0U)
237 {
238 return USBH_FAIL;
239 }
240 /*Fill report */
241 if (USBH_HID_FifoRead(&HID_Handle->fifo, &mouse_report_data, HID_Handle->length) == HID_Handle->length)
242 {
243 /*Decode report */
244 mouse_info.x = (uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &prop_x, 0U);
245 mouse_info.y = (uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &prop_y, 0U);
246
247 mouse_info.buttons[0] = (uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &prop_b1, 0U);
248 mouse_info.buttons[1] = (uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &prop_b2, 0U);
249 mouse_info.buttons[2] = (uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &prop_b3, 0U);
250
251 return USBH_OK;
252 }
253 return USBH_FAIL;
254 }
255
256 /**
257 * @}
258 */
259
260 /**
261 * @}
262 */
263
264 /**
265 * @}
266 */
267
268 /**
269 * @}
270 */
271
272
273 /**
274 * @}
275 */
276 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
277