Skip to content

Instantly share code, notes, and snippets.

@lukatavcer
Last active October 26, 2022 12:15
Show Gist options
  • Save lukatavcer/abdb6ba4dcc84917e15f666f3dcba73e to your computer and use it in GitHub Desktop.
Save lukatavcer/abdb6ba4dcc84917e15f666f3dcba73e to your computer and use it in GitHub Desktop.
Sending/receiving HDMI CEC signals and sending IR signals (Flutter/dart) using Flirc (USB IR transciever)

Understanding HDMI CEC commands

We want to send the following HDMI CEC commands from the Android Box to the TV:

  • get TV status (is it turned ON or OFF?)
  • get TV's current active source (HDMI1/HDMI2/...)
  • turn ON TV
  • turn off TV (standby)
  • switch HDMI source from HDMI1 to HDMI2

You can read more about CEC frames here: Understanding HDMI CEC frame

Sending CEC commands from RaspberryPi (RPI)

You can send HDMI CEC commands directly from the RPI with cec-utils.

CEC commands

Get TV status

You can get TV status with RPI and libcec with:

$ echo 'scan' | cec-client -s -d 1

# Result
CEC bus information
===================
device #0: TV
address:       0.0.0.0
active source: no
vendor:        Sony
osd string:    TV
CEC version:   1.4
power status:  standby
language:      eng


device #1: Recorder 1
address:       1.0.0.0
active source: no
vendor:        Pulse Eight
osd string:    CECTester
CEC version:   1.4
power status:  on
language:      eng


device #4: Playback 1
address:       3.0.0.0
active source: no
vendor:        Sony
osd string:    PlayStation 4
CEC version:   1.3a
power status:  standby
language:      ???

Sending IR Signals

Flirc is an USB IR transciever (transmitter & receiver) Flirc CLI

It has an library written in C language, maybe we can use it in our Flutter code using dart:ffi:

https://docs.flutter.dev/development/platform-integration/android/c-interop https://stackoverflow.com/questions/67793076/how-to-call-a-native-c-function-from-flutter

To use C code in your Flutter program, use the dart:ffi library (currently in beta).

Step 1: Use 3rd party library Flirc code
Step 2: Add C/C++ sources
Step 3: Load the code using the FFI library


Sources

Current progress

Working

  • Tv on/off is working everytime on most TVs.
    • It fails to work for turning TV ON after some runs on LG TV

Not working

  • Switching HDMI sources works the first time to switch to HDMI1 or HDMI2
    • it stops working after the first time and it randomly starts working after couple of minutes (5 - 10min) when testing on LG TV
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment