• 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 "";
            }
        }
        
  • 相关阅读:
    (备忘)解决用Xftp向CentOS7 传文件速度慢的问题
    CentOS上使用ntfs-3g挂载NTFS分区
    tomcat运行一段时间出“org.apache.coyote.http11.Http11Processor.service Error parsing HTTP request header”
    JQuery EasyUI treegrid展开与折叠,以及数据加载两次的问题
    goland 激活码
    golang 之xorm
    golang 之 go module
    golang 之单元测试
    golang 之反射
    golang 之sync &并发安全锁
  • 原文地址:https://www.cnblogs.com/zengjfgit/p/5322763.html
Copyright © 2020-2023  润新知