# HG changeset patch # User Hasan Yavuz Ă–ZDERYA # Date 2015-03-16 16:05:34 # Node ID 649401566a84c8c61c3a9d873464d86536319b0e # Parent 210c15d2007c4e98eb5898e895041647b115a7b4 implemented number format selection, currently supporting integer formats diff --git a/mainwindow.cpp b/mainwindow.cpp --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -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 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; + break; + case NumberFormat_int8: + sampleSize = 1; + readSample = &MainWindow::readSampleAs; + break; + case NumberFormat_uint16: + sampleSize = 2; + readSample = &MainWindow::readSampleAs; + break; + case NumberFormat_int16: + sampleSize = 2; + readSample = &MainWindow::readSampleAs; + break; + case NumberFormat_uint32: + sampleSize = 4; + readSample = &MainWindow::readSampleAs; + break; + case NumberFormat_int32: + sampleSize = 4; + readSample = &MainWindow::readSampleAs; + break; + } +} + +template double MainWindow::readSampleAs() +{ + T data; + this->serialPort.read((char*) &data, sizeof(data)); + return double(data); +} diff --git a/mainwindow.h b/mainwindow.h --- a/mainwindow.h +++ b/mainwindow.h @@ -2,6 +2,7 @@ #define MAINWINDOW_H #include +#include #include #include #include @@ -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 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 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); }; diff --git a/mainwindow.ui b/mainwindow.ui --- a/mainwindow.ui +++ b/mainwindow.ui @@ -342,6 +342,9 @@ uint8 + + true + @@ -536,7 +539,7 @@ 0 0 653 - 23 + 20