Changeset - 5faf2cdfb6a5
[Not reviewed]
stream
0 5 2
Hasan Yavuz ÖZDERYA - 7 years ago 2018-05-26 17:26:08
hy@ozderya.net
added new sample per second counter
7 files changed with 104 insertions and 15 deletions:
0 comments (0 inline, 0 general)
CMakeLists.txt
Show inline comments
 
@@ -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}
src/dataformatpanel.h
Show inline comments
 
@@ -63,7 +63,6 @@ signals:
 
    /// Active (selected) reader has changed.
 
    void sourceChanged(Source* source);
 
    void numOfChannelsChanged(unsigned);
 
    void samplesPerSecondChanged(unsigned);
 

	
 
private:
 
    Ui::DataFormatPanel *ui;
src/demoreader.h
Show inline comments
 
@@ -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;
src/mainwindow.cpp
Show inline comments
 
@@ -223,8 +223,7 @@ MainWindow::MainWindow(QWidget *parent) 
 
    spsLabel.setText("0sps");
 
    spsLabel.setToolTip("samples per second (per channel)");
 
    ui->statusBar->addPermanentWidget(&spsLabel);
 
    QObject::connect(&dataFormatPanel,
 
                     &DataFormatPanel::samplesPerSecondChanged,
 
    connect(&sampleCounter, &SampleCounter::spsChanged,
 
                     this, &MainWindow::onSpsChanged);
 

	
 
    // init demo
 
@@ -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()
src/mainwindow.h
Show inline comments
 
@@ -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);
 

	
src/samplecounter.cpp
Show inline comments
 
new file 100644
 
/*
 
  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 <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#include <QDateTime>
 
#include "samplecounter.h"
 

	
 
SampleCounter::SampleCounter()
 
{
 
    prevTimeMs = QDateTime::currentMSecsSinceEpoch();
 
    count = 0;
 
}
 

	
 
#include <QtDebug>
 

	
 
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;
 
    }
 
}
src/samplecounter.h
Show inline comments
 
new file 100644
 
/*
 
  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 <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#ifndef SAMPLECOUNTER_H
 
#define SAMPLECOUNTER_H
 

	
 
#include <QObject>
 
#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
0 comments (0 inline, 0 general)