Skip to content

A Python module to interface your Raspberry Pi application (connected to a TV) with the TV remote via HDMI-CEC.

Notifications You must be signed in to change notification settings

tjs-w/pi-tv-remote

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

10 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Raspberry Pi Logo

Pi TV Remote

Raspberry Pi Compatible HDMI-CEC Enabled License: MIT Python 3.7+

Control your TV from your Raspberry Pi application using the TV's own remote via HDMI-CEC.


๐Ÿ” Overview

Pi TV Remote is a Python module that enables your Raspberry Pi applications to interface with TV remote controls via HDMI-CEC. This library makes it easy to:

  • Receive button presses from the TV remote in your Python app
  • Send standard TV remote commands to control the TV
  • Create seamless integrations between your Pi app and the TV

Perfect for media centers, smart home displays, information kiosks, or any project where you want to control your application with the TV's existing remote.

โœจ Features

  • ๐Ÿ“ก Bi-directional Communication: Receive and send TV remote commands
  • ๐Ÿงฉ Simple Python API: Easy integration into existing Python projects
  • ๐Ÿ–ฅ๏ธ Command-line Interface: Test and debug CEC connections quickly
  • ๐Ÿ› ๏ธ Extensible Design: Build on top of the core functionality
  • ๐Ÿ“บ TV Control: Power on/off, volume control, and navigation
  • ๐Ÿ”Œ Multiple TV Support: Compatible with various TV manufacturers including LG, Samsung, and Sony

๐Ÿ“‹ Requirements

  • Raspberry Pi (any model with HDMI port)
  • TV with HDMI-CEC support
  • Python 3.7+
  • libCEC libraries

๐Ÿ“ฆ Installation

On Raspberry Pi

# Install libCEC development libraries
sudo apt-get update
sudo apt-get install libcec-dev python3-dev build-essential python3-venv

# Clone the repository
git clone https://github.com/yourusername/pi-tv-remote.git
cd pi-tv-remote

# Create and activate a virtual environment
python3 -m venv .venv
source .venv/bin/activate

# Install the package (will automatically handle dependencies)
pip install -e .

For Development (macOS/Linux)

# Clone the repository
git clone https://github.com/yourusername/pi-tv-remote.git
cd pi-tv-remote

# Create and activate a virtual environment
python3 -m venv .venv
source .venv/bin/activate

# Install development dependencies
pip install -e ".[dev]"

๐Ÿš€ Usage

Command-Line Interface

Test your CEC connection and observe remote control events directly from the command line:

python -m pi_tv_remote.cli --name "MyPi" --duration 60

Options:

  • --name NAME - Set the OSD name of the device (default: RaspberryPi)
  • --duration SECONDS - Run for a specified number of seconds (default: run indefinitely)
  • --help - Show help message and exit

Python API

Integrate the CEC adapter into your own Python applications:

from pi_tv_remote.cec_adapter import CECAdapter, CECConfig, RemoteButton

# Create configuration
config = CECConfig(device_name="MyPi")

# Initialize adapter
adapter = CECAdapter(config)
adapter.init()

# Listen for remote button presses
def on_button_press(key_code, duration):
    print(f"Button {key_code} pressed for {duration}ms")
    
    # Example: Map specific buttons to actions
    if key_code == RemoteButton.UP:
        print("Up navigation")
    elif key_code == RemoteButton.SELECT:
        print("Selected item")

adapter.add_keypress_callback(on_button_press)

# Send commands to the TV
adapter.power_on_tv()
adapter.send_remote_button(RemoteButton.VOLUME_UP)
adapter.standby_tv()

Advanced Usage: Command Callbacks and TV Control

from pi_tv_remote.cec_adapter import CECAdapter, CECConfig, RemoteButton, CECCommand

# Create and initialize the adapter
adapter = CECAdapter(CECConfig(device_name="MyDevice"))
adapter.init()

# Register for TVโ†’RPi commands
def handle_power_status_request(opcode, from_addr, to_addr, parameters):
    print("TV is checking if we're on!")
    # Custom logic before default response

# Add a callback for when TV requests power status
adapter.add_command_callback(CECCommand.GIVE_DEVICE_POWER_STATUS, handle_power_status_request)

# Send RPiโ†’TV commands
# Power on the TV
adapter.power_on_tv()

# Put TV in standby
adapter.standby_tv()

# Set this device as active source
adapter.set_active_source()

# Send remote button presses
adapter.send_remote_button(RemoteButton.UP)
adapter.send_remote_button(RemoteButton.SELECT)

