我正在用覆盆子皮1做伸展运动。repo中只有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 handler\n";
        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.so\n";
        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 devices\n";
        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);

当我将CBCecKeyPress重命名为keyPress并将FindAdapters重命名为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

我从keyPress得到了一些关于/usr/include/libcec/cectypes.h的信息:
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一起工作?

最佳答案

我是你要问的代码的作者。我知道你问这个问题已经一年了,但我只是偶然发现了这个stackoverflow问题,因为我自己在寻找解决方案!哈哈。幸好我自己解决了这个问题。
我已经更新了github代码以使其工作https://github.com/DrGeoff/cec_simplest并且我已经写了一篇关于代码https://drgeoffathome.wordpress.com/2018/10/07/a-simple-libcec4-example-for-the-raspberry-pi/的博客文章
简而言之,这些就是我必须做的改变。首先,on_keypress函数现在传递给一个指向cec_keypress消息的指针,而不是消息的按值副本。下一个变化是CEC框架将回调函数的名称从CBCecKeyPress更改为simple keyPress。类似地,FindAdapters函数现在是DetectAdapters(而不是您尝试的PingAdapters)。最后,DetectAdapters函数填充一个cec_adapter_描述符数组,而不是cec_adapter,它对采用strComName而不是简单的comm的开放调用具有流作用。

关于c++ - cec代码不适用于libcec 4,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46493851/

10-10 12:28