2018-12-24 18:49:00 +08:00
|
|
|
/*
|
2022-06-22 13:41:06 +08:00
|
|
|
* Copyright (c) 2006-2022, RT-Thread Development Team
|
2018-12-24 18:49:00 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2018-02-08 Zhangyihong the first version
|
2019-04-26 21:00:20 +08:00
|
|
|
* 2018-10-29 XY
|
|
|
|
* 2019-04-11 sundm75 modify for ls1c300 & RTGUI
|
2018-12-24 18:49:00 +08:00
|
|
|
*/
|
2022-06-22 13:41:06 +08:00
|
|
|
|
2018-12-24 18:49:00 +08:00
|
|
|
#include "drv_touch.h"
|
2019-04-26 21:00:20 +08:00
|
|
|
|
|
|
|
#define TOUCH_I2C_NAME "i2c1"
|
|
|
|
|
|
|
|
#ifndef TOUCH_SAMPLE_HZ
|
|
|
|
#define TOUCH_SAMPLE_HZ (50)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef TOUCH_I2C_NAME
|
|
|
|
#error "Please define touch i2c name!"
|
|
|
|
#endif
|
|
|
|
|
2018-12-24 18:49:00 +08:00
|
|
|
#ifdef TINA_USING_TOUCH
|
|
|
|
#if (defined PKG_USING_GUIENGINE) || (defined RT_USING_RTGUI)
|
|
|
|
#include <rtgui/event.h>
|
|
|
|
#include <rtgui/rtgui_server.h>
|
|
|
|
#endif
|
|
|
|
|
2019-04-26 21:00:20 +08:00
|
|
|
#if 0
|
|
|
|
#define TPDEBUG rt_kprintf
|
|
|
|
#else
|
|
|
|
#define TPDEBUG(...)
|
|
|
|
#endif
|
2018-12-24 18:49:00 +08:00
|
|
|
|
2019-04-26 21:00:20 +08:00
|
|
|
static rt_slist_t _driver_list;
|
|
|
|
static struct rt_i2c_bus_device *i2c_bus = RT_NULL;
|
2018-12-24 18:49:00 +08:00
|
|
|
|
2019-04-26 21:00:20 +08:00
|
|
|
static void post_down_event(rt_uint16_t x, rt_uint16_t y, rt_uint32_t id)
|
2018-12-24 18:49:00 +08:00
|
|
|
{
|
|
|
|
#if (defined PKG_USING_GUIENGINE) || (defined RT_USING_RTGUI)
|
2019-04-26 21:00:20 +08:00
|
|
|
rt_err_t result;
|
2018-12-24 18:49:00 +08:00
|
|
|
struct rtgui_event_mouse emouse;
|
|
|
|
|
|
|
|
emouse.parent.sender = RT_NULL;
|
|
|
|
emouse.wid = RT_NULL;
|
|
|
|
|
|
|
|
emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
|
|
|
|
emouse.button = RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN;
|
|
|
|
emouse.x = x;
|
|
|
|
emouse.y = y;
|
|
|
|
#ifdef PKG_USING_GUIENGINE
|
|
|
|
emouse.ts = rt_tick_get();
|
2019-04-26 21:00:20 +08:00
|
|
|
emouse.id = id;
|
2018-12-24 18:49:00 +08:00
|
|
|
#endif
|
2019-04-26 21:00:20 +08:00
|
|
|
|
|
|
|
#ifdef PKG_USING_GUIENGINE
|
|
|
|
do
|
|
|
|
{
|
|
|
|
result = rtgui_server_post_event(&emouse.parent, sizeof(emouse));
|
|
|
|
if (result != RT_EOK)
|
|
|
|
{
|
|
|
|
rt_thread_delay(RT_TICK_PER_SECOND / TOUCH_SAMPLE_HZ);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (result != RT_EOK);
|
|
|
|
#else
|
2018-12-24 18:49:00 +08:00
|
|
|
rtgui_server_post_event(&emouse.parent, sizeof(emouse));
|
2019-04-26 21:00:20 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
TPDEBUG("[TP] touch down [%d, %d]\n", emouse.x, emouse.y);
|
2018-12-24 18:49:00 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-04-26 21:00:20 +08:00
|
|
|
static void post_motion_event(rt_uint16_t x, rt_uint16_t y, rt_uint32_t id)
|
2018-12-24 18:49:00 +08:00
|
|
|
{
|
|
|
|
#if (defined PKG_USING_GUIENGINE) || (defined RT_USING_RTGUI)
|
|
|
|
struct rtgui_event_mouse emouse;
|
|
|
|
|
|
|
|
emouse.parent.sender = RT_NULL;
|
|
|
|
emouse.wid = RT_NULL;
|
|
|
|
|
|
|
|
emouse.button = RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN;
|
|
|
|
emouse.parent.type = RTGUI_EVENT_MOUSE_MOTION;
|
|
|
|
emouse.x = x;
|
|
|
|
emouse.y = y;
|
|
|
|
#ifdef PKG_USING_GUIENGINE
|
|
|
|
emouse.ts = rt_tick_get();
|
2019-04-26 21:00:20 +08:00
|
|
|
emouse.id = id;
|
2018-12-24 18:49:00 +08:00
|
|
|
#endif
|
|
|
|
rtgui_server_post_event(&emouse.parent, sizeof(emouse));
|
2019-04-26 21:00:20 +08:00
|
|
|
TPDEBUG("[TP] touch motion [%d, %d]\n", emouse.x, emouse.y);
|
2018-12-24 18:49:00 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-04-26 21:00:20 +08:00
|
|
|
static void post_up_event(rt_uint16_t x, rt_uint16_t y, rt_uint32_t id)
|
2018-12-24 18:49:00 +08:00
|
|
|
{
|
|
|
|
#if (defined PKG_USING_GUIENGINE) || (defined RT_USING_RTGUI)
|
2019-04-26 21:00:20 +08:00
|
|
|
rt_err_t result;
|
2018-12-24 18:49:00 +08:00
|
|
|
struct rtgui_event_mouse emouse;
|
|
|
|
|
|
|
|
emouse.parent.sender = RT_NULL;
|
|
|
|
emouse.wid = RT_NULL;
|
|
|
|
|
|
|
|
emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
|
|
|
|
emouse.button = RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_UP;
|
|
|
|
emouse.x = x;
|
|
|
|
emouse.y = y;
|
|
|
|
#ifdef PKG_USING_GUIENGINE
|
|
|
|
emouse.ts = rt_tick_get();
|
2019-04-26 21:00:20 +08:00
|
|
|
emouse.id = id;
|
2018-12-24 18:49:00 +08:00
|
|
|
#endif
|
2019-04-26 21:00:20 +08:00
|
|
|
|
|
|
|
#ifdef PKG_USING_GUIENGINE
|
|
|
|
do
|
|
|
|
{
|
|
|
|
result = rtgui_server_post_event(&emouse.parent, sizeof(emouse));
|
|
|
|
if (result != RT_EOK)
|
|
|
|
{
|
|
|
|
rt_thread_delay(RT_TICK_PER_SECOND / TOUCH_SAMPLE_HZ);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (result != RT_EOK);
|
|
|
|
#else
|
2018-12-24 18:49:00 +08:00
|
|
|
rtgui_server_post_event(&emouse.parent, sizeof(emouse));
|
2019-04-26 21:00:20 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
TPDEBUG("[TP] touch up [%d, %d]\n", emouse.x, emouse.y);
|
2018-12-24 18:49:00 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-04-26 21:00:20 +08:00
|
|
|
static void touch_run(void* parameter)
|
2018-12-24 18:49:00 +08:00
|
|
|
{
|
|
|
|
rt_tick_t emouse_id = 0;
|
2019-04-26 21:00:20 +08:00
|
|
|
struct touch_message msg;
|
|
|
|
rt_slist_t *driver_list = NULL;
|
|
|
|
struct touch_driver *current_driver = RT_NULL;
|
|
|
|
|
|
|
|
i2c_bus = rt_i2c_bus_device_find(TOUCH_I2C_NAME);
|
|
|
|
RT_ASSERT(i2c_bus);
|
|
|
|
|
|
|
|
if(rt_device_open(&i2c_bus->parent, RT_DEVICE_OFLAG_RDWR) != RT_EOK)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_slist_for_each(driver_list, &_driver_list)
|
2018-12-24 18:49:00 +08:00
|
|
|
{
|
2019-04-26 21:00:20 +08:00
|
|
|
current_driver = (struct touch_driver *)driver_list;
|
|
|
|
if(current_driver->probe(i2c_bus) == RT_TRUE)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
current_driver = RT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(current_driver == RT_NULL)
|
|
|
|
{
|
|
|
|
rt_kprintf("[TP] No touch pad or driver.\n");
|
|
|
|
rt_device_close((rt_device_t)i2c_bus);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2018-12-24 18:49:00 +08:00
|
|
|
|
2019-04-26 21:00:20 +08:00
|
|
|
current_driver->ops->init(i2c_bus);
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (rt_sem_take(current_driver->isr_sem, RT_WAITING_FOREVER) != RT_EOK)
|
2018-12-24 18:49:00 +08:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2019-04-26 21:00:20 +08:00
|
|
|
|
|
|
|
if (current_driver->ops->read_point(&msg) != RT_EOK)
|
2018-12-24 18:49:00 +08:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2019-04-26 21:00:20 +08:00
|
|
|
|
2018-12-24 18:49:00 +08:00
|
|
|
switch (msg.event)
|
|
|
|
{
|
2019-04-26 21:00:20 +08:00
|
|
|
case TOUCH_EVENT_MOVE:
|
|
|
|
post_motion_event(msg.x, msg.y, emouse_id);
|
2018-12-24 18:49:00 +08:00
|
|
|
break;
|
2019-04-26 21:00:20 +08:00
|
|
|
|
2018-12-24 18:49:00 +08:00
|
|
|
case TOUCH_EVENT_DOWN:
|
|
|
|
emouse_id = rt_tick_get();
|
|
|
|
post_down_event(msg.x, msg.y, emouse_id);
|
|
|
|
break;
|
2019-04-26 21:00:20 +08:00
|
|
|
|
|
|
|
case TOUCH_EVENT_UP:
|
|
|
|
post_up_event(msg.x, msg.y, emouse_id);
|
2018-12-24 18:49:00 +08:00
|
|
|
break;
|
2019-04-26 21:00:20 +08:00
|
|
|
|
2018-12-24 18:49:00 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2019-04-26 21:00:20 +08:00
|
|
|
|
|
|
|
rt_thread_delay(RT_TICK_PER_SECOND / TOUCH_SAMPLE_HZ);
|
2018-12-24 18:49:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-26 21:00:20 +08:00
|
|
|
rt_err_t rt_touch_drivers_register(touch_driver_t drv)
|
2018-12-24 18:49:00 +08:00
|
|
|
{
|
2019-04-26 21:00:20 +08:00
|
|
|
RT_ASSERT(drv != RT_NULL);
|
|
|
|
RT_ASSERT(drv->ops != RT_NULL);
|
|
|
|
RT_ASSERT(drv->probe != RT_NULL);
|
|
|
|
|
|
|
|
rt_slist_append(&_driver_list, &drv->list);
|
|
|
|
|
|
|
|
return RT_EOK;
|
2018-12-24 18:49:00 +08:00
|
|
|
}
|
|
|
|
|
2019-04-26 21:00:20 +08:00
|
|
|
static int rt_touch_list_init(void)
|
2018-12-24 18:49:00 +08:00
|
|
|
{
|
2019-04-26 21:00:20 +08:00
|
|
|
rt_slist_init(&_driver_list);
|
|
|
|
|
|
|
|
return RT_EOK;
|
2018-12-24 18:49:00 +08:00
|
|
|
}
|
2019-04-26 21:00:20 +08:00
|
|
|
INIT_BOARD_EXPORT(rt_touch_list_init);
|
2018-12-24 18:49:00 +08:00
|
|
|
|
2019-04-26 21:00:20 +08:00
|
|
|
static int rt_touch_init(void)
|
2018-12-24 18:49:00 +08:00
|
|
|
{
|
2019-04-26 21:00:20 +08:00
|
|
|
rt_thread_t thread = RT_NULL;
|
|
|
|
|
|
|
|
thread = rt_thread_create("touch", touch_run, RT_NULL, 2048, 28, 20);
|
|
|
|
if(thread)
|
|
|
|
{
|
|
|
|
return rt_thread_startup(thread);
|
|
|
|
}
|
|
|
|
|
2023-03-17 12:12:16 +08:00
|
|
|
return -RT_ERROR;
|
2018-12-24 18:49:00 +08:00
|
|
|
}
|
2019-04-26 21:00:20 +08:00
|
|
|
INIT_APP_EXPORT(rt_touch_init);
|
|
|
|
|
|
|
|
int rt_touch_read(rt_uint16_t addr, void *cmd_buf, size_t cmd_len, void *data_buf, size_t data_len)
|
2018-12-24 18:49:00 +08:00
|
|
|
{
|
2019-04-26 21:00:20 +08:00
|
|
|
struct rt_i2c_msg msgs[2];
|
|
|
|
|
|
|
|
msgs[0].addr = addr;
|
|
|
|
msgs[0].flags = RT_I2C_WR;
|
|
|
|
msgs[0].buf = cmd_buf;
|
|
|
|
msgs[0].len = cmd_len;
|
|
|
|
|
|
|
|
msgs[1].addr = addr;
|
|
|
|
msgs[1].flags = RT_I2C_RD;
|
|
|
|
msgs[1].buf = data_buf;
|
|
|
|
msgs[1].len = data_len;
|
|
|
|
|
|
|
|
if (rt_i2c_transfer(i2c_bus, msgs, 2) == 2)
|
|
|
|
return 0;
|
|
|
|
else
|
2018-12-24 18:49:00 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-04-26 21:00:20 +08:00
|
|
|
int rt_touch_write(rt_uint16_t addr, void *data_buf, size_t data_len)
|
|
|
|
{
|
|
|
|
struct rt_i2c_msg msgs[1];
|
|
|
|
|
|
|
|
msgs[0].addr = addr;
|
|
|
|
msgs[0].flags = RT_I2C_WR;
|
|
|
|
msgs[0].buf = data_buf;
|
|
|
|
msgs[0].len = data_len;
|
2018-12-24 18:49:00 +08:00
|
|
|
|
2019-04-26 21:00:20 +08:00
|
|
|
if (rt_i2c_transfer(i2c_bus, msgs, 1) == 1)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
2018-12-24 18:49:00 +08:00
|
|
|
#endif
|