summaryrefslogtreecommitdiffstats
path: root/utils/nwztools/plattools/test_ts.c
blob: af6c741e001b6f468f03b1f32099cf6db4e17529 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2016 Amaury Pouly
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "nwz_lib.h"

int main(int argc, char **argv)
{
    /* clear screen and display welcome message */
    nwz_lcdmsg(true, 0, 0, "test_ts");
    nwz_lcdmsg(false, 0, 2, "BACK: quit");
    /* open input device */
    int key_fd = nwz_key_open();
    if(key_fd < 0)
    {
        nwz_lcdmsg(false, 3, 4, "Cannot open key device");
        sleep(2);
        return 1;
    }
    int ts_fd = nwz_ts_open();
    if(ts_fd < 0)
    {
        nwz_lcdmsg(false, 3, 4, "Cannot open touch screen device");
        sleep(2);
        return 1;
    }
    /* init state and print maximum information */
    struct nwz_ts_state_t ts_state;
    if(nwz_ts_state_init(ts_fd, &ts_state) < 0)
    {
        nwz_lcdmsg(false, 3, 4, "Cannot init touch screen device");
        sleep(2);
        return 1;
    }
    /* display static information */
    nwz_lcdmsgf(false, 1, 6, "size: %d, %d       ", ts_state.max_x, ts_state.max_y);
    /* display input state in a loop */
    while(1)
    {
        /* wait for event */
        int fds[2] = {key_fd, ts_fd};
        int ret = nwz_wait_fds(fds, 2, -1);
        if(ret & 1) /* key_fd */
        {
            struct input_event evt;
            if(nwz_key_read_event(key_fd, &evt) == 1)
            {
                if(nwz_key_event_get_keycode(&evt) == NWZ_KEY_BACK &&
                        nwz_key_event_is_press(&evt))
                    break; /* quit */
            }
        }
        if(ret & 2) /* ts_fd */
        {
#define NR_TS_EVTS  16
            struct input_event evts[NR_TS_EVTS];
            int nr = nwz_ts_read_events(ts_fd, evts, NR_TS_EVTS);
            for(int i = 0; i < nr; i++)
                if(nwz_ts_state_update(&ts_state, &evts[i]) == 1)
                {
                    nwz_lcdmsgf(false, 1, 7, "touch: %s  ", ts_state.touch ? "yes" : "no");
                    nwz_lcdmsgf(false, 1, 8, "pos: %d, %d       ", ts_state.x, ts_state.y);
                    nwz_lcdmsgf(false, 1, 9, "pressure: %d     ", ts_state.pressure);
                    nwz_lcdmsgf(false, 1, 10, "width: %d     ", ts_state.tool_width);
                    nwz_lcdmsgf(false, 1, 11, "flick: %s ", ts_state.flick ? "yes" : "no");
                    nwz_lcdmsgf(false, 1, 12, "flick vec: %d, %d    ", ts_state.flick_x, ts_state.flick_y);
                    /* process touch */
                    nwz_ts_state_post_syn(&ts_state);
                }
#undef NR_TS_EVTS
        }
    }
    /* close input device */
    nwz_key_close(key_fd);
    nwz_ts_close(ts_fd);
    /* finish nicely */
    return 0;
}