xref: /btstack/port/stm32-f4discovery-usb/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Src/usbh_hid_keybd.c (revision a8f7f3fcbcd51f8d2e92aca076b6a9f812db358c)
1 /**
2   ******************************************************************************
3   * @file    usbh_hid_keybd.c
4   * @author  MCD Application Team
5   * @brief   This file is the application layer for USB Host HID Keyboard handling
6   *          QWERTY and AZERTY Keyboard are supported as per the selection in
7   *          usbh_hid_keybd.h
8   ******************************************************************************
9   * @attention
10   *
11   * <h2><center>&copy; Copyright (c) 2015 STMicroelectronics.
12   * All rights reserved.</center></h2>
13   *
14   * This software component is licensed by ST under Ultimate Liberty license
15   * SLA0044, the "License"; You may not use this file except in compliance with
16   * the License. You may obtain a copy of the License at:
17   *                      www.st.com/SLA0044
18   *
19   ******************************************************************************
20   */
21 
22 /* BSPDependencies
23 - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c"
24 - "stm32xxxxx_{eval}{discovery}_io.c"
25 - "stm32xxxxx_{eval}{discovery}{adafruit}_lcd.c"
26 - "stm32xxxxx_{eval}{discovery}_sdram.c"
27 EndBSPDependencies */
28 
29 /* Includes ------------------------------------------------------------------*/
30 #include "usbh_hid_keybd.h"
31 #include "usbh_hid_parser.h"
32 
33 /** @addtogroup USBH_LIB
34 * @{
35 */
36 
37 /** @addtogroup USBH_CLASS
38 * @{
39 */
40 
41 /** @addtogroup USBH_HID_CLASS
42 * @{
43 */
44 
45 /** @defgroup USBH_HID_KEYBD
46 * @brief    This file includes HID Layer Handlers for USB Host HID class.
47 * @{
48 */
49 
50 /** @defgroup USBH_HID_KEYBD_Private_TypesDefinitions
51 * @{
52 */
53 /**
54 * @}
55 */
56 
57 
58 /** @defgroup USBH_HID_KEYBD_Private_Defines
59 * @{
60 */
61 /**
62 * @}
63 */
64 #ifndef AZERTY_KEYBOARD
65 #define QWERTY_KEYBOARD
66 #endif
67 #define  KBD_LEFT_CTRL                                  0x01
68 #define  KBD_LEFT_SHIFT                                 0x02
69 #define  KBD_LEFT_ALT                                   0x04
70 #define  KBD_LEFT_GUI                                   0x08
71 #define  KBD_RIGHT_CTRL                                 0x10
72 #define  KBD_RIGHT_SHIFT                                0x20
73 #define  KBD_RIGHT_ALT                                  0x40
74 #define  KBD_RIGHT_GUI                                  0x80
75 #define  KBR_MAX_NBR_PRESSED                            6
76 
77 /** @defgroup USBH_HID_KEYBD_Private_Macros
78 * @{
79 */
80 /**
81 * @}
82 */
83 
84 /** @defgroup USBH_HID_KEYBD_Private_FunctionPrototypes
85 * @{
86 */
87 static USBH_StatusTypeDef USBH_HID_KeybdDecode(USBH_HandleTypeDef *phost);
88 /**
89 * @}
90 */
91 
92 /** @defgroup USBH_HID_KEYBD_Private_Variables
93 * @{
94 */
95 
96 HID_KEYBD_Info_TypeDef     keybd_info;
97 uint32_t                   keybd_rx_report_buf[2];
98 uint32_t                   keybd_report_data[2];
99 
100 static const HID_Report_ItemTypedef imp_0_lctrl =
101 {
102   (uint8_t *)(void *)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*/
112 };
113 static const HID_Report_ItemTypedef imp_0_lshift =
114 {
115   (uint8_t *)(void *)keybd_report_data + 0, /*data*/
116   1,     /*size*/
117   1,     /*shift*/
118   0,     /*count (only for array items)*/
119   0,     /*signed?*/
120   0,     /*min value read can return*/
121   1,     /*max value read can return*/
122   0,     /*min vale device can report*/
123   1,     /*max value device can report*/
124   1      /*resolution*/
125 };
126 static const HID_Report_ItemTypedef imp_0_lalt =
127 {
128   (uint8_t *)(void *)keybd_report_data + 0, /*data*/
129   1,     /*size*/
130   2,     /*shift*/
131   0,     /*count (only for array items)*/
132   0,     /*signed?*/
133   0,     /*min value read can return*/
134   1,     /*max value read can return*/
135   0,     /*min vale device can report*/
136   1,     /*max value device can report*/
137   1      /*resolution*/
138 };
139 static const HID_Report_ItemTypedef imp_0_lgui =
140 {
141   (uint8_t *)(void *)keybd_report_data + 0, /*data*/
142   1,     /*size*/
143   3,     /*shift*/
144   0,     /*count (only for array items)*/
145   0,     /*signed?*/
146   0,     /*min value read can return*/
147   1,     /*max value read can return*/
148   0,     /*min vale device can report*/
149   1,     /*max value device can report*/
150   1      /*resolution*/
151 };
152 static const HID_Report_ItemTypedef imp_0_rctrl =
153 {
154   (uint8_t *)(void *)keybd_report_data + 0, /*data*/
155   1,     /*size*/
156   4,     /*shift*/
157   0,     /*count (only for array items)*/
158   0,     /*signed?*/
159   0,     /*min value read can return*/
160   1,     /*max value read can return*/
161   0,     /*min vale device can report*/
162   1,     /*max value device can report*/
163   1      /*resolution*/
164 };
165 static const HID_Report_ItemTypedef imp_0_rshift =
166 {
167   (uint8_t *)(void *)keybd_report_data + 0, /*data*/
168   1,     /*size*/
169   5,     /*shift*/
170   0,     /*count (only for array items)*/
171   0,     /*signed?*/
172   0,     /*min value read can return*/
173   1,     /*max value read can return*/
174   0,     /*min vale device can report*/
175   1,     /*max value device can report*/
176   1      /*resolution*/
177 };
178 static const HID_Report_ItemTypedef imp_0_ralt =
179 {
180   (uint8_t *)(void *)keybd_report_data + 0, /*data*/
181   1,     /*size*/
182   6,     /*shift*/
183   0,     /*count (only for array items)*/
184   0,     /*signed?*/
185   0,     /*min value read can return*/
186   1,     /*max value read can return*/
187   0,     /*min vale device can report*/
188   1,     /*max value device can report*/
189   1      /*resolution*/
190 };
191 static const HID_Report_ItemTypedef imp_0_rgui =
192 {
193   (uint8_t *)(void *)keybd_report_data + 0, /*data*/
194   1,     /*size*/
195   7,     /*shift*/
196   0,     /*count (only for array items)*/
197   0,     /*signed?*/
198   0,     /*min value read can return*/
199   1,     /*max value read can return*/
200   0,     /*min vale device can report*/
201   1,     /*max value device can report*/
202   1      /*resolution*/
203 };
204 
205 static const HID_Report_ItemTypedef imp_0_key_array =
206 {
207   (uint8_t *)(void *)keybd_report_data + 2, /*data*/
208   8,     /*size*/
209   0,     /*shift*/
210   6,     /*count (only for array items)*/
211   0,     /*signed?*/
212   0,     /*min value read can return*/
213   101,   /*max value read can return*/
214   0,     /*min vale device can report*/
215   101,   /*max value device can report*/
216   1      /*resolution*/
217 };
218 
219 #ifdef QWERTY_KEYBOARD
220 static  const  uint8_t  HID_KEYBRD_Key[] =
221 {
222   '\0',  '`',  '1',  '2',  '3',  '4',  '5',  '6',
223   '7',  '8',  '9',  '0',  '-',  '=',  '\0', '\r',
224   '\t',  'q',  'w',  'e',  'r',  't',  'y',  'u',
225   'i',  'o',  'p',  '[',  ']',  '\\',
226   '\0',  'a',  's',  'd',  'f',  'g',  'h',  'j',
227   'k',  'l',  ';',  '\'', '\0', '\n',
228   '\0',  '\0', 'z',  'x',  'c',  'v',  'b',  'n',
229   'm',  ',',  '.',  '/',  '\0', '\0',
230   '\0',  '\0', '\0', ' ',  '\0', '\0', '\0', '\0',
231   '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
232   '\0',  '\0', '\0', '\0', '\0', '\r', '\0', '\0',
233   '\0', '\0', '\0', '\0', '\0', '\0',
234   '\0',  '\0', '7',  '4',  '1',
235   '\0',  '/',  '8',  '5',  '2',
236   '0',   '*',  '9',  '6',  '3',
237   '.',   '-',  '+',  '\0', '\n', '\0', '\0', '\0', '\0', '\0', '\0',
238   '\0',  '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
239   '\0', '\0', '\0', '\0'
240 };
241 
242 static  const  uint8_t  HID_KEYBRD_ShiftKey[] =
243 {
244   '\0', '~',  '!',  '@',  '#',  '$',  '%',  '^',  '&',  '*',  '(',  ')',
245   '_',  '+',  '\0', '\0', '\0', 'Q',  'W',  'E',  'R',  'T',  'Y',  'U',
246   'I',  'O',  'P',  '{',  '}',  '|',  '\0', 'A',  'S',  'D',  'F',  'G',
247   'H',  'J',  'K',  'L',  ':',  '"',  '\0', '\n', '\0', '\0', 'Z',  'X',
248   'C',  'V',  'B',  'N',  'M',  '<',  '>',  '?',  '\0', '\0',  '\0', '\0',
249   '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
250   '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
251   '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
252   '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
253   '\0', '\0', '\0', '\0', '\0', '\0', '\0',    '\0', '\0', '\0', '\0', '\0',
254   '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'
255 };
256 
257 #else
258 
259 static  const  uint8_t  HID_KEYBRD_Key[] =
260 {
261   '\0',  '`',  '1',  '2',  '3',  '4',  '5',  '6',  '7',  '8',  '9',  '0',
262   '-',  '=',  '\0', '\r', '\t',  'a',  'z',  'e',  'r',  't',  'y',  'u',
263   'i',  'o',  'p',  '[',  ']', '\\', '\0',  'q',  's',  'd',  'f',  'g',
264   'h',  'j',  'k',  'l',  'm',  '\0', '\0', '\n', '\0',  '\0', 'w',  'x',
265   'c',  'v',  'b',  'n',  ',',  ';',  ':',  '!',  '\0', '\0', '\0',  '\0',
266   '\0', ' ',  '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
267   '\0', '\0', '\0', '\0',  '\0', '\0', '\0', '\0', '\r', '\0', '\0', '\0',
268   '\0', '\0', '\0', '\0', '\0', '\0',  '\0', '7',  '4',  '1', '\0',  '/',
269   '8',  '5',  '2', '0',   '*',  '9',  '6',  '3', '.',   '-',  '+',  '\0',
270   '\n', '\0', '\0', '\0', '\0', '\0', '\0', '\0',  '\0', '\0', '\0', '\0', '\0',
271   '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'
272 };
273 
274 static  const  uint8_t  HID_KEYBRD_ShiftKey[] =
275 {
276   '\0', '~',  '!',  '@',  '#',  '$',  '%',  '^',  '&',  '*',  '(',  ')',  '_',
277   '+',  '\0', '\0', '\0', 'A',  'Z',  'E',  'R',  'T',  'Y',  'U',  'I',  'O',
278   'P',  '{',  '}',  '*', '\0', 'Q',  'S',  'D',  'F',  'G',  'H',  'J',  'K',
279   'L',  'M',  '%',  '\0', '\n', '\0', '\0', 'W',  'X',  'C',  'V',  'B',  'N',
280   '?',  '.',  '/',  '\0',  '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
281   '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
282   '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
283   '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
284   '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
285   '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'
286 };
287 #endif
288 
289 static  const  uint8_t  HID_KEYBRD_Codes[] =
290 {
291   0,     0,    0,    0,   31,   50,   48,   33,
292   19,   34,   35,   36,   24,   37,   38,   39,       /* 0x00 - 0x0F */
293   52,    51,   25,   26,   17,   20,   32,   21,
294   23,   49,   18,   47,   22,   46,    2,    3,       /* 0x10 - 0x1F */
295   4,    5,    6,    7,    8,    9,   10,   11,
296   43,  110,   15,   16,   61,   12,   13,   27,       /* 0x20 - 0x2F */
297   28,   29,   42,   40,   41,    1,   53,   54,
298   55,   30,  112,  113,  114,  115,  116,  117,       /* 0x30 - 0x3F */
299   118,  119,  120,  121,  122,  123,  124,  125,
300   126,   75,   80,   85,   76,   81,   86,   89,       /* 0x40 - 0x4F */
301   79,   84,   83,   90,   95,  100,  105,  106,
302   108,   93,   98,  103,   92,   97,  102,   91,       /* 0x50 - 0x5F */
303   96,  101,   99,  104,   45,  129,    0,    0,
304   0,    0,    0,    0,    0,    0,    0,    0,       /* 0x60 - 0x6F */
305   0,    0,    0,    0,    0,    0,    0,    0,
306   0,    0,    0,    0,    0,    0,    0,    0,       /* 0x70 - 0x7F */
307   0,    0,    0,    0,    0,  107,    0,   56,
308   0,    0,    0,    0,    0,    0,    0,    0,       /* 0x80 - 0x8F */
309   0,    0,    0,    0,    0,    0,    0,    0,
310   0,    0,    0,    0,    0,    0,    0,    0,       /* 0x90 - 0x9F */
311   0,    0,    0,    0,    0,    0,    0,    0,
312   0,    0,    0,    0,    0,    0,    0,    0,       /* 0xA0 - 0xAF */
313   0,    0,    0,    0,    0,    0,    0,    0,
314   0,    0,    0,    0,    0,    0,    0,    0,       /* 0xB0 - 0xBF */
315   0,    0,    0,    0,    0,    0,    0,    0,
316   0,    0,    0,    0,    0,    0,    0,    0,       /* 0xC0 - 0xCF */
317   0,    0,    0,    0,    0,    0,    0,    0,
318   0,    0,    0,    0,    0,    0,    0,    0,       /* 0xD0 - 0xDF */
319   58,   44,   60,  127,   64,   57,   62,  128        /* 0xE0 - 0xE7 */
320 };
321 
322 /**
323   * @brief  USBH_HID_KeybdInit
324   *         The function init the HID keyboard.
325   * @param  phost: Host handle
326   * @retval USBH Status
327   */
USBH_HID_KeybdInit(USBH_HandleTypeDef * phost)328 USBH_StatusTypeDef USBH_HID_KeybdInit(USBH_HandleTypeDef *phost)
329 {
330   uint32_t x;
331   HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData;
332 
333   keybd_info.lctrl = keybd_info.lshift = 0U;
334   keybd_info.lalt = keybd_info.lgui = 0U;
335   keybd_info.rctrl = keybd_info.rshift = 0U;
336   keybd_info.ralt = keybd_info.rgui = 0U;
337 
338 
339   for (x = 0U; x < (sizeof(keybd_report_data) / sizeof(uint32_t)); x++)
340   {
341     keybd_report_data[x] = 0U;
342     keybd_rx_report_buf[x] = 0U;
343   }
344 
345   if (HID_Handle->length > (sizeof(keybd_report_data)))
346   {
347     HID_Handle->length = (sizeof(keybd_report_data));
348   }
349   HID_Handle->pData = (uint8_t *)(void *)keybd_rx_report_buf;
350   USBH_HID_FifoInit(&HID_Handle->fifo, phost->device.Data, HID_QUEUE_SIZE * sizeof(keybd_report_data));
351 
352   return USBH_OK;
353 }
354 
355 /**
356   * @brief  USBH_HID_GetKeybdInfo
357   *         The function return keyboard information.
358   * @param  phost: Host handle
359   * @retval keyboard information
360   */
USBH_HID_GetKeybdInfo(USBH_HandleTypeDef * phost)361 HID_KEYBD_Info_TypeDef *USBH_HID_GetKeybdInfo(USBH_HandleTypeDef *phost)
362 {
363   if (USBH_HID_KeybdDecode(phost) == USBH_OK)
364   {
365     return &keybd_info;
366   }
367   else
368   {
369     return NULL;
370   }
371 }
372 
373 /**
374   * @brief  USBH_HID_KeybdDecode
375   *         The function decode keyboard data.
376   * @param  phost: Host handle
377   * @retval USBH Status
378   */
USBH_HID_KeybdDecode(USBH_HandleTypeDef * phost)379 static USBH_StatusTypeDef USBH_HID_KeybdDecode(USBH_HandleTypeDef *phost)
380 {
381   uint8_t x;
382 
383   HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData;
384   if (HID_Handle->length == 0U)
385   {
386     return USBH_FAIL;
387   }
388   /*Fill report */
389   if (USBH_HID_FifoRead(&HID_Handle->fifo, &keybd_report_data, HID_Handle->length) ==  HID_Handle->length)
390   {
391     keybd_info.lctrl = (uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_lctrl, 0U);
392     keybd_info.lshift = (uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_lshift, 0U);
393     keybd_info.lalt = (uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_lalt, 0U);
394     keybd_info.lgui = (uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_lgui, 0U);
395     keybd_info.rctrl = (uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_rctrl, 0U);
396     keybd_info.rshift = (uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_rshift, 0U);
397     keybd_info.ralt = (uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_ralt, 0U);
398     keybd_info.rgui = (uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_rgui, 0U);
399 
400     for (x = 0U; x < sizeof(keybd_info.keys); x++)
401     {
402       keybd_info.keys[x] = (uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_key_array, x);
403     }
404 
405     return USBH_OK;
406   }
407   return   USBH_FAIL;
408 }
409 
410 /**
411   * @brief  USBH_HID_GetASCIICode
412   *         The function decode keyboard data into ASCII characters.
413   * @param  phost: Host handle
414   * @param  info: Keyboard information
415   * @retval ASCII code
416   */
USBH_HID_GetASCIICode(HID_KEYBD_Info_TypeDef * info)417 uint8_t USBH_HID_GetASCIICode(HID_KEYBD_Info_TypeDef *info)
418 {
419   uint8_t   output;
420   if ((info->lshift == 1U) || (info->rshift))
421   {
422     output =  HID_KEYBRD_ShiftKey[HID_KEYBRD_Codes[info->keys[0]]];
423   }
424   else
425   {
426     output =  HID_KEYBRD_Key[HID_KEYBRD_Codes[info->keys[0]]];
427   }
428   return output;
429 }
430 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
431 
432