Controlling GPIO's with qt



  • Hi everyone,

    I was looking for a way to set values, read values of gpio's. I have read http://kernel.org/doc/Documentation/gpio.txt to have an opinion about gpio's.

    I learned that there are some linux commands to set/read values of gpio's. But is there a way to do that with qt.

    Thanks in advance.



  • [quote author="thehilmisu" date="1327579685"]But is there a way to do that with qt.[/quote]

    No (at least none that I know of), but the Linux API (which can be used without any problems and hesitation it Qt applications) is pretty straight forward and a Qt wrapper should be written in minutes if really neccessary.



  • [quote author="Lukas Geyer" date="1327586201"]
    No (at least none that I know of), but the Linux API (which can be used without any problems and hesitation it Qt applications) is pretty straight forward and a Qt wrapper should be written in minutes if really neccessary.[/quote]

    I am newbie at all of this. Can you be more specific ? or can you provide me some links ?

    Thanks



  • At the moment I have the same problem because I've no idea how I can set or read the gpio's. I know it is a old thread but If you have further information I would be appreciate.



  • Ok I found a solution. In the path /sys/devices/platform/leds-gpio.0 of my embedded linux target device I've edited manually (use of the echo instruction of linux) the values of the gpio.0 for example. To change the states of the gpio's out of the source code you can use the system() function.



  • In your case you have to use the system(linux command) funkction and insert the commands you've found in the http://kernel.org/doc/Documentation/gpio.txt instruction as parameter of the system() funtion. hope that will help you but I think you've found your're own solution in the meantime :)



  • My problem are the same of yours, but, I need to do it async! It's there a way to do that using Qt?!

    To be more specific, I need a function that "listen" or "watch" for changes in gpio and tell me when it occurs!

    Sorry for up an old thread and thanks all! :)



  • if you work on linux you must know that every thing in linux is file.
    you must write the linux device driver and load as module the you can call that as open file and write your command in that file.
    file gpio.h

    #ifndef GPIO_H
    #define GPIO_H

    #include <qstring.h>
    #include <qobject.h>

    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>

    //Header pin number definitions
    #define PIN9 160
    #define PIN10 161
    #define PIN11 162
    #define PIN12 163
    #define PIN13 164
    #define PIN14 165
    #define PIN15 166
    #define PIN16 192
    #define PIN17 193
    #define PIN18 195
    #define PIN19 197
    #define PIN20 198
    #define PIN21 199
    #define PIN22 201
    #define PIN23 202
    #define PIN24 203
    #define PIN25 139
    #define PIN26 140
    #define PIN27 141
    #define PIN28 194
    #define PIN29 142
    #define PIN30 143
    #define PIN31 32
    #define PIN32 33
    #define PIN33 233
    #define PIN34 234

    class GPIO : public QObject
    {
    Q_OBJECT
    public:
    GPIO(int pin); //pin is the pin nuber on con4 of the FriendlyARM board
    ~GPIO();

    enum Direction {In,Out,Err};
    int openPin();
    int closePin();
    int setDirection(Direction direction); //set direction in/out. returns
    

    0 if ok and -1 on error
    int getDirection(); //returns direction
    int setState(bool state);
    void setValue(bool value);
    bool getState();
    public:

    private:
    Direction _direction;
    int _state;
    int _pinNumber;
    QString _directionString;
    QString _valueString;
    QString _strPin;
    };
    #endif // GPIO_H

    file gpio.c

    #include "mini2440Gpio.h"
    //------------------------------------------------------------------------------

    GPIO::GPIO(int pin)
    { _pinNumber = pin;
    _valueString = QString("/sys/class/gpio/gpio%1/value").arg(pin);
    _directionString =
    QString("/sys/class/gpio/gpio%1/direction").arg(pin);
    _strPin = QString("%1").arg(pin);
    }
    //------------------------------------------------------------------------------

    GPIO::~GPIO()
    {
    FILE * fp;
    //This will create the folder /sys/class/gpio/gpio37
    if ((fp = fopen("/sys/class/gpio/unexport", "ab")) == NULL)
    return;
    rewind(fp); //Set pointer to begining of the
    file
    fwrite(_strPin.toLatin1(), sizeof(char),_strPin.length() ,
    fp);
    fclose(fp);
    }
    //------------------------------------------------------------------------------

    int GPIO::openPin()
    {
    FILE * fp;
    //This will create the folder /sys/class/gpio/gpio37
    if ((fp = fopen("/sys/class/gpio/export", "ab")) == NULL) return
    -1;
    rewind(fp);//Set pointer to begining of the file
    fwrite(_strPin.toLatin1(), sizeof(char),_strPin.length() , fp);
    fclose(fp);
    return 0;
    }
    //------------------------------------------------------------------------------

    int GPIO::closePin()
    {
    FILE * fp;
    //This will create the folder /sys/class/gpio/gpio37
    if ((fp = fopen("/sys/class/gpio/unexport", "ab")) == NULL)
    return -1;
    rewind(fp);//Set pointer to begining of the file
    fwrite(_strPin.toLatin1(), sizeof(char),_strPin.length() ,
    fp);
    fclose(fp);
    return 0;
    }
    //------------------------------------------------------------------------------

    int GPIO::setDirection(Direction direction)
    { //set direction in/out. returns 0 if ok and -1 on error
    FILE * fp;
    if ((fp = fopen(_directionString.toLatin1(), "rb+")) == NULL)
    return -1;
    rewind(fp);//Set pointer to begining of the file
    if(direction == In) fwrite("in", sizeof(char),2, fp);
    if(direction == Out) fwrite("out", sizeof(char),3, fp);
    fclose(fp);
    return 0;
    }
    //------------------------------------------------------------------------------

    /*
    int GPIO::getDirection()
    {//returns direction

    }*/
    //------------------------------------------------------------------------------

    int GPIO::setState(bool state)
    {//state is 0 or 1. No effect if other value. returns the new state or -1
    on error
    FILE * fp;
    if ((fp = fopen(_valueString .toLatin1(), "rb+")) == NULL) return -1;
    rewind(fp);//Set pointer to begining of the file
    if(state) fwrite("high", sizeof(char),4, fp);
    else fwrite("low", sizeof(char),3, fp);
    fclose(fp);
    return 0;
    }

    //------------------------------------------------------------------------------

    bool GPIO::getState()
    { //returns 1 or 0 - the pin state. Returns -1 on error
    FILE * fp;
    char value;
    if ((fp = fopen(_valueString.toLatin1(), "rb+")) == NULL) return
    false;
    rewind(fp);//Set pointer to begining of the file
    fread(&value, sizeof(char),1, fp);
    fclose(fp);
    if(value=='1') return true;
    if(value=='0') return false;
    return false;
    }
    //------------------------------------------------------------------------------

    to use gpio class you just declare like this:
    GPIO test = new GPIO(PIN32);
    test->openPin(); // open pin
    test->setDirection(GPIO::Out); // set direction to output
    test->setState(false); // set value to low.
    test->closePin();//close pin
    delete test; // delete pointer to avoid leak memory.

    Have funs!



  • if you work on linux you must know that every thing in linux is file.
    you must write the linux device driver and load as module the you can call that as open file and write your command in that file.
    file gpio.h

    #ifndef GPIO_H
    #define GPIO_H

    #include <qstring.h>
    #include <qobject.h>

    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>

    //Header pin number definitions
    #define PIN9 160
    #define PIN10 161
    #define PIN11 162
    #define PIN12 163
    #define PIN13 164
    #define PIN14 165
    #define PIN15 166
    #define PIN16 192
    #define PIN17 193
    #define PIN18 195
    #define PIN19 197
    #define PIN20 198
    #define PIN21 199
    #define PIN22 201
    #define PIN23 202
    #define PIN24 203
    #define PIN25 139
    #define PIN26 140
    #define PIN27 141
    #define PIN28 194
    #define PIN29 142
    #define PIN30 143
    #define PIN31 32
    #define PIN32 33
    #define PIN33 233
    #define PIN34 234

    class GPIO : public QObject
    {
    Q_OBJECT
    public:
    GPIO(int pin); //pin is the pin nuber on con4 of the FriendlyARM board
    ~GPIO();

    enum Direction {In,Out,Err};
    int openPin();
    int closePin();
    int setDirection(Direction direction); //set direction in/out. returns
    

    0 if ok and -1 on error
    int getDirection(); //returns direction
    int setState(bool state);
    void setValue(bool value);
    bool getState();
    public:

    private:
    Direction _direction;
    int _state;
    int _pinNumber;
    QString _directionString;
    QString _valueString;
    QString _strPin;
    };
    #endif // GPIO_H

    file gpio.c

    #include "mini2440Gpio.h"
    //------------------------------------------------------------------------------

    GPIO::GPIO(int pin)
    { _pinNumber = pin;
    _valueString = QString("/sys/class/gpio/gpio%1/value").arg(pin);
    _directionString =
    QString("/sys/class/gpio/gpio%1/direction").arg(pin);
    _strPin = QString("%1").arg(pin);
    }
    //------------------------------------------------------------------------------

    GPIO::~GPIO()
    {
    FILE * fp;
    //This will create the folder /sys/class/gpio/gpio37
    if ((fp = fopen("/sys/class/gpio/unexport", "ab")) == NULL)
    return;
    rewind(fp); //Set pointer to begining of the
    file
    fwrite(_strPin.toLatin1(), sizeof(char),_strPin.length() ,
    fp);
    fclose(fp);
    }
    //------------------------------------------------------------------------------

    int GPIO::openPin()
    {
    FILE * fp;
    //This will create the folder /sys/class/gpio/gpio37
    if ((fp = fopen("/sys/class/gpio/export", "ab")) == NULL) return
    -1;
    rewind(fp);//Set pointer to begining of the file
    fwrite(_strPin.toLatin1(), sizeof(char),_strPin.length() , fp);
    fclose(fp);
    return 0;
    }
    //------------------------------------------------------------------------------

    int GPIO::closePin()
    {
    FILE * fp;
    //This will create the folder /sys/class/gpio/gpio37
    if ((fp = fopen("/sys/class/gpio/unexport", "ab")) == NULL)
    return -1;
    rewind(fp);//Set pointer to begining of the file
    fwrite(_strPin.toLatin1(), sizeof(char),_strPin.length() ,
    fp);
    fclose(fp);
    return 0;
    }
    //------------------------------------------------------------------------------

    int GPIO::setDirection(Direction direction)
    { //set direction in/out. returns 0 if ok and -1 on error
    FILE * fp;
    if ((fp = fopen(_directionString.toLatin1(), "rb+")) == NULL)
    return -1;
    rewind(fp);//Set pointer to begining of the file
    if(direction == In) fwrite("in", sizeof(char),2, fp);
    if(direction == Out) fwrite("out", sizeof(char),3, fp);
    fclose(fp);
    return 0;
    }
    //------------------------------------------------------------------------------

    /*
    int GPIO::getDirection()
    {//returns direction

    }*/
    //------------------------------------------------------------------------------

    int GPIO::setState(bool state)
    {//state is 0 or 1. No effect if other value. returns the new state or -1
    on error
    FILE * fp;
    if ((fp = fopen(_valueString .toLatin1(), "rb+")) == NULL) return -1;
    rewind(fp);//Set pointer to begining of the file
    if(state) fwrite("high", sizeof(char),4, fp);
    else fwrite("low", sizeof(char),3, fp);
    fclose(fp);
    return 0;
    }

    //------------------------------------------------------------------------------

    bool GPIO::getState()
    { //returns 1 or 0 - the pin state. Returns -1 on error
    FILE * fp;
    char value;
    if ((fp = fopen(_valueString.toLatin1(), "rb+")) == NULL) return
    false;
    rewind(fp);//Set pointer to begining of the file
    fread(&value, sizeof(char),1, fp);
    fclose(fp);
    if(value=='1') return true;
    if(value=='0') return false;
    return false;
    }
    //------------------------------------------------------------------------------

    to use gpio class you just declare like this:
    GPIO test = new GPIO(PIN32);
    test->openPin(); // open pin
    test->setDirection(GPIO::Out); // set direction to output
    test->setState(false); // set value to low.
    test->closePin();//close pin
    delete test; // delete pointer to avoid leak memory.

    Have funs!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.