在 Raspberry Pi 上使用 HDMI-CEC

什么是HDMI-CEC

HDMI-CEC 是一种特殊协议,专为电视通过 HDMI 电缆与其他设备进行通信而设计。该协议允许电视控制另一个设备,同时还允许该设备控制电视。

大多数现代电视都支持此协议,但通常必须在电视设置中启用。搜索您的电视型号应该可以帮助您找到此设置的名称。

使用 HDMI-CEC 协议,您可以使用 Raspberry Pi 以各种不同的方式控制电视,例如关闭和打开电视或更改音量。

将 cec-client 安装到 Raspberry Pi

cec-client 是我们将在 Raspberry Pi 上使用的软件包,用于通过 HDMI-CEC 协议控制设备。

软件包安装

1.我们的首要任务是更新 Raspberry Pi 上的软件包列表以及升级当前安装的软件包。

我们可以通过运行以下两个命令来完成此任务。

sudo apt update 
sudo apt -y upgrade 
sudo apt install cec-utils -y

软件的使用方法

  1. 使用 Raspberry Pi 通过 HDMI-CEC 与电视交互

第一个是使用echo和管道(|)。将命令直接传递给cec-client软件,而无需启动它。

此外,我们在软件中同时使用-s和-d选项cec-client。

该-s选项告诉软件我们将发出一个命令。软件将启动,发出命令然后立即退出。

该-d 1选项设置软件的调试级别。通过将其设置为1,cec-client软件将只显示错误。

从 Raspberry Pi 扫描 HDMI-CEC 设备

echo 'scan' | cec-client -s -d 1

2.通过此命令,您应该会看到您的 Raspberry Pi 现在可以访问的设备列表。

您需要识别要与之交互的设备。通常,“ os string:”和“ vendor:”字段将帮助您识别要与之交互的设备。

识别出正确的设备后,记下“ address:”或设备编号。

输出有可能类似下面的内容:

opening a connection to the CEC adapter...
requesting CEC bus information ...
CEC bus information
===================
device #0: TV
address:       0.0.0.0
active source: no
vendor:        Sony
osd string:    TV
CEC version:   1.4
power status:  standby
language:      eng

device #1: Recorder 1
address:       1.0.0.0
active source: no
vendor:        Pulse Eight
osd string:    CECTester
CEC version:   1.4
power status:  on
language:      eng

device #4: Playback 1
address:       3.0.0.0
active source: no
vendor:        Sony
osd string:    PlayStation 4
CEC version:   1.3a
power status:  standby
language:      ???

请注意,device #1: Recorder 1此示例中的“ ”是 Raspberry Pi 自己的 CEC 连接,因此我们可以放心地忽略它。

3.例如,如果我们想控制我们的“索尼电视”,我们可以看到设备号是“ 0”,设备的地址是“ 0.0.0.0”。

获得设备编号或设备地址后,您就可以开始向其发送命令了。

通过 HDMI-CEC发送“ on ”命令

echo 'on <DEVICEADDRESS>' | cec-client -s -d 1

通过 HDMI-CEC发送“待机”命令

echo 'standby <DEVICEADDRESS>' | cec-client -s -d 1

通过 HDMI-CEC关闭设备的示例

使用此命令相对简单。

要将我们的索尼电视置于待机状态,我们需要做的就是发送“ standby”,然后发送我们的设备地址“ 0.0.0.0”。

echo 'standby 0.0.0.0 | cec-client -s -d 1

通过 HDMI-CEC 获取电源状态

echo 'pow <DEVICEADDRESS>' | cec-client -s -d 1

通过 HDMI-CEC 获取电源状态的示例

echo 'pow 0.0.0.0' | cec-client -s -d 1

如果您的设备处于待机状态,您将在终端中看到类似于我们下面的内容。
opening a connection to the CEC adapter...
power status: standby

检索其他 CEC 客户端命令

echo 'h' | cec-client -s -d 1

该命令的作用是检索cec-client软件知道如何处理的可用命令。

从这个命令中,你应该得到一个命令列表,如下所示。

================================================================================
Available commands:

[tx] {bytes}              transfer bytes over the CEC line.
[txn] {bytes}             transfer bytes but don't wait for transmission ACK.
[on] {address}            power on the device with the given logical address.
[standby] {address}       put the device with the given address in standby mode.
[la] {logical address}    change the logical address of the CEC adapter.
[p] {device} {port}       change the HDMI port number of the CEC adapter.
[pa] {physical address}   change the physical address of the CEC adapter.
[as]                      make the CEC adapter the active source.
[is]                      mark the CEC adapter as inactive source.
[osd] {addr} {string}     set OSD message on the specified device.
[ver] {addr}              get the CEC version of the specified device.
[ven] {addr}              get the vendor ID of the specified device.
[lang] {addr}             get the menu language of the specified device.
[pow] {addr}              get the power status of the specified device.
[name] {addr}             get the OSD name of the specified device.
[poll] {addr}             poll the specified device.
[lad]                     lists active devices on the bus
[ad] {addr}               checks whether the specified device is active.
[at] {type}               checks whether the specified device type is active.
[sp] {addr}               makes the specified physical address active.
[spl] {addr}              makes the specified logical address active.
[volup]                   send a volume up command to the amp if present
[voldown]                 send a volume down command to the amp if present
[mute]                    send a mute/unmute command to the amp if present
[self]                    show the list of addresses controlled by libCEC
[scan]                    scan the CEC bus and display device info
[mon] {1|0}               enable or disable CEC bus monitoring.
[log] {1 - 31}            change the log level. see cectypes.h for values.
[ping]                    send a ping command to the CEC adapter.
[bl]                      to let the adapter enter the bootloader, to upgrade
                          the flash rom.
[r]                       reconnect to the CEC adapter.
[h] or [help]             show this help.
[q] or [quit]             to quit the CEC test client and switch off all
                          connected CEC devices.
=======================================================================

至此,您现在应该已经学会了如何cec-client在您的 Raspberry Pi 上使用来控制支持该HDMI-CEC协议的设备。