From 7395b7c94f62bcf64a7ffc819619eb5f440e3d1c Mon Sep 17 00:00:00 2001 From: phil Date: Fri, 6 Feb 2026 17:44:18 +0100 Subject: [PATCH] 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 --- .gitignore | 15 ++++ README.md | 71 +++++++++++++++++++ cec-control.sh | 188 +++++++++++++++++++++++++++++++++++++++++++++++++ claude.md | 1 + 4 files changed, 275 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100755 cec-control.sh create mode 100644 claude.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..01d72d5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +# Backup files +*.bak +*~ + +# Editor files +*.swp +*.swo +.*.swp +.*.swo +.vscode/ +.idea/ + +# OS files +.DS_Store +Thumbs.db diff --git a/README.md b/README.md new file mode 100644 index 0000000..9fce468 --- /dev/null +++ b/README.md @@ -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 [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 ` | Switch HDMI input (0-15) | `./cec-control.sh input 1` | +| `source ` | 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 ` | 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 +``` diff --git a/cec-control.sh b/cec-control.sh new file mode 100755 index 0000000..c6a12eb --- /dev/null +++ b/cec-control.sh @@ -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 [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 diff --git a/claude.md b/claude.md new file mode 100644 index 0000000..90aaf79 --- /dev/null +++ b/claude.md @@ -0,0 +1 @@ +Create a script to send cec commands over the hdmi port of a raspberry pi 4