2017-11-11 10:51:47 +08:00
|
|
|
/*
|
2023-04-18 10:26:23 +08:00
|
|
|
* Copyright (c) 2006-2023, RT-Thread Development Team
|
2017-11-11 10:51:47 +08:00
|
|
|
*
|
2018-10-14 19:37:18 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2017-11-11 10:51:47 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2012-01-03 Yi Qiu first version
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <drivers/usb_host.h>
|
|
|
|
#include "hid.h"
|
2017-11-18 17:13:46 +08:00
|
|
|
|
2017-11-11 10:51:47 +08:00
|
|
|
#ifdef RT_USING_RTGUI
|
|
|
|
#include <rtgui/event.h>
|
|
|
|
#include <rtgui/rtgui_server.h>
|
|
|
|
#include "drv_lcd.h"
|
|
|
|
#endif
|
2017-11-18 17:13:46 +08:00
|
|
|
|
2017-11-11 10:51:47 +08:00
|
|
|
#if defined(RT_USBH_HID) && defined(RT_USBH_HID_MOUSE)
|
2023-06-29 23:24:25 +08:00
|
|
|
|
|
|
|
#define DBG_TAG "usbhost.umouse"
|
|
|
|
#define DBG_LVL DBG_INFO
|
|
|
|
#include <rtdbg.h>
|
|
|
|
|
2017-11-18 17:13:46 +08:00
|
|
|
static struct uprotocal mouse_protocal;
|
2017-11-11 10:51:47 +08:00
|
|
|
|
|
|
|
#ifdef RT_USING_RTGUI
|
|
|
|
#define LKEY_PRESS 0x01
|
|
|
|
#define RKEY_PRESS 0x02
|
|
|
|
#define MKEY_PRESS 0x04
|
|
|
|
#define MOUSE_SCALING 0x02
|
2017-11-18 17:13:46 +08:00
|
|
|
|
2017-11-11 10:51:47 +08:00
|
|
|
static rt_bool_t lkey_down=RT_FALSE;
|
|
|
|
//static rt_bool_t rkey_down=RT_FALSE;
|
|
|
|
//static rt_bool_t mkey_down=RT_FALSE;
|
|
|
|
static struct rtgui_event_mouse emouse;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static rt_err_t rt_usbh_hid_mouse_callback(void* arg)
|
|
|
|
{
|
2017-11-18 17:13:46 +08:00
|
|
|
struct uhid* hid;
|
2017-11-11 10:51:47 +08:00
|
|
|
#ifdef RT_USING_RTGUI
|
2017-11-18 17:13:46 +08:00
|
|
|
rt_uint16_t xoffset=0;
|
|
|
|
rt_uint16_t yoffset=0;
|
2017-11-11 10:51:47 +08:00
|
|
|
#endif
|
|
|
|
hid = (struct uhid*)arg;
|
|
|
|
|
2023-06-29 23:24:25 +08:00
|
|
|
LOG_D("hid 0x%x 0x%x",
|
2017-11-18 17:13:46 +08:00
|
|
|
*(rt_uint32_t*)hid->buffer,
|
2023-06-29 23:24:25 +08:00
|
|
|
*(rt_uint32_t*)(&hid->buffer[4]));
|
2017-11-18 17:13:46 +08:00
|
|
|
#ifdef RT_USING_RTGUI
|
|
|
|
if(hid->buffer[1]!=0)
|
|
|
|
{
|
|
|
|
if(hid->buffer[1]>127)
|
|
|
|
{
|
|
|
|
xoffset=(256-hid->buffer[1])*MOUSE_SCALING;
|
|
|
|
if(emouse.x>xoffset)
|
|
|
|
{
|
2017-11-11 10:51:47 +08:00
|
|
|
emouse.x-=xoffset;
|
2017-11-18 17:13:46 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-11 10:51:47 +08:00
|
|
|
emouse.x=0;
|
2017-11-18 17:13:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xoffset=(hid->buffer[1])*MOUSE_SCALING;
|
|
|
|
if((emouse.x+xoffset)<480)
|
|
|
|
{
|
2017-11-11 10:51:47 +08:00
|
|
|
emouse.x+=xoffset;
|
2017-11-18 17:13:46 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-11 10:51:47 +08:00
|
|
|
emouse.x=480;
|
2017-11-18 17:13:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(hid->buffer[2]!=0)
|
|
|
|
{
|
|
|
|
|
|
|
|
if(hid->buffer[2]>127)
|
|
|
|
{
|
|
|
|
yoffset=(256-hid->buffer[2])*MOUSE_SCALING;
|
|
|
|
if(emouse.y>yoffset)
|
|
|
|
{
|
2017-11-11 10:51:47 +08:00
|
|
|
emouse.y-=yoffset;
|
2017-11-18 17:13:46 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-11 10:51:47 +08:00
|
|
|
emouse.y=0;
|
2017-11-18 17:13:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
yoffset=hid->buffer[2]*MOUSE_SCALING;
|
|
|
|
if(emouse.y+yoffset<272)
|
|
|
|
{
|
2017-11-11 10:51:47 +08:00
|
|
|
emouse.y+=yoffset;
|
2017-11-18 17:13:46 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-11 10:51:47 +08:00
|
|
|
emouse.y=272;
|
2017-11-18 17:13:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(xoffset!=0||yoffset!=0)
|
|
|
|
{
|
2017-11-11 10:51:47 +08:00
|
|
|
cursor_set_position(emouse.x,emouse.y);
|
2017-11-18 17:13:46 +08:00
|
|
|
}
|
2017-11-11 10:51:47 +08:00
|
|
|
if(hid->buffer[0]&LKEY_PRESS)
|
2017-11-18 17:13:46 +08:00
|
|
|
{
|
|
|
|
if(lkey_down==RT_FALSE)
|
|
|
|
{
|
|
|
|
// rt_kprintf("mouse left key press down\n");
|
|
|
|
emouse.button = (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN);
|
|
|
|
rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
|
|
|
|
lkey_down=RT_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(lkey_down==RT_TRUE)
|
|
|
|
{
|
|
|
|
// rt_kprintf("mouse left key press up\n");
|
|
|
|
emouse.button = (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_UP);
|
|
|
|
rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
|
|
|
|
lkey_down=RT_FALSE;
|
|
|
|
}
|
|
|
|
#endif
|
2017-11-11 10:51:47 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2022-09-23 11:23:28 +08:00
|
|
|
static rt_thread_t mouse_thread;
|
|
|
|
static void mouse_task(void* param)
|
2021-02-23 13:42:19 +08:00
|
|
|
{
|
|
|
|
struct uhintf* intf = (struct uhintf*)param;
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (rt_usb_hcd_pipe_xfer(intf->device->hcd, ((struct uhid*)intf->user_data)->pipe_in,
|
|
|
|
((struct uhid*)intf->user_data)->buffer, ((struct uhid*)intf->user_data)->pipe_in->ep.wMaxPacketSize,
|
|
|
|
USB_TIMEOUT_BASIC) == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_usbh_hid_mouse_callback(intf->user_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-11 10:51:47 +08:00
|
|
|
static rt_err_t rt_usbh_hid_mouse_init(void* arg)
|
|
|
|
{
|
2021-02-23 13:42:19 +08:00
|
|
|
struct uhintf* intf = (struct uhintf*)arg;
|
2017-11-11 10:51:47 +08:00
|
|
|
|
2017-11-18 17:13:46 +08:00
|
|
|
RT_ASSERT(intf != RT_NULL);
|
|
|
|
|
2017-11-11 10:51:47 +08:00
|
|
|
rt_usbh_hid_set_protocal(intf, 0);
|
|
|
|
|
2021-02-23 13:42:19 +08:00
|
|
|
rt_usbh_hid_set_idle(intf, 0, 0);
|
|
|
|
|
2022-09-23 11:23:28 +08:00
|
|
|
mouse_thread = rt_thread_create("mouse0", mouse_task, intf, 1024, 8, 100);
|
2021-02-23 13:42:19 +08:00
|
|
|
rt_thread_startup(mouse_thread);
|
2017-11-11 10:51:47 +08:00
|
|
|
|
2023-06-29 23:24:25 +08:00
|
|
|
LOG_D("start usb mouse");
|
2017-11-18 17:13:46 +08:00
|
|
|
#ifdef RT_USING_RTGUI
|
|
|
|
RTGUI_EVENT_MOUSE_BUTTON_INIT(&emouse);
|
2017-11-11 10:51:47 +08:00
|
|
|
emouse.wid = RT_NULL;
|
2017-11-18 17:13:46 +08:00
|
|
|
cursor_display(RT_TRUE);
|
|
|
|
#endif
|
2017-11-11 10:51:47 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will define the hid mouse protocal, it will be register to the protocal list.
|
2017-11-18 17:13:46 +08:00
|
|
|
*
|
2017-11-11 10:51:47 +08:00
|
|
|
* @return the keyboard protocal structure.
|
|
|
|
*/
|
|
|
|
uprotocal_t rt_usbh_hid_protocal_mouse(void)
|
|
|
|
{
|
|
|
|
mouse_protocal.pro_id = USB_HID_MOUSE;
|
|
|
|
|
|
|
|
mouse_protocal.init = rt_usbh_hid_mouse_init;
|
|
|
|
mouse_protocal.callback = rt_usbh_hid_mouse_callback;
|
|
|
|
|
|
|
|
return &mouse_protocal;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|