简体   繁体   中英

C++ callback function in Qt5 with pigpio library

I'm having some trouble bringing together a GPIO library with Qt5 using C++11.

I would like to use this library: http://abyz.me.uk/rpi/pigpio/ with Qt to poll for a button press.

So far I have managed to set pins high or low, but now I would like to use the library to poll for a change in the GPIO state. My understanding is limited but I think the library uses a callback function. The error I am getting is

error: no matching function for call to 'gpioSetAlertFunc'
    Note: candidate function not viable: no known conversion fron 'void 
(MainWindow::*)()' to 'gpioAlertFunc_t' (aka 'void(*)(int, int, unsigned 
int)') for 2nd argument

Am I missing something obvious? Is there something particular I need to read up on? My code is attached below

Many thanks

piGPIO.h

typedef void (*gpioAlertFunc_t)    (int      gpio,
                                    int      level,
                                    uint32_t tick);

int gpioSetAlertFunc(unsigned user_gpio, gpioAlertFunc_t f);

Main.cpp

#include "mainwindow.h"

#include <pigpio.h>

int main(int argc, char *argv[])
{

    gpioInitialise();
    gpioSetAlertFunc(17, &MainWindow::gpioTest);

    QApplication a(argc, argv);
    MainWindow w;
    w.setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
    w.show();

    return a.exec();
}

MainWindow.h

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    Ui::MainWindow *ui;

public slots:

    void gpioTest(int gpio, int level, uint32_t tick);

signals:
    .
    .
    .

private slots:
    .
    .
    .

private:
    .
    .
    .    

};

MainWindow.cpp

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    .
    .
    .
}

void MainWindow::gpioTest(int gpio, int level, uint32_t tick){
    //gpioWrite() tested successfully 
    gpioWrite(22,1);
    return;
}

The problem is you are trying to bind to a non-static member function, but gpioTest needs to be called on a MainWindow object, hence the type mismatch. See https://isocpp.org/wiki/faq/pointers-to-members#memfnptr-vs-fnptr for details. That page will give you some ideas, but the exact solution (eg static function vs wrapper function vs std::invoke etc.) will be specific to your application's needs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM