Linux行走(3)——input子系统例子

来源:互联网 发布:嵌入式linux裁剪 编辑:程序博客网 时间:2024/05/28 23:11

搞了好久,这个真的很烦,稀里糊涂的搞出来测试程序,但是对于内容的理解还有问题,继续学习中,这里记录一下代码。



这里只贴出添加的代码,因为我没有硬件只能模拟数据,所以我采用字符设备输入,然后每次输入的时候input系统report。


我在init函数里面初始化input子系统

my_input_init();


static void my_input_init(void)
{
    int error;
    my_input_dev = input_allocate_device(); //通过input.c里面的函数分配一个输入设备
    my_input_dev->phys = "my_inpout"; ////提供给编程者的设备节点的名称  //别人告诉的
    __set_bit(EV_ABS,my_input_dev->evbit); //上面申请了设备,现在要告诉设备他支持哪些事件

    //input_dev 有两个成员,一个是evbit 一个是keybit 分别用来表示设备支持的事件类型,和按键类型

    //对于这里 真正的触摸屏还有一句

    __set_bit(ABS_X,idev->absbit);//详细的关系请参考http://bbs.csdn.net/topics/390315601?page=1#post-394567582
    input_set_abs_params(my_input_dev, ABS_Y, 0, 0x1750, 4, 0);//这句是抄袭的,后来根据网络查到的资料可以这么理解

    //对于Y轴范围是0-0x1750 数据误差是-4 到4 中间平滑位置是0//我抄袭的是触摸屏的代码所以先这样理解,当时主要是为了能跑起来

    error = input_register_device(my_input_dev);//注册设备
}

//这里执行结束后就注册了设备。

下面就看消息传送了

这个属于 input设备注册的一般流程,网上一查资料一大堆 


我在字符设备write的时候完成消息发送

具体函数

static void my_input_dev_function(int msg)
{
    input_report_abs(my_input_dev,ABS_Y,msg);//这里的msg 就是 字符设备写入的数据
    input_sync(my_input_dev);//用于消息同步暂不深究

}


就这样把驱动这段搞好了 下面看一下测试程序 只备注关键的地方


#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <pthread.h>
#include <linux/input.h>


#define DEVICE_NAME "/dev/hello"


int read_input(void);
void *thread_read_fn()
    {
        printf("thread_read_fn start\n");
        read_input();
        
return NULL;
    }  
int main(int argc , char**argv)
{
    int fd = -1;
    int val = 0;
    fd = open(DEVICE_NAME, O_RDWR);


    if(fd == -1)
    {
        printf("failed to open device %s.\n",DEVICE_NAME);
        return -1;
    }
    printf("read original value :\n");
    read(fd,&val,sizeof(val));
    printf("%d .\n\n",val);
    val = 521;


    pthread_t thread_read;
     
    pthread_create(&thread_read,NULL,thread_read_fn,NULL);//开启 读取 input节点的线程。
    for(;;){
            scanf("%d",&val);
   printf("write value %d to %s .\n\n",val,DEVICE_NAME);
            if(val == 521){
printf("exit!");
                break;
            }
   write(fd,&val,sizeof(val));


   printf("read the value again: \n");
 
   read(fd, &val,sizeof(val));


   printf("%d .\n\n",val);
    }
    close(fd);
    return 0;
}


int read_input(void)
{
    int input_fd;
    int key_value,i=0,count;
    struct input_event ev_key;
    printf("read_input start \n");
    input_fd = open("/dev/input/event6",O_RDWR);//这里的6说实话我也不知道为什么 但是通过一个方法可以找到这个值,当你把驱动那端搞好,把boot烧进去后 进行如下操作

//adb shell 回车 , cd proc/bus/input 回车 , cat  input 找到 你input 设备的对应名字 你就知道这个数值是多少了

   
    if(input_fd<0)
    {
        printf("open input error");
        return 0;
    }
    for(;;)
    {
        printf("read_input for start \n");
        count = read(input_fd,&ev_key,sizeof(struct input_event)); //读取消息 //对于这里的count 也没有理解 
        printf("read_input for read \n");
        for(i=0;i<(int)(count)/sizeof(struct input_event);i++)
        {
            printf("type:%d,code:%d,value:%d \n",ev_key.type,ev_key.code,ev_key.value);
}
        
    }
    close(input_fd);
}

暂时只是为了记录 代码连接稍后提供

http://download.csdn.net/detail/shen332401890/5430653 下载链接

原创粉丝点击