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

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

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

用 ir-keytable 測試 IR 硬體
  1. # 查看 gpio,確認接線
  2. pinout
  3. # 設定 /boot/config.txt,依接線設定 gpio
  4. dtoverlay=gpio-ir,gpio_pin=17
  5. dtoverlay=gpio-ir-tx,gpio_pin=18
  6. # 重開機
  7. sudo reboot
  8. # 安裝 ir-keytable
  9. sudo apt install ir-keytable
  10. # 開始測試,隨意按下遙控器
  11. sudo ir-keytable -p all -t
config.txt的配置語法
樹莓派因為沒有 BIOS,所以 Raspbian 對設備的加載都是依賴在 /boot/config.txt 中的配置來加載
當 Linux 內核加載時,會讀取 /boot/config.txt 中的設備配置和設備參數配置來把設備動態加載到 Device Tree(DT) 中
  1. dtoverlay=<device>
  2. dtparam=<param1>,<param2>,...
安裝 LIRC
  1. sudo apt update
  2. sudo apt install lirc
  3. # 安裝會出錯,此時再將 lirc_options.conf.dist 改名為 lirc_options.conf,再重新安裝
  4. sudo mv /etc/lirc/lirc_options.conf.dist /etc/lirc/lirc_options.conf
  5. sudo apt install lirc
  1. # 設備/dev/lirc1 是接收器,設備/dev/lirc0 是發送器
  2. # 編輯 /etc/lirc/lirc_options.conf
  3. driver = default
  4. device = /dev/lirc1
  5. # 啟動服務
  6. sudo systemctl stop lircd.service
  7. sudo systemctl start lircd.service
  8. sudo systemctl status lircd.service
  9. # 重開機
  10. sudo reboot
  11. # 測試
  12. sudo systemctl stop lircd.service
  13. sudo mode2 -d /dev/lirc1
設定按鍵
錄製方法如下,建議到官網抓取品牌範例,再修改
  1. # 列出可錄製的按鍵名
  2. irrecord -l
  3. # 錄製按鍵,並存檔至 ~/remoteName.lircd.conf
  4. # 常用 KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_OK
  5. irrecord -d /dev/lirc1 ~/lircd.conf
  6. # 依序為
  7. # 輸入表的名字
  8. # 任意鍵長按至 1 秒,同樣的鍵不超過十個點
  9. # 輸入要記錄鍵的名字,短按即可,結束請按 enter
  10. # 任意一個鍵,快速重覆按
lircd.conf 內容如下
  1. # this config file was automatically generated
  2. # using lirc-0.8.3pre1(default) on Mon Nov 26 23:16:08 2007
  3. #
  4. # contributed by Chris Moates <six|mox.ne>
  5. #
  6. # brand: Vizio
  7. # model no. of remote control: Unknown. Black remote with no backlighting.
  8. # devices being controlled by this remote: Vizio LCD TV
  9. #
  10. # I recorded this with irrecord, but then reversed the flags because they
  11. # make a lot more sense that way. I did try the other combinations according
  12. # to the pattern that is evident, but found no other codes aside from the
  13. # ones on the remote, so no discreets to switch to a particular HDMI input,
  14. # for example. :(
  15.  
  16. begin remote
  17.  
  18. name Vizio
  19. bits 16
  20. flags SPACE_ENC|CONST_LENGTH|REVERSE
  21. eps 30
  22. aeps 100
  23.  
  24. header 8939 4447
  25. one 547 1684
  26. zero 547 565
  27. ptrail 539
  28. pre_data_bits 16
  29. pre_data 0xFB04
  30. gap 107074
  31. toggle_bit_mask 0x0
  32.  
  33. begin codes
  34. KEY_INPUT 0xD02F # Was: input
  35. KEY_POWER 0xF708 # Was: power
  36. KEY_MENU 0xB04F # Was: menu
  37. KEY_INFO 0xE41B # Was: info
  38. KEY_UP 0xBA45 # Was: up
  39. KEY_LEFT 0xB847 # Was: left
  40. KEY_OK 0xBB44 # Was: ok
  41. KEY_RIGHT 0xB748 # Was: right
  42. KEY_DOWN 0xB946 # Was: down
  43. KEY_BACK 0xB54A # Was: back
  44. KEY_HOME 0xD22D # Was: home
  45. KEY_CC 0xC639 # Was: cc
  46. KEY_VOLUMEUP 0xFD02 # Was: volup
  47. KEY_LAST 0xE51A # Was: last
  48. KEY_CHANNELUP 0xFF00 # Was: chup
  49. KEY_VOLUMEDOWN 0xFC03 # Was: voldn
  50. KEY_MUTE 0xF609 # Was: mute
  51. KEY_CHANNELDOWN 0xFE01 # Was: chdown
  52. KEY_1 0xEE11 # Was: 1
  53. KEY_2 0xED12 # Was: 2
  54. KEY_3 0xEC13 # Was: 3
  55. KEY_4 0xEB14 # Was: 4
  56. KEY_5 0xEA15 # Was: 5
  57. KEY_6 0xE916 # Was: 6
  58. KEY_7 0xE817 # Was: 7
  59. KEY_8 0xE718 # Was: 8
  60. KEY_9 0xE619 # Was: 9
  61. KEY_PIC 0x9867 # Was: pic
  62. KEY_0 0xEF10 # Was: 0
  63. KEY_DASH 0x00FF # Was: dash
  64. KEY_WIDE 0x8877 # Was: wide
  65. KEY_EXIT 0xB649 # Was: exit
  66. end codes
  67.  
  68. end remote
  1. # 將按鍵表放到 /etc/lirc
  2. sudo cp ~/lircd.conf /etc/lirc/lircd.conf
IR 接收
  1. # 設備/dev/lirc1 是接收器,設備/dev/lirc0 是發送器
  2. # 編輯 /etc/lirc/lirc_options.conf
  3. driver = default
  4. device = /dev/lirc1
  1. # 重啟服務
  2. sudo systemctl restart lircd.service
  3. # 測試接收,隨意按遙控器,會看到對應按鍵名
  4. irw
IR 傳送
  1. # 設備/dev/lirc1 是接收器,設備/dev/lirc0 是發送器
  2. # 編輯 /etc/lirc/lirc_options.conf
  3. driver = default
  4. device = /dev/lirc0
  1. # 重啟服務
  2. sudo systemctl restart lircd.service
  3. # 列出所有 remote name
  4. irsend LIST "" ""
  5. # 檢查特定 remote 可以送出的按鈕
  6. irsend LIST remoteName ""
  7. # 測試傳送
  8. irsend SEND_ONCE remoteName KEY_UP
  9. 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

留言