STM32

北通BTP-2120键值

EP report 长度为8 bytes

ANALOG按键(模式切换),灯不亮时(摇杆不可用),没有按任何按键时,键值上报:

7F7F7F7F7F0F00A0

灯亮时(摇杆可用),没有按任何按键时,键值上报:

7E7C81687C0F0020

1.最后一个字节可用于判断是否摇杆模式(摇杆可用):0xA0为摇杆不使用,0x20为使用摇杆。

2.按键模式(不使用摇杆)

  • byte[0] Axis0_X, 00 – Left, FF – Right, 7F – not pressing
  • byte[1] Axis0_Y, 00 – Up, FF – Down, 7F – not pressing
  • byte[2] not use, (I don’t know this byte for what to do)
  • byte[3]~byte[4] not use in this mode
  • byte[5]
    • [7:4]bit: 1-button1, 2-button2, 4-button3, 8-button4
    • [3:0]bit: 0xF(useless)
  • byte[6]
    • [7:4]bit: 0x0
    • [3:0]bit: 1-buttonLB, 2-buttonRB, 4-buttonLT, 8-buttonRT
  • byte[7] 0xA0

3.摇杆模式(使用摇杆)

  • byte[0] Axis1_X, 0x00~0x7F(Left), 0x80~0xFF(Right)
  • byte[1] Axis1_Y, 0x00~0x7F(Up),  0x80~0xFF(Down)
  • byte[2] not use, (I don’t know this byte for what to do)
  • byte[3] Axis2_X, 0x00~0x7F(Left), 0x80~0xFF(Right)
  • byte[4] Axis2_Y, 0x00~0x7F(Up),  0x80~0xFF(Down)
  • byte[5]
    • [7:4]bit: 1-button1, 2-button2, 4-button3, 8-button4,
    • [3:0]bit: 0-Axis0_Up, 2-Axis0_Right, 4-Axis0_Down, 6-Axis0_Left
  • byte[6]
    • [7:4]bit: 0x0
    • [3:0]bit: 1-buttonLB, 2-buttonRB, 4-buttonLT, 8-buttonRT
  • byte[7] 0x20

NOTE: I did not find the KeyCode of the SELECT and START buttons in the report data. Maybe the joystick is too old and the buttons are broken.

 

STM32 usb host HID driver:

/**
 * @brief  USBH_HID_CustomDecode
 *         The function decode device data.
 * @param  phost: Host handle
 * @retval USBH Status
 */
static USBH_StatusTypeDef USBH_HID_CustomDecode(USBH_HandleTypeDef *phost)
{
	HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData;

	if (HID_Handle->length == 0U)
	{
		return USBH_FAIL;
	}
	/*Fill report */
	if (USBH_HID_FifoRead(&HID_Handle->fifo, &report_data, HID_Handle->length) ==  HID_Handle->length)
	{
		/*Decode report */
#if (HID_CUSTOM_DEVICE_TYPE == BeiTong_Asura_2)
		joystick_info.Button_Key = report_data[0];
		joystick_info.Button_Ctrl = report_data[1];
		joystick_info.Button_Axis0 = report_data[2];
		joystick_info.Axis1_X = report_data[3]; //L,R
		joystick_info.Axis1_Y = report_data[4]; //U,D
		joystick_info.Axis2_X = report_data[5]; //L,R
		joystick_info.Axis2_Y = report_data[6]; //U,D
#elif (HID_CUSTOM_DEVICE_TYPE == BeiTong_BTP_2120)
		uint8_t mode = report_data[7];
		uint8_t axis = 0x0F;
		if(mode == 0x20) //摇杆模式
		{
			joystick_info.Axis1_X = report_data[0]; //L,R
			joystick_info.Axis1_Y = report_data[1]; //U,D
			joystick_info.Axis2_X = report_data[3]; //L,R
			joystick_info.Axis2_Y = report_data[4]; //U,D
			joystick_info.Button_Key = (report_data[6]<<4 | report_data[5]>>4);
			joystick_info.Button_Axis0 = report_data[5] & 0x0F;
		}
		else if(mode == 0xA0) //按键模式
		{
			joystick_info.Axis1_X = 0x7F; //L,R
			joystick_info.Axis1_Y = 0x7F; //U,D
			joystick_info.Axis2_X = 0x7F; //L,R
			joystick_info.Axis2_Y = 0x7F; //U,D
			joystick_info.Button_Key = (report_data[6]<<4 | report_data[5]>>4);
			if(report_data[0] == 0x7F && report_data[1] == 0x00)
				axis = 0;
			if(report_data[0] == 0xFF && report_data[1] == 0x00)
				axis = 1;
			if(report_data[0] == 0xFF && report_data[1] == 0x7F)
				axis = 2;
			if(report_data[0] == 0xFF && report_data[1] == 0xFF)
				axis = 3;
			if(report_data[0] == 0x7F && report_data[1] == 0xFF)
				axis = 4;
			if(report_data[0] == 0x00 && report_data[1] == 0xFF)
				axis = 5;
			if(report_data[0] == 0x00 && report_data[1] == 0x7F)
				axis = 6;
			if(report_data[0] == 0x00 && report_data[1] == 0x00)
				axis = 7;
			joystick_info.Button_Axis0 = axis;
		}
		else
		{
			return USBH_FAIL;
		}
#else
#warning "Please define your HID device's report decode"
#endif
		return USBH_OK;
	}
	return USBH_FAIL;
}