CEC代码与LIBCEC 4一起工作

cec code not working with libcec 4

本文关键字:一起 工作 LIBCEC 代码 CEC      更新时间:2023-10-16

我在Raspberry Pi上运行伸展1.只有Libcec4和libcec4-dev。

我从github找到的简单代码基于libcec的旧版本。

// Build command:
// g++-4.8 -std=gnu++0x -fPIC -g -Wall -march=armv6 -mfpu=vfp -mfloat-abi=hard -isystem /opt/vc/include/ -isystem /opt/vc/include/interface/vcos/pthreads/ -isystem /opt/vc/include/interface/vmcs_host/linux/ -I/usr/local/include -L /opt/vc/lib -lcec -lbcm_host -ldl cec-simplest.cpp -o cec-simplest 
//#CXXFLAGS=-I/usr/local/include
//#LINKFLAGS=-lcec -ldl
#include <libcec/cec.h>
// cecloader.h uses std::cout _without_ including iosfwd or iostream
// Furthermore is uses cout and not std::cout
#include <iostream>
using std::cout;
using std::endl;
#include <libcec/cecloader.h>
#include "bcm_host.h"
//#LINKFLAGS=-lbcm_host
#include <algorithm>  // for std::min
// The main loop will just continue until a ctrl-C is received
#include <signal.h>
bool exit_now = false;
void handle_signal(int signal)
{
    exit_now = true;
}

//CEC::CBCecKeyPressType 
int on_keypress(void* not_used, const CEC::cec_keypress msg)
{
    std::string key;
    switch( msg.keycode )
    {
        case CEC::CEC_USER_CONTROL_CODE_SELECT: { key = "select"; break; }
        case CEC::CEC_USER_CONTROL_CODE_UP: { key = "up"; break; }
        case CEC::CEC_USER_CONTROL_CODE_DOWN: { key = "down"; break; }
        case CEC::CEC_USER_CONTROL_CODE_LEFT: { key = "left"; break; }
        case CEC::CEC_USER_CONTROL_CODE_RIGHT: { key = "right"; break; }
        default: break;
    };
    std::cout << "on_keypress: " << static_cast<int>(msg.keycode) << " " << key << std::endl;
    return 0;
}

int main(int argc, char* argv[])
{
    // Install the ctrl-C signal handler
    if( SIG_ERR == signal(SIGINT, handle_signal) )
    {
        std::cerr << "Failed to install the SIGINT signal handlern";
        return 1;
    }
    // Initialise the graphics pipeline for the raspberry pi. Yes, this is necessary.
    bcm_host_init();
    // Set up the CEC config and specify the keypress callback function
    CEC::ICECCallbacks        cec_callbacks;
    CEC::libcec_configuration cec_config;
    cec_config.Clear();
    cec_callbacks.Clear();
    const std::string devicename("CECExample");
    devicename.copy(cec_config.strDeviceName, std::min(devicename.size(),13u) );
    cec_config.clientVersion       = CEC::LIBCEC_VERSION_CURRENT;
    cec_config.bActivateSource     = 0;
    cec_config.callbacks           = &cec_callbacks;
    cec_config.deviceTypes.Add(CEC::CEC_DEVICE_TYPE_RECORDING_DEVICE);
    cec_callbacks.CBCecKeyPress    = &on_keypress;
    // Get a cec adapter by initialising the cec library
    CEC::ICECAdapter* cec_adapter = LibCecInitialise(&cec_config);
    if( !cec_adapter )
    { 
        std::cerr << "Failed loading libcec.son"; 
        return 1; 
    }
    // Try to automatically determine the CEC devices 
    CEC::cec_adapter devices[10];
    int8_t devices_found = cec_adapter->FindAdapters(devices, 10, NULL);
    if( devices_found <= 0)
    {
        std::cerr << "Could not automatically determine the cec adapter devicesn";
        UnloadLibCec(cec_adapter);
        return 1;
    }
    // Open a connection to the zeroth CEC device
    if( !cec_adapter->Open(devices[0].comm) )
    {        
        std::cerr << "Failed to open the CEC device on port " << devices[0].comm << std::endl;
        UnloadLibCec(cec_adapter);
        return 1;
    }
    // Loop until ctrl-C occurs
    while( !exit_now )
    {
        // nothing to do.  All happens in the CEC callback on another thread
        sleep(1);
    }
    // Close down and cleanup
    cec_adapter->Close();
    UnloadLibCec(cec_adapter);
    return 0;
}

