Skip to main content
Graduate
May 12, 2024
Question

Manual Keyboard Detection

  • May 12, 2024
  • 1 reply
  • 1451 views

hi,

USB keyboard working fine via HID and i need have small Manual Keyboard Detection  by pressing button . it's not compiling 

 

if( HAL_GPIO_ReadPin(GPIOC, button_Pin))
 {
 	if(USBH_HID_GetDeviceType(USBH_HandleTypeDef *phost) == HID_KEYBOARD)
 	{
 		HAL_UART_Transmit(&huart2, uart_TX_buf, sizeof(uart_TX_buf), 1000);
 		HAL_Delay(1000);
 	}
 }

 

 but how it's working inside the Event call  back .

 

void USBH_HID_EventCallback(USBH_HandleTypeDef *phost)
{
	
	if (USBH_HID_GetDeviceType(phost) == HID_KEYBOARD)
	{
		HID_KEYBD_Info_TypeDef *Keyboard_Info;
		Keyboard_Info = USBH_HID_GetKeybdInfo(phost);
		char key = USBH_HID_GetASCIICode (Keyboard_Info);

		int len = sprintf (uart_buf, "%c\n", key);
		HAL_UART_Transmit(&huart2, (uint8_t *)uart_buf, len, 1000);

	}
}

 

 

    This topic has been closed for replies.

    1 reply

    Super User
    May 12, 2024

    Hi, you're in a technical forum. When saying "it's not compiling" please provide the error message and other helpful details.

    MMARI.1Author
    Graduate
    May 13, 2024

    hi, sorry  for inconvenience .

    when i calling the this function  " if (USBH_HID_GetDeviceType(phost) == HID_KEYBOARD)" inside the main.c  then below error is appearing while compiling .

    error: 'phost' undeclared (first use in this function)
    122 | if (USBH_HID_GetDeviceType(phost) == HID_KEYBOARD)
    | ^~~~~

    then i try with USBH_HID_GetDeviceType(USBH_HandleTypeDef *phos)== HID_KEYBOARD) then below error is appearing. 

    error: expected expression before 'USBH_HandleTypeDef'

    122 | if (USBH_HID_GetDeviceType(USBH_HandleTypeDef *phost) == HID_KEYBOARD)

    | ^~~~~~~~~~~~~~~~~~

    then  i decralred USBH_HandleTypeDef *phost; in global level in main.c then calling the function doesn't creating any error but NO output as keyboard detection .

    /* Private typedef -----------------------------------------------------------*/

    /* USER CODE BEGIN PTD */

    USBH_HandleTypeDef *phost;

    /* USER CODE END PTD */

     

     

    if( HAL_GPIO_ReadPin(GPIOC, button_Pin))
     {
     	if(USBH_HID_GetDeviceType(phost) == HID_KEYBOARD)
     	{
     		HAL_UART_Transmit(&huart2, uart_TX_buf, sizeof(uart_TX_buf), 1000);
     		HAL_Delay(1000);
     	}
     }

     

     

    my understanding is "event callback " function is bringing the result of Device Type since Data is arrived from Keyboard NOT host is communicated to Device  and how below function handing Typedef without  any error.

     

     

    void USBH_HID_EventCallback(USBH_HandleTypeDef *phost)

     

     

    however i need have below two mentioned  function to be work separately   apart from "event call back" function .

    1. For manual Device Type - HID_TypeTypeDef USBH_HID_GetDeviceType(USBH_HandleTypeDef *phost);

    2. For the Manual Device Initialize - 
    USBH_StatusTypeDef USBH_HID_KeybdInit(USBH_HandleTypeDef *phost); 

    i request you to share your solution and comments .

     

     

     

     

     

     

     

     

    Super User
    May 13, 2024

    The key to solving compiler error messages is to read them literally:

     


    @MMARI.1 wrote:

    when i calling the this function  " if (USBH_HID_GetDeviceType(phost) == HID_KEYBOARD)" inside the main.c  then below error is appearing while compiling .

    error: 'phost' undeclared (first use in this function)
    122 | if (USBH_HID_GetDeviceType(phost) == HID_KEYBOARD)
    | ^~~~~


    On your screen, it probably looks more like this:

     

    error: 'phost' undeclared (first use in this function)
    
    122 | if (USBH_HID_GetDeviceType(phost) == HID_KEYBOARD)
     | ^~~~~

     

     Note how the ^~~~ is pointing to the place where the error occurs.

    This is why it's best to post errors using the source code button:

    AndrewNeil_0-1715591292072.png

     

    So it's telling you that your variable 'phost' is undeclared - that is, you have not declared it.

    The C programming language requires that you declare variables (and functions) before you use them.

    So the fix to this is to properly declare your variable 'phost'. 


    @MMARI.1 wrote:

    then i try with USBH_HID_GetDeviceType(USBH_HandleTypeDef *phos)== HID_KEYBOARD) then below error is appearing. 

    error: expected expression before 'USBH_HandleTypeDef'

    122 | if (USBH_HID_GetDeviceType(USBH_HandleTypeDef *phost) == HID_KEYBOARD)

    | ^~~~~~~~~~~~~~~~~~


    Again, to see where the error marker is pointing:

    error: expected expression before 'USBH_HandleTypeDef'
    
    122 | if (USBH_HID_GetDeviceType(USBH_HandleTypeDef *phost) == HID_KEYBOARD)
     | ^~~~~~~~~~~~~~~~~~

    That's not valid C - did you mean that to be a typecast?

     if ( USBH_HID_GetDeviceType( (USBH_HandleTypeDef *)phost ) == HID_KEYBOARD )

    Note that would not fix the problem of phost not being declared.

     


    @MMARI.1 wrote:

     

    then  i decralred USBH_HandleTypeDef *phost; in global level in main.c then calling the function doesn't creating any error but NO output as keyboard detection .


    Yes, providing a declaration would fix the error of it being un-declared .