Changeset - 649401566a84
[Not reviewed]
default
0 3 0
Hasan Yavuz Ă–ZDERYA - 11 years ago 2015-03-16 16:05:34
hy@ozderya.net
implemented number format selection, currently supporting integer formats
3 files changed with 111 insertions and 3 deletions:
0 comments (0 inline, 0 general)
mainwindow.cpp
Show inline comments
 
@@ -42,6 +42,17 @@ MainWindow::MainWindow(QWidget *parent) 
 
    QObject::connect(ui->spYmax, SIGNAL(valueChanged(double)),
 
                     this, SLOT(onYScaleChanged()));
 

	
 
    // setup number format buttons
 
    numberFormatButtons.addButton(ui->rbUint8,  NumberFormat_uint8);
 
    numberFormatButtons.addButton(ui->rbUint16, NumberFormat_uint16);
 
    numberFormatButtons.addButton(ui->rbUint32, NumberFormat_uint32);
 
    numberFormatButtons.addButton(ui->rbInt8,   NumberFormat_int8);
 
    numberFormatButtons.addButton(ui->rbInt16,  NumberFormat_int16);
 
    numberFormatButtons.addButton(ui->rbInt32,  NumberFormat_int32);
 

	
 
    QObject::connect(&numberFormatButtons, SIGNAL(buttonToggled(int, bool)),
 
                     this, SLOT(onNumberFormatButtonToggled(int, bool)));
 

	
 
    // init port signals
 
    QObject::connect(&(this->serialPort), SIGNAL(error(QSerialPort::SerialPortError)),
 
                     this, SLOT(onPortError(QSerialPort::SerialPortError)));
 
@@ -70,6 +81,16 @@ MainWindow::MainWindow(QWidget *parent) 
 
    }
 
    curve.setSamples(dataX, dataArray);
 
    curve.attach(ui->plot);
 

	
 
    // init number format
 
    if (numberFormatButtons.checkedId() >= 0)
 
    {
 
        selectNumberFormat((NumberFormat) numberFormatButtons.checkedId());
 
    }
 
    else
 
    {
 
        selectNumberFormat(NumberFormat_uint8);
 
    }
 
}
 

	
 
MainWindow::~MainWindow()
 
@@ -187,8 +208,24 @@ void MainWindow::onDataReady()
 
{
 
    if (!ui->actionPause->isChecked())
 
    {
 
        QByteArray data = serialPort.readAll();
 
        addData((unsigned char)(data[0]));
 
        int bytesAvailable = serialPort.bytesAvailable();
 
        if (bytesAvailable < sampleSize)
 
        {
 
            return;
 
        }
 
        else
 
        {
 
            int numOfSamplesToRead =
 
                (bytesAvailable - (bytesAvailable % sampleSize)) / sampleSize;
 
            QVector<double> samples(numOfSamplesToRead);
 
            int i = 0;
 
            while(i < numOfSamplesToRead)
 
            {
 
                samples.replace(i, (this->*readSample)());
 
                i++;
 
            }
 
            addData(samples[0]);
 
        }
 
    }
 
}
 

	
 
@@ -277,3 +314,48 @@ void MainWindow::onYScaleChanged()
 
    ui->plot->setAxisScale(QwtPlot::yLeft, ui->spYmin->value(),
 
                           ui->spYmax->value());
 
}
 

	
 
void MainWindow::onNumberFormatButtonToggled(int numberFormatId, bool checked)
 
{
 
    if (checked) selectNumberFormat((NumberFormat) numberFormatId);
 
}
 

	
 
void MainWindow::selectNumberFormat(NumberFormat numberFormatId)
 
{
 
    numberFormat = numberFormatId;
 

	
 
    switch(numberFormat)
 
    {
 
        case NumberFormat_uint8:
 
            sampleSize = 1;
 
            readSample = &MainWindow::readSampleAs<quint8>;
 
            break;
 
        case NumberFormat_int8:
 
            sampleSize = 1;
 
            readSample = &MainWindow::readSampleAs<qint8>;
 
            break;
 
        case NumberFormat_uint16:
 
            sampleSize = 2;
 
            readSample = &MainWindow::readSampleAs<quint16>;
 
            break;
 
        case NumberFormat_int16:
 
            sampleSize = 2;
 
            readSample = &MainWindow::readSampleAs<qint16>;
 
            break;
 
        case NumberFormat_uint32:
 
            sampleSize = 4;
 
            readSample = &MainWindow::readSampleAs<quint32>;
 
            break;
 
        case NumberFormat_int32:
 
            sampleSize = 4;
 
            readSample = &MainWindow::readSampleAs<qint32>;
 
            break;
 
    }
 
}
 

	
 
template<typename T> double MainWindow::readSampleAs()
 
{
 
    T data;
 
    this->serialPort.read((char*) &data, sizeof(data));
 
    return double(data);
 
}
mainwindow.h
Show inline comments
 
@@ -2,6 +2,7 @@
 
#define MAINWINDOW_H
 

	
 
#include <QMainWindow>
 
#include <QButtonGroup>
 
#include <QString>
 
#include <QVector>
 
#include <QSerialPort>
 
@@ -20,7 +21,19 @@ public:
 
    ~MainWindow();
 

	
 
private:
 
    enum NumberFormat
 
    {
 
        NumberFormat_uint8,
 
        NumberFormat_uint16,
 
        NumberFormat_uint32,
 
        NumberFormat_int8,
 
        NumberFormat_int16,
 
        NumberFormat_int32
 
    };
 

	
 
    Ui::MainWindow *ui;
 
    QButtonGroup numberFormatButtons;
 

	
 
    QSerialPort serialPort;
 

	
 
    unsigned int numOfSamples;
 
@@ -29,6 +42,13 @@ private:
 
    QVector<double> dataX;
 
    void addData(double data);
 

	
 
    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();
 

	
 
private slots:
 
    void loadPortList();
 
    void loadBaudRateList();
 
@@ -44,6 +64,9 @@ private slots:
 
    void onAutoScaleChecked(bool checked);
 
    void onYScaleChanged();
 

	
 
    void onNumberFormatButtonToggled(int numberFormatId, bool checked);
 
    void selectNumberFormat(NumberFormat numberFormatId);
 

	
 
signals:
 
    void portToggled(bool open);
 
};
mainwindow.ui
Show inline comments
 
@@ -342,6 +342,9 @@
 
             <property name="text">
 
              <string>uint8</string>
 
             </property>
 
             <property name="checked">
 
              <bool>true</bool>
 
             </property>
 
            </widget>
 
           </item>
 
           <item row="2" column="0">
 
@@ -536,7 +539,7 @@
 
     <x>0</x>
 
     <y>0</y>
 
     <width>653</width>
 
     <height>23</height>
 
     <height>20</height>
 
    </rect>
 
   </property>
 
   <widget class="QMenu" name="menuFile">
0 comments (0 inline, 0 general)