From c12031158e55c0454f14183608adf23bb6a9445d Mon Sep 17 00:00:00 2001 From: Collin Kidder Date: Mon, 15 Apr 2019 20:56:39 -0400 Subject: [PATCH] Implemented the signal viewer and enabled it in the menu. --- dbc/dbc_classes.cpp | 8 ++-- dbc/dbc_classes.h | 4 +- mainwindow.cpp | 7 +++- signalviewerwindow.cpp | 87 ++++++++++++++++++++++++++++++++++++---- signalviewerwindow.h | 7 +++- ui/signalviewerwindow.ui | 43 ++++++++++++-------- 6 files changed, 123 insertions(+), 33 deletions(-) diff --git a/dbc/dbc_classes.cpp b/dbc/dbc_classes.cpp index f9106e1..f46b4b7 100644 --- a/dbc/dbc_classes.cpp +++ b/dbc/dbc_classes.cpp @@ -36,7 +36,7 @@ s 5 47 46 45 44 43 42 41 40 So, the bits are 12, 11, 10, 9, 8, 23, 22, 21. Yes, that's confusing. They now go in reverse value order too. Bit 12 is worth 128, 11 is worth 64, etc until bit 21 is worth 1. */ -bool DBC_SIGNAL::processAsText(const CANFrame &frame, QString &outString) +bool DBC_SIGNAL::processAsText(const CANFrame &frame, QString &outString, bool outputName) { int64_t result = 0; bool isSigned = false; @@ -95,16 +95,16 @@ bool DBC_SIGNAL::processAsText(const CANFrame &frame, QString &outString) endResult = (*((double *)(&result)) * factor) + bias; } - outString = makePrettyOutput(endResult, result); + outString = makePrettyOutput(endResult, result, outputName); cachedValue = endResult; return true; } -QString DBC_SIGNAL::makePrettyOutput(double floatVal, int64_t intVal) +QString DBC_SIGNAL::makePrettyOutput(double floatVal, int64_t intVal, bool outputName) { QString outputString; - outputString = name + ": "; + if (outputName) outputString = name + ": "; if (valList.count() > 0) //if this is a value list type then look it up and display the proper string { diff --git a/dbc/dbc_classes.h b/dbc/dbc_classes.h index 76fe85b..5d32b67 100644 --- a/dbc/dbc_classes.h +++ b/dbc/dbc_classes.h @@ -101,10 +101,10 @@ public: //TODO: this is sloppy. It shouldn't all be public! QList attributes; QList valList; - bool processAsText(const CANFrame &frame, QString &outString); + bool processAsText(const CANFrame &frame, QString &outString, bool outputName = true); bool processAsInt(const CANFrame &frame, int32_t &outValue); bool processAsDouble(const CANFrame &frame, double &outValue); - QString makePrettyOutput(double floatVal, int64_t intVal); + QString makePrettyOutput(double floatVal, int64_t intVal, bool outputName = true); DBC_ATTRIBUTE_VALUE *findAttrValByName(QString name); DBC_ATTRIBUTE_VALUE *findAttrValByIdx(int idx); }; diff --git a/mainwindow.cpp b/mainwindow.cpp index 15181e1..1e8c7a5 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -186,7 +186,7 @@ MainWindow::MainWindow(QWidget *parent) : //so if you want to enable them and play with them then go for it. ui->actionFirmware_Update->setVisible(false); ui->actionMotorControlConfig->setVisible(false); - ui->actionSignal_Viewer->setVisible(false); + //ui->actionSignal_Viewer->setVisible(false); ui->actionSingle_Multi_State_2->setVisible(false); installEventFilter(this); @@ -1015,7 +1015,10 @@ void MainWindow::showSignalViewer() { if (!signalViewerWindow) { - signalViewerWindow = new SignalViewerWindow(); + if (!useFiltered) + signalViewerWindow = new SignalViewerWindow(model->getListReference()); + else + signalViewerWindow = new SignalViewerWindow(model->getFilteredListReference()); } signalViewerWindow->show(); } diff --git a/signalviewerwindow.cpp b/signalviewerwindow.cpp index 3c77f7e..a093bee 100644 --- a/signalviewerwindow.cpp +++ b/signalviewerwindow.cpp @@ -1,14 +1,18 @@ #include "signalviewerwindow.h" #include "ui_signalviewerwindow.h" #include "helpwindow.h" +#include "mainwindow.h" +#include -SignalViewerWindow::SignalViewerWindow(QWidget *parent) : +SignalViewerWindow::SignalViewerWindow(const QVector *frames, QWidget *parent) : QDialog(parent), ui(new Ui::SignalViewerWindow) { ui->setupUi(this); setWindowFlags(Qt::Window); + modelFrames = frames; + QStringList headers; headers << "Signal" << "Value"; ui->tableViewer->setHorizontalHeaderLabels(headers); @@ -21,6 +25,8 @@ SignalViewerWindow::SignalViewerWindow(QWidget *parent) : connect(ui->cbMessages, SIGNAL(currentIndexChanged(int)), this, SLOT(loadSignals(int))); connect(ui->btnAdd, SIGNAL(clicked(bool)), this, SLOT(addSignal())); + connect(MainWindow::getReference(), SIGNAL(framesUpdated(int)), this, SLOT(updatedFrames(int))); + connect(ui->btnRemove, SIGNAL(clicked(bool)), this, SLOT(removeSelectedSignal())); loadMessages(); } @@ -30,14 +36,79 @@ SignalViewerWindow::~SignalViewerWindow() delete ui; } +void SignalViewerWindow::updatedFrames(int numFrames) +{ + CANFrame thisFrame; + + if (numFrames == -1) //all frames deleted. Don't care + { + } + else if (numFrames == -2) //all new set of frames. Reset + { + for (int i = 0; i < modelFrames->count(); i++) + { + thisFrame = modelFrames->at(i); + processFrame(thisFrame); + } + } + else //just got some new frames. See if they are relevant. + { + if (numFrames > modelFrames->count()) return; + + for (int i = modelFrames->count() - numFrames; i < modelFrames->count(); i++) + { + thisFrame = modelFrames->at(i); + processFrame(thisFrame); + } + } +} + +void SignalViewerWindow::processFrame(CANFrame &frame) +{ + QString sigString; + DBC_SIGNAL *sig; + for (int i = 0; i < signalList.count(); i++) + { + sig = signalList.at(i); + if (!sig) return; + if (sig->parentMessage->ID == frame.ID) + { + if (sig->processAsText(frame, sigString, false)) //if true we could interpret the signal so update it in the list + { + QTableWidgetItem *item = ui->tableViewer->item(i, 1); + if (!item) + { + item = new QTableWidgetItem(sigString); + ui->tableViewer->setItem(i, 1, item); + } + else item->setText(sigString); + } + } + } +} + +void SignalViewerWindow::removeSelectedSignal() +{ + int selRow = ui->tableViewer->currentRow(); + if (selRow < 0) return; //no selected row + signalList.removeAt(selRow); + ui->tableViewer->removeRow(selRow); +} + void SignalViewerWindow::loadMessages() { + int numFiles; ui->cbMessages->clear(); - if (dbcHandler == NULL) return; - if (dbcHandler->getFileCount() == 0) dbcHandler->createBlankFile(); - for (int x = 0; x < dbcHandler->getFileByIdx(0)->messageHandler->getCount(); x++) + if (dbcHandler == nullptr) return; + if ((numFiles = dbcHandler->getFileCount()) == 0) return; + qDebug() << numFiles; + for (int f = 0; f < numFiles; f++) { - ui->cbMessages->addItem(dbcHandler->getFileByIdx(0)->messageHandler->findMsgByIdx(x)->name); + qDebug() << dbcHandler->getFileByIdx(f)->messageHandler->getCount(); + for (int x = 0; x < dbcHandler->getFileByIdx(f)->messageHandler->getCount(); x++) + { + ui->cbMessages->addItem(dbcHandler->getFileByIdx(f)->messageHandler->findMsgByIdx(x)->name); + } } } @@ -48,9 +119,9 @@ void SignalViewerWindow::loadSignals(int idx) //in the data structure so it should have been possible to just //look it up based on index but by name is probably safer and this operation //is not time critical at all. - DBC_MESSAGE *msg = dbcHandler->getFileByIdx(0)->messageHandler->findMsgByName(ui->cbMessages->currentText()); + DBC_MESSAGE *msg = dbcHandler->findMessage(ui->cbMessages->currentText()); - if (msg == NULL) return; + if (msg == nullptr) return; ui->cbSignals->clear(); for (int x = 0; x < msg->sigHandler->getCount(); x++) { @@ -60,7 +131,7 @@ void SignalViewerWindow::loadSignals(int idx) void SignalViewerWindow::addSignal() { - DBC_MESSAGE *msg = dbcHandler->getFileByIdx(0)->messageHandler->findMsgByName(ui->cbMessages->currentText()); + DBC_MESSAGE *msg = dbcHandler->findMessage(ui->cbMessages->currentText()); if (!msg) return; DBC_SIGNAL *sig = msg->sigHandler->findSignalByName(ui->cbSignals->currentText()); if (!sig) return; diff --git a/signalviewerwindow.h b/signalviewerwindow.h index 68a0553..17b1bde 100644 --- a/signalviewerwindow.h +++ b/signalviewerwindow.h @@ -13,19 +13,24 @@ class SignalViewerWindow : public QDialog Q_OBJECT public: - explicit SignalViewerWindow(QWidget *parent = 0); + explicit SignalViewerWindow(const QVector *frames, QWidget *parent = 0); ~SignalViewerWindow(); private slots: void loadMessages(); void loadSignals(int idx); void addSignal(); + void removeSelectedSignal(); + void updatedFrames(int); private: Ui::SignalViewerWindow *ui; DBCHandler *dbcHandler; QList signalList; + const QVector *modelFrames; + + void processFrame(CANFrame &frame); }; #endif // SIGNALVIEWERWINDOW_H diff --git a/ui/signalviewerwindow.ui b/ui/signalviewerwindow.ui index 84b642b..b5b5d5d 100644 --- a/ui/signalviewerwindow.ui +++ b/ui/signalviewerwindow.ui @@ -15,22 +15,33 @@ - - - 2 - - - 300 - - - true - - - false - - - - + + + + + 2 + + + 300 + + + true + + + false + + + + + + + + + Remove Selected Signal + + + +