它不会使用libcec4和libcec4-dev进行编译,并抛出这些错误::

cec-simplest.cpp: In function ‘int main(int, char**)’:
cec-simplest.cpp:76:19: error: ‘CEC::ICECCallbacks {aka struct CEC::ICECCallbacks}’ has no member named ‘CBCecKeyPress’; did you mean ‘keyPress’?
     cec_callbacks.CBCecKeyPress    = &on_keypress;
                   ^~~~~~~~~~~~~
cec-simplest.cpp:88:41: error: ‘class CEC::ICECAdapter’ has no member named ‘FindAdapters’; did you mean ‘PingAdapter’?
     int8_t devices_found = cec_adapter->FindAdapters(devices, 10, NULL);

当我将 CBCecKeyPresskeyPressFindAdapters重命名为 PingAdapter时,我得到了这些错误::

cec-simplest.cpp: In function ‘int main(int, char**)’:
cec-simplest.cpp:76:33: error: invalid conversion from ‘int (*)(void*, CEC::cec_keypress)’ to ‘void (*)(void*, const cec_keypress*) {aka void (*)(void*, const CEC::cec_keypress*)}’ [-fpermissive]
     cec_callbacks.keyPress    = &on_keypress;
                                 ^~~~~~~~~~~~
cec-simplest.cpp:88:70: error: no matching function for call to ‘CEC::ICECAdapter::PingAdapter(CEC::cec_adapter [10], int, NULL)’
     int8_t devices_found = cec_adapter->PingAdapter(devices, 10, NULL);
                                                                      ^
In file included from cec-simplest.cpp:5:0:
/usr/include/libcec/cec.h:77:18: note: candidate: virtual bool CEC::ICECAdapter::PingAdapter()
     virtual bool PingAdapter(void) = 0;
                  ^~~~~~~~~~~
/usr/include/libcec/cec.h:77:18: note:   candidate expects 0 arguments, 3 provided

我从/usr/include/libcec/cectypes.h ::

获得了有关keyPress的一些信息
typedef struct cec_keypress
{
  cec_user_control_code keycode;   /**< the keycode */
  unsigned int          duration;  /**< the duration of the keypress */
} cec_keypress;

typedef struct ICECCallbacks
{
void (CEC_CDECL* keyPress)(void* cbparam, const cec_keypress* key);
  /*!
   * @brief Transfer a CEC command from libCEC to the client.
   * @param cbparam             Callback parameter provided when the callbacks were set up
   * @param command             The command to transfer.
   */
  void Clear(void)
  {
    keyPress             = nullptr;

Libcec没有任何文档。

我需要做什么修改才能使其与libcec4一起使用?

我是您要问的代码的作者。我知道自从您问这个问题以来已经过去了一年,但是我只是偶然发现了这个堆叠的问题,因为我自己正在寻找解决方案!哈哈。幸运的是,我自己解决了这个问题。

我已经更新了github代码https://github.com/drgeoff/cec_simplest,并且我已经写了一篇有关代码的博客文章https://drgeoffathome.wordpress.com/2018/10/10/07/a-simple-libcec4-示例 - raspberry-pi/

简而言之,这些是我必须做出的更改。首先,现在传递了 on_keypress 函数,将指针指向 cec_keypress 消息,而不是消息的副价值副本。下一个更改是CEC框架已将回调函数的名称从 cbcecepress 更改为简单。同样, findAdapters 函数现在是 detectAdapters (您尝试过的不是 pingAdapters )。最后, detectAdapters 函数填充 cec_adapter_descriptor 而不是 cec_adapter /em>调用 strcomname 而不是简单的 comm