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
- Initialise libcec4
- Give libcec4 a callback that will handle the CEC keypresses
- 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.