# Request information from TV
adapter.request_power_status()
adapter.request_vendor_id()

# Send any arbitrary CEC command
adapter.send_command(CECCommand.VENDOR_COMMAND, 
                    destination=0, # TV address
                    parameters=b'\x01\x02\x03')

adapter.run()

๐Ÿ“ Project Structure

pi_tv_remote/         # Main package directory
โ”œโ”€โ”€ __init__.py       # Package initialization
โ”œโ”€โ”€ cec_adapter.py    # Core CEC adapter implementation
โ”œโ”€โ”€ cec_utils.py      # Utility functions for CEC
โ””โ”€โ”€ cli.py            # Command-line interface

๐Ÿ› ๏ธ Development

# Format code
black pi_tv_remote
isort pi_tv_remote

# Run static type checking
mypy pi_tv_remote

# Run tests
./run_tests

๐Ÿ“š CEC Button Reference

The following buttons are supported by default:

Button Code Symbol Description
UP 0x01 โฌ†๏ธ Navigate up
DOWN 0x02 โฌ‡๏ธ Navigate down
LEFT 0x03 โฌ…๏ธ Navigate left
RIGHT 0x04 โžก๏ธ Navigate right
SELECT 0x00 โบ๏ธ Confirm selection
BACK 0x0D ๐Ÿ”™ Return/exit
PLAY 0x44 โ–ถ๏ธ Start playback
STOP 0x45 โน๏ธ Stop playback
PAUSE 0x46 โธ๏ธ Pause playback
REWIND 0x48 โช Rewind content
FAST_FORWARD 0x49 โฉ Fast forward
BLUE 0x71 ๐Ÿ”ต Blue function button
RED 0x72 ๐Ÿ”ด Red function button
GREEN 0x73 ๐ŸŸข Green function button
YELLOW 0x74 ๐ŸŸก Yellow function button
NUMBER_0 to NUMBER_9 0x20-0x29 0๏ธโƒฃ-9๏ธโƒฃ Number keys
VOLUME_UP 0x41 ๐Ÿ”Š Increase volume
VOLUME_DOWN 0x42 ๐Ÿ”‰ Decrease volume
MUTE 0x43 ๐Ÿ”‡ Mute audio

You can define custom callbacks for these or add support for additional buttons as needed.

๐Ÿ“Š CEC Command Direction Reference

This table shows HDMI-CEC commands according to the official HDMI specification and their typical implementations:

