Changeset - b44879156c7c
[Not reviewed]
recording
0 5 0
Hasan Yavuz Ă–ZDERYA - 9 years ago 2017-02-12 15:04:38
hy@ozderya.net
implemented data writing not yet integrated
5 files changed with 68 insertions and 4 deletions:
0 comments (0 inline, 0 general)
CMakeLists.txt
Show inline comments
 
@@ -106,6 +106,7 @@ add_executable(${PROGRAM_NAME} WIN32
 
  src/dataformatpanel.cpp
 
  src/plotcontrolpanel.cpp
 
  src/recordpanel.cpp
 
  src/datarecorder.cpp
 
  src/tooltipfilter.cpp
 
  src/sneakylineedit.cpp
 
  src/channelmanager.cpp
src/datarecorder.cpp
Show inline comments
 
@@ -19,22 +19,67 @@
 

	
 
#include "datarecorder.h"
 

	
 
DataRecorder::DataRecorder(QObject *parent) : QObject(parent)
 
#include <QtDebug>
 

	
 
DataRecorder::DataRecorder(QObject *parent) :
 
    QObject(parent),
 
    fileStream(&file)
 
{
 

	
 
    lastNumChannels = 0;
 
}
 

	
 
bool DataRecorder::startRecording(QString fileName, QStringList channelNames)
 
{
 
    Q_ASSERT(!file.isOpen());
 

	
 
    // open file
 
    file.setFileName(fileName);
 
    if (!file.open(QIODevice::WriteOnly))
 
    {
 
        qCritical() << "Opening file " << fileName
 
                    << " for recording failed with error: " << file.error();
 
        return false;
 
    }
 

	
 
    // write header line
 
    if (!channelNames.isEmpty())
 
    {
 
        fileStream << channelNames.join(",");
 
        fileStream << "\n";
 
        lastNumChannels = channelNames.length();
 
    }
 
    return true;
 
}
 

	
 
void DataRecorder::addData(double* data, unsigned length, unsigned numOfChannels)
 
{
 
    Q_ASSERT(length > 0);
 
    Q_ASSERT(length % numOfChannels == 0);
 

	
 
    if (lastNumChannels != 0 && numOfChannels != lastNumChannels)
 
    {
 
        qWarning() << "Number of channels changed from " << lastNumChannels
 
                   << " to " << numOfChannels <<
 
            " during recording, CSV file is corrupted but no data will be lost.";
 
    }
 
    lastNumChannels = numOfChannels;
 

	
 
    unsigned numOfSamples = length / numOfChannels; // per channel
 
    for (unsigned int i = 0; i < numOfSamples; i++)
 
    {
 
        for (unsigned ci = 0; ci < numOfChannels; ci++)
 
        {
 
            fileStream << data[ci * numOfSamples + i];
 
            if (ci != numOfChannels-1) fileStream << ",";
 
        }
 
        fileStream << '\n';
 
    }
 
}
 

	
 
void DataRecorder::stopRecording()
 
{
 
    Q_ASSERT(file.isOpen());
 

	
 
    file.close();
 
    lastNumChannels = 0;
 
}
src/datarecorder.h
Show inline comments
 
@@ -21,6 +21,8 @@
 
#define DATARECORDER_H
 

	
 
#include <QObject>
 
#include <QFile>
 
#include <QTextStream>
 

	
 
class DataRecorder : public QObject
 
{
 
@@ -63,6 +65,8 @@ public:
 

	
 
private:
 
    unsigned lastNumChannels;   ///< used for error message only
 
    QFile file;
 
    QTextStream fileStream;
 
};
 

	
 
#endif // DATARECORDER_H
src/recordpanel.cpp
Show inline comments
 
@@ -33,7 +33,8 @@ RecordPanel::RecordPanel(QWidget *parent
 
    QWidget(parent),
 
    ui(new Ui::RecordPanel),
 
    recordToolBar(tr("Record Toolbar")),
 
    recordAction(QIcon::fromTheme("media-record"), tr("Record"), this)
 
    recordAction(QIcon::fromTheme("media-record"), tr("Record"), this),
 
    recorder(this)
 
{
 
    overwriteSelected = false;
 

	
 
@@ -196,12 +197,22 @@ bool RecordPanel::confirmOverwrite(QStri
 

	
 
void RecordPanel::startRecording(void)
 
{
 
    // TODO
 
    qDebug() << "start recording";
 

	
 
    // TEST CODE
 
    QStringList cn;
 
    cn << "chan0" << "chan1" << "chan2";
 

	
 
    recorder.startRecording(selectedFile, cn);
 

	
 
    // add test data
 
    double data[3] = {15., 15., 15.};
 
    recorder.addData(data, 3, 3);
 
}
 

	
 
void RecordPanel::stopRecording(void)
 
{
 
    // TODO
 
    qDebug() << "stop recording";
 
    recorder.stopRecording();
 
}
src/recordpanel.h
Show inline comments
 
@@ -25,6 +25,8 @@
 
#include <QToolBar>
 
#include <QAction>
 

	
 
#include "datarecorder.h"
 

	
 
namespace Ui {
 
class RecordPanel;
 
}
 
@@ -45,6 +47,7 @@ private:
 
    QAction recordAction;
 
    QString selectedFile;
 
    bool overwriteSelected;
 
    DataRecorder recorder;
 

	
 
    /**
 
     * @brief Increments the file name.
0 comments (0 inline, 0 general)