OK335xS tmp75 Qt 温度读取

/*******************************************************************
 *                    OK335xS tmp75 Qt 温度读取
 * 说明:
 *     简单的Qt显示tmp75温度值,其驱动已经在Linux驱动中存在,只需要
 * 注册一下I2C设备就行了。
 *
 *                                2016-3-26 深圳 南山平山村 曾剑锋
 ******************************************************************/

                    \\\\\\\\\\\-*- 目录 -*-///
                    |  一、cat main.c
                    |  二、cat mainwindow.h
                    |  三、cat mainwindow.cpp
                    |  四、cat temperaturethread.h
                    |  五、cat temperaturethread.cpp
                    ||||||||||||||||||||||||||||||||||  


一、cat main.c
    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.setWindowFlags(w.windowFlags()& ~Qt::WindowMaximizeButtonHint& ~Qt::WindowMinimizeButtonHint);
        w.show();
    
        return a.exec();
    }

二、cat mainwindow.h
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <temperaturethread.h>
    #include <QString>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
        TemperatureThread tempThread;
    
    public slots:
        void dealWithData(QString);
    
    protected:
        void moveEvent(QMoveEvent *);
        void resizeEvent(QResizeEvent *);
        void closeEvent(QCloseEvent *);
    
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    
三、cat mainwindow.cpp
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QMessageBox>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        connect(&tempThread, SIGNAL(msg(QString)),this, SLOT(dealWithData(QString)));
    
        tempThread.threadRunning = true;
        tempThread.start();
    }
    
    void MainWindow::dealWithData(QString data) {
    
        if( data.trimmed().length() == 0 ) {
            QMessageBox::about(this, "About", "Please check your temperature modle.");
            exit(-1);
        }
    
        ui->temp->setText(QString("Temperature: ").append(QString::number(data.toFloat()/1000)).append(" C"));
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::moveEvent(QMoveEvent *)
    {
        this->move(QPoint(0,0));
    }
    
    void MainWindow::resizeEvent(QResizeEvent *)
    {
        this->showMaximized();
    }
    
    void MainWindow::closeEvent(QCloseEvent *)
    {
        exit(0);
    }


四、cat temperaturethread.h
    #ifndef TEMPERATURETHREAD_H
    #define TEMPERATURETHREAD_H
    
    #include <QThread>
    #include <sys/ioctl.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <QFileInfo>
    #include <QMessageBox>
    #include <QTreeView>
    #include <QDir>
    #include <QFile>
    #include <QDebug>
    #include <QMessageBox>
    
    
    class TemperatureThread : public QThread
    {
        Q_OBJECT
    public:
        explicit TemperatureThread(QObject *parent = 0);
        QString readTempFile(void);
    
        bool threadRunning;
    
    signals:
        void msg(QString str);
    
    public slots:
        void run();
    
    };
    
    #endif // TEMPERATURETHREAD_H


五、cat temperaturethread.cpp
    #include "temperaturethread.h"
    
    #define tmp75Path "/sys/bus/i2c/devices/3-004c/temp1_input"
    
    TemperatureThread::TemperatureThread(QObject *parent) :
        QThread(parent)
    {
    }
    
    void TemperatureThread::run(){
    
        while(threadRunning) {
    
            emit msg(readTempFile());
    
            msleep(500);
    
        }
    
    }
    
    QString TemperatureThread::readTempFile(){
    
        QFile *file=new QFile(tmp75Path);

        if ( file->exists() ) {
            file->open(QIODevice::ReadOnly|QIODevice::Text);
            QString data = QString(file->readAll());
            file->close();
            return data;
        } else {
            return "";
        }
    }
    

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值