[Raspberry Pi] Raspberry Pi OS IR Remote 設定 (LIRC)

軟體:LIRC
Package:
lirc
ir-keytable
LIRC 官網

功能:傳送/接收 紅外線遙控

用 ir-keytable 測試 IR 硬體
# 查看 gpio,確認接線
pinout
# 設定 /boot/config.txt,依接線設定 gpio
dtoverlay=gpio-ir,gpio_pin=17
dtoverlay=gpio-ir-tx,gpio_pin=18
# 重開機
sudo reboot
# 安裝 ir-keytable
sudo apt install ir-keytable
# 開始測試,隨意按下遙控器
sudo ir-keytable -p all -t
config.txt的配置語法
樹莓派因為沒有 BIOS,所以 Raspbian 對設備的加載都是依賴在 /boot/config.txt 中的配置來加載
當 Linux 內核加載時,會讀取 /boot/config.txt 中的設備配置和設備參數配置來把設備動態加載到 Device Tree(DT) 中
dtoverlay=<device>
dtparam=<param1>,<param2>,...
安裝 LIRC
sudo apt update
sudo apt install lirc
# 安裝會出錯,此時再將 lirc_options.conf.dist 改名為 lirc_options.conf,再重新安裝
sudo mv /etc/lirc/lirc_options.conf.dist /etc/lirc/lirc_options.conf
sudo apt install lirc
# 設備/dev/lirc1 是接收器,設備/dev/lirc0 是發送器
# 編輯 /etc/lirc/lirc_options.conf 
driver = default
device = /dev/lirc1
# 啟動服務
sudo systemctl stop lircd.service
sudo systemctl start lircd.service
sudo systemctl status lircd.service
# 重開機
sudo reboot
# 測試
sudo systemctl stop lircd.service
sudo mode2 -d /dev/lirc1
設定按鍵
錄製方法如下,建議到官網抓取品牌範例,再修改
# 列出可錄製的按鍵名
irrecord -l
# 錄製按鍵,並存檔至 ~/remoteName.lircd.conf
# 常用 KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_OK
irrecord -d /dev/lirc1 ~/lircd.conf
# 依序為
# 輸入表的名字
# 任意鍵長按至 1 秒,同樣的鍵不超過十個點
# 輸入要記錄鍵的名字,短按即可,結束請按 enter
# 任意一個鍵,快速重覆按
lircd.conf 內容如下
# this config file was automatically generated
# using lirc-0.8.3pre1(default) on Mon Nov 26 23:16:08 2007
#
# contributed by Chris Moates <six|mox.ne>
#
# brand:                                   Vizio
# model no. of remote control:             Unknown. Black remote with no backlighting.
# devices being controlled by this remote: Vizio LCD TV
#
# I recorded this with irrecord, but then reversed the flags because they
# make a lot more sense that way. I did try the other combinations according
# to the pattern that is evident, but found no other codes aside from the
# ones on the remote, so no discreets to switch to a particular HDMI input,
# for example. :(

begin remote

  name  Vizio
  bits           16
  flags SPACE_ENC|CONST_LENGTH|REVERSE
  eps            30
  aeps          100

  header       8939  4447
  one           547  1684
  zero          547   565
  ptrail        539
  pre_data_bits   16
  pre_data       0xFB04
  gap          107074
  toggle_bit_mask 0x0

      begin codes
          KEY_INPUT                0xD02F                    #  Was: input
          KEY_POWER                0xF708                    #  Was: power
          KEY_MENU                 0xB04F                    #  Was: menu
          KEY_INFO                 0xE41B                    #  Was: info
          KEY_UP                   0xBA45                    #  Was: up
          KEY_LEFT                 0xB847                    #  Was: left
          KEY_OK                   0xBB44                    #  Was: ok
          KEY_RIGHT                0xB748                    #  Was: right
          KEY_DOWN                 0xB946                    #  Was: down
          KEY_BACK                 0xB54A                    #  Was: back
          KEY_HOME                 0xD22D                    #  Was: home
          KEY_CC                   0xC639                    #  Was: cc
          KEY_VOLUMEUP             0xFD02                    #  Was: volup
          KEY_LAST                 0xE51A                    #  Was: last
          KEY_CHANNELUP            0xFF00                    #  Was: chup
          KEY_VOLUMEDOWN           0xFC03                    #  Was: voldn
          KEY_MUTE                 0xF609                    #  Was: mute
          KEY_CHANNELDOWN          0xFE01                    #  Was: chdown
          KEY_1                    0xEE11                    #  Was: 1
          KEY_2                    0xED12                    #  Was: 2
          KEY_3                    0xEC13                    #  Was: 3
          KEY_4                    0xEB14                    #  Was: 4
          KEY_5                    0xEA15                    #  Was: 5
          KEY_6                    0xE916                    #  Was: 6
          KEY_7                    0xE817                    #  Was: 7
          KEY_8                    0xE718                    #  Was: 8
          KEY_9                    0xE619                    #  Was: 9
          KEY_PIC                  0x9867                    #  Was: pic
          KEY_0                    0xEF10                    #  Was: 0
          KEY_DASH                 0x00FF                    #  Was: dash
          KEY_WIDE                 0x8877                    #  Was: wide
          KEY_EXIT                 0xB649                    #  Was: exit
      end codes

end remote
# 將按鍵表放到 /etc/lirc
sudo cp ~/lircd.conf /etc/lirc/lircd.conf
IR 接收
# 設備/dev/lirc1 是接收器,設備/dev/lirc0 是發送器
# 編輯 /etc/lirc/lirc_options.conf 
driver = default
device = /dev/lirc1
# 重啟服務
sudo systemctl restart lircd.service
# 測試接收,隨意按遙控器,會看到對應按鍵名
irw
IR 傳送
# 設備/dev/lirc1 是接收器,設備/dev/lirc0 是發送器
# 編輯 /etc/lirc/lirc_options.conf 
driver = default
device = /dev/lirc0
# 重啟服務
sudo systemctl restart lircd.service
# 列出所有 remote name
irsend LIST "" ""
# 檢查特定 remote 可以送出的按鈕
irsend LIST remoteName ""
# 測試傳送
irsend SEND_ONCE remoteName KEY_UP
irsend SEND_ONCE remoteName KEY_UP KEY_Down

參考

IR Remote Control Support on Raspbian Buster – JustBoom
raspberry-lirc
Easy Setup IR Remote Control Using LIRC for the Raspberry PI (RPi) - July 2019 [Part 1]
Using a simple IR receiver such as IR5040 or TSOPxxxxx and a Raspberry Pi running LibreELEC + Kodi
Raspberry Pi + LIRC 紅外線遙控實做
Using an IR Remote with a Raspberry Pi Media Center
在樹莓派3上使用紅外遙控器控制libreELEC

留言