Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Event-Driven Programming (GPIO)

Event-Driven Programming (GPIO)

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
gpioevent-driven
4 Posts 2 Posters 2.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kafanti
    wrote on 7 Dec 2015, 13:48 last edited by kafanti 12 Jul 2015, 13:51
    #1

    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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 7 Dec 2015, 22:38 last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      K 1 Reply Last reply 8 Dec 2015, 13:53
      0
      • S SGaist
        7 Dec 2015, 22:38

        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.

        K Offline
        K Offline
        kafanti
        wrote on 8 Dec 2015, 13:53 last edited by
        #3

        @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

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 8 Dec 2015, 21:02 last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          3/4

          8 Dec 2015, 13:53

          • Login

          • Login or register to search.
          3 out of 4
          • First post
            3/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved