A simple libCEC4 example for the Raspberry Pi

Many moons ago I wrote A simple libCEC example for the Raspberry Pi which worked fine until libcec4 came out in late October 2016. Yes, this post is long overdue.

As before, I managed to achieve great code reduction by tossing away almost all of cec-client’s features. I have managed to create a short (75 source lines of code count) example which is much easier to understand. The source code for this really short example can be found on github .

The basic idea of the program is

  1. Initialise libcec4
  2. Give libcec4 a callback that will handle the CEC keypresses
  3. Go to sleep until ctrl-c finishes the program

Any keys pressed on your remote control then wake up the callback function which I creatively called on_keypress.

The changes to the code from the prior version of libcec are fairly simple. First up, the on_keypress function now is passed a pointer to a cec_keypress message rather than a by-value copy of a message. The next change is that the CEC framework has changed the name of  the callback function from CBCecKeyPress to the simple keyPress.  In a similar vein, the FindAdapters function is now DetectAdapters.  And finally, the DetectAdapters function fills in an array of cec_adapter_descriptor rather than cec_adapter, which has the flow on effect to the Open call taking a strComName rather than simply comm.

One final note: this is simple tutorial code and is not production ready C++.  For example, in production code you would use RAII to encapsulate the LibCecInitialise/UnloadLibCec construction/destruction pair of functions.

 

 

 

Posted in Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: