Qt:在树莓配中控制gpio阵脚

关于树莓派的介绍这里不做描述,关于gpio更多的信息可以移步:

http://www.cnblogs.com/vamei/p/6751992.html


写了一个简单的demo,控制20号针脚,直接上代码:

jqgpio.h
#ifndef JQGPIO_H_
#define JQGPIO_H_

// Qt lib import
#include <QObject>
#include <QMutex>

class JQGpio
{
private:
    JQGpio() = default;

public:
    ~JQGpio() = default;

    static bool exportGpio(const int &portId, const bool &isOutPort);

    static bool unexportGpio(const int &portId);

    static QPair< bool, bool > readGpio(const int &portId);

    static bool writeGpio(const int &portId, const bool &state);

    static QString gpioExportFilePath();

    static QString gpioUnexportFilePath();

    static QString gpioDirectionFilePath(const int &portId);

    static QString gpioValueFilePath(const int &portId);

private:
    static QMutex mutex_;
};

#endif//JQGPIO_H_
jqgpio.cpp
#include "jqgpio.h"

// Qt lib import
#include <QDebug>
#include <QFile>
#include <QThread>

QMutex JQGpio::mutex_;

bool JQGpio::exportGpio(const int &portId, const bool &isOutPort)
{
    mutex_.lock();

    {
        QFile file( gpioExportFilePath() );
        if ( !file.open( QIODevice::WriteOnly ) )
        {
            mutex_.unlock();
            return false;
        }

        if ( !file.write( QString( "%1\n" ).arg( portId ).toLatin1() ) )
        {
            mutex_.unlock();
            return false;
        }

        file.waitForBytesWritten( 5000 );
    }

    {
        for ( auto tryCount = 0; tryCount < 5; ++tryCount )
        {
            QThread::msleep( 100 );

            if ( QFile( gpioDirectionFilePath( portId ) ).exists() )
            {
                break;
            }

            if ( tryCount == 4 )
            {
                qDebug() << "JQGpio::exportGpio: direction not exist error:" << portId;
                mutex_.unlock();
                return false;
            }
        }

        QFile file( gpioDirectionFilePath( portId ) );
        if ( !file.open( QIODevice::WriteOnly ) )
        {
            mutex_.unlock();
            return false;
        }

        if ( isOutPort )
        {
            if ( !file.write( "out\n" ) )
            {
                mutex_.unlock();
                return false;
            }
        }
        else
        {
            if ( !file.write( "in\n" ) )
            {
                mutex_.unlock();
                return false;
            }
        }

        file.waitForBytesWritten( 5000 );
    }

    mutex_.unlock();
    return true;
}

bool JQGpio::unexportGpio(const int &portId)
{
    mutex_.lock();

    QFile file( gpioUnexportFilePath() );
    if ( !file.open( QIODevice::WriteOnly ) )
    {
        mutex_.unlock();
        return false;
    }

    if ( !file.write( QString( "%1\n" ).arg( portId ).toLatin1() ) )
    {
        mutex_.unlock();
        return false;
    }

    file.waitForBytesWritten( 5000 );

    mutex_.unlock();
    return true;
}

QPair< bool, bool > JQGpio::readGpio(const int &portId)
{
    QFile file( gpioValueFilePath( portId ) );
    if ( !file.open( QIODevice::ReadOnly ) ) { return { false, false }; }

    const auto &&data = file.readAll();

    if ( data.startsWith( "0" ) ) { return { true, false }; }
    if ( data.startsWith( "1" ) ) { return { true, true }; }

    return { false, false };
}

bool JQGpio::writeGpio(const int &portId, const bool &state)
{
    QFile file( gpioValueFilePath( portId ) );
    if ( !file.open( QIODevice::WriteOnly ) ) { return false; }

    if ( state )
    {
        if ( !file.write( "1\n" ) ) { return false; }
    }
    else
    {
        if ( !file.write( "0\n" ) ) { return false; }
    }

    file.waitForBytesWritten( 5000 );

    return true;
}

QString JQGpio::gpioExportFilePath()
{
    return QString( "/sys/class/gpio/export" );
}

QString JQGpio::gpioUnexportFilePath()
{
    return QString( "/sys/class/gpio/unexport" );
}

QString JQGpio::gpioDirectionFilePath(const int &portId)
{
    return QString( "/sys/class/gpio/gpio%1/direction" ).arg( portId );
}

QString JQGpio::gpioValueFilePath(const int &portId)
{
    return QString( "/sys/class/gpio/gpio%1/value" ).arg( portId );
}
main.cpp
// Qt lib import
#include <QCoreApplication>
#include <QDebug>
#include <QThread>

// JQLibrary lib import
#include "jqgpio.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    qDebug() << "JQGpio::exportGpio( 20, true ):" << JQGpio::exportGpio( 20, true );

    for ( auto i = 0; i < 10; ++i )
    {
        qDebug() << "JQGpio::writeGpio( 20, true ): "<< JQGpio::writeGpio( 20, true );
        QThread::sleep( 1 );
        qDebug() << "JQGpio::writeGpio( 20, false ): "<< JQGpio::writeGpio( 20, false );
        QThread::sleep( 1 );
    }

    qDebug() << "JQGpio::unexportGpio( 20 ):" << JQGpio::unexportGpio( 20 );

    return 0;
}

猜你喜欢

转载自blog.csdn.net/wsj18808050/article/details/78340213