Using GPIO on Pi
-
Hi All,
I'm a newbie as far as Qt is concerned. Please take pity!
I designed an ADC board for the Raspberry Pi and wrote a C++ program to take readings. I had two push button switches and three led's connected to the GPIO's too and the switches controlled when to start and stop the readings and the led's indicated power on, program running and sampling.
I am now playing round with Qt and QCustomPlot to display the readings. I have simply used one of the QCustomPlot's example codes for testing and have replaced one of the routines called when a mouse button is pressed with my own code to take a reading and display it and it is working.
The next bit is where I am stuck. I want to detect one of the buttons being pressed and then call the routine to take the readings etc as a result.
So, in my original C++ code, the following lines were in main()
mmapGpio rpiGpio; // instantiate the mmapGpio class
rpiGpio.setPinDir(17,mmapGpio::INPUT); // set GPIO17 to inputand
if(rpiGpio.readPin(17) == mmapGpio::LOW) // read pin state
{go get the readings
}
I'm wondering how I can make 'if(rpiGpio.readPin(17) == mmapGpio::LOW)' a signal to call the getreadings() slot
any suggestions? I've tried to google it but all the examples I can find are for pushing widget buttons, clicking mouse etc...
Thanks, Steve.
-
Hi and welcome to devnet,
You are checking this at a regular interval right ?
Then you could create a QObject where you reimplement timerEvent, do the check there and emit the signal
Hope it helps
-
Hi, thanks for the reply.
Yes, the original code loops round and does the check to see if pin 17 is high or low on each loop.
Now, as I said, I am a newbie with Qt. If I was using VB on Windows, it'd be a doddle and what you are suggesting sounds roughly what I would do with a timer in VB so if you could elaborate a little on just how I would create a QObject and associate a timerEvent with it to emit the signal I'd be grateful.
I did look on QtCreator to see if there was any timers etc I could use but couldn't find anything.
Regards, Steve
-
Hi,
Maybe you can also multitasking using a QThread and check there all the interrupts, if something happen, you can emit a signal
@
class Interrupt: public QThread{
Q_OBJECT
public:
void setNextEvent(bool arg);
bool getNextEvent();
protected:
void run();
private:
QMutex mMutex;
bool state;
signals:
void mySignal();
}
@@void Interrupt::run(){
while(true){
if(rpiGpio.readPin(17) == mmapGpio::LOW && getNextEvent()){
setNextEvent(false);
emit mySignal();
}
else if(rpiGpio.readPin(17) == mmapGpio::HIGH && !getNextEvent() ){
setNextEvent(true);
}
QThread::msleep(100);
}
}void Interrupt::setNextEvent(bool arg){
mMutex.lock();
state = arg;
mMutex.unlock();
}bool Interrupt::getNextEvent(){
bool ret;mMutex.lock(); ret = state; mMutex.unlock(); return state;
}
@