注册 登录
电子工程世界-论坛 返回首页 EEWORLD首页 频道 EE大学堂 下载中心 Datasheet 专题
damiaa的个人空间 https://home.eeworld.com.cn/space-uid-59433.html [收藏] [复制] [分享] [RSS]
日志

【 正点原子瑞芯微RV1126测评】 5 input子系统

已有 203 次阅读2024-2-16 21:59 |个人分类:瑞芯微

正点原子瑞芯微RV1126测评】 5 input子系统
 
 
一、input子系统介绍
 
Linux中的input子系统是一个用于管理不同类型输入设备的设备框架,包括input驱动层、input核心层和input事件处理层,为应用程序提供统一的接口来访问这些设备。
Input子系统支持多种类型的输入设备,如键盘、鼠标、触摸屏、游戏手柄、传感器等等,但它们通过统一的接口和框架,允许用户空间应用程序和设备驱动程序与输入设备交互,实现输入数据的获取和处理。
简单的Linux输入子系统(input subsystem)驱动的例子。这个例子会创建一个虚拟的键盘设备,当打开设备时会发送一个按键事件。
#include <linux/module.h>
#include <linux/init.h>
#include <linux/input.h>
static struct input_dev *vir_dev;
static int __init vir_key_init(void) {
int error;
vir_dev = input_allocate_device();
if (!vir_dev)
return -ENOMEM;
// 设置设备信息
set_bit(EV_KEY, vir_dev->evbit); // 支持按键事件
set_bit(EV_REP, vir_dev->evbit); // 支持重复事件
set_bit(KEY_A, vir_dev->keybit); // 定义按键A
// 注册设备
error = input_register_device(vir_dev);
if (error)
goto fail1;
// 发送按键事件
input_report_key(vir_dev, KEY_A, 1); // 按下按键A
input_sync(vir_dev); // 事件同步
input_report_key(vir_dev, KEY_A, 0); // 释放按键A
input_sync(vir_dev); // 事件同步
printk("Virtual keyboard device created\n");
return 0;
fail1:
input_free_device(vir_dev);
return error;
}
static void __exit vir_key_exit(void) {
input_unregister_device(vir_dev);
input_free_device(vir_dev);
printk("Virtual keyboard device removed\n");
}
module_init(vir_key_init);
module_exit(vir_key_exit);
MODULE_LICENSE("GPL");
这段代码定义了一个虚拟键盘设备,在初始化函数中,它创建了一个input_dev结构体实例,并设置了该设备支持的事件类型和具体的按键。然后,它将这个设备注册到输入子系统中,并发送了一个按键事件。在退出函数中,它将注销设备并释放设备资源。
 
二、下面就RV1126开发板实际谈一下input子系统并做一下实验。
 
板子上有四个按键,就是input子系统实现的。
我们 可以插上鼠标、按键查看input子系统里的鼠标和按键事件。
Linux系统通过输入子系统来管理输入设备(按键,鼠标,触摸屏,游戏摇杆)等等,相应的驱动安装好并接上相应的接口后(usb设备要插到usb设备口),系统会在/dev/input/生成对应的设备,如下图:
同样在/proc/bus/input也可以看到信息如下:
没插鼠标键盘时如下:
插上鼠标键盘如下:
当输入设备有事件产生,内核就会将事件上报到设备文件,事件的数据以struct input_event 为 单位存入设备文件,读取事件数据就使用这个结构体: /linux/input.h中
struct input_event {
struct timeval time;//事件产生的时间
__u16 type; //事件的类型
__u16 code; //事件的代码
__s32 value; //事件的值
};
应用程序只需要关系这个结构体的内容就可以了。
/*
* Event 类型有很多
*/
#define EV_SYN0x00
#define EV_KEY0x01
#define EV_REL0x02
#define EV_ABS0x03
#define EV_MSC0x04
#define EV_SW0x05
#define EV_LED0x11
#define EV_SND0x12
#define EV_REP0x14
#define EV_FF0x15
#define EV_PWR0x16
#define EV_FF_STATUS0x17
#define EV_MAX0x1f
#define EV_CNT(EV_MAX+1)
比如键盘 事件的类型EV_KEY
事件的代码就更加具体的描述事件。比如键盘的键值(哪个按键),鼠标的位置,滚轮的信息,触摸屏的位置。
Value就更具体的描述事件,比如按键是按下还是抬起,鼠标的x,y值,触摸屏的几个值等等。
 
