switch_power/
switch_power.rs

1/*!
2 * Set Logic address and switch Devices from and to standby
3 */
4
5use std::{thread::sleep, time::Duration};
6
7use cec_linux::*;
8
9fn main() -> std::io::Result<()> {
10    let cec = CecDevice::open("/dev/cec0")?;
11    let capas = cec.get_capas()?;
12    println!("capas  {:?}", capas);
13
14    // clear existing logical addresses
15    let log = CecLogAddrs::default();
16    cec.set_log(log)?;
17
18    // set new address (PLAYBACK)
19    let log = CecLogAddrs::new(
20        VendorID::NONE,
21        Version::V1_4,
22        "pi4".to_string().try_into().unwrap(),
23        &[CecPrimDevType::PLAYBACK],
24        &[CecLogAddrType::PLAYBACK],
25    );
26    cec.set_log(log)?;
27
28    // ask Audiosystem to turn on (from standby)
29    cec.turn_on(CecLogicalAddress::Playback2, CecLogicalAddress::Audiosystem)?;
30
31    sleep(Duration::from_millis(10000));
32
33    // ask Audiosystem to switch to standby
34    cec.transmit(
35        CecLogicalAddress::Playback2,
36        CecLogicalAddress::Audiosystem,
37        CecOpcode::Standby,
38    )?;
39
40    sleep(Duration::from_millis(10000));
41
42    // ask TV to turn on
43    cec.turn_on(CecLogicalAddress::Playback2, CecLogicalAddress::Tv)?;
44
45    sleep(Duration::from_millis(10000));
46
47    // ask TV to switch to standby
48    cec.transmit(
49        CecLogicalAddress::Playback2,
50        CecLogicalAddress::Tv,
51        CecOpcode::Standby,
52    )?;
53
54    Ok(())
55}