Qt:在树莓配中控制gpio阵脚
来源:互联网 发布:我的世界js编辑器jside 编辑:程序博客网 时间:2024/06/14 16:08
关于树莓派的介绍这里不做描述,关于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;}
阅读全文
0 0
- Qt:在树莓配中控制gpio阵脚
- qt gpio
- 树莓派3QT+wiringPI控制GPIO输入和输出
- GPIO控制
- GPIO在用户空间(Sysfs)中的检测与控制
- 2440 GPIO 控制方法
- S3C2440--GPIO控制.
- s3c2440的GPIO控制
- dsp gpio控制
- raspberry GPIO控制第一步
- GPIO口控制学习
- 用C控制GPIO
- Am335x 之GPIO控制
- mtk android ,gpio控制
- BeagleBone的GPIO控制
- echo命令控制gpio
- 树莓派控制GPIO
- 树莓派GPIO控制
- java实现文件下载
- 微信退款IIS调用接口(https)提示错误的解决方案
- 房客多可以帮助经纪人手机微信卖房?
- windows server2016 安装sql server2008r2
- recyclerview的高级使用
- Qt:在树莓配中控制gpio阵脚
- resultType之三redirectAction重定向到另一个action
- HDU
- CentOS6.5升级手动安装GCC4.8.2
- 常用win+r运行命令总结
- 网络连接的判断与封装以及网络获取数据的封装
- hive--add columns问题
- sql高级查询
- NGinx+RTMP 实现流媒体服务器