#!/bin/bash # CEC Control Script for Raspberry Pi 4 # Requires: libcec (cec-client) # Install with: sudo apt-get install libcec-rpi CEC_CLIENT="cec-client" # Check if cec-client is installed if ! command -v $CEC_CLIENT &> /dev/null; then echo "Error: cec-client not found" echo "Install with: sudo apt-get install libcec-rpi" exit 1 fi # Send a CEC command send_command() { echo "$1" | $CEC_CLIENT -s -d 1 } # Show available commands show_help() { cat << EOF CEC Control Script for Raspberry Pi 4 Usage: $0 [options] Commands: tv-on Turn on the TV tv-off Turn off the TV standby Put TV in standby mode wake Wake TV from standby vol-up Increase volume vol-down Decrease volume mute Mute audio input Switch to HDMI input port (0-15) source Switch to source device (0-15) scan Scan for CEC devices status Show CEC adapter status info Show TV information raw Send raw CEC command (hex) help Show this help message Examples: $0 tv-on $0 vol-up $0 input 1 $0 raw 4F:82:10:00 EOF } # Command functions tv_on() { echo "Turning TV on..." send_command "on 0" } tv_off() { echo "Turning TV off..." send_command "standby 0" } standby() { echo "Putting TV in standby..." send_command "standby 0" } wake() { echo "Waking TV..." send_command "on 0" } vol_up() { echo "Volume up..." send_command "volup" } vol_down() { echo "Volume down..." send_command "voldown" } mute() { echo "Muting..." send_command "mute" } input_switch() { local port=$1 if [ -z "$port" ]; then echo "Error: Please specify input port (0-15)" exit 1 fi echo "Switching to HDMI input $port..." send_command "tx 1f:82:$(printf '%x' $port):00" } source_switch() { local device=$1 if [ -z "$device" ]; then echo "Error: Please specify source device (0-15)" exit 1 fi echo "Switching to source device $device..." send_command "tx 1f:82:$(printf '%x' $device):00" } scan_devices() { echo "Scanning for CEC devices..." $CEC_CLIENT -s -d 1 < /dev/null } show_status() { echo "CEC Adapter Status:" $CEC_CLIENT -s -d 1 <<<'q' } show_info() { echo "TV Information:" send_command "poll 0" } raw_command() { local cmd=$1 if [ -z "$cmd" ]; then echo "Error: Please specify raw command" exit 1 fi echo "Sending raw command: $cmd" send_command "tx $cmd" } # Main script logic case "$1" in tv-on) tv_on ;; tv-off) tv_off ;; standby) standby ;; wake) wake ;; vol-up) vol_up ;; vol-down) vol_down ;; mute) mute ;; input) input_switch "$2" ;; source) source_switch "$2" ;; scan) scan_devices ;; status) show_status ;; info) show_info ;; raw) raw_command "$2" ;; help|--help|-h|"") show_help ;; *) echo "Unknown command: $1" echo "Run '$0 help' for usage information" exit 1 ;; esac