Changeset - 07ecd1b1fb2a
[Not reviewed]
new-reader
0 7 0
Hasan Yavuz ÖZDERYA - 9 years ago 2016-05-23 14:56:53
hy@ozderya.net
re-enabled sps counter
7 files changed with 37 insertions and 49 deletions:
0 comments (0 inline, 0 general)
src/abstractreader.cpp
Show inline comments
 
@@ -21,7 +21,26 @@
 

	
 
AbstractReader::AbstractReader(QIODevice* device, ChannelManager* channelMan, QObject *parent) :
 
    QObject(parent)
 
{
 
    _device = device;
 
    _channelMan = channelMan;
 

	
 
    // initialize sps counter
 
    sampleCount = 0;
 
    samplesPerSecond = 0;
 
    QObject::connect(&spsTimer, &QTimer::timeout,
 
                     this, &AbstractReader::spsTimerTimeout);
 
    // TODO: start sps timer when reader is enabled
 
    spsTimer.start(SPS_UPDATE_TIMEOUT * 1000);
 
}
 

	
 
void AbstractReader::spsTimerTimeout()
 
{
 
    unsigned currentSps = samplesPerSecond;
 
    samplesPerSecond = (sampleCount/numOfChannels())/SPS_UPDATE_TIMEOUT;
 
    if (currentSps != samplesPerSecond)
 
    {
 
        emit samplesPerSecondChanged(samplesPerSecond);
 
    }
 
    sampleCount = 0;
 
}
src/abstractreader.h
Show inline comments
 
@@ -20,12 +20,13 @@
 
#ifndef ABSTRACTREADER_H
 
#define ABSTRACTREADER_H
 

	
 
#include <QObject>
 
#include <QIODevice>
 
#include <QWidget>
 
#include <QTimer>
 

	
 
#include "channelmanager.h"
 

	
 
/**
 
 * All reader classes must inherit this class.
 
 */
 
@@ -69,9 +70,20 @@ public slots:
 
     */
 
    virtual void pause(bool) = 0;
 

	
 
protected:
 
    QIODevice* _device;
 
    ChannelManager* _channelMan;
 

	
 
    /// Implementing class should simply increase this count as samples are read
 
    unsigned sampleCount;
 

	
 
private:
 
    const int SPS_UPDATE_TIMEOUT = 1;  // second
 
    unsigned samplesPerSecond;
 
    QTimer spsTimer;
 

	
 
private slots:
 
    void spsTimerTimeout();
 
};
 

	
 
#endif // ABSTRACTREADER_H
src/asciireader.h
Show inline comments
 
@@ -37,13 +37,12 @@ public slots:
 
    void pause(bool);
 

	
 
private:
 
    AsciiReaderSettings _settingsWidget;
 
    unsigned _numOfChannels;
 
    bool paused;
 
    unsigned sampleCount; ///< used for sps counter
 

	
 
private slots:
 
    void onDataReady();
 
};
 

	
 
#endif // ASCIIREADER_H
src/binarystreamreader.cpp
Show inline comments
 
@@ -44,14 +44,12 @@ BinaryStreamReader::BinaryStreamReader(Q
 
    // enable skip byte button
 
    connect(&_settingsWidget, &BinaryStreamReaderSettings::skipByteRequested,
 
            [this]()
 
            {
 
                skipByteRequested = true;
 
            });
 

	
 
    // TODO sps counter
 
}
 

	
 
QWidget* BinaryStreamReader::settingsWidget()
 
{
 
    return &_settingsWidget;
 
}
src/binarystreamreader.h
Show inline comments
 
@@ -43,13 +43,12 @@ public slots:
 
