AD7606 QT demo
-
QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets charts CONFIG += c++11 # You can make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ main.cpp \ mainwindow.cpp HEADERS += \ mainwindow.h FORMS += \ mainwindow.ui TRANSLATIONS += \ ad7606_zh_CN.ts CONFIG += lrelease CONFIG += embed_translations # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QSplineSeries> using namespace QtCharts; QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); void setChart(); int getValue(char *channel); int getScale(); void timerEvent(QTimerEvent *e); private slots: void on_pushButton_2_clicked(); void on_pushButton_clicked(); private: Ui::MainWindow *ui; QSplineSeries *serials0; QSplineSeries *serials1; QSplineSeries *serials2; QSplineSeries *serials3; QSplineSeries *serials4; QSplineSeries *serials5; QSplineSeries *serials6; QSplineSeries *serials7; QChart *chart; int scale; int timerid1; float value[8]; }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QtCharts> #include <QtCharts/QChartView> #include <QSplineSeries> #include <QProcess> #include <QString> #include <QTimer> #include <fcntl.h> #include <unistd.h> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); setChart(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::setChart() { scale = getScale(); serials0 = new QtCharts::QSplineSeries(); serials1 = new QtCharts::QSplineSeries(); serials2 = new QtCharts::QSplineSeries(); serials3 = new QtCharts::QSplineSeries(); serials4 = new QtCharts::QSplineSeries(); serials5 = new QtCharts::QSplineSeries(); serials6 = new QtCharts::QSplineSeries(); serials7 = new QtCharts::QSplineSeries(); chart = new QChart(); chart->addSeries(serials7); chart->createDefaultAxes(); chart->axisX()->setRange(0, 99); chart->axisY()->setRange(-10, 10); chart->legend()->hide(); ui->widget->setChart(chart); } int MainWindow::getValue(char *channel) { int dev_fd,ret; char DEV_PATH[64]; char value[5]; QString str_data; sprintf(DEV_PATH,"/sys/bus/spi/devices/spi0.0/iio:device0/in_voltage%s_raw",channel); dev_fd = open(DEV_PATH,O_RDONLY); if(dev_fd == -1) { perror("open channel failed!\n"); return -1; } if(read(dev_fd,value,5)<0) { perror("read channel failed!\n"); return -1; } ::close(dev_fd); str_data = QString::fromLocal8Bit(value,5); ret = str_data.toInt(); return ret; } int MainWindow::getScale() { int dev_fd; int ret; char DEV_PATH[64]; char value[5]; QString str_data; sprintf(DEV_PATH,"/sys/bus/spi/devices/spi0.0/iio:device0/in_voltage_scale"); dev_fd = open(DEV_PATH,O_RDONLY); if(dev_fd == -1) { perror("open scale failed!\n"); return -1; } if(read(dev_fd,value,5)<0) { perror("read scale failed!\n"); return -1; } ::close(dev_fd); str_data = QString::fromLocal8Bit(value,5); ret = str_data.toFloat()*1000; return ret; } void MainWindow::timerEvent(QTimerEvent *e) { int i; QVector<QPointF> oldData = serials7->pointsVector(); QVector<QPointF> data; if (oldData.size() < 100) { data = serials7->pointsVector(); } else { for (i = 1; i < oldData.size(); ++i) { data.append(QPointF(i - 1 , oldData.at(i).y())); } } qint64 size = data.size(); for(i = 0; i < 1; ++i){ value[7] = getValue("7"); data.append(QPointF(i + size, value[7]*scale/1000000)); } serials7->replace(data); } void MainWindow::on_pushButton_2_clicked() { serials7->clear(); timerid1 = startTimer(1); } void MainWindow::on_pushButton_clicked() { killTimer(timerid1); }
source code ad7606.zip