From 06ee0c9301f446b5a74edbd2548ea4edf36aaefc Mon Sep 17 00:00:00 2001 From: Collin Kidder Date: Sun, 24 May 2015 10:10:27 -0400 Subject: [PATCH] Added frames per second read out. Fixed some crashes. --- canframemodel.cpp | 10 ++++++++++ canframemodel.h | 1 + mainwindow.cpp | 14 +++++++++----- mainwindow.h | 4 ++-- mainwindow.ui | 28 +++++++++++++++++++++++++++- serialworker.cpp | 17 +++++++++++++---- serialworker.h | 5 ++++- 7 files changed, 66 insertions(+), 13 deletions(-) diff --git a/canframemodel.cpp b/canframemodel.cpp index 42ca0a5..77d94ca 100644 --- a/canframemodel.cpp +++ b/canframemodel.cpp @@ -128,9 +128,11 @@ QVariant CANFrameModel::headerData(int section, Qt::Orientation orientation, void CANFrameModel::addFrame(CANFrame &frame, bool autoRefresh = false) { + mutex.lock(); if (autoRefresh) beginInsertRows(QModelIndex(), frames.count() + 1, frames.count() + 1); frames.append(frame); if (autoRefresh) endInsertRows(); + mutex.unlock(); } void CANFrameModel::sendRefresh() @@ -151,15 +153,23 @@ void CANFrameModel::sendRefresh(int pos) void CANFrameModel::sendBulkRefresh(int num) { //qDebug() << "Bulk refresh of " << num; + //the next three lines protect against a crash in case someone clicked clear frames + //in between the time we got some frames and the time this was called + //otherwise it's possible that the grid is in an odd state. + if (num == 0) return; + if (frames.count() == 0) return; + if (num > frames.count()) num = frames.count(); beginInsertRows(QModelIndex(), frames.count() - num, frames.count() - 1); endInsertRows(); } void CANFrameModel::clearFrames() { + mutex.lock(); this->beginResetModel(); frames.clear(); this->endResetModel(); + mutex.unlock(); } //Is this safe? Maybe not but if we don't change it then that's OK diff --git a/canframemodel.h b/canframemodel.h index af2499d..51d7f8a 100644 --- a/canframemodel.h +++ b/canframemodel.h @@ -35,6 +35,7 @@ public: private: QVector frames; DBCHandler *dbcHandler; + QMutex mutex; bool interpretFrames; //should we use the dbcHandler? bool overwriteDups; //should we display all frames or only the newest for each ID? }; diff --git a/mainwindow.cpp b/mainwindow.cpp index 134f64e..7377f60 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -118,22 +118,25 @@ MainWindow::MainWindow(QWidget *parent) : ui->statusBar->addWidget(&lbStatusBauds); ui->statusBar->addWidget(&lbStatusDatabase); + ui->lbFPS->setText("0"); + ui->lbNumFrames->setText("0"); + isConnected = false; } MainWindow::~MainWindow() { - delete ui; + serialWorkerThread.quit(); + serialWorkerThread.wait(); + if (graphingWindow) delete graphingWindow; if (frameInfoWindow) delete frameInfoWindow; if (playbackWindow) delete playbackWindow; if (flowViewWindow) delete flowViewWindow; if (frameSenderWindow) delete frameSenderWindow; + delete ui; delete dbcHandler; - - serialWorkerThread.quit(); - serialWorkerThread.wait(); } void MainWindow::updateBaudLabel(int baud0, int baud1) @@ -150,10 +153,11 @@ void MainWindow::updateBaudLabel(int baud0, int baud1) //most of the work is handled elsewhere. Need only to update the # of frames //and maybe auto scroll -void MainWindow::gotFrames() +void MainWindow::gotFrames(int FPS) { ui->lbNumFrames->setText(QString::number(model->rowCount())); if (ui->cbAutoScroll->isChecked()) ui->canFramesView->scrollToBottom(); + ui->lbFPS->setText(QString::number(FPS)); } void MainWindow::addFrameToDisplay(CANFrame &frame, bool autoRefresh = false) diff --git a/mainwindow.h b/mainwindow.h index 3690413..a2655cb 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -13,7 +13,7 @@ #include "framesenderwindow.h" #include "dbchandler.h" -#define VERSION 102 +#define VERSION 103 namespace Ui { class MainWindow; @@ -46,7 +46,7 @@ private slots: void connectionSucceeded(int, int); public slots: - void gotFrames(); + void gotFrames(int); signals: diff --git a/mainwindow.ui b/mainwindow.ui index cc84b91..570310a 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -151,7 +151,7 @@ - # Of Frames: + Total Frames Captured: @@ -168,6 +168,32 @@ + + + + + 0 + 0 + + + + Frames Per Second: + + + + + + + + 0 + 0 + + + + + + + diff --git a/serialworker.cpp b/serialworker.cpp index 860037b..b497880 100644 --- a/serialworker.cpp +++ b/serialworker.cpp @@ -13,6 +13,8 @@ SerialWorker::SerialWorker(CANFrameModel *model, QObject *parent) : QObject(pare canModel = model; gotFrames = 0; ticker = NULL; + elapsedTime = NULL; + framesPerSec = 0; } SerialWorker::~SerialWorker() @@ -69,6 +71,11 @@ void SerialWorker::setSerialPort(QSerialPortInfo *port) ticker = new QTimer; connect(ticker, SIGNAL(timeout()), this, SLOT(handleTick())); } + if (elapsedTime == NULL) + { + elapsedTime = new QTime; + elapsedTime->start(); + } ticker->setInterval(250); //tick four times per second ticker->setSingleShot(false); //keep ticking ticker->start(); @@ -341,12 +348,14 @@ void SerialWorker::procRXChar(unsigned char c) void SerialWorker::handleTick() { //qDebug() << "Tick!"; - if (gotFrames > 0) - { - emit receivedFrames(); //notify interested parties that there are new frames + framesPerSec += gotFrames * 1000 / elapsedTime->elapsed() - (framesPerSec / 4); + elapsedTime->restart(); + //if (gotFrames > 0) + //{ + emit receivedFrames(framesPerSec / 4); //misnamed now. Just notifies of FPS and that frames might exist canModel->sendBulkRefresh(gotFrames); gotFrames = 0; - } + //} } //totally shuts down the whole thing diff --git a/serialworker.h b/serialworker.h index e41f7fe..65eda45 100644 --- a/serialworker.h +++ b/serialworker.h @@ -1,6 +1,7 @@ #ifndef SERIALWORKER_H #define SERIALWORKER_H +#include #include #include #include @@ -32,7 +33,7 @@ public: signals: //we emit signals void error(const QString &); - void receivedFrames(); //since the last tick we got at least one more frame + void receivedFrames(int); //since the last tick we got at least one more frame void connectionSuccess(int, int); void connectionFailure(); void deviceInfo(int, int); @@ -55,6 +56,8 @@ private: QSerialPort *serial; CANFrameModel *canModel; QTimer *ticker; + QTime *elapsedTime; + int framesPerSec; int gotFrames; STATE rx_state; int rx_step;