我们现在就以一个测试程序来演示事件的情况。
1,插上无线键盘鼠标(我这里是二合一的,其实什么都可以,只要是键盘鼠标)。
2,交叉编译下面的例子程序,并用adb推送到板子,修改执行属性。
/* 
 * Copyright 2002 Red Hat Inc., Durham, North Carolina. 
 * 
 * All Rights Reserved. 
 * 
 * Permission is hereby granted, free of charge, to any person obtaining 
 * a copy of this software and associated documentation files (the 
 * "Software"), to deal in the Software without restriction, including 
 * without limitation on the rights to use, copy, modify, merge, 
 * publish, distribute, sublicense, and/or sell copies of the Software, 
 * and to permit persons to whom the Software is furnished to do so, 
 * subject to the following conditions: 
 * 
 * The above copyright notice and this permission notice (including the 
 * next paragraph) shall be included in all copies or substantial 
 * portions of the Software. 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NON-INFRINGEMENT.  IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS 
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 * SOFTWARE. 
 * 
 * This is a simple test program that reads from /dev/input/event*, 
 * decoding events into a human readable form. 
 */  

/* 
 * Authors: 
 *   Rickard E. (Rik) Faith <faith@redhat.com> 
 * 
 */ 

#include <stdio.h>  
#include <stdlib.h>  
#include <unistd.h>  
#include <string.h>  
#include <sys/types.h>  
#include <fcntl.h>  
#include <errno.h>  
#include <time.h>  
#include <linux/input.h>  

struct input_event event;  

