Changeset - e65811fa0522
[Not reviewed]
default
0 3 0
Hasan Yavuz Ă–ZDERYA - 11 years ago 2015-04-19 06:57:00
hy@ozderya.net
added number of channels selection
3 files changed with 74 insertions and 2 deletions:
0 comments (0 inline, 0 general)
mainwindow.cpp
Show inline comments
 
@@ -27,96 +27,101 @@
 
#include "utils.h"
 

	
 
MainWindow::MainWindow(QWidget *parent) :
 
    QMainWindow(parent),
 
    ui(new Ui::MainWindow)
 
{
 
    ui->setupUi(this);
 
    setupAboutDialog();
 

	
 
    // init UI signals
 

	
 
    // menu signals
 
    QObject::connect(ui->actionHelpAbout, &QAction::triggered,
 
              &aboutDialog, &QWidget::show);
 

	
 
    // port tab signals
 
    QObject::connect(ui->pbReloadPorts, &QPushButton::clicked,
 
                     this, &MainWindow::loadPortList);
 

	
 
    QObject::connect(ui->pbOpenPort, &QPushButton::clicked,
 
                     this, &MainWindow::togglePort);
 

	
 
    QObject::connect(this, &MainWindow::portToggled,
 
                     this, &MainWindow::onPortToggled);
 

	
 
    QObject::connect(ui->cbPortList,
 
                     SELECT<const QString&>::OVERLOAD_OF(&QComboBox::activated),
 
                     this, &MainWindow::selectPort);
 

	
 
    QObject::connect(ui->cbBaudRate,
 
                     SELECT<const QString&>::OVERLOAD_OF(&QComboBox::activated),
 
                     this, &MainWindow::selectBaudRate);
 

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

	
 
    QObject::connect(ui->actionClear, SIGNAL(triggered(bool)),
 
                     this, SLOT(clearPlot()));
 

	
 
    // setup number of channels spinbox
 
    QObject::connect(ui->spNumOfChannels,
 
                     SELECT<int>::OVERLOAD_OF(&QSpinBox::valueChanged),
 
                     this, &MainWindow::onNumOfChannelsChanged);
 

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

	
 
    // setup parity selection buttons
 
    parityButtons.addButton(ui->rbNoParity, (int) QSerialPort::NoParity);
 
    parityButtons.addButton(ui->rbEvenParity, (int) QSerialPort::EvenParity);
 
    parityButtons.addButton(ui->rbOddParity, (int) QSerialPort::OddParity);
 

	
 
    QObject::connect(&parityButtons,
 
                     SELECT<int>::OVERLOAD_OF(&QButtonGroup::buttonClicked),
 
                     this, &MainWindow::selectParity);
 

	
 
    // setup data bits selection buttons
 
    dataBitsButtons.addButton(ui->rb8Bits, (int) QSerialPort::Data8);
 
    dataBitsButtons.addButton(ui->rb7Bits, (int) QSerialPort::Data7);
 
    dataBitsButtons.addButton(ui->rb6Bits, (int) QSerialPort::Data6);
 
    dataBitsButtons.addButton(ui->rb5Bits, (int) QSerialPort::Data5);
 

	
 
    QObject::connect(&dataBitsButtons,
 
                     SELECT<int>::OVERLOAD_OF(&QButtonGroup::buttonClicked),
 
                     this, &MainWindow::selectDataBits);
 

	
 
    // setup stop bits selection buttons
 
    stopBitsButtons.addButton(ui->rb1StopBit, (int) QSerialPort::OneStop);
 
    stopBitsButtons.addButton(ui->rb2StopBit, (int) QSerialPort::TwoStop);
 

	
 
    QObject::connect(&stopBitsButtons,
 
                     SELECT<int>::OVERLOAD_OF(&QButtonGroup::buttonClicked),
 
                     this, &MainWindow::selectStopBits);
 

	
 
    // setup flow control selection buttons
 
    flowControlButtons.addButton(ui->rbNoFlowControl,
 
                                 (int) QSerialPort::NoFlowControl);
 
    flowControlButtons.addButton(ui->rbHardwareControl,
 
                                 (int) QSerialPort::HardwareControl);
 
    flowControlButtons.addButton(ui->rbSoftwareControl,
 
                                 (int) QSerialPort::SoftwareControl);
 

	
 
    QObject::connect(&flowControlButtons,
 
                     SELECT<int>::OVERLOAD_OF(&QButtonGroup::buttonClicked),
 
@@ -454,96 +459,125 @@ void MainWindow::addChannelData(unsigned
 
        }
 
    }
 

	
 
    // update plot
 
    curves[channel]->setSamples(dataX, (*channelDataArray));
 
    ui->plot->replot(); // TODO: replot after all channel data updated
 
}
 

	
 
