# HG changeset patch # User Hasan Yavuz ÖZDERYA # Date 2018-05-26 17:26:08 # Node ID 5faf2cdfb6a5e94fbbbe3988666ffe9b2eea5497 # Parent 1920946627a94bc2cf529671c33ddd6b1590f2e5 added new sample per second counter diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -148,6 +148,7 @@ add_executable(${PROGRAM_NAME} WIN32 src/samplepack.cpp src/source.cpp src/sink.cpp + src/samplecounter.cpp misc/windows_icon.rc ${UI_FILES} ${RES_FILES} diff --git a/src/dataformatpanel.h b/src/dataformatpanel.h --- a/src/dataformatpanel.h +++ b/src/dataformatpanel.h @@ -63,7 +63,6 @@ signals: /// Active (selected) reader has changed. void sourceChanged(Source* source); void numOfChannelsChanged(unsigned); - void samplesPerSecondChanged(unsigned); private: Ui::DataFormatPanel *ui; diff --git a/src/demoreader.h b/src/demoreader.h --- a/src/demoreader.h +++ b/src/demoreader.h @@ -53,10 +53,6 @@ public slots: /// Sets the number of channels, this doesn't trigger a `numOfChannelsChanged` signal. void setNumOfChannels(unsigned value); -signals: - // TODO: added for build only, remove later - void samplesPerSecondChanged(unsigned); - private: bool paused; unsigned _numChannels; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -223,9 +223,8 @@ MainWindow::MainWindow(QWidget *parent) spsLabel.setText("0sps"); spsLabel.setToolTip("samples per second (per channel)"); ui->statusBar->addPermanentWidget(&spsLabel); - QObject::connect(&dataFormatPanel, - &DataFormatPanel::samplesPerSecondChanged, - this, &MainWindow::onSpsChanged); + connect(&sampleCounter, &SampleCounter::spsChanged, + this, &MainWindow::onSpsChanged); // init demo QObject::connect(ui->actionDemoMode, &QAction::toggled, @@ -236,8 +235,8 @@ MainWindow::MainWindow(QWidget *parent) // init stream connections connect(&dataFormatPanel, &DataFormatPanel::sourceChanged, - this, &MainWindow::setStreamSource); - setStreamSource(dataFormatPanel.activeSource()); + this, &MainWindow::onSourceChanged); + onSourceChanged(dataFormatPanel.activeSource()); // load default settings QSettings settings("serialplot", "serialplot"); @@ -346,10 +345,10 @@ void MainWindow::onPortToggled(bool open ui->actionDemoMode->setEnabled(!open); } -void MainWindow::setStreamSource(Source* source) +void MainWindow::onSourceChanged(Source* source) { - // connect to new source source->connectSink(&stream); + source->connectSink(&sampleCounter); } void MainWindow::clearPlot() @@ -365,9 +364,10 @@ void MainWindow::onNumOfSamplesChanged(i plotMan->replot(); } -void MainWindow::onSpsChanged(unsigned sps) +void MainWindow::onSpsChanged(float sps) { - spsLabel.setText(QString::number(sps) + "sps"); + int precision = sps < 1. ? 3 : 0; + spsLabel.setText(QString::number(sps, 'f', precision) + "sps"); } bool MainWindow::isDemoRunning() diff --git a/src/mainwindow.h b/src/mainwindow.h --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -45,6 +45,7 @@ #include "plotmanager.h" #include "plotmenu.h" #include "updatecheckdialog.h" +#include "samplecounter.h" namespace Ui { class MainWindow; @@ -80,6 +81,7 @@ private: PlotManager* plotMan; QWidget* secondaryPlot; SnapshotManager snapshotMan; + SampleCounter sampleCounter; QLabel spsLabel; CommandPanel commandPanel; @@ -110,11 +112,11 @@ private: private slots: void onPortToggled(bool open); - void setStreamSource(Source* source); + void onSourceChanged(Source* source); void onNumOfSamplesChanged(int value); void clearPlot(); - void onSpsChanged(unsigned sps); + void onSpsChanged(float sps); void enableDemo(bool enabled); void showBarPlot(bool show); diff --git a/src/samplecounter.cpp b/src/samplecounter.cpp new file mode 100644 --- /dev/null +++ b/src/samplecounter.cpp @@ -0,0 +1,44 @@ +/* + Copyright © 2018 Hasan Yavuz Özderya + + This file is part of serialplot. + + serialplot is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + serialplot is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with serialplot. If not, see . +*/ + +#include +#include "samplecounter.h" + +SampleCounter::SampleCounter() +{ + prevTimeMs = QDateTime::currentMSecsSinceEpoch(); + count = 0; +} + +#include + +void SampleCounter::feedIn(const SamplePack& data) +{ + count += data.numSamples(); + + qint64 current = QDateTime::currentMSecsSinceEpoch(); + auto diff = current - prevTimeMs; + if (diff > 1000) // 1sec + { + emit spsChanged(1000 * float(count) / diff); + + prevTimeMs = current; + count = 0; + } +} diff --git a/src/samplecounter.h b/src/samplecounter.h new file mode 100644 --- /dev/null +++ b/src/samplecounter.h @@ -0,0 +1,47 @@ +/* + Copyright © 2018 Hasan Yavuz Özderya + + This file is part of serialplot. + + serialplot is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + serialplot is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with serialplot. If not, see . +*/ + +#ifndef SAMPLECOUNTER_H +#define SAMPLECOUNTER_H + +#include +#include "sink.h" + +/// A `Sink` class for counting and reporting number of samples per second. +class SampleCounter : public QObject, public Sink +{ + Q_OBJECT + +public: + SampleCounter(); + +protected: + // implementations for `Sink` + virtual void feedIn(const SamplePack& data); + +signals: + /// Emitted per second if SPS value has changed. + void spsChanged(float value); + +private: + qint64 prevTimeMs; + unsigned count; +}; + +#endif // SAMPLECOUNTER_H