private:
 
    BinaryStreamReaderSettings _settingsWidget;
 
    unsigned _numOfChannels;
 
    unsigned sampleSize;
 
    bool paused;
 
    bool skipByteRequested;
 
    unsigned sampleCount; ///< used for sps counter
 

	
 
    /// points to the readSampleAs function for currently selected number format
 
    double (BinaryStreamReader::*readSample)();
 

	
 
    /**
 
     * Reads 1 sample from the device in given format.
src/dataformatpanel.cpp
Show inline comments
 
/*
 
  Copyright © 2015 Hasan Yavuz Özderya
 
  Copyright © 2016 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
 
@@ -46,59 +46,43 @@ DataFormatPanel::DataFormatPanel(QSerial
 
    bsReader.enable();
 
    ui->rbBinary->setChecked(true);
 
    ui->horizontalLayout->addWidget(bsReader.settingsWidget(), 1);
 
    connect(&bsReader, SIGNAL(dataAdded()), this, SIGNAL(dataAdded()));
 
    connect(&bsReader, SIGNAL(numOfChannelsChanged(unsigned)),
 
            this, SIGNAL(numOfChannelsChanged(unsigned)));
 
    connect(&bsReader, SIGNAL(samplesPerSecondChanged(unsigned)),
 
            this, SIGNAL(samplesPerSecondChanged(unsigned)));
 

	
 
    // initalize reader selection buttons
 
    connect(ui->rbBinary, &QRadioButton::toggled, [this](bool checked)
 
            {
 
                if (checked) selectReader(&bsReader);
 
            });
 

	
 
    connect(ui->rbAscii, &QRadioButton::toggled, [this](bool checked)
 
            {
 
                if (checked) selectReader(&asciiReader);
 
            });
 

	
 
    // Init sps (sample per second) counter
 
    // sampleCount = 0;
 
    // QObject::connect(&spsTimer, &QTimer::timeout,
 
    //                  this, &DataFormatPanel::spsTimerTimeout);
 
    // spsTimer.start(SPS_UPDATE_TIMEOUT * 1000);
 

	
 
    // Init demo mode
 
    demoCount = 0;
 
    demoTimer.setInterval(100);
 
    QObject::connect(&demoTimer, &QTimer::timeout,
 
                     this, &DataFormatPanel::demoTimerTimeout);
 
}
 

	
 
DataFormatPanel::~DataFormatPanel()
 
{
 
    delete ui;
 
}
 

	
 
// TODO: remove
 
bool DataFormatPanel::skipByteEnabled()
 
{
 
    return false;
 
}
 

	
 
unsigned DataFormatPanel::numOfChannels()
 
{
 
    return currentReader->numOfChannels();
 
}
 

	
 
// TODO: remove
 
void DataFormatPanel::requestSkipByte()
 
{
 
    skipByteRequested = true;
 
}
 

	
 
void DataFormatPanel::pause(bool enabled)
 
{
 
    currentReader->pause(enabled);
 
}
 

	
 
void DataFormatPanel::enableDemo(bool enabled)
 
@@ -110,23 +94,12 @@ void DataFormatPanel::enableDemo(bool en
 
    else
 
    {
 
        demoTimer.stop();
 
    }
 
}
 

	
 
void DataFormatPanel::spsTimerTimeout()
 
{
 
    // unsigned currentSps = _samplesPerSecond;
 
    // _samplesPerSecond = (sampleCount/_numOfChannels)/SPS_UPDATE_TIMEOUT;
 
    // if (currentSps != _samplesPerSecond)
 
    // {
 
    //     emit samplesPerSecondChanged(_samplesPerSecond);
 
    // }
 
    // sampleCount = 0;
 
}
 

	
 
void DataFormatPanel::demoTimerTimeout()
 
{
 
    const double period = 100;
 
    demoCount++;
 
    if (demoCount >= 100) demoCount = 0;
 

	
 
@@ -143,25 +116,26 @@ void DataFormatPanel::demoTimerTimeout()
 
}
 

	
 
void DataFormatPanel::addChannelData(unsigned int channel,
 
                                     double* data, unsigned size)
 
{
 
    _channelMan->addChannelData(channel, data, size);
 
    sampleCount += size;
 
}
 

	
 
void DataFormatPanel::selectReader(AbstractReader* reader)
 
{
 
    currentReader->enable(false);
 
    reader->enable();
 

	
 
    // re-connect signals
 
    disconnect(currentReader, 0, this, 0);
 
    connect(reader, SIGNAL(dataAdded()), this, SIGNAL(dataAdded()));
 
    connect(reader, SIGNAL(numOfChannelsChanged(unsigned)),
 
            this, SIGNAL(numOfChannelsChanged(unsigned)));
 
    connect(reader, SIGNAL(samplesPerSecondChanged(unsigned)),
 
            this, SIGNAL(samplesPerSecondChanged(unsigned)));
 

	
 
    // switch the settings widget
 
    ui->horizontalLayout->removeWidget(currentReader->settingsWidget());
 
    currentReader->settingsWidget()->hide();
 
    ui->horizontalLayout->addWidget(reader->settingsWidget(), 1);
 
    reader->settingsWidget()->show();
src/dataformatpanel.h
Show inline comments
 
/*
 
  Copyright © 2015 Hasan Yavuz Özderya
 
  Copyright © 2016 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
 
@@ -44,26 +44,20 @@ public:
 
    explicit DataFormatPanel(QSerialPort* port,
 
                             ChannelManager* channelMan,
 
                             QWidget *parent = 0);
 
    ~DataFormatPanel();
 

	
 
    unsigned numOfChannels();
 
    unsigned samplesPerSecond();
 
    bool skipByteEnabled(void); // true for binary formats
 

	
 
public slots:
 
    // during next read operation reader will skip 1 byte,
 
    // requests are not accumulated
 
    void requestSkipByte();
 
    void pause(bool);
 
    void enableDemo(bool); // demo shouldn't be enabled when port is open
 

	
 
signals:
 
    void numOfChannelsChanged(unsigned);
 
    void samplesPerSecondChanged(unsigned);
 
    void skipByteEnabledChanged(bool); // remove
 
    void dataAdded();
 

	
 
private:
 
    Ui::DataFormatPanel *ui;
 

	
 
    QSerialPort* serialPort;
 
@@ -73,27 +67,20 @@ private:
 
    AsciiReader asciiReader;
 
    /// Currently selected reader
 
    AbstractReader* currentReader;
 
    /// Disable current reader and enable a another one
 
    void selectReader(AbstractReader* reader);
 

	
 
    bool skipByteRequested; // remove
 
    bool paused; // remove
 

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

	
 
    // demo
 
    QTimer demoTimer;
 
    int demoCount;
 

	
 
    // `data` contains i th channels data
 
    void addChannelData(unsigned int channel, double* data, unsigned size);
 

	
 
private slots:
 
    void spsTimerTimeout();
 
    void demoTimerTimeout();
 
};
 

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