Command Icon TVโ†’RPi RPiโ†’TV Description
Navigation Controls
UP (0x01) โฌ†๏ธ โœ“ โœ“ Navigate up - part of User Control Pressed (0x44)
DOWN (0x02) โฌ‡๏ธ โœ“ โœ“ Navigate down - part of User Control Pressed (0x44)
LEFT (0x03) โฌ…๏ธ โœ“ โœ“ Navigate left - part of User Control Pressed (0x44)
RIGHT (0x04) โžก๏ธ โœ“ โœ“ Navigate right - part of User Control Pressed (0x44)
SELECT (0x00) โบ๏ธ โœ“ โœ“ Confirm selection - part of User Control Pressed (0x44)
BACK/EXIT (0x0D) ๐Ÿ”™ โœ“ โœ“ Return/exit - part of User Control Pressed (0x44)
Color Buttons
RED (0x72) ๐Ÿ”ด โœ“ โœ“ Red function button - part of User Control Pressed (0x44)
GREEN (0x73) ๐ŸŸข โœ“ โœ“ Green function button - part of User Control Pressed (0x44)
YELLOW (0x74) ๐ŸŸก โœ“ โœ“ Yellow function button - part of User Control Pressed (0x44)
BLUE (0x71) ๐Ÿ”ต โœ“ โœ“ Blue function button - part of User Control Pressed (0x44)
Media Controls
PLAY (0x44) โ–ถ๏ธ โœ“ โœ“ Start playback - part of User Control Pressed (0x44)
STOP (0x45) โน๏ธ โœ“ โœ“ Stop playback - part of User Control Pressed (0x44)
PAUSE (0x46) โธ๏ธ โœ“ โœ“ Pause playback - part of User Control Pressed (0x44)
REWIND (0x48) โช โœ“ โœ“ Rewind content - part of User Control Pressed (0x44)
FAST_FORWARD (0x49) โฉ โœ“ โœ“ Fast forward - part of User Control Pressed (0x44)
Number Keys
NUMBER_0 (0x20) 0๏ธโƒฃ โ–ณ โœ“ Number key 0 - part of User Control Pressed (0x44)
NUMBER_1 (0x21) 1๏ธโƒฃ โ–ณ โœ“ Number key 1 - part of User Control Pressed (0x44)
NUMBER_2 (0x22) 2๏ธโƒฃ โ–ณ โœ“ Number key 2 - part of User Control Pressed (0x44)
NUMBER_3 (0x23) 3๏ธโƒฃ โ–ณ โœ“ Number key 3 - part of User Control Pressed (0x44)
NUMBER_4 (0x24) 4๏ธโƒฃ โ–ณ โœ“ Number key 4 - part of User Control Pressed (0x44)
NUMBER_5 (0x25) 5๏ธโƒฃ โ–ณ โœ“ Number key 5 - part of User Control Pressed (0x44)
NUMBER_6 (0x26) 6๏ธโƒฃ โ–ณ โœ“ Number key 6 - part of User Control Pressed (0x44)
NUMBER_7 (0x27) 7๏ธโƒฃ โ–ณ โœ“ Number key 7 - part of User Control Pressed (0x44)
NUMBER_8 (0x28) 8๏ธโƒฃ โ–ณ โœ“ Number key 8 - part of User Control Pressed (0x44)
NUMBER_9 (0x29) 9๏ธโƒฃ โ–ณ โœ“ Number key 9 - part of User Control Pressed (0x44)
Volume Controls
VOLUME_UP (0x41) ๐Ÿ”Š โ–ณ โœ“ Increase volume - typically handled by TV/audio system
VOLUME_DOWN (0x42) ๐Ÿ”‰ โ–ณ โœ“ Decrease volume - typically handled by TV/audio system
MUTE (0x43) ๐Ÿ”‡ โ–ณ โœ“ Mute audio - typically handled by TV/audio system
Power Controls
STANDBY (0x36) โป โœ“ Turn off TV - RPi can send to put TV into standby
IMAGE_VIEW_ON (0x04) ๐Ÿ“บ โœ“ Turn on TV - RPi can power on TV
Device Status
GIVE_POWER_STATUS (0x8F) ๐Ÿ”Œ โœ“ Query TV power status - Device Power Status feature
REPORT_POWER_STATUS (0x90) ๐Ÿ”‹ โœ“ โœ“ Report power status - Device Power Status feature
Input Controls
ACTIVE_SOURCE (0x82) ๐Ÿ“ฑ โœ“ โœ“ Set device as active - One Touch Play & Routing Control
SET_STREAM_PATH (0x86) ๐Ÿ“บ โœ“ Request device to become active - Routing Control
ROUTING_CHANGE (0x80) ๐Ÿ”€ โœ“ โœ“ Change input routing - Routing Control feature
Device Information
SET_OSD_NAME (0x47) ๐Ÿ“ โœ“ Set device name - Device OSD Name Transfer feature
GIVE_OSD_NAME (0x46) ๐Ÿ“‹ โœ“ โœ“ Request device name - Device OSD Name Transfer feature
REPORT_PHYSICAL_ADDR (0x84) ๐Ÿ”ข โœ“ โœ“ Report physical address - System Information feature
DEVICE_VENDOR_ID (0x87) ๐Ÿข โœ“ โœ“ Report vendor ID - Vendor Specific Command feature
Remote Control
USER_CONTROL_PRESSED (0x44) ๐ŸŽฎ โœ“ โœ“ Key press - Remote Control Pass Through feature
USER_CONTROL_RELEASED (0x45) ๐ŸŽฎ โœ“ โœ“ Key release - Remote Control Pass Through feature

Legend:
โœ“ - Defined in the standard and typically implemented
โ–ณ - Defined in the standard but inconsistently implemented by manufacturers

โš ๏ธ TV Manufacturer Compatibility Notes

While the HDMI-CEC specification defines standard commands, actual implementation varies by manufacturer:

  • LG (SimpLink): Reliably passes navigation and media controls. Typically intercepts volume and number keys.
  • Samsung (Anynet+): Generally good support for standard CEC, more likely to pass volume controls.
  • Sony (BRAVIA Sync): Good compliance with the spec, but may have proprietary extensions.

๐Ÿ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿค Contributing

Contributions are welcome! Feel free to open issues or submit pull requests.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Built with โค๏ธ for Raspberry Pi enthusiasts

About

A Python module to interface your Raspberry Pi application (connected to a TV) with the TV remote via HDMI-CEC.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published