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;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要实现这个功能,你需要安装摄像头模块,并在树莓派上安装OpenCV库。以下是一个简单的思路: 1. 用树莓派连接摄像头,并打开摄像头模块; 2. 使用OpenCV库读取摄像头传回的图像,然后将其转换为HSV颜色空间; 3. 根据HSV颜色空间中黄色和绿色的颜色范围,使用OpenCV库进行颜色识别; 4. 计算黄色区域的面积大小; 5. 如果黄色面积大于指定阈值,则执行GPIO操作。 以下是一个简单的Python代码框架: ``` import cv2 import numpy as np import RPi.GPIO as GPIO # 初始化GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(18, GPIO.OUT) # 定义颜色范围 lower_yellow = np.array([22, 60, 200]) upper_yellow = np.array([60, 255, 255]) lower_green = np.array([35, 43, 46]) upper_green = np.array([77, 255, 255]) # 设置黄色面积阈值 yellow_area_threshold = 1000 # 创建摄像头对象 cap = cv2.VideoCapture(0) while True: # 读取摄像头图像 ret, frame = cap.read() # 将图像转换为HSV颜色空间 hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # 进行颜色识别 mask_yellow = cv2.inRange(hsv, lower_yellow, upper_yellow) mask_green = cv2.inRange(hsv, lower_green, upper_green) # 计算黄色区域的面积大小 contours_yellow, _ = cv2.findContours(mask_yellow, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) if len(contours_yellow) > 0: max_contour_yellow = max(contours_yellow, key=cv2.contourArea) yellow_area = cv2.contourArea(max_contour_yellow) if yellow_area > yellow_area_threshold: # 执行GPIO操作 GPIO.output(18, GPIO.HIGH) else: GPIO.output(18, GPIO.LOW) # 显示图像 cv2.imshow('frame', frame) # 等待按键退出 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放资源 cap.release() GPIO.cleanup() cv2.destroyAllWindows() ``` 请注意,这只是一个简单的代码示例,可能需要进行进一步的优化和调整以适应实际应用场景。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值