void MainWindow::clearPlot()
 
{
 
    for (int ci = 0; ci < numOfChannels; ci++)
 
    {
 
        channelsData[ci].fill(0.0);
 
        curves[ci]->setSamples(dataX, channelsData[ci]);
 
    }
 

	
 
    // 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 (int ci = 0; ci < numOfChannels; ci++)
 
        {
 
            channelsData[ci].remove(0, oldNum - numOfSamples);
 
        }
 
    }
 
    else if(numOfSamples > oldNum)
 
    {
 
        dataX.resize(numOfSamples);
 
        for (unsigned int i = oldNum; i < numOfSamples; i++)
 
        {
 
            dataX[i] = i;
 
            for (int ci = 0; ci < numOfChannels; ci++)
 
            {
 
                channelsData[ci].prepend(0);
 
            }
 
        }
 
    }
 
}
 

	
 
void MainWindow::onNumOfChannelsChanged(int value)
 
{
 
    unsigned int oldNum = this->numOfChannels;
 
    this->numOfChannels = value;
 

	
 
    if (numOfChannels > oldNum)
 
    {
 
        // add new channels
 
        for (int i = 0; i < numOfChannels - oldNum; i++)
 
        {
 
            channelsData.append(DataArray(numOfSamples, 0.0));
 
            curves.append(new QwtPlotCurve());
 
            curves.last()->setSamples(dataX, channelsData.last());
 
            curves.last()->attach(ui->plot);
 
        }
 
    }
 
    else if(numOfChannels < oldNum)
 
    {
 
        // remove channels
 
        for (int i = 0; i < oldNum - numOfChannels; i++)
 
        {
 
            channelsData.removeLast();
 
            auto curve = curves.takeLast();
 
            curve->detach();
 
            delete curve;
 
        }
 
    }
 
}
 

	
 
void MainWindow::onAutoScaleChecked(bool checked)
 
