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