Event-Driven Programming (GPIO)
-
Hello,
I am trying to do a simple pushbutton(physical)-LED circuit. As always, when the pushbutton pushed, LED will on and when it released, LED will off.
Here is my main loop looks like;
mmap_gpio gpio; gpio.setMode(4, mmap_gpio::output); gpio.setMode(17, mmap_gpio::input); while (1) { if (gpio.read(17) == mmap_gpio::low) { usleep(20000); if (gpio.read(17) == mmap_gpio::low) { gpio.write(4, mmap_gpio::high); while (gpio.read(17) == mmap_gpio::low) ; } } gpio.write(4, mmap_gpio::low); }
But when I loop like that, the program just consumes whole CPU cycles.. When I monitor with htop, one of the processor is always working %100. I would like to make it by native way, so I can understand how Qt works.
I use Raspberry Pi 2 Model B.
Thank you,
Sina -
Hi,
If you want to understand how Qt works you should rather read its sources. The event dispatcher will be one of the part you're interested in.
-
@SGaist Thank you SGaist, I will definetly check this. I found a way but not sure if it is a native one.. So here it is how this way is;
QCoreApplication-derived mmapApplication, on the constructor, QBasicTimer is allocated on heap and interval is setted to 50ms,
mmapApplication::mmapApplication(int &argc, char && argc) : QCoreApplication(argc, argv) { QBasicTimer pTimer = new QBasicTimer; pTimer->start(50, this); ... }
and on the mmapApplication::timerEvent(QTimerEvent *event) override, can call a GPIO function to check if any of the pins state has changed or not. If changed, it can post a custom event which has the information about the state of the pins.
Sina
-
What is your definition of native ?
A while loop is the same thing on Linux, OS X or Windows.
From a pure design point of view, I'd rather create a dedicated QObject derived class rather than subclass QCoreApplication.