F1 and F3 HAL / LL libraries
[betaflight.git] / lib / main / STM32F1 / Middlewares / ST / STM32_USB_Host_Library / Class / HID / Src / usbh_hid_keybd.c
blob6323f2a180614831ff381b1a39bedc29e48e5560
1 /**
2 ******************************************************************************
3 * @file usbh_hid_keybd.c
4 * @author MCD Application Team
5 * @version V3.2.2
6 * @date 07-July-2015
7 * @brief This file is the application layer for USB Host HID Keyboard handling
8 * QWERTY and AZERTY Keyboard are supported as per the selection in
9 * usbh_hid_keybd.h
10 ******************************************************************************
11 * @attention
13 * <h2><center>&copy; COPYRIGHT 2015 STMicroelectronics</center></h2>
15 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
16 * You may not use this file except in compliance with the License.
17 * You may obtain a copy of the License at:
19 * http://www.st.com/software_license_agreement_liberty_v2
21 * Unless required by applicable law or agreed to in writing, software
22 * distributed under the License is distributed on an "AS IS" BASIS,
23 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 * See the License for the specific language governing permissions and
25 * limitations under the License.
27 ******************************************************************************
28 */
31 /* Includes ------------------------------------------------------------------*/
32 #include "usbh_hid_keybd.h"
33 #include "usbh_hid_parser.h"
35 /** @addtogroup USBH_LIB
36 * @{
39 /** @addtogroup USBH_CLASS
40 * @{
43 /** @addtogroup USBH_HID_CLASS
44 * @{
47 /** @defgroup USBH_HID_KEYBD
48 * @brief This file includes HID Layer Handlers for USB Host HID class.
49 * @{
50 */
52 /** @defgroup USBH_HID_KEYBD_Private_TypesDefinitions
53 * @{
54 */
55 /**
56 * @}
57 */
60 /** @defgroup USBH_HID_KEYBD_Private_Defines
61 * @{
62 */
63 /**
64 * @}
65 */
66 #ifndef AZERTY_KEYBOARD
67 #define QWERTY_KEYBOARD
68 #endif
69 #define KBD_LEFT_CTRL 0x01
70 #define KBD_LEFT_SHIFT 0x02
71 #define KBD_LEFT_ALT 0x04
72 #define KBD_LEFT_GUI 0x08
73 #define KBD_RIGHT_CTRL 0x10
74 #define KBD_RIGHT_SHIFT 0x20
75 #define KBD_RIGHT_ALT 0x40
76 #define KBD_RIGHT_GUI 0x80
77 #define KBR_MAX_NBR_PRESSED 6
79 /** @defgroup USBH_HID_KEYBD_Private_Macros
80 * @{
81 */
82 /**
83 * @}
84 */
86 /** @defgroup USBH_HID_KEYBD_Private_FunctionPrototypes
87 * @{
88 */
89 static USBH_StatusTypeDef USBH_HID_KeybdDecode(USBH_HandleTypeDef *phost);
90 /**
91 * @}
92 */
94 /** @defgroup USBH_HID_KEYBD_Private_Variables
95 * @{
98 HID_KEYBD_Info_TypeDef keybd_info;
99 uint32_t keybd_report_data[2];
101 static const HID_Report_ItemTypedef imp_0_lctrl={
102 (uint8_t*)keybd_report_data+0, /*data*/
103 1, /*size*/
104 0, /*shift*/
105 0, /*count (only for array items)*/
106 0, /*signed?*/
107 0, /*min value read can return*/
108 1, /*max value read can return*/
109 0, /*min vale device can report*/
110 1, /*max value device can report*/
111 1 /*resolution*/
113 static const HID_Report_ItemTypedef imp_0_lshift={
114 (uint8_t*)keybd_report_data+0, /*data*/
115 1, /*size*/
116 1, /*shift*/
117 0, /*count (only for array items)*/
118 0, /*signed?*/
119 0, /*min value read can return*/
120 1, /*max value read can return*/
121 0, /*min vale device can report*/
122 1, /*max value device can report*/
123 1 /*resolution*/
125 static const HID_Report_ItemTypedef imp_0_lalt={
126 (uint8_t*)keybd_report_data+0, /*data*/
127 1, /*size*/
128 2, /*shift*/
129 0, /*count (only for array items)*/
130 0, /*signed?*/
131 0, /*min value read can return*/
132 1, /*max value read can return*/
133 0, /*min vale device can report*/
134 1, /*max value device can report*/
135 1 /*resolution*/
137 static const HID_Report_ItemTypedef imp_0_lgui={
138 (uint8_t*)keybd_report_data+0, /*data*/
139 1, /*size*/
140 3, /*shift*/
141 0, /*count (only for array items)*/
142 0, /*signed?*/
143 0, /*min value read can return*/
144 1, /*max value read can return*/
145 0, /*min vale device can report*/
146 1, /*max value device can report*/
147 1 /*resolution*/
149 static const HID_Report_ItemTypedef imp_0_rctrl={
150 (uint8_t*)keybd_report_data+0, /*data*/
151 1, /*size*/
152 4, /*shift*/
153 0, /*count (only for array items)*/
154 0, /*signed?*/
155 0, /*min value read can return*/
156 1, /*max value read can return*/
157 0, /*min vale device can report*/
158 1, /*max value device can report*/
159 1 /*resolution*/
161 static const HID_Report_ItemTypedef imp_0_rshift={
162 (uint8_t*)keybd_report_data+0, /*data*/
163 1, /*size*/
164 5, /*shift*/
165 0, /*count (only for array items)*/
166 0, /*signed?*/
167 0, /*min value read can return*/
168 1, /*max value read can return*/
169 0, /*min vale device can report*/
170 1, /*max value device can report*/
171 1 /*resolution*/
173 static const HID_Report_ItemTypedef imp_0_ralt={
174 (uint8_t*)keybd_report_data+0, /*data*/
175 1, /*size*/
176 6, /*shift*/
177 0, /*count (only for array items)*/
178 0, /*signed?*/
179 0, /*min value read can return*/
180 1, /*max value read can return*/
181 0, /*min vale device can report*/
182 1, /*max value device can report*/
183 1 /*resolution*/
185 static const HID_Report_ItemTypedef imp_0_rgui={
186 (uint8_t*)keybd_report_data+0, /*data*/
187 1, /*size*/
188 7, /*shift*/
189 0, /*count (only for array items)*/
190 0, /*signed?*/
191 0, /*min value read can return*/
192 1, /*max value read can return*/
193 0, /*min vale device can report*/
194 1, /*max value device can report*/
195 1 /*resolution*/
198 static const HID_Report_ItemTypedef imp_0_key_array={
199 (uint8_t*)keybd_report_data+2, /*data*/
200 8, /*size*/
201 0, /*shift*/
202 6, /*count (only for array items)*/
203 0, /*signed?*/
204 0, /*min value read can return*/
205 101, /*max value read can return*/
206 0, /*min vale device can report*/
207 101, /*max value device can report*/
208 1 /*resolution*/
211 #ifdef QWERTY_KEYBOARD
212 static const int8_t HID_KEYBRD_Key[] = {
213 '\0', '`', '1', '2', '3', '4', '5', '6',
214 '7', '8', '9', '0', '-', '=', '\0', '\r',
215 '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u',
216 'i', 'o', 'p', '[', ']', '\\',
217 '\0', 'a', 's', 'd', 'f', 'g', 'h', 'j',
218 'k', 'l', ';', '\'', '\0', '\n',
219 '\0', '\0', 'z', 'x', 'c', 'v', 'b', 'n',
220 'm', ',', '.', '/', '\0', '\0',
221 '\0', '\0', '\0', ' ', '\0', '\0', '\0', '\0',
222 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
223 '\0', '\0', '\0', '\0', '\0', '\r', '\0', '\0',
224 '\0', '\0', '\0', '\0', '\0', '\0',
225 '\0', '\0', '7', '4', '1',
226 '\0', '/', '8', '5', '2',
227 '0', '*', '9', '6', '3',
228 '.', '-', '+', '\0', '\n', '\0', '\0', '\0', '\0', '\0', '\0',
229 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
230 '\0', '\0', '\0', '\0'
233 static const int8_t HID_KEYBRD_ShiftKey[] = {
234 '\0', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
235 '_', '+', '\0', '\0', '\0', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U',
236 'I', 'O', 'P', '{', '}', '|', '\0', 'A', 'S', 'D', 'F', 'G',
237 'H', 'J', 'K', 'L', ':', '"', '\0', '\n', '\0', '\0', 'Z', 'X',
238 'C', 'V', 'B', 'N', 'M', '<', '>', '?', '\0', '\0', '\0', '\0',
239 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
240 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
241 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
242 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
243 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
244 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'
247 #else
249 static const int8_t HID_KEYBRD_Key[] = {
250 '\0', '`', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
251 '-', '=', '\0', '\r', '\t', 'a', 'z', 'e', 'r', 't', 'y', 'u',
252 'i', 'o', 'p', '[', ']', '\\', '\0', 'q', 's', 'd', 'f', 'g',
253 'h', 'j', 'k', 'l', 'm', '\0', '\0', '\n', '\0', '\0', 'w', 'x',
254 'c', 'v', 'b', 'n', ',', ';', ':', '!', '\0', '\0', '\0', '\0',
255 '\0', ' ', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
256 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\r', '\0', '\0', '\0',
257 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '7', '4', '1','\0', '/',
258 '8', '5', '2', '0', '*', '9', '6', '3', '.', '-', '+', '\0',
259 '\n', '\0', '\0', '\0', '\0', '\0', '\0','\0', '\0', '\0', '\0', '\0', '\0',
260 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'
263 static const int8_t HID_KEYBRD_ShiftKey[] = {
264 '\0', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_',
265 '+', '\0', '\0', '\0', 'A', 'Z', 'E', 'R', 'T', 'Y', 'U', 'I', 'O',
266 'P', '{', '}', '*', '\0', 'Q', 'S', 'D', 'F', 'G', 'H', 'J', 'K',
267 'L', 'M', '%', '\0', '\n', '\0', '\0', 'W', 'X', 'C', 'V', 'B', 'N',
268 '?', '.', '/', '\0', '\0', '\0','\0', '\0', '\0', '\0', '\0', '\0', '\0',
269 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
270 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
271 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
272 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
273 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'
275 #endif
277 static const uint8_t HID_KEYBRD_Codes[] = {
278 0, 0, 0, 0, 31, 50, 48, 33,
279 19, 34, 35, 36, 24, 37, 38, 39, /* 0x00 - 0x0F */
280 52, 51, 25, 26, 17, 20, 32, 21,
281 23, 49, 18, 47, 22, 46, 2, 3, /* 0x10 - 0x1F */
282 4, 5, 6, 7, 8, 9, 10, 11,
283 43, 110, 15, 16, 61, 12, 13, 27, /* 0x20 - 0x2F */
284 28, 29, 42, 40, 41, 1, 53, 54,
285 55, 30, 112, 113, 114, 115, 116, 117, /* 0x30 - 0x3F */
286 118, 119, 120, 121, 122, 123, 124, 125,
287 126, 75, 80, 85, 76, 81, 86, 89, /* 0x40 - 0x4F */
288 79, 84, 83, 90, 95, 100, 105, 106,
289 108, 93, 98, 103, 92, 97, 102, 91, /* 0x50 - 0x5F */
290 96, 101, 99, 104, 45, 129, 0, 0,
291 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60 - 0x6F */
292 0, 0, 0, 0, 0, 0, 0, 0,
293 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 - 0x7F */
294 0, 0, 0, 0, 0, 107, 0, 56,
295 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 - 0x8F */
296 0, 0, 0, 0, 0, 0, 0, 0,
297 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 - 0x9F */
298 0, 0, 0, 0, 0, 0, 0, 0,
299 0, 0, 0, 0, 0, 0, 0, 0, /* 0xA0 - 0xAF */
300 0, 0, 0, 0, 0, 0, 0, 0,
301 0, 0, 0, 0, 0, 0, 0, 0, /* 0xB0 - 0xBF */
302 0, 0, 0, 0, 0, 0, 0, 0,
303 0, 0, 0, 0, 0, 0, 0, 0, /* 0xC0 - 0xCF */
304 0, 0, 0, 0, 0, 0, 0, 0,
305 0, 0, 0, 0, 0, 0, 0, 0, /* 0xD0 - 0xDF */
306 58, 44, 60, 127, 64, 57, 62, 128 /* 0xE0 - 0xE7 */
310 * @brief USBH_HID_KeybdInit
311 * The function init the HID keyboard.
312 * @param phost: Host handle
313 * @retval USBH Status
315 USBH_StatusTypeDef USBH_HID_KeybdInit(USBH_HandleTypeDef *phost)
317 uint32_t x;
318 HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData;
320 keybd_info.lctrl=keybd_info.lshift= 0;
321 keybd_info.lalt=keybd_info.lgui= 0;
322 keybd_info.rctrl=keybd_info.rshift= 0;
323 keybd_info.ralt=keybd_info.rgui=0;
326 for(x=0; x< (sizeof(keybd_report_data)/sizeof(uint32_t)); x++)
328 keybd_report_data[x]=0;
331 if(HID_Handle->length > (sizeof(keybd_report_data)/sizeof(uint32_t)))
333 HID_Handle->length = (sizeof(keybd_report_data)/sizeof(uint32_t));
335 HID_Handle->pData = (uint8_t*)keybd_report_data;
336 fifo_init(&HID_Handle->fifo, phost->device.Data, HID_QUEUE_SIZE * sizeof(keybd_report_data));
338 return USBH_OK;
342 * @brief USBH_HID_GetKeybdInfo
343 * The function return keyboard information.
344 * @param phost: Host handle
345 * @retval keyboard information
347 HID_KEYBD_Info_TypeDef *USBH_HID_GetKeybdInfo(USBH_HandleTypeDef *phost)
349 if(USBH_HID_KeybdDecode(phost) == USBH_OK)
351 return &keybd_info;
353 else
355 return NULL;
360 * @brief USBH_HID_KeybdDecode
361 * The function decode keyboard data.
362 * @param phost: Host handle
363 * @retval USBH Status
365 static USBH_StatusTypeDef USBH_HID_KeybdDecode(USBH_HandleTypeDef *phost)
367 uint8_t x;
369 HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData;
370 if(HID_Handle->length == 0)
372 return USBH_FAIL;
374 /*Fill report */
375 if(fifo_read(&HID_Handle->fifo, &keybd_report_data, HID_Handle->length) == HID_Handle->length)
378 keybd_info.lctrl=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_lctrl, 0);
379 keybd_info.lshift=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_lshift, 0);
380 keybd_info.lalt=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_lalt, 0);
381 keybd_info.lgui=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_lgui, 0);
382 keybd_info.rctrl=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_rctrl, 0);
383 keybd_info.rshift=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_rshift, 0);
384 keybd_info.ralt=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_ralt, 0);
385 keybd_info.rgui=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_rgui, 0);
387 for(x=0; x < sizeof(keybd_info.keys); x++)
389 keybd_info.keys[x]=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_key_array, x);
392 return USBH_OK;
394 return USBH_FAIL;
398 * @brief USBH_HID_GetASCIICode
399 * The function decode keyboard data into ASCII characters.
400 * @param phost: Host handle
401 * @param info: Keyboard information
402 * @retval ASCII code
404 uint8_t USBH_HID_GetASCIICode(HID_KEYBD_Info_TypeDef *info)
406 uint8_t output;
407 if((info->lshift == 1) || (info->rshift))
409 output = HID_KEYBRD_ShiftKey[HID_KEYBRD_Codes[info->keys[0]]];
411 else
413 output = HID_KEYBRD_Key[HID_KEYBRD_Codes[info->keys[0]]];
415 return output;
417 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/