{
 
    if (checked)
 
    {
 
        ui->plot->setAxisAutoScale(QwtPlot::yLeft);
 
        ui->lYmin->setEnabled(false);
 
        ui->lYmax->setEnabled(false);
 
        ui->spYmin->setEnabled(false);
 
        ui->spYmax->setEnabled(false);
 
    }
 
    else
 
    {
 
        ui->lYmin->setEnabled(true);
 
        ui->lYmax->setEnabled(true);
 
        ui->spYmin->setEnabled(true);
 
        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:
mainwindow.h
Show inline comments
 
@@ -62,58 +62,59 @@ private:
 
    QButtonGroup flowControlButtons;
 

	
 
    QDialog aboutDialog;
 
    void setupAboutDialog();
 

	
 
    QSerialPort serialPort;
 

	
 
    unsigned int numOfSamples;
 
    unsigned int numOfChannels;
 

	
 
    QList<QwtPlotCurve*> curves;
 
    typedef QVector<double> DataArray;
 
    DataArray dataX;   // array that simply contains numbers 0..numberOfSamples
 
    QList<DataArray> channelsData;
 

	
 
    // `data` contains channel specific data
 
    void addChannelData(unsigned int channel, DataArray 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();
 

	
 
    bool skipByteRequested;
 

	
 
private slots:
 
    void loadPortList();
 
    void loadBaudRateList();
 
    void togglePort();
 
    void selectPort(QString portName);
 
    void onPortToggled(bool open);
 
    void selectBaudRate(QString baudRate);
 
    void selectParity(int parity); // parity must be one of QSerialPort::Parity
 
    void selectDataBits(int dataBits); // bits must be one of QSerialPort::DataBits
 
    void selectStopBits(int stopBits); // stopBits must be one of QSerialPort::StopBits
 
    void selectFlowControl(int flowControl); // flowControl must be one of QSerialPort::FlowControl
 

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

	
 
    void skipByte();
 

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

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

	
 
    void clearPlot();
 

	
 
signals:
 
    void portToggled(bool open);
 
};
 

	
 
#endif // MAINWINDOW_H
mainwindow.ui
Show inline comments
 
@@ -299,170 +299,207 @@
 
        <item>
 
         <layout class="QVBoxLayout" name="verticalLayout_4">
 
          <item>
 
           <widget class="CustomCheckableButton" name="pbOpenPort">
 
            <property name="minimumSize">
 
             <size>
 
              <width>0</width>
 
              <height>50</height>
 
             </size>
 
            </property>
 
            <property name="text">
 
             <string>Open</string>
 
            </property>
 
            <property name="checkable">
 
             <bool>true</bool>
 
            </property>
 
           </widget>
 
          </item>
 
          <item>
 
           <widget class="QPushButton" name="pbSkipByte">
 
            <property name="text">
 
             <string>Skip Byte</string>
 
            </property>
 
           </widget>
 
          </item>
 
          <item>
 
           <spacer name="verticalSpacer">
 
            <property name="orientation">
 
             <enum>Qt::Vertical</enum>
 
            </property>
 
            <property name="sizeHint" stdset="0">
 
             <size>
 
              <width>20</width>
 
              <height>40</height>
 
             </size>
 
            </property>
 
           </spacer>
 
          </item>
 
         </layout>
 
        </item>
 
       </layout>
 
      </widget>
 
      <widget class="QWidget" name="tabDataFormat">
 
       <attribute name="title">
 
        <string>Data Format</string>
 
       </attribute>
 
       <layout class="QHBoxLayout" name="horizontalLayout_5">
 
        <item>
 
         <layout class="QFormLayout" name="formLayout_3">
 
          <item row="0" column="0">
 
           <widget class="QLabel" name="label_4">
 
            <property name="text">
 
             <string>Number Of Channels:</string>
 
            </property>
 
           </widget>
 
          </item>
 
          <item row="0" column="1">
 
           <widget class="QSpinBox" name="spNumOfChannels">
 
            <property name="minimumSize">
 
             <size>
 
              <width>60</width>
 
              <height>0</height>
 
             </size>
 
            </property>
 
            <property name="minimum">
 
             <number>1</number>
 
            </property>
 
            <property name="maximum">
 
             <number>32</number>
 
            </property>
 
           </widget>
 
          </item>
 
         </layout>
 
        </item>
 
        <item>
 
         <widget class="Line" name="line_2">
 
          <property name="orientation">
 
           <enum>Qt::Vertical</enum>
 
          </property>
 
         </widget>
 
        </item>
 
        <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">
 
              <string>uint16</string>
 
             </property>
 
            </widget>
 
           </item>
 
           <item row="1" column="1">
 
            <widget class="QRadioButton" name="rbInt16">
 
             <property name="text">
 
              <string>int16</string>
 
             </property>
 
            </widget>
 
           </item>
 
           <item row="2" column="1">
 
            <widget class="QRadioButton" name="rbInt32">
 
             <property name="text">
 
              <string>int32</string>
 
             </property>
 
            </widget>
 
           </item>
 
           <item row="0" column="1">
 
            <widget class="QRadioButton" name="rbInt8">
 
             <property name="text">
 
              <string>int8</string>
 
             </property>
 
            </widget>
 
           </item>
 
           <item row="3" column="0">
 
            <spacer name="verticalSpacer_6">
 
             <property name="orientation">
 
              <enum>Qt::Vertical</enum>
 
             </property>
 
             <property name="sizeHint" stdset="0">
 
              <size>
 
               <width>20</width>
 
               <height>40</height>
 
              </size>
 
             </property>
 
            </spacer>
 
           </item>
 
          </layout>
 
         </widget>
 
        </item>
 
        <item>
 
         <spacer name="horizontalSpacer_3">
 
          <property name="orientation">
 
           <enum>Qt::Horizontal</enum>
 
          </property>
 
          <property name="sizeType">
 
           <enum>QSizePolicy::MinimumExpanding</enum>
 
          </property>
 
          <property name="sizeHint" stdset="0">
 
           <size>
 
            <width>445</width>
 
            <width>20</width>
 
            <height>20</height>
 
           </size>
 
          </property>
 
         </spacer>
 
        </item>
 
       </layout>
 
      </widget>
 
      <widget class="QWidget" name="tabPlot">
 
       <attribute name="title">
 
        <string>Plot</string>
 
       </attribute>
 
       <layout class="QHBoxLayout" name="horizontalLayout_4">
 
        <item>
 
         <spacer name="horizontalSpacer_2">
 
          <property name="orientation">
 
           <enum>Qt::Horizontal</enum>
 
          </property>
 
          <property name="sizeHint" stdset="0">
 
           <size>
 
            <width>370</width>
 
            <height>20</height>
 
           </size>
 
          </property>
 
         </spacer>
 
        </item>
 
        <item>
 
         <widget class="Line" name="line">
 
          <property name="orientation">
 
           <enum>Qt::Vertical</enum>
 
          </property>
 
         </widget>
 
        </item>
 
        <item>
 
         <layout class="QFormLayout" name="formLayout_2">
 
          <property name="fieldGrowthPolicy">
 
           <enum>QFormLayout::AllNonFixedFieldsGrow</enum>
 
          </property>
 
          <item row="3" column="1">
 
           <widget class="QDoubleSpinBox" name="spYmin">
 
            <property name="enabled">
 
             <bool>false</bool>
 
            </property>
 
            <property name="maximumSize">
 
             <size>
 
              <width>75</width>
 
              <height>16777215</height>
 
             </size>
 
            </property>
 
@@ -512,97 +549,97 @@
 
            <property name="text">
 
             <string>Ymin</string>
 
            </property>
 
           </widget>
 
          </item>
 
          <item row="2" column="0">
 
           <widget class="QLabel" name="lYmax">
 
            <property name="enabled">
 
             <bool>false</bool>
 
            </property>
 
            <property name="text">
 
             <string>Ymax</string>
 
            </property>
 
           </widget>
 
          </item>
 
          <item row="2" column="1">
 
           <widget class="QDoubleSpinBox" name="spYmax">
 
            <property name="enabled">
 
             <bool>false</bool>
 
            </property>
 
            <property name="maximumSize">
 
             <size>
 
              <width>75</width>
 
              <height>16777215</height>
 
             </size>
 
            </property>
 
            <property name="maximum">
 
             <double>1000.000000000000000</double>
 
            </property>
 
            <property name="value">
 
             <double>1000.000000000000000</double>
 
            </property>
 
           </widget>
 
          </item>
 
         </layout>
 
        </item>
 
       </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>20</height>
 
     <height>23</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>
 
    </property>
 
    <addaction name="actionHelpDataFormats"/>
 
    <addaction name="actionHelpAbout"/>
 
   </widget>
 
   <addaction name="menuFile"/>
 
   <addaction name="menuHelp"/>
 
  </widget>
 
  <widget class="QToolBar" name="mainToolBar">
 
   <attribute name="toolBarArea">
 
    <enum>TopToolBarArea</enum>
 
   </attribute>
 
   <attribute name="toolBarBreak">
 
    <bool>false</bool>
 
   </attribute>
 
   <addaction name="actionPause"/>
 
   <addaction name="actionClear"/>
 
  </widget>
 
  <widget class="QStatusBar" name="statusBar"/>
 
  <action name="actionPause">
 
   <property name="checkable">
 
    <bool>true</bool>
 
   </property>
 
   <property name="checked">
 
    <bool>false</bool>
 
   </property>
 
   <property name="text">
 
    <string>Pause</string>
 
   </property>
 
   <property name="toolTip">
 
    <string>Pause Plotting</string>
 
   </property>
 
  </action>
 
  <action name="actionClear">
 
   <property name="text">
 
    <string>Clear</string>
 
   </property>
0 comments (0 inline, 0 general)