Files
pi-cec/cec-control.sh
phil d0d0bb35a4 Add fix command and troubleshooting for errno=16 error
- Add 'fix' command to diagnose CEC connection issues
- Add detailed troubleshooting section for common errors
- Fix handles errno=16 (device busy) by checking processes and trying sudo
- Document solutions for CEC permission and connection issues

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-06 17:57:47 +01:00

233 lines
4.6 KiB
Bash
Executable File

#!/bin/bash
# CEC Control Script for Raspberry Pi 4
# Requires: cec-utils
# Install with: sudo apt-get install cec-utils
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 cec-utils"
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 <command> [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 <port> Switch to HDMI input port (0-15)
source <device> Switch to source device (0-15)
scan Scan for CEC devices
status Show CEC adapter status
info Show TV information
fix Troubleshoot connection issues
raw <command> 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"
}
fix_connection() {
echo "Attempting to fix CEC connection issues..."
echo ""
# Check if running as root
if [ "$EUID" -ne 0 ]; then
echo "Checking for existing CEC processes..."
pgrep -a cec-client || echo "No cec-client processes found"
echo ""
echo "Trying with sudo - you may need to enter your password:"
echo ""
sudo "$0" "$@"
exit $?
fi
echo "Running as root - checking CEC device status..."
echo ""
# Check if device exists
if [ -e /dev/cec0 ]; then
echo "CEC device /dev/cec0 exists"
ls -l /dev/cec0
echo ""
# Check what's using the device
echo "Checking what's using the CEC device:"
lsof /dev/cec0 2>/dev/null || echo "Nothing is locking /dev/cec0"
echo ""
else
echo "Warning: /dev/cec0 does not exist"
echo "Available devices:"
ls -la /dev/cec* /dev/tty* 2>/dev/null | grep -E "cec|ttyAMA|ttyS0" || echo "No CEC devices found"
echo ""
fi
echo "Attempting to scan for devices..."
echo ""
$CEC_CLIENT -s -d 1 < /dev/null
}
# 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
;;
fix)
fix_connection
;;
raw)
raw_command "$2"
;;
help|--help|-h|"")
show_help
;;
*)
echo "Unknown command: $1"
echo "Run '$0 help' for usage information"
exit 1
;;
esac