Changeset - 3d321162263e
[Not reviewed]
default
0 5 2
Hasan Yavuz ÖZDERYA - 10 years ago 2015-08-13 05:05:07
hy@ozderya.net
moved data storage related code to a new class FrameBuffer
7 files changed with 200 insertions and 86 deletions:
0 comments (0 inline, 0 general)
CMakeLists.txt
Show inline comments
 
@@ -62,6 +62,7 @@ add_executable(${PROGRAM_NAME} WIN32
 
  plot.cpp
 
  zoomer.cpp
 
  hidabletabwidget.cpp
 
  framebuffer.cpp
 
  ${UI_FILES}
 
  misc/windows_icon.rc
 
  )
framebuffer.cpp
Show inline comments
 
new file 100644
 
/*
 
  Copyright © 2015 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 "framebuffer.h"
 

	
 
FrameBuffer::FrameBuffer(size_t size) :
 
    data(size, 0.)
 
{
 

	
 
}
 

	
 
void FrameBuffer::resize(size_t size)
 
{
 
    size_t old_size = this->size();
 
    size_t new_size = size;
 

	
 
    if (new_size < old_size)
 
    {
 
        data.remove(0, old_size - new_size);
 
    }
 
    else if (new_size > old_size)
 
    {
 
        // This is seriously inefficient!
 
        for (size_t i = old_size; i < new_size; i++)
 
        {
 
            data.prepend(0);
 
        }
 
    }
 
}
 

	
 
void FrameBuffer::addSamples(QVector<double> samples)
 
{
 
    int offset = size() - samples.size();
 

	
 
    if (offset < 0)
 
    {
 
        // new samples exceed the size of frame buffer
 
        // excess part (from beginning) of the input will be ignored
 
        for (unsigned int i = 0; i < size(); i++)
 
        {
 
            data[i] = samples[i - offset];
 
        }
 
    }
 
    else if (offset == 0) // input is the same size as the framebuffer
 
    {
 
        data = samples;
 
    }
 
    else // regular case; input is smaller than framebuffer
 
    {
 
        // shift old samples
 
        int shift = samples.size();
 
        for (int i = 0; i < offset; i++)
 
        {
 
            data[i] = data[i + shift];
 
        }
 
        // place new samples
 
        for (int i = 0; i < samples.size(); i++)
 
        {
 
            data[offset + i] = samples[i];
 
        }
 
    }
 
}
 

	
 
void FrameBuffer::clear()
 
{
 
    data.fill(0);
 
}
 

	
 
size_t FrameBuffer::size() const
 
{
 
    return (size_t) data.size();
 
}
 

	
 
QPointF FrameBuffer::sample(size_t i) const
 
{
 
    return QPointF(i, data[i]);
 
}
 

	
 
QRectF FrameBuffer::boundingRect() const
 
{
 
    return qwtBoundingRect(*this);
 
}
framebuffer.h
Show inline comments
 
new file 100644
 
/*
 
  Copyright © 2015 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 FRAMEBUFFER_H
 
#define FRAMEBUFFER_H
 

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

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

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

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

	
 
private:
 
    QVector<double> data;
 
};
 

	
 
#endif // FRAMEBUFFER_H
main.cpp
Show inline comments
 
@@ -23,6 +23,12 @@
 

	
 
#include "version.h"
 

	
 
// TEST CODE
 
#include "framebuffer.h"
 
// #include <QtDebug>
 
#include <qwt_plot.h>
 
#include <qwt_plot_curve.h>
 

	
 
MainWindow* pMainWindow;
 

	
 
void messageHandler(QtMsgType type, const QMessageLogContext &context,
 
@@ -45,5 +51,33 @@ int main(int argc, char *argv[])
 
    qDebug() << "Revision" << VERSION_REVISION;
 

	
 
    w.show();
 

	
 
    // test framebuffer
 
    FrameBuffer buffer(10);
 
    QVector<double> s(3,15.);
 
    s.append(1.0);
 
    s.append(2.0);
 
    s.append(3.0);
 
    buffer.addSamples(s);
 
    // buffer.addSamples(s);
 
    // buffer.resize(5);
 
    QwtPlot plot;
 
    QwtPlotCurve curve;
 
    curve.setSamples(&buffer);
 
    curve.attach(&plot);
 
    plot.show();
 
    qDebug() << buffer.sample(0);
 
    qDebug() << buffer.sample(1);
 
    qDebug() << "size:" << buffer.size();
 
    buffer.addSamples(s);
 
    // curve.setSamples(&buffer);
 
    plot.replot();
 
    // qDebug() << buffer.sample(5);
 
    // qDebug() << buffer.sample(7);
 
    // qDebug() << buffer.sample(8);
 
    // qDebug() << buffer.sample(9);
 
    // qDebug() << buffer.sample(15);
 
    // qDebug() << buffer.sample(19);
 

	
 
    return a.exec();
 
}
mainwindow.cpp
Show inline comments
 
@@ -137,18 +137,12 @@ MainWindow::MainWindow(QWidget *parent) 
 
    numOfSamples = ui->spNumOfSamples->value();
 
    numOfChannels = 1;
 

	
 
    dataX.resize(numOfSamples);
 
    for (int i = 0; i < dataX.size(); i++)
 
    {
 
        dataX[i] = i;
 
    }
 

	
 
    // init channel data and curve list
 
    for (unsigned int i = 0; i < numOfChannels; i++)
 
    {
 
        channelsData.append(DataArray(numOfSamples, 0.0));
 
        channelBuffers.append(new FrameBuffer(numOfSamples));
 
        curves.append(new QwtPlotCurve());
 
        curves[i]->setSamples(dataX, channelsData[i]);
 
        curves[i]->setSamples(channelBuffers[i]);
 
        curves[i]->setPen(makeColor(i));
 
        curves[i]->attach(ui->plot);
 
    }
 
@@ -205,6 +199,7 @@ MainWindow::~MainWindow()
 
{
 
    for (auto curve : curves)
 
    {
 
        // also deletes respective FrameBuffer
 
        delete curve;
 
    }
 

	
 
@@ -396,39 +391,8 @@ void MainWindow::skipByte()
 

	
 
void MainWindow::addChannelData(unsigned int channel, DataArray data)
 
{
 
    DataArray* channelDataArray = &(channelsData[channel]);
 
    int offset = numOfSamples - data.size();
 

	
 
    if (offset < 0)
 
    {
 
        for (unsigned int i = 0; i < numOfSamples; i++)
 
        {
 
            (*channelDataArray)[i] = data[i - offset];
 
        }
 
    }
 
    else if (offset == 0)
 
    {
 
        (*channelDataArray) = data;
 
    }
 
    else
 
    {
 
        // shift old samples
 
        int shift = data.size();
 
        for (int i = 0; i < offset; i++)
 
        {
 
            (*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));
 
    channelBuffers[channel]->addSamples(data);
 
    ui->plot->replot(); // TODO: replot after all channel data updated
 

	
 
    sampleCount += data.size();
 
}
 

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

	
 
    // update plot
 
    ui->plot->replot();
 
}
 

	
 
void MainWindow::onNumOfSamplesChanged(int value)
 
{
 
    unsigned int oldNum = this->numOfSamples;
 
    numOfSamples = value;
 

	
 
    // resize data arrays
 
    if (numOfSamples < oldNum)
 
    {
 
        dataX.resize(numOfSamples);
 
        for (unsigned int ci = 0; ci < numOfChannels; ci++)
 
        {
 
            channelsData[ci].remove(0, oldNum - numOfSamples);
 
            curves[ci]->setSamples(dataX, channelsData[ci]);
 
        }
 
        ui->plot->replot();
 
    }
 
    else if(numOfSamples > oldNum)
 
    for (unsigned int ci = 0; ci < numOfChannels; ci++)
 
    {
 
        // update data arrays
 
        dataX.resize(numOfSamples);
 
        for (unsigned int i = oldNum; i < numOfSamples; i++)
 
        {
 
            dataX[i] = i;
 
            for (unsigned int ci = 0; ci < numOfChannels; ci++)
 
            {
 
                // TODO: opportunity of major optimization here
 
                //       let's hope nobody sees this
 
                channelsData[ci].prepend(0);
 
            }
 
        }
 
        // update curves
 
        for (unsigned int ci = 0; ci < numOfChannels; ci++)
 
        {
 
            curves[ci]->setSamples(dataX, channelsData[ci]);
 
        }
 
        ui->plot->replot();
 
        channelBuffers[ci]->resize(numOfSamples);
 
    }
 

	
 
    ui->plot->replot();
 
}
 

	
 
void MainWindow::onNumOfChannelsChanged(int value)
 
@@ -493,9 +427,9 @@ void MainWindow::onNumOfChannelsChanged(
 
        // add new channels
 
        for (unsigned int i = 0; i < numOfChannels - oldNum; i++)
 
        {
 
            channelsData.append(DataArray(numOfSamples, 0.0));
 
            channelBuffers.append(new FrameBuffer(numOfSamples));
 
            curves.append(new QwtPlotCurve());
 
            curves.last()->setSamples(dataX, channelsData.last());
 
            curves.last()->setSamples(channelBuffers.last());
 
            curves.last()->setPen(makeColor(curves.length()-1));
 
            curves.last()->attach(ui->plot);
 
        }
 
@@ -505,10 +439,9 @@ void MainWindow::onNumOfChannelsChanged(
 
        // remove channels
 
        for (unsigned int i = 0; i < oldNum - numOfChannels; i++)
 
        {
 
            channelsData.removeLast();
 
            auto curve = curves.takeLast();
 
            curve->detach();
 
            delete curve;
 
            // also deletes owned FrameBuffer
 
            delete curves.takeLast();
 
            channelBuffers.removeLast();
 
        }
 
    }
 
}
 
@@ -732,7 +665,7 @@ void MainWindow::onExportCsv()
 
            {
 
                for (unsigned int ci = 0; ci < numOfChannels; ci++)
 
                {
 
                    fileStream << channelsData[ci][i];
 
                    fileStream << channelBuffers[ci]->sample(i).y();
 
                    if (ci != numOfChannels-1) fileStream << ",";
 
                }
 
                fileStream << '\n';
mainwindow.h
Show inline comments
 
@@ -36,6 +36,7 @@
 

	
 
#include "portcontrol.h"
 
#include "ui_about_dialog.h"
 
#include "framebuffer.h"
 

	
 
namespace Ui {
 
class MainWindow;
 
@@ -79,8 +80,8 @@ private:
 

	
 
    QList<QwtPlotCurve*> curves;
 
    typedef QVector<double> DataArray;
 
    DataArray dataX;   // array that simply contains numbers 0..numberOfSamples
 
    QList<DataArray> channelsData;
 
    // 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);
serialplot.pro
Show inline comments
 
@@ -39,7 +39,8 @@ SOURCES += main.cpp\
 
    portcontrol.cpp \
 
    plot.cpp \
 
    zoomer.cpp \
 
    hidabletabwidget.cpp
 
    hidabletabwidget.cpp \
 
    framebuffer.cpp
 

	
 
HEADERS  += mainwindow.h \
 
    utils.h \
 
@@ -48,7 +49,8 @@ HEADERS  += mainwindow.h \
 
    floatswap.h \
 
    plot.h \
 
    zoomer.h \
 
    hidabletabwidget.h
 
    hidabletabwidget.h \
 
    framebuffer.h
 

	
 
FORMS    += mainwindow.ui \
 
    about_dialog.ui \
0 comments (0 inline, 0 general)