From 4767a7349f041bf87cbe47a09816ab0e3b398cee Mon Sep 17 00:00:00 2001 From: Collin Kidder Date: Sun, 28 Feb 2016 16:10:55 -0500 Subject: [PATCH] Added ability for sub windows to get frames much more rapidly than before. Decoupled SerialWorker from MainWindow a bit and refactored things. --- canframemodel.cpp | 9 +++++++-- mainwindow.cpp | 34 +++++++++++++++++++++++++++------- mainwindow.h | 8 +++++++- serialworker.cpp | 23 +++++++++-------------- serialworker.h | 12 +++++------- 5 files changed, 55 insertions(+), 31 deletions(-) diff --git a/canframemodel.cpp b/canframemodel.cpp index 98385cf..dc0216d 100644 --- a/canframemodel.cpp +++ b/canframemodel.cpp @@ -6,7 +6,12 @@ int CANFrameModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent); - return filteredFrames.count(); + if (filteredFrames.data()) return filteredFrames.count(); + + //just in case somehow data is invalid which I have seen before. + //But, this should not happen so issue a debugging message too + qDebug() << "Invalid data for filteredFrames. Returning 0."; + return 0; } int CANFrameModel::totalFrameCount() @@ -341,7 +346,7 @@ void CANFrameModel::sendRefresh() } } filteredFrames.clear(); - filteredFrames = tempContainer; + filteredFrames.append(tempContainer); lastUpdateNumFrames = filteredFrames.count(); endResetModel(); mutex.unlock(); diff --git a/mainwindow.cpp b/mainwindow.cpp index 71bba18..1782687 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -69,7 +69,7 @@ MainWindow::MainWindow(QWidget *parent) : connect(&serialWorkerThread, &QThread::finished, worker, &QObject::deleteLater); connect(&serialWorkerThread, &QThread::started, worker, &SerialWorker::run); //setup timers within the proper thread connect(this, &MainWindow::sendSerialPort, worker, &SerialWorker::setSerialPort, Qt::QueuedConnection); - connect(worker, &SerialWorker::frameUpdateTick, this, &MainWindow::gotFrames, Qt::QueuedConnection); + connect(worker, &SerialWorker::frameUpdateRapid, this, &MainWindow::gotFrames, Qt::QueuedConnection); connect(this, &MainWindow::updateBaudRates, worker, &SerialWorker::updateBaudRates, Qt::QueuedConnection); connect(this, &MainWindow::sendCANFrame, worker, &SerialWorker::sendFrame, Qt::QueuedConnection); connect(worker, &SerialWorker::connectionSuccess, this, &MainWindow::connectionSucceeded, Qt::QueuedConnection); @@ -101,6 +101,7 @@ MainWindow::MainWindow(QWidget *parent) : dbcHandler = new DBCHandler; bDirty = false; inhibitFilterUpdate = false; + rxFrames = 0; model->setDBCHandler(dbcHandler); @@ -150,6 +151,13 @@ MainWindow::MainWindow(QWidget *parent) : ui->lbFPS->setText("0"); ui->lbNumFrames->setText("0"); + connect(&updateTimer, &QTimer::timeout, this, &MainWindow::tickGUIUpdate); + updateTimer.setInterval(250); + updateTimer.start(); + + elapsedTime = new QTime; + elapsedTime->start(); + isConnected = false; allowCapture = true; @@ -266,6 +274,8 @@ MainWindow::~MainWindow() delete udsScanWindow; } + delete elapsedTime; + delete ui; delete dbcHandler; model->clearFrames(); @@ -493,20 +503,30 @@ void MainWindow::filterClearAll() model->setAllFilters(false); } -//most of the work is handled elsewhere. Need only to update the # of frames -//and maybe auto scroll -void MainWindow::gotFrames(int FPS, int framesSinceLastUpdate) +void MainWindow::tickGUIUpdate() { + framesPerSec += rxFrames * 1000 / elapsedTime->elapsed() - (framesPerSec / 4); + elapsedTime->restart(); + model->sendBulkRefresh(rxFrames); + ui->lbNumFrames->setText(QString::number(model->rowCount())); if (ui->cbAutoScroll->isChecked()) ui->canFramesView->scrollToBottom(); - ui->lbFPS->setText(QString::number(FPS)); - if (framesSinceLastUpdate > 0) + ui->lbFPS->setText(QString::number(framesPerSec / 4)); + if (rxFrames > 0) { bDirty = true; - emit framesUpdated(framesSinceLastUpdate); //anyone care that frames were updated? + emit framesUpdated(rxFrames); //anyone care that frames were updated? } if (model->needsFilterRefresh()) updateFilterList(); + + rxFrames = 0; +} + +void MainWindow::gotFrames(int framesSinceLastUpdate) +{ + rxFrames += framesSinceLastUpdate; + emit frameUpdateRapid(framesSinceLastUpdate); } void MainWindow::addFrameToDisplay(CANFrame &frame, bool autoRefresh = false) diff --git a/mainwindow.h b/mainwindow.h index cf99eba..bbe6416 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -75,6 +75,7 @@ private slots: void gridDoubleClicked(QModelIndex); void interpretToggled(bool); void overwriteToggled(bool); + void tickGUIUpdate(); void toggleCapture(); void normalizeTiming(); void updateFilterList(); @@ -83,7 +84,7 @@ private slots: void filterClearAll(); public slots: - void gotFrames(int, int); + void gotFrames(int); void updateSettings(); void gotCenterTimeID(int32_t ID, double timestamp); void updateConnectionSettings(QString connectionType, QString port, int speed0, int speed1); @@ -98,6 +99,7 @@ signals: //-1 = frames cleared, -2 = a new file has been loaded (so all frames are different), otherwise # of new frames void framesUpdated(int numFrames); //something has updated the frame list + void frameUpdateRapid(int numFrames); void settingsUpdated(); void sendCenterTimeID(int32_t ID, double timestamp); @@ -111,6 +113,10 @@ private: QThread serialWorkerThread; SerialWorker *worker; QByteArray inputBuffer; + QTimer updateTimer; + QTime *elapsedTime; + int framesPerSec; + int rxFrames; bool inhibitFilterUpdate; bool useHex; bool allowCapture; diff --git a/serialworker.cpp b/serialworker.cpp index d70fc6c..b36940e 100644 --- a/serialworker.cpp +++ b/serialworker.cpp @@ -12,10 +12,8 @@ SerialWorker::SerialWorker(CANFrameModel *model, QObject *parent) : QObject(pare rx_step = 0; buildFrame = new CANFrame; canModel = model; - gotFrames = 0; ticker = NULL; - elapsedTime = NULL; - framesPerSec = 0; + framesRapid = 0; capturing = true; gotValidated = true; isAutoRestart = false; @@ -45,9 +43,6 @@ void SerialWorker::run() ticker = new QTimer; connect(ticker, SIGNAL(timeout()), this, SLOT(handleTick())); - elapsedTime = new QTime; - elapsedTime->start(); - ticker->setInterval(250); //tick four times per second ticker->setSingleShot(false); //keep ticking ticker->start(); @@ -148,6 +143,11 @@ void SerialWorker::readSerialData() c = data.at(i); procRXChar(c); } + if (framesRapid > 0) + { + emit frameUpdateRapid(framesRapid); + framesRapid = 0; + } } void SerialWorker::sendFrame(const CANFrame *frame, int bus = 0) @@ -162,7 +162,7 @@ void SerialWorker::sendFrame(const CANFrame *frame, int bus = 0) //show our sent frames in the list too. This happens even if we're not connected. canModel->addFrame(tempFrame, false); - gotFrames++; + framesRapid++; if (serial == NULL) return; if (!serial->isOpen()) return; @@ -315,8 +315,8 @@ void SerialWorker::procRXChar(unsigned char c) if (capturing) { buildFrame->isReceived = true; - canModel->addFrame(*buildFrame, false); - gotFrames++; + canModel->addFrame(*buildFrame, false); + framesRapid++; if (buildFrame->ID == targetID) emit gotTargettedFrame(canModel->rowCount() - 1); } } @@ -447,11 +447,6 @@ void SerialWorker::handleTick() } } - framesPerSec += gotFrames * 1000 / elapsedTime->elapsed() - (framesPerSec / 4); - elapsedTime->restart(); - emit frameUpdateTick(framesPerSec / 4, gotFrames); //sends stats to interested parties - canModel->sendBulkRefresh(gotFrames); - gotFrames = 0; if (doValidation && serial && serial->isOpen()) sendCommValidation(); } diff --git a/serialworker.h b/serialworker.h index 2b71266..e0730f0 100644 --- a/serialworker.h +++ b/serialworker.h @@ -40,8 +40,8 @@ public: void targetFrameID(int); signals: //we emit signals - void error(const QString &); - void frameUpdateTick(int, int); //update interested parties about the # of frames that have come in + void error(const QString &); + void frameUpdateRapid(int); //sent *much* more rapidly than the above signal - one param for # of frames void connectionSuccess(int, int); void connectionFailure(); void deviceInfo(int, int); @@ -74,11 +74,9 @@ private: QSerialPort *serial; QSerialPortInfo *currentPort; CANFrameModel *canModel; - QTimer *ticker; - QTime *elapsedTime; - QMutex sendBulkMutex; - int framesPerSec; - int gotFrames; + QTimer *ticker; + QMutex sendBulkMutex; + int framesRapid; int targetID; STATE rx_state; int rx_step;