Changeset - 8fd74552f317
[Not reviewed]
default
0 2 0
Hasan Yavuz Ă–ZDERYA - 10 years ago 2015-06-24 03:24:03
hy@ozderya.net
added sample per second counter
2 files changed with 25 insertions and 0 deletions:
0 comments (0 inline, 0 general)
mainwindow.cpp
Show inline comments
 
@@ -130,42 +130,51 @@ MainWindow::MainWindow(QWidget *parent) 
 
    }
 

	
 
    // init number format
 
    if (numberFormatButtons.checkedId() >= 0)
 
    {
 
        selectNumberFormat((NumberFormat) numberFormatButtons.checkedId());
 
    }
 
    else
 
    {
 
        selectNumberFormat(NumberFormat_uint8);
 
    }
 

	
 
    // Init sps (sample per second) counter
 
    sampleCount = 0;
 
    spsLabel.setText("0sps");
 
    ui->statusBar->addPermanentWidget(&spsLabel);
 
    spsTimer.start(SPS_UPDATE_TIMEOUT * 1000);
 
    QObject::connect(&spsTimer, &QTimer::timeout,
 
                     this, &MainWindow::spsTimerTimeout);
 

	
 
    // Init demo mode
 
    demoCount = 0;
 
    demoTimer.setInterval(100);
 
    QObject::connect(&demoTimer, &QTimer::timeout,
 
                     this, &MainWindow::demoTimerTimeout);
 
    QObject::connect(ui->actionDemoMode, &QAction::toggled,
 
                     this, &MainWindow::enableDemo);
 

	
 
    {   // init demo indicator
 
        QwtText demoText(" DEMO RUNNING ");  // looks better with spaces
 
        demoText.setColor(QColor("white"));
 
        demoText.setBackgroundBrush(Qt::darkRed);
 
        demoText.setBorderRadius(4);
 
        demoText.setRenderFlags(Qt::AlignLeft | Qt::AlignTop);
 
        demoIndicator.setText(demoText);
 
        demoIndicator.hide();
 
        demoIndicator.attach(ui->plot);
 
    }
 

	
 
}
 

	
 
MainWindow::~MainWindow()
 
{
 
    for (auto curve : curves)
 
    {
 
        delete curve;
 
    }
 

	
 
    if (serialPort.isOpen())
 
    {
 
        serialPort.close();
 
@@ -368,24 +377,26 @@ void MainWindow::addChannelData(unsigned
 
            (*channelDataArray)[i] = (*channelDataArray)[i + shift];
 
        }
 
        // place new samples
 
        for (int i = 0; i < data.size(); i++)
 
        {
 
            (*channelDataArray)[offset + i] = data[i];
 
        }
 
    }
 

	
 
    // update plot
 
    curves[channel]->setSamples(dataX, (*channelDataArray));
 
    ui->plot->replot(); // TODO: replot after all channel data updated
 

	
 
    sampleCount += data.size();
 
}
 

	
 
void MainWindow::clearPlot()
 
{
 
    for (unsigned int ci = 0; ci < numOfChannels; ci++)
 
    {
 
        channelsData[ci].fill(0.0);
 
        curves[ci]->setSamples(dataX, channelsData[ci]);
 
    }
 

	
 
    // update plot
 
    ui->plot->replot();
 
@@ -551,24 +562,30 @@ template<typename T> double MainWindow::
 
    {
 
        data = qFromBigEndian(data);
 
    }
 

	
 
    return double(data);
 
}
 

	
 
bool MainWindow::isDemoRunning()
 
{
 
    return ui->actionDemoMode->isChecked();
 
}
 

	
 
void MainWindow::spsTimerTimeout()
 
{
 
    spsLabel.setText(QString::number(sampleCount/SPS_UPDATE_TIMEOUT) + "sps");
 
    sampleCount = 0;
 
}
 

	
 
void MainWindow::demoTimerTimeout()
 
{
 
    demoCount++;
 
    if (demoCount > 100) demoCount = 0;
 

	
 
    if (!ui->actionPause->isChecked())
 
    {
 
        for (unsigned int ci = 0; ci < numOfChannels; ci++)
 
        {
 
            DataArray data(1);
 
            data.replace(0, (ci + 1)*demoCount);
 
            addChannelData(ci, data);
mainwindow.h
Show inline comments
 
@@ -13,24 +13,25 @@
 
  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 MAINWINDOW_H
 
#define MAINWINDOW_H
 

	
 
#include <QMainWindow>
 
#include <QButtonGroup>
 
#include <QLabel>
 
#include <QString>
 
#include <QVector>
 
#include <QList>
 
#include <QSerialPort>
 
#include <QSignalMapper>
 
#include <QTimer>
 
#include <QColor>
 
#include <QtGlobal>
 
#include <qwt_plot_curve.h>
 
#include <qwt_plot_textlabel.h>
 

	
 
#include "portcontrol.h"
 
@@ -84,24 +85,29 @@ private:
 
    // `data` contains channel specific data
 
    void addChannelData(unsigned int channel, DataArray data);
 

	
 
    NumberFormat numberFormat;
 
    unsigned int sampleSize; // number of bytes in the selected number format
 
    double (MainWindow::*readSample)();
 

	
 
    // note that serialPort should already have enough bytes present
 
    template<typename T> double readSampleAs();
 

	
 
    bool skipByteRequested;
 

	
 
    const int SPS_UPDATE_TIMEOUT = 1;  // second
 
    QLabel spsLabel;
 
    unsigned int sampleCount;
 
    QTimer spsTimer;
 

	
 
    // demo
 
    QTimer demoTimer;
 
    int demoCount;
 
    bool isDemoRunning();
 
    QwtPlotTextLabel demoIndicator;
 

	
 
    QColor makeColor(unsigned int channelIndex);
 

	
 
private slots:
 
    void onPortToggled(bool open);
 
    void onDataReady();      // used with binary number formats
 
    void onDataReadyASCII(); // used with ASCII number format
 
@@ -110,19 +116,21 @@ private slots:
 
    void skipByte();
 

	
 
    void onNumOfSamplesChanged(int value);
 
    void onAutoScaleChecked(bool checked);
 
    void onYScaleChanged();
 

	
 
    void onNumOfChannelsChanged(int value);
 
    void onNumberFormatButtonToggled(int numberFormatId, bool checked);
 
    void selectNumberFormat(NumberFormat numberFormatId);
 

	
 
    void clearPlot();
 

	
 
    void spsTimerTimeout();
 

	
 
    void demoTimerTimeout();
 
    void enableDemo(bool enabled);
 

	
 
    void onExportCsv();
 
};
 

	
 
#endif // MAINWINDOW_H
0 comments (0 inline, 0 general)