Initial commit: Add CEC control script for Raspberry Pi 4
- Add cec-control.sh with power, volume, and input commands - Add README with installation and usage instructions Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
15
.gitignore
vendored
Normal file
15
.gitignore
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
# Backup files
|
||||
*.bak
|
||||
*~
|
||||
|
||||
# Editor files
|
||||
*.swp
|
||||
*.swo
|
||||
.*.swp
|
||||
.*.swo
|
||||
.vscode/
|
||||
.idea/
|
||||
|
||||
# OS files
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
71
README.md
Normal file
71
README.md
Normal file
@@ -0,0 +1,71 @@
|
||||
# CEC Control for Raspberry Pi 4
|
||||
|
||||
Control HDMI-connected devices (TVs, receivers, etc.) from your Raspberry Pi 4 using CEC (Consumer Electronics Control).
|
||||
|
||||
## Installation
|
||||
|
||||
1. Install the required CEC library:
|
||||
```bash
|
||||
sudo apt-get update
|
||||
sudo apt-get install libcec-rpi
|
||||
```
|
||||
|
||||
2. Make the script executable (already done):
|
||||
```bash
|
||||
chmod +x cec-control.sh
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```bash
|
||||
./cec-control.sh <command> [options]
|
||||
```
|
||||
|
||||
## Available Commands
|
||||
|
||||
| Command | Description | Example |
|
||||
|---------|-------------|---------|
|
||||
| `tv-on` | Turn on the TV | `./cec-control.sh tv-on` |
|
||||
| `tv-off` | Turn off the TV | `./cec-control.sh tv-off` |
|
||||
| `standby` | Put TV in standby | `./cec-control.sh standby` |
|
||||
| `wake` | Wake TV from standby | `./cec-control.sh wake` |
|
||||
| `vol-up` | Increase volume | `./cec-control.sh vol-up` |
|
||||
| `vol-down` | Decrease volume | `./cec-control.sh vol-down` |
|
||||
| `mute` | Mute audio | `./cec-control.sh mute` |
|
||||
| `input <port>` | Switch HDMI input (0-15) | `./cec-control.sh input 1` |
|
||||
| `source <device>` | Switch source device (0-15) | `./cec-control.sh source 2` |
|
||||
| `scan` | Scan for CEC devices | `./cec-control.sh scan` |
|
||||
| `status` | Show CEC adapter status | `./cec-control.sh status` |
|
||||
| `info` | Show TV information | `./cec-control.sh info` |
|
||||
| `raw <command>` | Send raw CEC command | `./cec-control.sh raw 4f:82:10:00` |
|
||||
| `help` | Show help message | `./cec-control.sh help` |
|
||||
|
||||
## Quick Start
|
||||
|
||||
1. Connect your Raspberry Pi to your TV via HDMI
|
||||
2. Enable CEC on your TV (usually in settings)
|
||||
3. Test the connection:
|
||||
```bash
|
||||
./cec-control.sh scan
|
||||
```
|
||||
4. Turn on your TV:
|
||||
```bash
|
||||
./cec-control.sh tv-on
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
- **CEC not working**: Make sure CEC is enabled in your TV settings
|
||||
- **Permission denied**: Run with `sudo` if needed
|
||||
- **Device not found**: Check HDMI connection and try rescanning with `scan` command
|
||||
|
||||
## Advanced Usage
|
||||
|
||||
You can integrate this script into your own automation:
|
||||
```bash
|
||||
# Turn on TV and switch to input 1
|
||||
./cec-control.sh tv-on && sleep 2 && ./cec-control.sh input 1
|
||||
|
||||
# Use in cron jobs or other scripts
|
||||
@reboot /path/to/cec-control.sh tv-on
|
||||
```
|
||||
188
cec-control.sh
Executable file
188
cec-control.sh
Executable file
@@ -0,0 +1,188 @@
|
||||
#!/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 <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
|
||||
|
||||
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"
|
||||
}
|
||||
|
||||
# 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
|
||||
Reference in New Issue
Block a user