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
 
@@ -33,24 +33,35 @@ MainWindow::MainWindow(QWidget *parent) 
 
    QObject::connect(ui->spNumOfSamples, SELECT<int>::OVERLOAD_OF(&QSpinBox::valueChanged),
 
                     this, &MainWindow::onNumOfSamplesChanged);
 

	
 
    QObject::connect(ui->cbAutoScale, &QCheckBox::toggled,
 
                     this, &MainWindow::onAutoScaleChecked);
 

	
 
    QObject::connect(ui->spYmin, SIGNAL(valueChanged(double)),
 
                     this, SLOT(onYScaleChanged()));
 

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

	
 
    QObject::connect(&(this->serialPort), &QSerialPort::readyRead,
 
                     this, &MainWindow::onDataReady);
 

	
 
    loadPortList();
 
    loadBaudRateList();
 
    ui->cbBaudRate->setCurrentIndex(ui->cbBaudRate->findText("9600"));
 

	
 
    // set limits for axis limit boxes
 
@@ -61,24 +72,34 @@ MainWindow::MainWindow(QWidget *parent) 
 
                         std::numeric_limits<double>::max());
 

	
 
    // init plot
 
    numOfSamples = ui->spNumOfSamples->value();
 
    dataArray.resize(numOfSamples);
 
    dataX.resize(numOfSamples);
 
    for (int i = 0; i < dataX.size(); i++)
 
    {
 
        dataX[i] = i;
 
    }
 
    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()
 
{
 
    if (serialPort.isOpen())
 
    {
 
        serialPort.close();
 
    }
 
    delete ui;
 
}
 

	
 
void MainWindow::loadPortList()
 
@@ -178,26 +199,42 @@ void MainWindow::selectBaudRate(QString 
 
    }
 
}
 

	
 
void MainWindow::onPortToggled(bool open)
 
{
 
    ui->pbOpenPort->setChecked(open);
 
}
 

	
 
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]);
 
        }
 
    }
 
}
 

	
 
void MainWindow::onPortError(QSerialPort::SerialPortError error)
 
{
 
    switch(error)
 
    {
 
        case QSerialPort::NoError :
 
            break;
 
        case QSerialPort::ResourceError :
 
            qDebug() << "Port error: resource unavaliable; most likely device removed.";
 
            if (serialPort.isOpen())
 
@@ -268,12 +305,57 @@ void MainWindow::onAutoScaleChecked(bool
 
        ui->spYmax->setEnabled(true);
 

	
 
        ui->plot->setAxisScale(QwtPlot::yLeft, ui->spYmin->value(),
 
                               ui->spYmax->value());
 
    }
 
}
 

	
 
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
 
#ifndef MAINWINDOW_H
 
#define MAINWINDOW_H
 

	
 
#include <QMainWindow>
 
#include <QButtonGroup>
 
#include <QString>
 
#include <QVector>
 
#include <QSerialPort>
 
#include <qwt_plot_curve.h>
 

	
 
namespace Ui {
 
class MainWindow;
 
}
 

	
 
class MainWindow : public QMainWindow
 
{
 
    Q_OBJECT
 

	
 
public:
 
    explicit MainWindow(QWidget *parent = 0);
 
    ~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;
 
    QwtPlotCurve curve;
 
    QVector<double> dataArray;
 
    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();
 
    void togglePort();
 
    void selectPort(QString portName);
 
    void onPortToggled(bool open);
 
    void selectBaudRate(QString baudRate);
 

	
 
    void onDataReady();
 
    void onPortError(QSerialPort::SerialPortError error);
 

	
 
    void onNumOfSamplesChanged(int value);
 
    void onAutoScaleChecked(bool checked);
 
    void onYScaleChanged();
 

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

	
 
signals:
 
    void portToggled(bool open);
 
};
 

	
 
#endif // MAINWINDOW_H
mainwindow.ui
Show inline comments
 
@@ -333,24 +333,27 @@
 
       <layout class="QHBoxLayout" name="horizontalLayout_5">
 
        <item>
 
         <widget class="QGroupBox" name="groupBox">
 
          <property name="title">
 
           <string>Number Format:</string>
 
          </property>
 
          <layout class="QGridLayout" name="gridLayout_2">
 
           <item row="0" column="0">
 
            <widget class="QRadioButton" name="rbUint8">
 
             <property name="text">
 
              <string>uint8</string>
 
             </property>
 
             <property name="checked">
 
              <bool>true</bool>
 
             </property>
 
            </widget>
 
           </item>
 
           <item row="2" column="0">
 
            <widget class="QRadioButton" name="rbUint32">
 
             <property name="text">
 
              <string>uint32</string>
 
             </property>
 
            </widget>
 
           </item>
 
           <item row="1" column="0">
 
            <widget class="QRadioButton" name="rbUint16">
 
             <property name="text">
 
@@ -527,25 +530,25 @@
 
       </layout>
 
      </widget>
 
     </widget>
 
    </item>
 
   </layout>
 
  </widget>
 
  <widget class="QMenuBar" name="menuBar">
 
   <property name="geometry">
 
    <rect>
 
     <x>0</x>
 
     <y>0</y>
 
     <width>653</width>
 
     <height>23</height>
 
     <height>20</height>
 
    </rect>
 
   </property>
 
   <widget class="QMenu" name="menuFile">
 
    <property name="title">
 
     <string>File</string>
 
    </property>
 
    <addaction name="actionFileLoadProfile"/>
 
    <addaction name="actionFileSaveProfile"/>
 
   </widget>
 
   <widget class="QMenu" name="menuHelp">
 
    <property name="title">
 
     <string>Help</string>
0 comments (0 inline, 0 general)