1 /*
2  * Copyright (c) 2006-2022, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2022-02-25     Wayne        the first version
9  */
10 
11 #include <rtthread.h>
12 #include <rtdevice.h>
13 #include "ili.h"
14 
15 #define THREAD_PRIORITY   5
16 #define THREAD_STACK_SIZE 2048
17 #define THREAD_TIMESLICE  5
18 
19 static rt_sem_t  tpc_sem = RT_NULL;
20 
nu_touch_inputevent_cb(rt_int16_t x,rt_int16_t y,rt_uint8_t state)21 rt_weak void nu_touch_inputevent_cb(rt_int16_t x, rt_int16_t y, rt_uint8_t state)
22 {
23     rt_kprintf("[%d] %d %d\n", state, x, y);
24 }
25 
rx_callback(rt_device_t dev,rt_size_t size)26 static rt_err_t rx_callback(rt_device_t dev, rt_size_t size)
27 {
28     return rt_sem_release(tpc_sem);
29 }
30 
tpc_entry(void * parameter)31 static void tpc_entry(void *parameter)
32 {
33     struct rt_touch_data *read_data;
34     struct rt_touch_info info;
35     rt_device_t  dev = RT_NULL;
36 
37     const char *name = "ili_tpc";
38     rt_uint32_t x_range = BSP_LCD_WIDTH;
39     rt_uint32_t y_range = BSP_LCD_HEIGHT;
40 
41     dev = rt_device_find(name);
42     if (dev == RT_NULL)
43     {
44         rt_kprintf("can't find device:%s\n", name);
45         return;
46     }
47 
48     if (rt_device_open(dev, RT_DEVICE_FLAG_INT_RX) != RT_EOK)
49     {
50         rt_kprintf("open device failed!");
51         return;
52     }
53     rt_kprintf("[%s] x: %d, y: %d\n", __func__, x_range, y_range);
54 
55     rt_device_control(dev, RT_TOUCH_CTRL_SET_X_RANGE, &x_range);  /* if possible you can set your x y coordinate*/
56     rt_device_control(dev, RT_TOUCH_CTRL_SET_Y_RANGE, &y_range);
57 
58     tpc_sem = rt_sem_create("dsem", 0, RT_IPC_FLAG_FIFO);
59     if (tpc_sem == RT_NULL)
60     {
61         rt_kprintf("create dynamic semaphore failed.\n");
62         return;
63     }
64 
65     rt_device_set_rx_indicate(dev, rx_callback);
66 
67     rt_device_control(dev, RT_TOUCH_CTRL_GET_INFO, &info);
68     rt_kprintf("range_x = %d \n",   info.range_x);
69     rt_kprintf("range_y = %d \n",   info.range_y);
70     rt_kprintf("point_num = %d \n", info.point_num);
71 
72     read_data = (struct rt_touch_data *)rt_malloc(sizeof(struct rt_touch_data) * info.point_num);
73     RT_ASSERT(read_data);
74 
75     rt_memset(read_data, 0, sizeof(struct rt_touch_data) * info.point_num);
76 
77     while (1)
78     {
79         rt_sem_take(tpc_sem, RT_WAITING_FOREVER);
80         rt_device_control(dev, RT_TOUCH_CTRL_DISABLE_INT, RT_NULL);
81 
82         if (rt_device_read(dev, 0, read_data, info.point_num) == info.point_num)
83         {
84             for (rt_uint8_t i = 0; i < 1; i++) // Only report one point.
85             {
86                 if (read_data[i].event == RT_TOUCH_EVENT_DOWN
87                         || read_data[i].event == RT_TOUCH_EVENT_UP
88                         || read_data[i].event == RT_TOUCH_EVENT_MOVE)
89                 {
90                     //rt_kprintf("[%d] %d %d\n", read_data[i].event, read_data[i].x_coordinate, read_data[i].y_coordinate);
91 
92                     nu_touch_inputevent_cb(read_data[i].x_coordinate, read_data[i].y_coordinate, read_data[i].event);
93                 }
94             }
95         }
96         rt_device_control(dev, RT_TOUCH_CTRL_ENABLE_INT, RT_NULL);
97     }
98 }
99 
100 
101 /* Test function */
tpc_sample(void)102 int tpc_sample(void)
103 {
104     rt_thread_t  tpc_thread;
105     tpc_thread = rt_thread_create("tpc",
106                                   tpc_entry,
107                                   RT_NULL,
108                                   THREAD_STACK_SIZE,
109                                   THREAD_PRIORITY,
110                                   THREAD_TIMESLICE);
111 
112     if (tpc_thread != RT_NULL)
113         rt_thread_startup(tpc_thread);
114 
115     return 0;
116 }
117 INIT_APP_EXPORT(tpc_sample);
118