LingYun Studio embeded system framwork software, such as thirdparty build shell and lingyun library
guowenxue
2024-09-25 e8d34d11799fc79c7c53bdcd40f9b4ee7be7a2c5
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
/*********************************************************************************
 *      Copyright:  (C) 2024 LingYun IoT System Studio
 *                  All rights reserved.
 *
 *       Filename:  key.c
 *    Description:  This file is linux GPIO button driver test code.
 *
 ********************************************************************************/
 
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <libgen.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <linux/input.h>
#include <linux/kd.h>
#include <linux/keyboard.h>
 
#if 0 /* Just for comment here, Reference to linux-3.3/include/linux/input.h */
struct input_event
{
    struct timeval time;
    __u16 type;  /* 0x00:EV_SYN 0x01:EV_KEY 0x04:EV_MSC 0x11:EV_LED*/
    __u16 code;  /* key value, which key */
    __s32 value; /* 1: Pressed  0:Not pressed  2:Always Pressed */
};
#endif
 
#define EV_RELEASED        0
#define EV_PRESSED         1
#define EV_REPEAT          2
 
#define BUTTON_CNT         1
 
#define MODE_POLL          0x01
#define MODE_NORMAL        0x02
 
void usage(char *name);
void display_button_event(struct input_event *ev, int cnt);
 
static void program_usage(const char *progname)
{
    printf("Usage: %s [OPTION]...\n", progname);
    printf(" This is linux GPIO button driver test program.\n");
 
    printf(" -d[device  ]  Specify input device number, such as 0\n");
    printf(" -h[help    ]  Display this help information\n");
 
    printf("\n");
    printf("%s program Version v1.0.0\n", progname);
    printf("Copyright (C) 2023 LingYun IoT System Studio.\n");
 
    return;
}
 
int main(int argc, char **argv)
{
    char                   *progname=NULL;
    char                    kbd_dev[128];
    char                    kbd_name[128] = "Unknown";
    int                     kbd_fd = -1;
    struct input_event      ev[BUTTON_CNT];
    int                     size = sizeof (struct input_event);
 
    int                     which = -1;
    int                     rv, opt;
    fd_set                  rds;
 
    struct option long_options[] = {
        {"device", required_argument, NULL, 'd'},
        {"help", no_argument, NULL, 'h'},
        {NULL, 0, NULL, 0}
    };
 
    progname = basename(argv[0]);
 
    /* Parser the command line parameters */
    while ((rv = getopt_long(argc, argv, "d:h", long_options, NULL)) != -1)
    {
        switch (rv)
        {
            case 'd': /*  Set infrared number, such as 0...max */
                which = atoi(optarg);
                break;
 
            case 'h':  /* Get help information */
                program_usage(progname);
                return 0;
 
            default:
                break;
        }
    }
 
    if( !which )
    {
        program_usage(progname);
        return 1;
    }
 
    if ((getuid ()) != 0)
        printf ("You are not root! This may not work...\n");
 
    snprintf(kbd_dev, sizeof(kbd_dev), "/dev/input/event%d", which);
    if ((kbd_fd = open(kbd_dev, O_RDONLY)) < 0)
    {
        printf("Open %s failure: %s", kbd_dev, strerror(errno));
        return -1;
    }
 
    ioctl (kbd_fd, EVIOCGNAME (sizeof (kbd_name)), kbd_name);
    printf ("Monitor input device %s (%s) event on poll mode:\n", kbd_dev, kbd_name);
 
    while (1)
    {
        FD_ZERO(&rds);
        FD_SET(kbd_fd, &rds);
 
        rv = select(kbd_fd + 1, &rds, NULL, NULL, NULL);
        if (rv < 0)
        {
            printf("select() system call failure: %s\n", strerror(errno));
            goto cleanup;
        }
        else if (FD_ISSET(kbd_fd, &rds))
        {
            if ((rv=read(kbd_fd, ev, size*BUTTON_CNT )) < size)
            {
                printf("Reading data from kbd_fd failure: %s\n", strerror(errno));
                break;
            }
            else
            {
                display_button_event(ev, rv/size);
            }
        }
    }
 
cleanup:
    close(kbd_fd);
 
    return 0;
}
 
void display_button_event(struct input_event *ev, int cnt)
{
    int i;
    struct timeval        pressed_time, duration_time;
 
    for(i=0; i<cnt; i++)
    {
        //printf("type:%d code:%d value:%d\n", ev[i].type, ev[i].code, ev[i].value);
        if(EV_KEY==ev[i].type && EV_PRESSED==ev[i].value)
        {
            pressed_time = ev[i].time;
            printf("Keypad[%d] pressed time: %ld.%ld\n", ev[i].code, pressed_time.tv_sec, pressed_time.tv_usec);
        }
 
        if(EV_KEY==ev[i].type && EV_RELEASED==ev[i].value)
        {
            timersub(&ev[i].time, &pressed_time, &duration_time);
            printf("keypad[%d] duration time: %ld.%ld\n", ev[i].code, duration_time.tv_sec, duration_time.tv_usec);
        }
    }
}