int main(int argc, char **argv)  
{  
    char          name[64];           /* RATS: Use ok, but could be better */  
    char          buf[256] = { 0, };  /* RATS: Use ok */  
    unsigned char mask[EV_MAX/8 + 1]; /* RATS: Use ok */  
    int           version;  
    int           fd = 0;  
    int           rc;  
    int           i, j;  
    char          *tmp;  

#define test_bit(bit) (mask[(bit)/8] & (1 << ((bit)%8)))  

    for (i = 0; i < 32; i++) {  
        sprintf(name, "/dev/input/event%d", i);  
        if ((fd = open(name, O_RDONLY, 0)) >= 0) {  
            ioctl(fd, EVIOCGVERSION, &version);  
            ioctl(fd, EVIOCGNAME(sizeof(buf)), buf);  
            ioctl(fd, EVIOCGBIT(0, sizeof(mask)), mask);  
            printf("%s\n", name);  
            printf("    evdev version: %d.%d.%d\n",  
                   version >> 16, (version >> 8) & 0xff, version & 0xff);  
            printf("    name: %s\n", buf);  
            printf("    features:");  
            for (j = 0; j < EV_MAX; j++) {  
                if (test_bit(j)) {  
                    const char *type = "unknown";  
                    switch(j) {  
                    case EV_KEY: type = "keys/buttons"; break;  
                    case EV_REL: type = "relative";     break;  
                    case EV_ABS: type = "absolute";     break;  
                    case EV_MSC: type = "reserved";     break;  
                    case EV_LED: type = "leds";         break;  
                    case EV_SND: type = "sound";        break;  
                    case EV_REP: type = "repeat";       break;  
                    case EV_FF:  type = "feedback";     break;  
                    }  
                    printf(" %s", type);  
                }  
            }  
            printf("\n");  
            close(fd);  
        }  
    }  

    if (argc > 1) {  
        sprintf(name, "/dev/input/event%d", atoi(argv[1]));  
        if ((fd = open(name, O_RDWR, 0)) >= 0) {  
            printf("%s: open, fd = %d\n", name, fd);  
            for (i = 0; i < LED_MAX; i++) {  
                event.time.tv_sec  = time(0);  
                event.time.tv_usec = 0;  
                event.type         = EV_LED;  
                event.code         = i;  
                event.value        = 0;  
                write(fd, &event, sizeof(event));  
            }  

            while ((rc = read(fd, &event, sizeof(event))) > 0) {  
                printf("%-24.24s.%06lu type 0x%04x; code 0x%04x;"  
                       " value 0x%08x; ",  
                       ctime(&event.time.tv_sec),  
                       event.time.tv_usec,  
                       event.type, event.code, event.value);  
                switch (event.type) {  
                case EV_KEY:  
                    if (event.code > BTN_MISC) {  
                        printf("Button %d %s",  
                               event.code & 0xff,  
                               event.value ? "press" : "release");  
                    } else {  
                        printf("Key %d (0x%x) %s",  
                               event.code & 0xff,  
                               event.code & 0xff,  
                               event.value ? "press" : "release");  
                    }  
                    break;  
                case EV_REL:  
                    switch (event.code) {  
                    case REL_X:      tmp = "X";       break;  
                    case REL_Y:      tmp = "Y";       break;  
                    case REL_HWHEEL: tmp = "HWHEEL";  break;  
                    case REL_DIAL:   tmp = "DIAL";    break;  
                    case REL_WHEEL:  tmp = "WHEEL";   break;  
                    case REL_MISC:   tmp = "MISC";    break;  
                    default:         tmp = "UNKNOWN"; break;  
                    }  
                    printf("Relative %s %d", tmp, event.value);  
                    break;  
                case EV_ABS:  
                    switch (event.code) {  
                    case ABS_X:        tmp = "X";        break;  
                    case ABS_Y:        tmp = "Y";        break;  
                    case ABS_Z:        tmp = "Z";        break;  
                    case ABS_RX:       tmp = "RX";       break;  
                    case ABS_RY:       tmp = "RY";       break;  
                    case ABS_RZ:       tmp = "RZ";       break;  
                    case ABS_THROTTLE: tmp = "THROTTLE"; break;  
                    case ABS_RUDDER:   tmp = "RUDDER";   break;  
                    case ABS_WHEEL:    tmp = "WHEEL";    break;  
                    case ABS_GAS:      tmp = "GAS";      break;  
                    case ABS_BRAKE:    tmp = "BRAKE";    break;  
                    case ABS_HAT0X:    tmp = "HAT0X";    break;  
                    case ABS_HAT0Y:    tmp = "HAT0Y";    break;  
                    case ABS_HAT1X:    tmp = "HAT1X";    break;  
                    case ABS_HAT1Y:    tmp = "HAT1Y";    break;  
                    case ABS_HAT2X:    tmp = "HAT2X";    break;  
                    case ABS_HAT2Y:    tmp = "HAT2Y";    break;  
                    case ABS_HAT3X:    tmp = "HAT3X";    break;  
                    case ABS_HAT3Y:    tmp = "HAT3Y";    break;  
                    case ABS_PRESSURE: tmp = "PRESSURE"; break;  
                    case ABS_DISTANCE: tmp = "DISTANCE"; break;  
                    case ABS_TILT_X:   tmp = "TILT_X";   break;  
                    case ABS_TILT_Y:   tmp = "TILT_Y";   break;  
                    case ABS_MISC:     tmp = "MISC";     break;  
					case ABS_MT_SLOT:  tmp = "MT_SLOT";     break;  
					case ABS_MT_TRACKING_ID: tmp = "MT_TRACKING_ID";     break;  
					case ABS_MT_POSITION_X:  tmp = "MT_X";     break;  
					case ABS_MT_POSITION_Y:  tmp = "MT_Y";     break;  
                    default:           tmp = "UNKNOWN";  break;  
                    }  
                    printf("Absolute %s %d", tmp, event.value);  
                    break;  
                case EV_MSC: printf("Misc"); break;  
                case EV_LED: printf("Led");  break;  
                case EV_SND: printf("Snd");  break;  
                case EV_REP: printf("Rep");  break;  
                case EV_FF:  printf("FF");   break;  
                    break;  
                }  
                printf("\n");  
            }  
            printf("rc = %d, (%s)\n", rc, strerror(errno));  
            close(fd);  
        }  
    }  
    return 0;  
}

 

3,运行./inputeventtest 观看提示:
4,运行./inputeventtest 1 测试黄色的一排按键并观察打印出来的数据。
5,运行./inputeventtest 2 测试按下键盘按键并观察打印出来的数据。
6,运行./inputeventtest 3 测试鼠标(左按,右按,滚轮等)并观察打印出来的数据。
至此,我们对input的应用有了比较清楚的了解了。
谢谢大家,下次再聊。

本文来自论坛,点击查看完整帖子内容。

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

热门文章