summaryrefslogtreecommitdiff
path: root/bsp/loongson/ls1cdev/drivers/drv_touch.c
blob: 8b2e70eadf4a55e81fe5777e12eaeab62034ba2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-02-08     Zhangyihong  the first version
 * 2018-10-29     XY
 * 2019-04-11     sundm75      modify for ls1c300 & RTGUI
 */

#include "drv_touch.h"

#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

#ifdef TINA_USING_TOUCH
#if (defined PKG_USING_GUIENGINE) || (defined RT_USING_RTGUI)
#include <rtgui/event.h>
#include <rtgui/rtgui_server.h>
#endif

#if 0
#define TPDEBUG     rt_kprintf
#else
#define TPDEBUG(...)
#endif

static rt_slist_t _driver_list;
static struct rt_i2c_bus_device *i2c_bus = RT_NULL;

static void post_down_event(rt_uint16_t x, rt_uint16_t y, rt_uint32_t id)
{
#if (defined PKG_USING_GUIENGINE) || (defined RT_USING_RTGUI)
    rt_err_t result;
    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();
    emouse.id = id;
#endif

#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
    rtgui_server_post_event(&emouse.parent, sizeof(emouse));
#endif

    TPDEBUG("[TP] touch down [%d, %d]\n", emouse.x, emouse.y);
#endif
}

static void post_motion_event(rt_uint16_t x, rt_uint16_t y, rt_uint32_t id)
{
#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();
    emouse.id = id;
#endif
    rtgui_server_post_event(&emouse.parent, sizeof(emouse));
    TPDEBUG("[TP] touch motion [%d, %d]\n", emouse.x, emouse.y);
#endif
}

static void post_up_event(rt_uint16_t x, rt_uint16_t y, rt_uint32_t id)
{
#if (defined PKG_USING_GUIENGINE) || (defined RT_USING_RTGUI)
    rt_err_t result;
    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();
    emouse.id = id;
#endif

#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
    rtgui_server_post_event(&emouse.parent, sizeof(emouse));
#endif

    TPDEBUG("[TP] touch up [%d, %d]\n", emouse.x, emouse.y);
#endif
}

static void touch_run(void* parameter)
{
    rt_tick_t emouse_id = 0;
    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)
    {
        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;
    }

    current_driver->ops->init(i2c_bus);

    while (1)
    {
        if (rt_sem_take(current_driver->isr_sem, RT_WAITING_FOREVER) != RT_EOK)
        {
            continue;
        }

        if (current_driver->ops->read_point(&msg) != RT_EOK)
        {
            continue;
        }

        switch (msg.event)
        {
        case TOUCH_EVENT_MOVE:
            post_motion_event(msg.x, msg.y, emouse_id);
            break;

        case TOUCH_EVENT_DOWN:
            emouse_id = rt_tick_get();
            post_down_event(msg.x, msg.y, emouse_id);
            break;

        case TOUCH_EVENT_UP:
            post_up_event(msg.x, msg.y, emouse_id);
            break;

        default:
            break;
        }

        rt_thread_delay(RT_TICK_PER_SECOND / TOUCH_SAMPLE_HZ);
    }
}

rt_err_t rt_touch_drivers_register(touch_driver_t drv)
{
    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;
}

static int rt_touch_list_init(void)
{
    rt_slist_init(&_driver_list);

    return RT_EOK;
}
INIT_BOARD_EXPORT(rt_touch_list_init);

static int rt_touch_init(void)
{
    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);
    }

    return -RT_ERROR;
}
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)
{
    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
        return -1;
}

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;

    if (rt_i2c_transfer(i2c_bus, msgs, 1) == 1)
        return 0;
    else
        return -1;
}
#endif