我的树莓派4B的OS版本Raspbian GNU/Linux 10 (buster),获取的命令为 cat /etc/os-release
- 修改/boot/config.txt
sudo nano /boot/config.txt. 把下列行的注释去掉(去掉井号),gpio-ir负责接收,gpio-ir-tx负责发送。我只需要接收,所以gpio-ir-tx仍然保持注释
dtoverlay=gpio-ir,gpio_pin=17
#dtoverlay=gpio-ir-tx,gpio_pin=18
- 安装lirc
sudo apt-get update
sudo apt-get install lirc
- 编辑Edit Lirc_options.conf
编辑/etc/lirc/lirc_options.conf, 修改下面的两行
driver = default
device = /dev/lirc0
驱动使用 default,自带的里面是devinput。device使用 /dev/lirc0,对应于红外接收设备
- 放置lircd的配置文件
把对应于自己遥控器的lircd文件放到 /etc/lirc/lircd.conf。 如果没有lircd的配置文件,可能需要自己录制一个,可以参考irrecord命令 - 重启。重启后,在/dev下应该有lirc0设备。如果启用了gpio-ir-tx,还会有lirc1设备。
- 输入irw,这时按遥控器按钮应该有输出。
pi@raspberrypi:~/.config $ irw
000000000000000c 00 KEY_NUMERIC_2 ./lircd.conf
000000000000000c 00 KEY_NUMERIC_2 ./lircd.conf
000000000000000b 00 KEY_NUMERIC_1 ./lircd.conf
000000000000000a 00 KEY_NUMERIC_0 ./lircd.conf
0000000000000014 00 BTN_0 ./lircd.conf
0000000000000015 00 BTN_1 ./lircd.con
- python集成
如果要用python调用,sudo apt-get install python-pylirc. 不过这个包只有在python2下安装了。我还没研究怎么装到python3下面。
写了一段代码,可以用遥控器控制Turtle模块画图。这里的init用到的/etc/lirc/zzpconf是lircrc的config,参考LIRC配置文件格式
import pylirc
import time
import turtle
pen = turtle.Pen()
pen.speed(200)
pylirc.init("pylirc", "/etc/lirc/zzpconf", 1)
while True:
x = pylirc.nextcode()
print(x)
if x is None:
continue
if x[0] == 'KEY_NUMERIC_2':
pen.setheading(90)
pen.forward(20)
elif x[0] == 'KEY_NUMERIC_8':
pen.setheading(-90)
pen.forward(20)
elif x[0] == 'KEY_NUMERIC_4':
pen.setheading(180)
pen.forward(20)
elif x[0] == 'KEY_NUMERIC_6':
pen.setheading(0)
pen.forward(20)
elif x[0] == 'KEY_NUMERIC_5':
pen.clear()
elif x[0] == 'KEY_VOLUMEUP':
pen.penup()
elif x[0] == 'KEY_VOLUMEDOWN':
pen.pendown()
我的lircrc配置文件供参考
begin
remote = *
button = KEY_CHANNELDOWN
prog = pylirc
config = KEY_CHANNELDOWN
end
begin
remote = *
button = KEY_CHANNEL
prog = pylirc
config = KEY_CHANNEL
end
begin
remote = *
button = KEY_CHANNELUP
prog = pylirc
config = KEY_CHANNELUP
end
begin
remote = *
button = KEY_PREVIOUS
prog = pylirc
config = KEY_PREVIOUS
end
begin
remote = *
button = KEY_NEXT
prog = pylirc
config = KEY_NEXT
end
begin
remote = *
button = KEY_PLAYPAUSE
prog = pylirc
config = KEY_PLAYPAUSE
end
begin
remote = *
button = KEY_VOLUMEDOWN
prog = pylirc
config = KEY_VOLUMEDOWN
end
begin
remote = *
button = KEY_VOLUMEUP
prog = pylirc
config = KEY_VOLUMEUP
end
begin
remote = *
button = KEY_EQUAL
prog = pylirc
config = KEY_EQUAL
end
begin
remote = *
button = KEY_NUMERIC_0
prog = pylirc
config = KEY_NUMERIC_0
end
begin
remote = *
button = KEY_NUMERIC_1
prog = pylirc
config = KEY_NUMERIC_1
end
begin
remote = *
button = KEY_NUMERIC_2
prog = pylirc
config = KEY_NUMERIC_2
end
begin
remote = *
button = KEY_NUMERIC_3
prog = pylirc
config = KEY_NUMERIC_3
end
begin
remote = *
button = KEY_NUMERIC_4
prog = pylirc
config = KEY_NUMERIC_4
end
begin
remote = *
button = KEY_NUMERIC_5
prog = pylirc
config = KEY_NUMERIC_5
end
begin
remote = *
button = KEY_NUMERIC_6
prog = pylirc
config = KEY_NUMERIC_6
end
begin
remote = *
button = KEY_NUMERIC_7
prog = pylirc
config = KEY_NUMERIC_7
end
begin
remote = *
button = KEY_NUMERIC_8
prog = pylirc
config = KEY_NUMERIC_8
end
begin
remote = *
button = KEY_NUMERIC_9
prog = pylirc
config = KEY_NUMERIC_9
end
begin
remote = *
button = BTN_0
prog = pylirc
config = BTN_0
end
begin
remote = *
button = BTN_1
prog = pylirc
config = BTN_1:
end
参考:
Easy Setup IR Remote Control Using LIRC for the Raspberry PI (RPi) - Updated Oct 2021 [Part 1] : 7 Steps - Instructables
LIRC配置文件格式
LIRCD文档
python-lirc · PyPI
Infra-Red Remotes - LibreELEC.wiki