STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
usbh_hid_keybd.c
Go to the documentation of this file.
1 
31 /* Includes ------------------------------------------------------------------*/
32 #include "usbh_hid_keybd.h"
33 #include "usbh_hid_parser.h"
34 
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
78 
89 static USBH_StatusTypeDef USBH_HID_KeybdDecode(USBH_HandleTypeDef *phost);
99 uint32_t keybd_report_data[2];
100 
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*/
112 };
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*/
124 };
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*/
136 };
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*/
148 };
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*/
160 };
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*/
172 };
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*/
184 };
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*/
196 };
197 
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*/
209 };
210 
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'
231 };
232 
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'
245 };
246 
247 #else
248 
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'
261 };
262 
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'
274 };
275 #endif
276 
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 */
307 };
308 
316 {
317  uint32_t x;
318  HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData;
319 
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;
324 
325 
326  for(x=0; x< (sizeof(keybd_report_data)/sizeof(uint32_t)); x++)
327  {
328  keybd_report_data[x]=0;
329  }
330 
331  if(HID_Handle->length > (sizeof(keybd_report_data)/sizeof(uint32_t)))
332  {
333  HID_Handle->length = (sizeof(keybd_report_data)/sizeof(uint32_t));
334  }
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));
337 
338  return USBH_OK;
339 }
340 
348 {
349  if(USBH_HID_KeybdDecode(phost) == USBH_OK)
350  {
351  return &keybd_info;
352  }
353  else
354  {
355  return NULL;
356  }
357 }
358 
365 static USBH_StatusTypeDef USBH_HID_KeybdDecode(USBH_HandleTypeDef *phost)
366 {
367  uint8_t x;
368 
369  HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData;
370  if(HID_Handle->length == 0)
371  {
372  return USBH_FAIL;
373  }
374  /*Fill report */
375  if(fifo_read(&HID_Handle->fifo, &keybd_report_data, HID_Handle->length) == HID_Handle->length)
376  {
377 
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);
386 
387  for(x=0; x < sizeof(keybd_info.keys); x++)
388  {
389  keybd_info.keys[x]=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_key_array, x);
390  }
391 
392  return USBH_OK;
393  }
394  return USBH_FAIL;
395 }
396 
405 {
406  uint8_t output;
407  if((info->lshift == 1) || (info->rshift))
408  {
409  output = HID_KEYBRD_ShiftKey[HID_KEYBRD_Codes[info->keys[0]]];
410  }
411  else
412  {
413  output = HID_KEYBRD_Key[HID_KEYBRD_Codes[info->keys[0]]];
414  }
415  return output;
416 }
417 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
418 
#define HID_QUEUE_SIZE
Definition: usbh_hid.h:67
USBH_DeviceTypeDef device
Definition: usbh_def.h:456
uint16_t length
Definition: usbh_hid.h:226
This file contains all the prototypes for the usbh_hid_keybd.c.
FIFO_TypeDef fifo
Definition: usbh_hid.h:224
uint8_t USBH_HID_GetASCIICode(HID_KEYBD_Info_TypeDef *info)
USBH_HID_GetASCIICode The function decode keyboard data into ASCII characters.
#define NULL
Definition: usbd_def.h:53
HID_KEYBD_Info_TypeDef keybd_info
USBH_StatusTypeDef USBH_HID_KeybdInit(USBH_HandleTypeDef *phost)
USBH_HID_KeybdInit The function init the HID keyboard.
uint8_t * pData
Definition: usbh_hid.h:225
HID_KEYBD_Info_TypeDef * USBH_HID_GetKeybdInfo(USBH_HandleTypeDef *phost)
USBH_HID_GetKeybdInfo The function return keyboard information.
uint32_t keybd_report_data[2]
USBH_StatusTypeDef
Definition: usbh_def.h:302
uint16_t fifo_read(FIFO_TypeDef *f, void *buf, uint16_t nbytes)
fifo_read Read from FIFO.
Definition: usbh_hid.c:723
uint8_t Data[USBH_MAX_DATA_BUFFER]
Definition: usbh_def.h:424
USBH_ClassTypeDef * pActiveClass
Definition: usbh_def.h:458
void fifo_init(FIFO_TypeDef *f, uint8_t *buf, uint16_t size)
fifo_init Initialize FIFO.
Definition: usbh_hid.c:706
uint32_t HID_ReadItem(HID_Report_ItemTypedef *ri, uint8_t ndx)
HID_ReadItem The function read a report item.