Qt and wiringPiISR

Alles über qtforum.de
Antworten
Fuxi
Beiträge: 25
Registriert: 24. Mai 2011 20:58

Qt and wiringPiISR

Beitrag von Fuxi »

private:
Ui::MainWindow *ui;
static void positiveFlanke(void);
int kwh=0;



MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
wiringPiISR(19,1,&positiveFlanke)
}

void MainWindow::positiveFlanke(void)
{
ui->label->setText(QString::number(kwh)); ERROR invalid use of member 'ui' in static member function
kwh++;
}

Hello, I'd like to monitor the rising edge of my Raspberry Pi using an interrupt.
Each pulse should be incremented and displayed on a label.
The problem is that the values ​​are static and non-static. Is there a way to cast this?
Or is there another solution?
Thanks.
Fuxi
Beiträge: 25
Registriert: 24. Mai 2011 20:58

Re: Qt and wiringPiISR

Beitrag von Fuxi »

I figured it out myself:

// myclass.h
#include <wiringPi.h>
#include <QObject>

class MyClass : public QObject {
Q_OBJECT
public:
MyClass(int pin);
static void isrWrapper(); // Static callback
void handleInterrupt(); // Actual handler

private:
static MyClass *instance; // Singleton pointer
int m_pin;
};

// myclass.cpp
MyClass *MyClass::instance = nullptr;

MyClass::MyClass(int pin) : m_pin(pin) {
instance = this; // Set instance for static method
wiringPiISR(m_pin, INT_EDGE_RISING, &MyClass::isrWrapper);
}

void MyClass::isrWrapper() {
if (instance) instance->handleInterrupt();
}

void MyClass::handleInterrupt() {
// Process interrupt here (e.g., emit a signal)
}
Antworten