Changeset - 8bc730cfda45
[Not reviewed]
default
0 4 0
Hasan Yavuz Ă–ZDERYA - 10 years ago 2015-08-15 13:17:08
hy@ozderya.net
use simple double array instead of QVector for FrameBuffer interface
4 files changed with 17 insertions and 18 deletions:
0 comments (0 inline, 0 general)
framebuffer.cpp
Show inline comments
 
@@ -53,27 +53,27 @@ void FrameBuffer::resize(size_t size)
 
        {
 
            newData[i] = 0;
 
        }
 
    }
 

	
 
    // data is ready, clean and re-point
 
    delete data;
 
    data = newData;
 
    headIndex = 0;
 
    _size = size;
 
}
 

	
 
void FrameBuffer::addSamples(QVector<double> samples)
 
void FrameBuffer::addSamples(double* samples, size_t size)
 
{
 
    unsigned shift = samples.size();
 
    unsigned shift = size;
 
    if (shift < _size)
 
    {
 
        unsigned x = _size - headIndex; // distance of `head` to end
 

	
 
        if (shift <= x) // there is enough room at the end of array
 
        {
 
            for (size_t i = 0; i < shift; i++)
 
            {
 
                data[i+headIndex] = samples[i];
 
            }
 

	
 
            if (shift == x) // we used all the room at the end
framebuffer.h
Show inline comments
 
@@ -22,25 +22,25 @@
 

	
 
#include <qwt_series_data.h>
 
#include <QPointF>
 
#include <QRectF>
 

	
 
class FrameBuffer : public QwtSeriesData<QPointF>
 
{
 
public:
 
    FrameBuffer(size_t size);
 
    ~FrameBuffer();
 

	
 
    void resize(size_t size);
 
    void addSamples(QVector<double> samples);
 
    void addSamples(double* samples, size_t size);
 
    void clear(); // fill 0
 

	
 
    // QwtSeriesData implementations
 
    size_t size() const;
 
    QPointF sample(size_t i) const;
 
    QRectF boundingRect() const;
 

	
 
private:
 
    size_t _size; // size of `data`
 
    double* data;
 
    size_t headIndex; // indicates the actual `0` index of the ring buffer
 

	
mainwindow.cpp
Show inline comments
 
@@ -246,44 +246,45 @@ void MainWindow::onDataReady()
 

	
 
    if (bytesAvailable > 0 && skipByteRequested)
 
    {
 
        serialPort.read(1);
 
        skipByteRequested = false;
 
        bytesAvailable--;
 
    }
 

	
 
    if (bytesAvailable < packageSize) return;
 

	
 
    int numOfPackagesToRead =
 
        (bytesAvailable - (bytesAvailable % packageSize)) / packageSize;
 
    QVector<DataArray> channelSamples(numOfChannels);
 
    for (unsigned int ci = 0; ci < numOfChannels; ci++)
 
    {
 
        channelSamples[ci].resize(numOfPackagesToRead);
 
    }
 
    double* channelSamples = new double[numOfPackagesToRead*numOfChannels];
 

	
 
    int i = 0;
 
    while(i < numOfPackagesToRead)
 
    {
 
        for (unsigned int ci = 0; ci < numOfChannels; ci++)
 
        {
 
            channelSamples[ci].replace(i, (this->*readSample)());
 
            // channelSamples[ci].replace(i, (this->*readSample)());
 
            channelSamples[ci*numOfPackagesToRead+i] = (this->*readSample)();
 
        }
 
        i++;
 
    }
 

	
 
    for (unsigned int ci = 0; ci < numOfChannels; ci++)
 
    {
 
        addChannelData(ci, channelSamples[ci]);
 
        addChannelData(ci,
 
                       channelSamples + ci*numOfPackagesToRead,
 
                       numOfPackagesToRead);
 
    }
 

	
 
    delete channelSamples;
 
}
 

	
 
void MainWindow::onDataReadyASCII()
 
{
 
    while(serialPort.canReadLine())
 
    {
 
        QByteArray line = serialPort.readLine();
 

	
 
        // discard data if paused
 
        if (ui->actionPause->isChecked())
 
        {
 
            return;
 
@@ -301,25 +302,25 @@ void MainWindow::onDataReadyASCII()
 
        {
 
            numReadChannels = separatedValues.length();
 
            qWarning() << "Incoming data is missing data for some channels!";
 
        }
 

	
 
        // parse read line
 
        for (int ci = 0; ci < numReadChannels; ci++)
 
        {
 
            bool ok;
 
            double channelSample = separatedValues[ci].toDouble(&ok);
 
            if (ok)
 
            {
 
                addChannelData(ci, DataArray({channelSample}));
 
                addChannelData(ci, &channelSample, 1);
 
            }
 
            else
 
            {
 
                qWarning() << "Data parsing error for channel: " << ci;
 
            }
 
        }
 
    }
 
}
 

	
 
void MainWindow::onPortError(QSerialPort::SerialPortError error)
 
{
 
    switch(error)
 
@@ -374,29 +375,29 @@ required privileges or device is already
 
            break;
 
        default:
 
            qCritical() << "Unhandled port error: " << error;
 
            break;
 
    }
 
}
 

	
 
void MainWindow::skipByte()
 
{
 
    skipByteRequested = true;
 
}
 

	
 
void MainWindow::addChannelData(unsigned int channel, DataArray data)
 
void MainWindow::addChannelData(unsigned int channel, double* data, unsigned size)
 
{
 
    channelBuffers[channel]->addSamples(data);
 
    channelBuffers[channel]->addSamples(data, size);
 
    ui->plot->replot(); // TODO: replot after all channel data updated
 
    sampleCount += data.size();
 
    sampleCount += size;
 
}
 

	
 
void MainWindow::clearPlot()
 
{
 
    for (unsigned int ci = 0; ci < numOfChannels; ci++)
 
    {
 
        channelBuffers[ci]->clear();
 
    }
 
    ui->plot->replot();
 
}
 

	
 
void MainWindow::onNumOfSamplesChanged(int value)
 
@@ -556,29 +557,27 @@ void MainWindow::spsTimerTimeout()
 
}
 

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

	
 
    if (!ui->actionPause->isChecked())
 
    {
 
        for (unsigned int ci = 0; ci < numOfChannels; ci++)
 
        {
 
            DataArray data(1);
 
            // we are calculating the fourier components of square wave
 
            double value = 4*sin(2*M_PI*double((ci+1)*demoCount)/period)/((2*(ci+1))*M_PI);
 
            data.replace(0, value);
 
            addChannelData(ci, data);
 
            addChannelData(ci, &value, 1);
 
        }
 
    }
 
}
 

	
 
void MainWindow::enableDemo(bool enabled)
 
{
 
    if (enabled)
 
    {
 
        if (!serialPort.isOpen())
 
        {
 
            demoTimer.start();
 
            ui->actionDemoMode->setChecked(true);
mainwindow.h
Show inline comments
 
@@ -75,25 +75,25 @@ private:
 
    QSerialPort serialPort;
 
    PortControl portControl;
 

	
 
    unsigned int numOfSamples;
 
    unsigned int numOfChannels;
 

	
 
    QList<QwtPlotCurve*> curves;
 
    typedef QVector<double> DataArray;
 
    // Note: FrameBuffer s are owned by their respective QwtPlotCurve s.
 
    QList<FrameBuffer*> channelBuffers;
 

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

	
 
    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;
0 comments (0 inline, 0 general)