• 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 "";
            }
        }
        
  • 相关阅读:
    LVS+Heartbeat 高可用集群方案操作记录
    Haproxy和Nginx负载均衡测试效果对比记录
    Haproxy+Keepalived高可用环境部署梳理(主主和主从模式)
    Haproxy 重定向跳转设置
    Haproxy+Heartbeat 高可用集群方案操作记录
    haproxy反向代理环境部署(http和https代理)
    Linux下对lvm逻辑卷分区大小的调整(针对xfs和ext4不同文件系统)
    LVM基础详细说明及动态扩容lvm逻辑卷的操作记录
    Apache运维中常用功能配置笔记梳理
    LVS负载均衡-基础知识梳理
  • 原文地址:https://www.cnblogs.com/zengjfgit/p/5322763.html
Copyright © 2020-2023  润新知