【DIY手势翻页笔】成果贴+USB模拟HID、手势完成PPT翻页
编译软件:MDK5.38a,调试器使用板载的ST-linkV3、USB-TYPEC线两根
之前对STM32H503开发板的学习,对硬件的IIC驱动手势模块的底层驱动、串口3映射printf打印输出调试数据,通过经验的积累和实际的调试代码操作,自己对STM32cubx软件的熟悉,在接下来的学习中对USB模拟HID设备进行学习,通过利用手势模块进行翻页。
使用到的硬件:
1:NUCLEO-H503 开发板
2:手势模块:PAJ7620U2
3:自制开发板底板,用于安装开发板使用,同时已将接口引出。
4:type-c两根
底层驱动代码生成步骤:
1:在原IIC驱动读取手势模块的基础上,增加USB模拟HID的驱动部分:
2:使能USB外设,使能USB中断
3:打开 RTOS 配置USB协议栈
4:变更外设的时基。
底层代码部分:
1、打开生成的软件代码,打开C文件 ux_device_keyboard.c,需要自行添加hid_keyboard.H
并且这里要定义一个结构体UX_SLAVE_CLASS_HID *hid_keyboard;初始化函数
UX_SLAVE_CLASS_HID *hid_keyboard; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ /** * @brief USBD_HID_Keyboard_Activate * This function is called when insertion of a HID Keyboard device. * @param hid_instance: Pointer to the hid class instance. * @retval none */ VOID USBD_HID_Keyboard_Activate(VOID *hid_instance) { /* USER CODE BEGIN USBD_HID_Keyboard_Activate */ //UX_PARAMETER_NOT_USED(hid_instance); hid_keyboard = (UX_SLAVE_CLASS_HID*)hid_instance; /* USER CODE END USBD_HID_Keyboard_Activate */ return; }
2:在app_usbx_device.c中添加USB模拟HID的任务,具体代码如下截图:在动态创建任务之后,需要指定入口参数为:static VOID usbx_hidkeyboard_thread_entry(ULONG thread_input)
static VOID usbx_hidkeyboard_thread_entry(ULONG thread_input) { /* USER CODE BEGIN app_ux_device_thread_entry */ UX_SLAVE_DEVICE *device; UX_SLAVE_CLASS_HID_EVENT hid_event; TX_PARAMETER_NOT_USED(thread_input); device = &_ux_system_slave->ux_system_slave_device; ux_utility_memory_set(&hid_event, 0, sizeof(UX_SLAVE_CLASS_HID_EVENT)); while (1) { /* Check if the device state already configured */ if ((device->ux_slave_device_state == UX_DEVICE_CONFIGURED) && (hid_keyboard != UX_NULL)) { /* sleep for 10ms */ tx_thread_sleep(MS_TO_TICK(100)); if (gesture_key.valid != 0) { // printf("gesture_key.valid=%d %d\n", gesture_key.valid, gesture_key.type); gesture_key.valid = 0; Gesture_GetKeyData(&hid_event); //发送键盘码 ux_device_class_hid_event_set(hid_keyboard, &hid_event); } else { ClearKeyData(&hid_event); ux_device_class_hid_event_set(hid_keyboard, &hid_event); } } else { /* sleep for 10ms */ tx_thread_sleep(MS_TO_TICK(10)); } } /*
3:添加手势模块PAJ7620的驱动代码到工程代码中,具体驱动在前面的文章中已经介绍这里就不重复介绍,
4:将驱动得到的手势状态添加到工程代码中,并且在USB模拟HID发送函数中进行自定义,其代码如下
static void Gesture_GetKeyData(UX_SLAVE_CLASS_HID_EVENT *hid_event) { hid_event->ux_device_class_hid_event_length = 8; hid_event->ux_device_class_hid_event_buffer[0] = 0; hid_event->ux_device_class_hid_event_buffer[1] = 0; if (gesture_key.type) { switch (gesture_key.type) { case PAJ_UP://上 hid_event->ux_device_class_hid_event_buffer[2] = 0x52; break; case PAJ_DOWN://下 hid_event->ux_device_class_hid_event_buffer[2] = 0x51; break; case PAJ_LEFT://左 hid_event->ux_device_class_hid_event_buffer[2] = 0x50; break; case PAJ_RIGHT://右 hid_event->ux_device_class_hid_event_buffer[2] = 0x4F; break; case PAJ_FORWARD: break; case PAJ_BACKWARD: break; case PAJ_CLOCKWISE: hid_event->ux_device_class_hid_event_buffer[2] = 0x3e; break; //F5 case PAJ_COUNT_CLOCKWISE: hid_event->ux_device_class_hid_event_buffer[2] = 0x29; break; //ESC case PAJ_WAVE: hid_event->ux_device_class_hid_event_buffer[2] = 0; break; // default: break; } } }
5:添加的用户层代码如下:
/** * @brief USBX_APP_Device_Init * Initialization of USB device. * @param none * @retval none */ VOID USBX_APP_Device_Init(VOID) { /* USER CODE BEGIN USB_Device_Init_PreTreatment_0 */ /* USER CODE END USB_Device_Init_PreTreatment_0 */ /* initialize the device controller HAL driver */ MX_USB_PCD_Init(); /* USER CODE BEGIN USB_Device_Init_PreTreatment_1 */ HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x00, PCD_SNG_BUF, 0x14); HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x80, PCD_SNG_BUF, 0x54); HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x81, PCD_SNG_BUF, 0x94); HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x01, PCD_SNG_BUF, 0xD4); HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x82, PCD_SNG_BUF, 0x114); /* USER CODE END USB_Device_Init_PreTreatment_1 */ /* Initialize and link controller HAL driver */ ux_dcd_stm32_initialize((ULONG)USB_DRD_FS, (ULONG)&hpcd_USB_DRD_FS); /* Start the USB device */ HAL_PCD_Start(&hpcd_USB_DRD_FS); /* USER CODE BEGIN USB_Device_Init_PostTreatment */ /* USER CODE END USB_Device_Init_PostTreatment */ }
试验现象:通过在手势模块前端做动作,完成对PPT的翻页功能。
PAJ_UP------------- PageUp
PAJ_DOWN-------- PageDown
试验结果视频如下:
https://www.bilibili.com/video/BV1qa8deWEfB/