Changeset - abcdecae08b4
[Not reviewed]
default
0 3 0
Hasan Yavuz Ă–ZDERYA - 10 years ago 2015-04-27 17:02:53
hy@ozderya.net
added simple demo mode for testing
3 files changed with 71 insertions and 0 deletions:
0 comments (0 inline, 0 general)
mainwindow.cpp
Show inline comments
 
@@ -170,24 +170,32 @@ MainWindow::MainWindow(QWidget *parent) 
 
        curves[i]->attach(ui->plot);
 
    }
 

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

	
 
    // Init demo mode
 
    demoCount = 0;
 
    demoTimer.setInterval(100);
 
    QObject::connect(&demoTimer, &QTimer::timeout,
 
                     this, &MainWindow::demoTimerTimeout);
 
    QObject::connect(ui->actionDemoMode, &QAction::toggled,
 
                     this, &MainWindow::enableDemo);
 
}
 

	
 
MainWindow::~MainWindow()
 
{
 
    for (auto curve : curves)
 
    {
 
        delete curve;
 
    }
 

	
 
    if (serialPort.isOpen())
 
    {
 
        serialPort.close();
 
@@ -339,24 +347,27 @@ void MainWindow::selectFlowControl(int f
 
    if (serialPort.isOpen())
 
    {
 
        if(!serialPort.setFlowControl((QSerialPort::FlowControl) flowControl))
 
        {
 
            qDebug() << "Set flow control failed: " << serialPort.error();
 
        }
 
    }
 
}
 

	
 
void MainWindow::onPortToggled(bool open)
 
{
 
    ui->pbOpenPort->setChecked(open);
 
    // make sure demo mode is disabled
 
    if (open && isDemoRunning()) enableDemo(false);
 
    ui->actionDemoMode->setEnabled(!open);
 
}
 

	
 
void MainWindow::onDataReady()
 
{
 
    if (!ui->actionPause->isChecked())
 
    {
 
        // a package is a set of channel data like {CHAN0_SAMPLE, CHAN1_SAMPLE...}
 
        int packageSize = sampleSize * numOfChannels;
 
        int bytesAvailable = serialPort.bytesAvailable();
 

	
 
        if (bytesAvailable > 0 && skipByteRequested)
 
        {
 
@@ -596,12 +607,51 @@ void MainWindow::selectNumberFormat(Numb
 
            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);
 
}
 

	
 
bool MainWindow::isDemoRunning()
 
{
 
    return ui->actionDemoMode->isChecked();
 
}
 

	
 
void MainWindow::demoTimerTimeout()
 
{
 
    demoCount++;
 
    if (demoCount > 100) demoCount = 0;
 

	
 
    for (int ci = 0; ci < numOfChannels; ci++)
 
    {
 
        DataArray data(1);
 
        data.replace(0, (ci + 1)*demoCount);
 
        addChannelData(ci, data);
 
    }
 
}
 

	
 
void MainWindow::enableDemo(bool enabled)
 
{
 
    if (enabled)
 
    {
 
        if (!serialPort.isOpen())
 
        {
 
            demoTimer.start();
 
            ui->actionDemoMode->setChecked(true);
 
        }
 
        else
 
        {
 
            ui->actionDemoMode->setChecked(false);
 
        }
 
    }
 
    else
 
    {
 
        demoTimer.stop();
 
        ui->actionDemoMode->setChecked(false);
 
    }
 
}
mainwindow.h
Show inline comments
 
@@ -18,24 +18,25 @@
 
*/
 

	
 
#ifndef MAINWINDOW_H
 
#define MAINWINDOW_H
 

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

	
 
#include "ui_about_dialog.h"
 

	
 
namespace Ui {
 
class MainWindow;
 
}
 

	
 
class MainWindow : public QMainWindow
 
{
 
    Q_OBJECT
 

	
 
@@ -77,24 +78,29 @@ private:
 
    // `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;
 

	
 
    // demo
 
    QTimer demoTimer;
 
    int demoCount;
 
    bool isDemoRunning();
 

	
 
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
 

	
 
@@ -104,17 +110,20 @@ private slots:
 
    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();
 

	
 
    void demoTimerTimeout();
 
    void enableDemo(bool enabled);
 

	
 
signals:
 
    void portToggled(bool open);
 
};
 

	
 
#endif // MAINWINDOW_H
mainwindow.ui
Show inline comments
 
@@ -592,24 +592,25 @@
 
   <property name="geometry">
 
    <rect>
 
     <x>0</x>
 
     <y>0</y>
 
     <width>653</width>
 
     <height>23</height>
 
    </rect>
 
   </property>
 
   <widget class="QMenu" name="menuHelp">
 
    <property name="title">
 
     <string>Help</string>
 
    </property>
 
    <addaction name="actionDemoMode"/>
 
    <addaction name="actionHelpAbout"/>
 
   </widget>
 
   <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"/>
 
@@ -631,24 +632,35 @@
 
   </property>
 
  </action>
 
  <action name="actionClear">
 
   <property name="text">
 
    <string>Clear</string>
 
   </property>
 
  </action>
 
  <action name="actionHelpAbout">
 
   <property name="text">
 
    <string>About</string>
 
   </property>
 
  </action>
 
  <action name="actionDemoMode">
 
   <property name="checkable">
 
    <bool>true</bool>
 
   </property>
 
   <property name="text">
 
    <string>Demo Mode</string>
 
   </property>
 
   <property name="toolTip">
 
    <string>Toggle Demo Mode</string>
 
   </property>
 
  </action>
 
 </widget>
 
 <layoutdefault spacing="6" margin="11"/>
 
 <customwidgets>
 
  <customwidget>
 
   <class>QwtPlot</class>
 
   <extends>QWidget</extends>
 
   <header>qwt_plot.h</header>
 
   <container>1</container>
 
  </customwidget>
 
  <customwidget>
 
   <class>CustomCheckableButton</class>
 
   <extends>QPushButton</extends>
0 comments (0 inline, 0 general)