6ul/6ull-linux4.1.15添加矩阵键盘


开发板:OKMX6UL-C/OKMX6ULL-S (Linux4.1.15)


矩阵键盘添加方法:

1、切换到root用户

whl@ubuntu:~/work/temp/linux-4.1.15$ sudo su root

2、设置环境变量

root@ubuntu:/home/whl/work/temp/linux-4.1.15# . /opt/fsl-imx-x11/4.1.15-2.0.0/environment-setup-cortexa7hf-neon-poky-linux-gnueabi 

3、打开配置菜单添加矩阵键盘配置

root@ubuntu:/home/whl/work/temp/linux-4.1.15# make menuconfig

->Device Drivers

​ ->Input device support

​ ->Keyboards

​ GPIO driven matrix keypad support
在这里插入图片描述
4、修改设备树,添加matrix-keypad节点(例如添加一个3x2的矩阵键盘)

matrix-keypad {
                compatible = "gpio-matrix-keypad";
                debounce-delay-ms = <5>;
                col-scan-delay-us = <2>;

                row-gpios = <&gpio3 23 0
                             &gpio3 24 0
                             &gpio3 25 0>;

                col-gpios = <&gpio3 26 0 
                             &gpio3 27 0>;
                linux,keymap = <0x0000008B
                        0x0100009E
                        0x02000069
                        0x0001006A
                        0x0101001C
                        0x0201006C>;
        };

其中linux,keymap属性中,每一组数据代表:行、列、键值

row << 24 | column << 16 | key-code

0x0000008B:代表0行,0列,键值139

0x0100009E:代表1行,0列,键值158

0x02000069:代表2行,0列,键值105

0x0001006A:代表0行,1列,键值106

0x0101001C:代表1行,1列,键值28

0x0201006C:代表2行,1列,键值108

5、重新编译内核和设备树,重新烧录

6、进入文件系统后,查看是否生成对应节点
在这里插入图片描述
从打印信息中可以看出有生成matrix-keypad设备和event0节点

7、测试

使用hexdump命令监控/dev/input/event0节点
在这里插入图片描述

/*按下时*/
hexdump序列号          秒             微妙         键盘事件    code=KEY_L     value=1(按下)
0000010			  6238 59c3      fe06 000e		  0001        008b           0001 0000
/*抬起时*/
hexdump序列号          秒             微妙         键盘事件    code=KEY_L     value=0(松开)
0000040			  6238 59c3      41fe 0001		  0001        008b           0000 0000

8、测试程序

#include <stdio.h>
#include <linux/input.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define DEV_PATH "/dev/input/event0"

int main()
{
        int keys_fd;
        struct input_event key_event;

        keys_fd=open(DEV_PATH, O_RDONLY);
        if(keys_fd <= 0)
        {
                printf("open /dev/input/event1 device error!\n");
                return -1;
        }
        while(1)
        {
                if(read(keys_fd, &key_event, sizeof(key_event)) == sizeof(key_event))
                {
                        if(key_event.type==EV_KEY)
                                if(key_event.value==0 || key_event.value==1)
                                {
                                        printf("key %d %s \n",key_event.code,(key_event.value)?"Pressed":"Released");

                                }
                }
        }
        close(keys_fd);
        return 0;
}

input_event结构体,在input.h中有定义

struct input_event {
	struct timeval time;
	__u16 type;
	__u16 code;
	__s32 value;
};

运行结果

root@imx6ulevk:~# ./key_test 
key 139 Pressed 
key 139 Released 
key 158 Pressed 
key 158 Released 
key 105 Pressed 
key 105 Released 
key 106 Pressed 
key 106 Released 
key 28 Pressed 
key 28 Released 
key 108 Pressed 
key 108 Released
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TOLA--

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值