用shell脚本读取gpio按键的对应的eventx

时间:2022-07-21 16:15:29

我们可以从cat /proc/bus/input/devices可以看到所有input设备和eventx对应的关系:

I: Bus=0019 Vendor=0001 Product=0001 Version=0100
N: Name="gpio_keys_polled.7"
P: Phys=gpio-keys-polled/input0
S: Sysfs=/devices/soc.0/gpio_keys_polled.7/input/input0
U: Uniq=
H: Handlers=kbd event0
B: PROP=0
B: EV=100003
B: KEY=780 0 0 0

I: Bus=0003 Vendor=046d Product=c077 Version=0111
N: Name="Logitech USB Optical Mouse"
P: Phys=usb-ffb40000.usb-1.1/input0
S: Sysfs=/devices/soc.0/ffb40000.usb/usb1/1-1/1-1.1/1-1.1:1.0/input/input1
U: Uniq=
H: Handlers=mouse0 event1
B: PROP=0
B: EV=17
B: KEY=ff0000 0 0 0 0 0 0 0 0
B: REL=103
B: MSC=10

I: Bus=0003 Vendor=413c Product=2107 Version=0111
N: Name="DELL Dell USB Entry Keyboard"
P: Phys=usb-ffb40000.usb-1.3/input0
S: Sysfs=/devices/soc.0/ffb40000.usb/usb1/1-1/1-1.3/1-1.3:1.0/input/input2
U: Uniq=
H: Handlers=sysrq kbd event2
B: PROP=0
B: EV=120013
B: KEY=10000 7 ff9f207a c14057ff febeffdf ffefffff ffffffff fffffffe
B: MSC=10

 

 

read_eventx.sh脚本:

#/bin/bash

line=$(cat cat /proc/bus/input/devices| grep -n "gpio-keys" | awk -F: '{print $1}')
echo $line
line=$(($line + 3))
echo $line

cat cat /proc/bus/input/devices| sed -n ''$line'p' |awk '{print $3}'

cat cat /proc/bus/input/devices| sed -n "$line"p |awk '{print $3}'

 

很简单,这里需要在sed命令中使用变量中的行号。