2022-03-29 07:10:35 +08:00
|
|
|
/*
|
2022-09-12 19:36:11 +08:00
|
|
|
* Copyright (c) 2006-2022, RT-Thread Development Team
|
2022-03-29 07:10:35 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2022-02-25 Wayne the first version
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <rtdevice.h>
|
|
|
|
|
|
|
|
#define THREAD_PRIORITY 5
|
|
|
|
#define THREAD_STACK_SIZE 2048
|
|
|
|
#define THREAD_TIMESLICE 5
|
|
|
|
|
|
|
|
static rt_sem_t tpc_sem = RT_NULL;
|
|
|
|
|
2022-12-12 02:12:03 +08:00
|
|
|
rt_weak void nu_touch_inputevent_cb(rt_int16_t x, rt_int16_t y, rt_uint8_t state)
|
2022-03-29 07:10:35 +08:00
|
|
|
{
|
|
|
|
rt_kprintf("[%d] %d %d\n", state, x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t rx_callback(rt_device_t dev, rt_size_t size)
|
|
|
|
{
|
|
|
|
return rt_sem_release(tpc_sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tpc_entry(void *parameter)
|
|
|
|
{
|
|
|
|
struct rt_touch_data *read_data;
|
|
|
|
struct rt_touch_info info;
|
|
|
|
rt_device_t dev = RT_NULL;
|
|
|
|
|
2022-06-03 10:01:15 +08:00
|
|
|
const char *name = (const char *)parameter;
|
2022-03-29 07:10:35 +08:00
|
|
|
rt_uint32_t x_range = BSP_LCD_WIDTH;
|
|
|
|
rt_uint32_t y_range = BSP_LCD_HEIGHT;
|
|
|
|
|
|
|
|
dev = rt_device_find(name);
|
|
|
|
if (dev == RT_NULL)
|
|
|
|
{
|
|
|
|
rt_kprintf("can't find device:%s\n", name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rt_device_open(dev, RT_DEVICE_FLAG_INT_RX) != RT_EOK)
|
|
|
|
{
|
|
|
|
rt_kprintf("open device failed!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rt_kprintf("[%s] x: %d, y: %d\n", __func__, x_range, y_range);
|
|
|
|
|
|
|
|
rt_device_control(dev, RT_TOUCH_CTRL_SET_X_RANGE, &x_range); /* if possible you can set your x y coordinate*/
|
|
|
|
rt_device_control(dev, RT_TOUCH_CTRL_SET_Y_RANGE, &y_range);
|
|
|
|
|
|
|
|
tpc_sem = rt_sem_create("dsem", 0, RT_IPC_FLAG_FIFO);
|
|
|
|
if (tpc_sem == RT_NULL)
|
|
|
|
{
|
|
|
|
rt_kprintf("create dynamic semaphore failed.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_device_set_rx_indicate(dev, rx_callback);
|
|
|
|
|
|
|
|
rt_device_control(dev, RT_TOUCH_CTRL_GET_INFO, &info);
|
|
|
|
rt_kprintf("range_x = %d \n", info.range_x);
|
|
|
|
rt_kprintf("range_y = %d \n", info.range_y);
|
|
|
|
rt_kprintf("point_num = %d \n", info.point_num);
|
|
|
|
|
|
|
|
read_data = (struct rt_touch_data *)rt_malloc(sizeof(struct rt_touch_data) * info.point_num);
|
|
|
|
RT_ASSERT(read_data);
|
|
|
|
|
|
|
|
rt_memset(read_data, 0, sizeof(struct rt_touch_data) * info.point_num);
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
rt_sem_take(tpc_sem, RT_WAITING_FOREVER);
|
|
|
|
rt_device_control(dev, RT_TOUCH_CTRL_DISABLE_INT, RT_NULL);
|
|
|
|
|
|
|
|
if (rt_device_read(dev, 0, read_data, info.point_num) == info.point_num)
|
|
|
|
{
|
|
|
|
for (rt_uint8_t i = 0; i < 1; i++) // Only report one point.
|
|
|
|
{
|
|
|
|
if (read_data[i].event == RT_TOUCH_EVENT_DOWN
|
|
|
|
|| read_data[i].event == RT_TOUCH_EVENT_UP
|
|
|
|
|| read_data[i].event == RT_TOUCH_EVENT_MOVE)
|
|
|
|
{
|
2022-06-03 10:01:15 +08:00
|
|
|
rt_uint16_t u16X, u16Y;
|
2022-03-29 07:10:35 +08:00
|
|
|
|
2022-06-03 10:01:15 +08:00
|
|
|
u16X = read_data[i].x_coordinate;
|
|
|
|
u16Y = read_data[i].y_coordinate;
|
|
|
|
|
2022-09-20 22:30:21 +08:00
|
|
|
#if defined(NU_PKG_TPC_REVERSE_XY)
|
|
|
|
u16X = info.range_x - u16X;
|
|
|
|
u16Y = info.range_y - u16Y;
|
|
|
|
#endif
|
2022-06-03 10:01:15 +08:00
|
|
|
|
|
|
|
//rt_kprintf("[%d] %d %d\n", read_data[i].event, u16X, u16Y);
|
|
|
|
|
|
|
|
nu_touch_inputevent_cb(u16X, u16Y, read_data[i].event);
|
2022-03-29 07:10:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rt_device_control(dev, RT_TOUCH_CTRL_ENABLE_INT, RT_NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Test function */
|
2022-06-03 10:01:15 +08:00
|
|
|
int tpc_sample(const char *tpc_name)
|
2022-03-29 07:10:35 +08:00
|
|
|
{
|
|
|
|
rt_thread_t tpc_thread;
|
|
|
|
tpc_thread = rt_thread_create("tpc",
|
|
|
|
tpc_entry,
|
2022-06-03 10:01:15 +08:00
|
|
|
(void *)tpc_name,
|
2022-03-29 07:10:35 +08:00
|
|
|
THREAD_STACK_SIZE,
|
|
|
|
THREAD_PRIORITY,
|
|
|
|
THREAD_TIMESLICE);
|
|
|
|
|
|
|
|
if (tpc_thread != RT_NULL)
|
|
|
|
rt_thread_startup(tpc_thread);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|