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
 
@@ -103,12 +103,13 @@ add_executable(${PROGRAM_NAME} WIN32
 
  src/commandpanel.cpp
 
  src/commandwidget.cpp
 
  src/commandedit.cpp
 
  src/dataformatpanel.cpp
 
  src/plotcontrolpanel.cpp
 
  src/recordpanel.cpp
 
  src/datarecorder.cpp
 
  src/tooltipfilter.cpp
 
  src/sneakylineedit.cpp
 
  src/channelmanager.cpp
 
  src/channelinfomodel.cpp
 
  src/framebufferseries.cpp
 
  src/numberformatbox.cpp
src/datarecorder.cpp
Show inline comments
 
@@ -16,25 +16,70 @@
 
  You should have received a copy of the GNU General Public License
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#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
 
@@ -18,12 +18,14 @@
 
*/
 

	
 
#ifndef DATARECORDER_H
 
#define DATARECORDER_H
 

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

	
 
class DataRecorder : public QObject
 
{
 
    Q_OBJECT
 
public:
 
    explicit DataRecorder(QObject *parent = 0);
 
@@ -60,9 +62,11 @@ public:
 

	
 
    /// Stops recording, closes file.
 
    void stopRecording();
 

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

	
 
#endif // DATARECORDER_H
src/recordpanel.cpp
Show inline comments
 
@@ -30,13 +30,14 @@
 
#include <QtDebug>
 

	
 
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;
 

	
 
    ui->setupUi(this);
 

	
 
    recordToolBar.setObjectName("tbRecord");
 
@@ -193,15 +194,25 @@ bool RecordPanel::confirmOverwrite(QStri
 
        return selectFile();
 
    }
 
}
 

	
 
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
 
@@ -22,12 +22,14 @@
 

	
 
#include <QWidget>
 
#include <QString>
 
#include <QToolBar>
 
#include <QAction>
 

	
 
#include "datarecorder.h"
 

	
 
namespace Ui {
 
class RecordPanel;
 
}
 

	
 
class RecordPanel : public QWidget
 
{
 
@@ -42,12 +44,13 @@ public:
 
private:
 
    Ui::RecordPanel *ui;
 
    QToolBar recordToolBar;
 
    QAction recordAction;
 
    QString selectedFile;
 
    bool overwriteSelected;
 
    DataRecorder recorder;
 

	
 
    /**
 
     * @brief Increments the file name.
 
     *
 
     * If file name doesn't have a number at the end of it, a number is appended
 
     * with underscore starting from 1.
0 comments (0 inline, 0 general)