Qt World Summit: Submit your Presentation

QFileSystemWatcher + /sys/class/gpio

  • Hello.
    I run my Qt (4.8.4) application on Linux 3.2.18. Platform is AT91SAM9G45.
    I want to watch for changes in the file /sys/class/gpio/gpioXXX/value, which displays a current state of signal on one of pin board.
    This file may contain two values: "0" or "1", if i manual change pin value i can see this change with 'cat /sys/class/gpio/gpioXXX/value', but QFileSystemWatcher does not catch these changes. However, the files are changed manually or by other programs respond correctly.
    I try to use poll:
    No results.
    Any ideas?

  • Moderators

    1. Are you running your program by sudo privileges ?
    2. Have you added full file path(viz. /sys/class/gpio/gpioXXX/value) using addPath ( const QString & path ) ?

  • [quote author="p3c0" date="1379068293"]1. Are you running your program by sudo privileges ?
    2. Have you added full file path(viz. /sys/class/gpio/gpioXXX/value) using addPath ( const QString & path ) ?
    i have only one user in this system - root
    file path added. on other files function work properly.

  • I'm also interested in this, using Qt5 on Raspberry Pi

    My tests show that even running my demo app as root, I only get events when I do any of the following, using the CLI:

    • touch the value file for the gpio pin
    • echo some string into it - it fails to write but my running demo app picks up the event
      If I attempt to trigger the change in the value using the sensor on the GPIO port, CLI shows that the GPIO pin value changes, but my demo app won't pick it up...

  • After some experiments i decided to use QSocketNotifier, but and here i failed - this component based on select() function and generate signal on EOF, i.e. always.
    Now i wrote my one simple class based on poll() and QTimer - work fine.

  • I ended up using a similar approach - QFile and QTimer to poll the /sys/class/gpio/gpioXXX/value every 200msec

    It wasn't the desired soluion, but it doesn't seem to affect Pi's perfomance and the use of QThreads prevents UI thread blocking.

    I did find however - and it did work - how to use GPIO interrupts and WiringPi to achieve the initial objective, for later Qt integration:


  • Moderators

    Is this file "/sys/class/gpio/gpioXXX/value" renamed(to a temporary file for negligible amount of time and then back to original file name) or deleted anytime whenever its value is updated ?

    Because according to the docs,

    bq. Note that QFileSystemWatcher stops monitoring files once they have been renamed or removed from disk, and directories once they have been removed from disk.

  • I'm not sure, but I'd say it's just updated in content, by the system itself. If you attempt to manually edit the file and change the content from 0 to 1 or vice-versa, even as root you might get a warning and the value would remain unchanged - however, in that case, QFileSystemWatcher reacts to "something" and notifies.

    The issue with QFileSystemWatcher is that it never picks up a change in content - in the scenario you describe, it would stand to reason that at least on a first attempt, QFileSystemWatcher would be triggered - and afterwards become silent, either because the value file had been deleted or renamed.

    From what I found online about this so far, I believe the issue lies with the file type we're working with - device files - and QFileSystemWatcher isn't quite the proper tool to work with them.

Log in to reply