/******************************************************************* * 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 ""; } }