Nirvana Jay / Mbed 2 deprecated F7DISCO_Demo

Dependencies:   BSP_DISCO_F746NG_patch mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers usbh_hid_keybd.c Source File

usbh_hid_keybd.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    usbh_hid_keybd.c 
00004   * @author  MCD Application Team
00005   * @version V3.2.2
00006   * @date    07-July-2015
00007   * @brief   This file is the application layer for USB Host HID Keyboard handling
00008   *          QWERTY and AZERTY Keyboard are supported as per the selection in 
00009   *          usbh_hid_keybd.h              
00010   ******************************************************************************
00011   * @attention
00012   *
00013   * <h2><center>&copy; COPYRIGHT 2015 STMicroelectronics</center></h2>
00014   *
00015   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
00016   * You may not use this file except in compliance with the License.
00017   * You may obtain a copy of the License at:
00018   *
00019   *        http://www.st.com/software_license_agreement_liberty_v2
00020   *
00021   * Unless required by applicable law or agreed to in writing, software 
00022   * distributed under the License is distributed on an "AS IS" BASIS, 
00023   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00024   * See the License for the specific language governing permissions and
00025   * limitations under the License.
00026   *
00027   ******************************************************************************
00028   */ 
00029 
00030 
00031 /* Includes ------------------------------------------------------------------*/
00032 #include "usbh_hid_keybd.h"
00033 #include "usbh_hid_parser.h"
00034 
00035 /** @addtogroup USBH_LIB
00036 * @{
00037 */
00038 
00039 /** @addtogroup USBH_CLASS
00040 * @{
00041 */
00042 
00043 /** @addtogroup USBH_HID_CLASS
00044 * @{
00045 */
00046 
00047 /** @defgroup USBH_HID_KEYBD 
00048 * @brief    This file includes HID Layer Handlers for USB Host HID class.
00049 * @{
00050 */ 
00051 
00052 /** @defgroup USBH_HID_KEYBD_Private_TypesDefinitions
00053 * @{
00054 */ 
00055 /**
00056 * @}
00057 */ 
00058 
00059 
00060 /** @defgroup USBH_HID_KEYBD_Private_Defines
00061 * @{
00062 */ 
00063 /**
00064 * @}
00065 */ 
00066 #ifndef AZERTY_KEYBOARD
00067   #define QWERTY_KEYBOARD
00068 #endif
00069 #define  KBD_LEFT_CTRL                                  0x01
00070 #define  KBD_LEFT_SHIFT                                 0x02
00071 #define  KBD_LEFT_ALT                                   0x04
00072 #define  KBD_LEFT_GUI                                   0x08
00073 #define  KBD_RIGHT_CTRL                                 0x10
00074 #define  KBD_RIGHT_SHIFT                                0x20
00075 #define  KBD_RIGHT_ALT                                  0x40
00076 #define  KBD_RIGHT_GUI                                  0x80
00077 #define  KBR_MAX_NBR_PRESSED                            6
00078 
00079 /** @defgroup USBH_HID_KEYBD_Private_Macros
00080 * @{
00081 */ 
00082 /**
00083 * @}
00084 */ 
00085 
00086 /** @defgroup USBH_HID_KEYBD_Private_FunctionPrototypes
00087 * @{
00088 */ 
00089 static USBH_StatusTypeDef USBH_HID_KeybdDecode(USBH_HandleTypeDef *phost);
00090 /**
00091 * @}
00092 */ 
00093  
00094 /** @defgroup USBH_HID_KEYBD_Private_Variables
00095 * @{
00096 */
00097 
00098 HID_KEYBD_Info_TypeDef     keybd_info;
00099 uint32_t                   keybd_report_data[2];
00100 
00101 static const HID_Report_ItemTypedef imp_0_lctrl={
00102   (uint8_t*)keybd_report_data+0, /*data*/
00103   1,     /*size*/
00104   0,     /*shift*/
00105   0,     /*count (only for array items)*/ 
00106   0,     /*signed?*/ 
00107   0,     /*min value read can return*/
00108   1,     /*max value read can return*/
00109   0,     /*min vale device can report*/
00110   1,     /*max value device can report*/
00111   1      /*resolution*/
00112 };
00113 static const HID_Report_ItemTypedef imp_0_lshift={
00114   (uint8_t*)keybd_report_data+0, /*data*/
00115   1,     /*size*/
00116   1,     /*shift*/
00117   0,     /*count (only for array items)*/  
00118   0,     /*signed?*/ 
00119   0,     /*min value read can return*/
00120   1,     /*max value read can return*/
00121   0,     /*min vale device can report*/
00122   1,     /*max value device can report*/
00123   1      /*resolution*/
00124 };
00125 static const HID_Report_ItemTypedef imp_0_lalt={
00126   (uint8_t*)keybd_report_data+0, /*data*/
00127   1,     /*size*/
00128   2,     /*shift*/
00129   0,     /*count (only for array items)*/  
00130   0,     /*signed?*/   
00131   0,     /*min value read can return*/
00132   1,     /*max value read can return*/
00133   0,     /*min vale device can report*/
00134   1,     /*max value device can report*/
00135   1      /*resolution*/
00136 };
00137 static const HID_Report_ItemTypedef imp_0_lgui={
00138   (uint8_t*)keybd_report_data+0, /*data*/
00139   1,     /*size*/
00140   3,     /*shift*/
00141   0,     /*count (only for array items)*/  
00142   0,     /*signed?*/
00143   0,     /*min value read can return*/
00144   1,     /*max value read can return*/
00145   0,     /*min vale device can report*/
00146   1,     /*max value device can report*/
00147   1      /*resolution*/
00148 };
00149 static const HID_Report_ItemTypedef imp_0_rctrl={
00150   (uint8_t*)keybd_report_data+0, /*data*/
00151   1,     /*size*/
00152   4,     /*shift*/
00153   0,     /*count (only for array items)*/  
00154   0,     /*signed?*/
00155   0,     /*min value read can return*/
00156   1,     /*max value read can return*/
00157   0,     /*min vale device can report*/
00158   1,     /*max value device can report*/
00159   1      /*resolution*/
00160 };
00161 static const HID_Report_ItemTypedef imp_0_rshift={
00162   (uint8_t*)keybd_report_data+0, /*data*/
00163   1,     /*size*/
00164   5,     /*shift*/
00165   0,     /*count (only for array items)*/  
00166   0,     /*signed?*/
00167   0,     /*min value read can return*/
00168   1,     /*max value read can return*/
00169   0,     /*min vale device can report*/
00170   1,     /*max value device can report*/
00171   1      /*resolution*/
00172 };
00173 static const HID_Report_ItemTypedef imp_0_ralt={
00174   (uint8_t*)keybd_report_data+0, /*data*/
00175   1,     /*size*/
00176   6,     /*shift*/
00177   0,     /*count (only for array items)*/  
00178   0,     /*signed?*/
00179   0,     /*min value read can return*/
00180   1,     /*max value read can return*/
00181   0,     /*min vale device can report*/
00182   1,     /*max value device can report*/
00183   1      /*resolution*/
00184 };
00185 static const HID_Report_ItemTypedef imp_0_rgui={
00186   (uint8_t*)keybd_report_data+0, /*data*/
00187   1,     /*size*/
00188   7,     /*shift*/
00189   0,     /*count (only for array items)*/  
00190   0,     /*signed?*/
00191   0,     /*min value read can return*/
00192   1,     /*max value read can return*/
00193   0,     /*min vale device can report*/
00194   1,     /*max value device can report*/
00195   1      /*resolution*/
00196 };
00197 
00198 static const HID_Report_ItemTypedef imp_0_key_array={
00199   (uint8_t*)keybd_report_data+2, /*data*/
00200   8,     /*size*/
00201   0,     /*shift*/
00202   6,     /*count (only for array items)*/
00203   0,     /*signed?*/  
00204   0,     /*min value read can return*/
00205   101,   /*max value read can return*/
00206   0,     /*min vale device can report*/
00207   101,   /*max value device can report*/
00208   1      /*resolution*/
00209 };
00210 
00211 #ifdef QWERTY_KEYBOARD
00212 static  const  int8_t  HID_KEYBRD_Key[] = {
00213   '\0',  '`',  '1',  '2',  '3',  '4',  '5',  '6',
00214   '7',  '8',  '9',  '0',  '-',  '=',  '\0', '\r',
00215   '\t',  'q',  'w',  'e',  'r',  't',  'y',  'u', 
00216   'i',  'o',  'p',  '[',  ']',  '\\',
00217   '\0',  'a',  's',  'd',  'f',  'g',  'h',  'j',  
00218   'k',  'l',  ';',  '\'', '\0', '\n',
00219   '\0',  '\0', 'z',  'x',  'c',  'v',  'b',  'n', 
00220   'm',  ',',  '.',  '/',  '\0', '\0',
00221   '\0',  '\0', '\0', ' ',  '\0', '\0', '\0', '\0', 
00222   '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
00223   '\0',  '\0', '\0', '\0', '\0', '\r', '\0', '\0', 
00224   '\0', '\0', '\0', '\0', '\0', '\0',
00225   '\0',  '\0', '7',  '4',  '1',
00226   '\0',  '/',  '8',  '5',  '2',
00227   '0',   '*',  '9',  '6',  '3',
00228   '.',   '-',  '+',  '\0', '\n', '\0', '\0', '\0', '\0', '\0', '\0',
00229   '\0',  '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', 
00230   '\0', '\0', '\0', '\0'
00231 };
00232 
00233 static  const  int8_t  HID_KEYBRD_ShiftKey[] = {
00234   '\0', '~',  '!',  '@',  '#',  '$',  '%',  '^',  '&',  '*',  '(',  ')',
00235   '_',  '+',  '\0', '\0', '\0', 'Q',  'W',  'E',  'R',  'T',  'Y',  'U', 
00236   'I',  'O',  'P',  '{',  '}',  '|',  '\0', 'A',  'S',  'D',  'F',  'G', 
00237   'H',  'J',  'K',  'L',  ':',  '"',  '\0', '\n', '\0', '\0', 'Z',  'X',  
00238   'C',  'V',  'B',  'N',  'M',  '<',  '>',  '?',  '\0', '\0',  '\0', '\0',
00239   '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', 
00240   '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
00241   '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', 
00242   '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
00243   '\0', '\0', '\0', '\0', '\0', '\0', '\0',    '\0', '\0', '\0', '\0', '\0',
00244   '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'
00245 };
00246 
00247 #else
00248 
00249 static  const  int8_t  HID_KEYBRD_Key[] = {
00250   '\0',  '`',  '1',  '2',  '3',  '4',  '5',  '6',  '7',  '8',  '9',  '0',
00251   '-',  '=',  '\0', '\r', '\t',  'a',  'z',  'e',  'r',  't',  'y',  'u', 
00252   'i',  'o',  'p',  '[',  ']', '\\', '\0',  'q',  's',  'd',  'f',  'g', 
00253   'h',  'j',  'k',  'l',  'm',  '\0', '\0', '\n', '\0',  '\0', 'w',  'x', 
00254   'c',  'v',  'b',  'n',  ',',  ';',  ':',  '!',  '\0', '\0', '\0',  '\0', 
00255   '\0', ' ',  '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', 
00256   '\0', '\0', '\0', '\0',  '\0', '\0', '\0', '\0', '\r', '\0', '\0', '\0', 
00257   '\0', '\0', '\0', '\0', '\0', '\0',  '\0', '7',  '4',  '1','\0',  '/', 
00258   '8',  '5',  '2', '0',   '*',  '9',  '6',  '3', '.',   '-',  '+',  '\0', 
00259   '\n', '\0', '\0', '\0', '\0', '\0', '\0','\0',  '\0', '\0', '\0', '\0', '\0',
00260   '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'
00261 };
00262 
00263 static  const  int8_t  HID_KEYBRD_ShiftKey[] = {
00264   '\0', '~',  '!',  '@',  '#',  '$',  '%',  '^',  '&',  '*',  '(',  ')',  '_',
00265   '+',  '\0', '\0', '\0', 'A',  'Z',  'E',  'R',  'T',  'Y',  'U',  'I',  'O',
00266   'P',  '{',  '}',  '*', '\0', 'Q',  'S',  'D',  'F',  'G',  'H',  'J',  'K', 
00267   'L',  'M',  '%',  '\0', '\n', '\0', '\0', 'W',  'X',  'C',  'V',  'B',  'N',
00268   '?',  '.',  '/',  '\0',  '\0', '\0','\0', '\0', '\0', '\0', '\0', '\0', '\0', 
00269   '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
00270   '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
00271   '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
00272   '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', 
00273   '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'
00274 };
00275 #endif
00276 
00277 static  const  uint8_t  HID_KEYBRD_Codes[] = {
00278   0,     0,    0,    0,   31,   50,   48,   33, 
00279   19,   34,   35,   36,   24,   37,   38,   39,       /* 0x00 - 0x0F */
00280   52,    51,   25,   26,   17,   20,   32,   21,
00281   23,   49,   18,   47,   22,   46,    2,    3,       /* 0x10 - 0x1F */
00282   4,    5,    6,    7,    8,    9,   10,   11, 
00283   43,  110,   15,   16,   61,   12,   13,   27,       /* 0x20 - 0x2F */
00284   28,   29,   42,   40,   41,    1,   53,   54,  
00285   55,   30,  112,  113,  114,  115,  116,  117,       /* 0x30 - 0x3F */
00286   118,  119,  120,  121,  122,  123,  124,  125,  
00287   126,   75,   80,   85,   76,   81,   86,   89,       /* 0x40 - 0x4F */
00288   79,   84,   83,   90,   95,  100,  105,  106,
00289   108,   93,   98,  103,   92,   97,  102,   91,       /* 0x50 - 0x5F */
00290   96,  101,   99,  104,   45,  129,    0,    0, 
00291   0,    0,    0,    0,    0,    0,    0,    0,       /* 0x60 - 0x6F */
00292   0,    0,    0,    0,    0,    0,    0,    0,
00293   0,    0,    0,    0,    0,    0,    0,    0,       /* 0x70 - 0x7F */
00294   0,    0,    0,    0,    0,  107,    0,   56,
00295   0,    0,    0,    0,    0,    0,    0,    0,       /* 0x80 - 0x8F */
00296   0,    0,    0,    0,    0,    0,    0,    0,
00297   0,    0,    0,    0,    0,    0,    0,    0,       /* 0x90 - 0x9F */
00298   0,    0,    0,    0,    0,    0,    0,    0,
00299   0,    0,    0,    0,    0,    0,    0,    0,       /* 0xA0 - 0xAF */
00300   0,    0,    0,    0,    0,    0,    0,    0, 
00301   0,    0,    0,    0,    0,    0,    0,    0,       /* 0xB0 - 0xBF */
00302   0,    0,    0,    0,    0,    0,    0,    0, 
00303   0,    0,    0,    0,    0,    0,    0,    0,       /* 0xC0 - 0xCF */
00304   0,    0,    0,    0,    0,    0,    0,    0, 
00305   0,    0,    0,    0,    0,    0,    0,    0,       /* 0xD0 - 0xDF */
00306   58,   44,   60,  127,   64,   57,   62,  128        /* 0xE0 - 0xE7 */
00307 };
00308 
00309 /**
00310   * @brief  USBH_HID_KeybdInit 
00311   *         The function init the HID keyboard.
00312   * @param  phost: Host handle
00313   * @retval USBH Status
00314   */
00315 USBH_StatusTypeDef USBH_HID_KeybdInit(USBH_HandleTypeDef *phost)
00316 {
00317   uint32_t x;
00318   HID_HandleTypeDef *HID_Handle =  (HID_HandleTypeDef *) phost->pActiveClass->pData;  
00319     
00320   keybd_info.lctrl=keybd_info.lshift= 0;
00321   keybd_info.lalt=keybd_info.lgui= 0;
00322   keybd_info.rctrl=keybd_info.rshift= 0;
00323   keybd_info.ralt=keybd_info.rgui=0;
00324   
00325   
00326   for(x=0; x< (sizeof(keybd_report_data)/sizeof(uint32_t)); x++)
00327   {
00328     keybd_report_data[x]=0;
00329   }
00330   
00331   if(HID_Handle->length > (sizeof(keybd_report_data)/sizeof(uint32_t)))
00332   {
00333     HID_Handle->length = (sizeof(keybd_report_data)/sizeof(uint32_t));
00334   }
00335   HID_Handle->pData = (uint8_t*)keybd_report_data;
00336   fifo_init(&HID_Handle->fifo, phost->device.Data, HID_QUEUE_SIZE * sizeof(keybd_report_data));
00337   
00338   return USBH_OK;    
00339 }
00340 
00341 /**
00342   * @brief  USBH_HID_GetKeybdInfo 
00343   *         The function return keyboard information.
00344   * @param  phost: Host handle
00345   * @retval keyboard information
00346   */
00347 HID_KEYBD_Info_TypeDef *USBH_HID_GetKeybdInfo(USBH_HandleTypeDef *phost)
00348 {
00349   if(USBH_HID_KeybdDecode(phost) == USBH_OK)
00350  {
00351   return &keybd_info;
00352  }
00353  else
00354  {
00355   return NULL; 
00356  }  
00357 }
00358 
00359 /**
00360   * @brief  USBH_HID_KeybdDecode 
00361   *         The function decode keyboard data.
00362   * @param  phost: Host handle
00363   * @retval USBH Status
00364   */
00365 static USBH_StatusTypeDef USBH_HID_KeybdDecode(USBH_HandleTypeDef *phost)
00366 {
00367   uint8_t x;
00368   
00369   HID_HandleTypeDef *HID_Handle =  (HID_HandleTypeDef *) phost->pActiveClass->pData;
00370   if(HID_Handle->length == 0)
00371   {
00372     return USBH_FAIL;
00373   }
00374   /*Fill report */
00375   if(fifo_read(&HID_Handle->fifo, &keybd_report_data, HID_Handle->length) ==  HID_Handle->length)
00376   {
00377     
00378     keybd_info.lctrl=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_lctrl, 0);
00379     keybd_info.lshift=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_lshift, 0);
00380     keybd_info.lalt=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_lalt, 0);
00381     keybd_info.lgui=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_lgui, 0);
00382     keybd_info.rctrl=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_rctrl, 0);
00383     keybd_info.rshift=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_rshift, 0);
00384     keybd_info.ralt=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_ralt, 0);
00385     keybd_info.rgui=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_rgui, 0);
00386     
00387     for(x=0; x < sizeof(keybd_info.keys); x++)
00388     {    
00389       keybd_info.keys[x]=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_key_array, x);
00390     }
00391     
00392     return USBH_OK; 
00393   }
00394   return   USBH_FAIL;  
00395 }
00396 
00397 /**
00398   * @brief  USBH_HID_GetASCIICode 
00399   *         The function decode keyboard data into ASCII characters.
00400   * @param  phost: Host handle
00401   * @param  info: Keyboard information
00402   * @retval ASCII code
00403   */
00404 uint8_t USBH_HID_GetASCIICode(HID_KEYBD_Info_TypeDef *info)
00405 {
00406   uint8_t   output;  
00407   if((info->lshift == 1) || (info->rshift))
00408   {
00409   output =  HID_KEYBRD_ShiftKey[HID_KEYBRD_Codes[info->keys[0]]];
00410   }
00411   else
00412   {
00413   output =  HID_KEYBRD_Key[HID_KEYBRD_Codes[info->keys[0]]];
00414   }
00415   return output;  
00416 }
00417 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
00418