linuxqtlinux-device-driverembedded-linux

How do Linux drivers communicate with my Qt app


I'm creating an embedded Linux app with Qt Quick. Basically, I have some sensors that I need to output in the UI. I'm trying to wrap my head around all this process, but I still have some gray areas.

As far as I understand, a temperature sensor will be controlled by the I2C driver in my embedded Linux app. This driver is something that I need to add in my layers when using Yocto. So far I feel I understand this part.

On the other side, I've a UI created in QT which has a main.cpp file. If I understand correctly, this is my main entry point to my entire application? From this main.cpp I can create a simple call to the I2C driver and read the sensor data?

I can't even write the correct question because I can't wrap my head around all these binaries and the code flow.

I already have a Qt quick app working and a placeholder code to simulate the sensor values and update the UI.


Solution

  • The user interface to Linux drivers can be found under /dev in the file system. Look for devices like i2c-0, i2c-1, ... These devices need to be opened by the application, for example like this:

    int file = open("/dev/i2c-0", O_RDWR); // Name your correct device!
    

    If this works, you can try to communicate with your device:

    int addr = 0x40; /* Insert correct I2C address */
    
    if (ioctl(file, I2C_SLAVE, addr) < 0) {
      /* Check errno for errors */
      exit(1);
    }
    

    Now you can read() or write() to your I2C device:

    char buf[3] = { /* Whatever */ };
    
    if (write(file, buf, 3) != 3) {
      /* ERROR HANDLING: I2C transaction failed */
    }
    

    Note: there is no difference to calling a UART device or any other Linux driver, so you can take any code you find using Linux drivers.

    Now wrap all this up into a module and hook it up to the Qt Quick C++ API.