diff --git a/SavvyCAN.pro.user b/SavvyCAN.pro.user index bab45db..2380d48 100644 --- a/SavvyCAN.pro.user +++ b/SavvyCAN.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/graphingwindow.cpp b/graphingwindow.cpp index 553b528..004da18 100644 --- a/graphingwindow.cpp +++ b/graphingwindow.cpp @@ -1,16 +1,35 @@ #include "graphingwindow.h" #include "ui_graphingwindow.h" #include "newgraphdialog.h" +#include -GraphingWindow::GraphingWindow(QWidget *parent) : +GraphingWindow::GraphingWindow(QList *frames, QWidget *parent) : QDialog(parent), ui(new Ui::GraphingWindow) { ui->setupUi(this); + modelFrames = frames; + ui->graphingView->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes | QCP::iSelectLegend | QCP::iSelectPlottables); + ui->graphingView->xAxis->setRange(-8, 8); + ui->graphingView->yAxis->setRange(-5, 5); + ui->graphingView->axisRect()->setupFullAxesBox(); + + //ui->graphingView->plotLayout()->insertRow(0); + //ui->graphingView->plotLayout()->addElement(0, 0, new QCPPlotTitle(ui->graphingView, "Data Graphing")); + + ui->graphingView->xAxis->setLabel("Time Axis"); + ui->graphingView->yAxis->setLabel("Value Axis"); + ui->graphingView->legend->setVisible(true); + QFont legendFont = font(); + legendFont.setPointSize(10); + ui->graphingView->legend->setFont(legendFont); + ui->graphingView->legend->setSelectedFont(legendFont); + ui->graphingView->legend->setSelectableParts(QCPLegend::spItems); // legend box shall not be selectable, only legend items + connect(ui->graphingView, SIGNAL(mousePress(QMouseEvent*)), this, SLOT(mousePress())); connect(ui->graphingView, SIGNAL(mouseWheel(QWheelEvent*)), this, SLOT(mouseWheel())); @@ -191,8 +210,107 @@ void GraphingWindow::contextMenuRequest(QPoint pos) void GraphingWindow::addNewGraph() { + int tempVal; NewGraphDialog *thisDialog = new NewGraphDialog(); - thisDialog->show(); + if (thisDialog->exec() == QDialog::Accepted) + { + GraphParams params; + thisDialog->getParams(params); + qDebug() << "Returned ID: " << params.ID; + qDebug() << "Start byte: " << params.startByte; + qDebug() << "End Byte: " << params.endByte; + qDebug() << "Mask: " << params.mask; + + frameCache.clear(); + for (int i = 0; i < modelFrames->count(); i++) + { + CANFrame thisFrame = modelFrames->at(i); + if (thisFrame.ID == params.ID) frameCache.append(thisFrame); + } + + int numEntries = frameCache.count() / params.stride; + + QVector x(numEntries), y(numEntries); + + if (params.endByte == -1 || params.startByte == params.endByte) + { + for (int j = 0; j < numEntries; j++) + { + tempVal = (frameCache[j * params.stride].data[params.startByte] & params.mask); + if (params.isSigned && tempVal > 127) + { + tempVal = tempVal - 256; + } + x[j] = j; + y[j] = (tempVal + params.bias) * params.scale; + } + } + else if (params.endByte > params.startByte) //big endian + { + float tempValue; + int tempValInt; + int numBytes = (params.endByte - params.startByte) + 1; + int shiftRef = 1 << (numBytes * 8); + for (int j = 0; j < numEntries; j++) + { + tempValInt = 0; + int expon = 1; + for (int c = 0; c < numBytes; c++) + { + tempValInt += (frameCache[j * params.stride].data[params.endByte - c] * expon); + expon *= 256; + } + + tempValInt &= params.mask; + + if (params.isSigned && tempValInt > ((shiftRef / 2) - 1)) + { + tempValInt = tempValInt - shiftRef; + } + + tempValue = (float)tempValInt; + x[j] = j; + y[j] = (tempValue + params.bias) * params.scale; + } + } + else //little endian + { + float tempValue; + int tempValInt; + int numBytes = (params.startByte - params.endByte) + 1; + int shiftRef = 1 << (numBytes * 8); + for (int j = 0; j < numEntries; j++) + { + tempValInt = 0; + int expon = 1; + for (int c = 0; c < numBytes; c++) + { + tempValInt += frameCache[j * params.stride].data[params.endByte + c] * expon; + expon *= 256; + } + tempValInt &= params.mask; + + if (params.isSigned && tempValInt > ((shiftRef / 2) - 1)) + { + tempValInt = tempValInt - shiftRef; + } + + tempValue = (float)tempValInt; + x[j] = j; + y[j] = (tempValue + params.bias) * params.scale; + } + } + ui->graphingView->addGraph(); + ui->graphingView->graph()->setName(QString("Graph %1").arg(ui->graphingView->graphCount()-1)); + ui->graphingView->graph()->setData(x,y); + ui->graphingView->graph()->setLineStyle(QCPGraph::lsLine); //connect points with lines + QPen graphPen; + graphPen.setColor(params.color); + graphPen.setWidth(1); + ui->graphingView->graph()->setPen(graphPen); + ui->graphingView->replot(); + } + delete thisDialog; } void GraphingWindow::moveLegend() diff --git a/graphingwindow.h b/graphingwindow.h index 124c9cb..a158167 100644 --- a/graphingwindow.h +++ b/graphingwindow.h @@ -2,6 +2,7 @@ #define GRAPHINGWINDOW_H #include "qcustomplot.h" +#include "can_structs.h" #include @@ -14,7 +15,7 @@ class GraphingWindow : public QDialog Q_OBJECT public: - explicit GraphingWindow(QWidget *parent = 0); + explicit GraphingWindow(QList *, QWidget *parent = 0); ~GraphingWindow(); private slots: @@ -32,6 +33,21 @@ private slots: private: Ui::GraphingWindow *ui; + QList frameCache; + QList *modelFrames; +}; + +class GraphParams +{ +public: + int32_t ID; + int startByte, endByte; + bool isSigned; + int32_t mask; + float bias; + float scale; + int stride; + QColor color; }; #endif // GRAPHINGWINDOW_H diff --git a/mainwindow.cpp b/mainwindow.cpp index 0edf988..17cd536 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -245,7 +245,7 @@ void MainWindow::connButtonPress() void MainWindow::showGraphingWindow() { - if (!graphingWindow) graphingWindow = new GraphingWindow(); + if (!graphingWindow) graphingWindow = new GraphingWindow(model->getListReference()); graphingWindow->show(); } diff --git a/newgraphdialog.cpp b/newgraphdialog.cpp index 59f80a9..1e4d689 100644 --- a/newgraphdialog.cpp +++ b/newgraphdialog.cpp @@ -1,14 +1,72 @@ #include "newgraphdialog.h" #include "ui_newgraphdialog.h" +#include +#include "utility.h" NewGraphDialog::NewGraphDialog(QWidget *parent) : QDialog(parent), ui(new Ui::NewGraphDialog) { ui->setupUi(this); + + connect(ui->colorSwatch, SIGNAL(clicked(bool)), this, SLOT(colorSwatchClick())); + connect(ui->btnAddGraph, SIGNAL(clicked(bool)), this, SLOT(addButtonClicked())); + + // Seed the random generator with current time + QTime time = QTime::currentTime(); + qsrand((uint)time.msec()); + + QPalette p = ui->colorSwatch->palette(); + p.setColor(QPalette::Button, QColor(qrand() % 255,qrand() % 255,qrand() % 255)); + ui->colorSwatch->setPalette(p); + } NewGraphDialog::~NewGraphDialog() { delete ui; } + +void NewGraphDialog::addButtonClicked() +{ + accept(); +} + +void NewGraphDialog::colorSwatchClick() +{ + QColor newColor = QColorDialog::getColor(ui->colorSwatch->palette().button().color()); + + QPalette p = ui->colorSwatch->palette(); + p.setColor(QPalette::Button, newColor); + ui->colorSwatch->setPalette(p); + +} + +void NewGraphDialog::getParams(GraphParams ¶ms) +{ + params.ID = Utility::ParseStringToNum(ui->txtID->text()); + params.bias = ui->txtBias->text().toFloat(); + params.color = ui->colorSwatch->palette().button().color(); + params.isSigned = ui->cbSigned->isChecked(); + params.mask = Utility::ParseStringToNum(ui->txtMask->text()); + params.scale = Utility::ParseStringToNum(ui->txtScale->text()); + params.stride = Utility::ParseStringToNum(ui->txtStride->text()); + + QStringList values = ui->txtData->text().split('-'); + params.startByte = -1; + params.endByte = -1; + if (values.count() > 0) + { + params.startByte = values[0].toInt(); + if (values.count() > 1) + { + params.endByte = values[1].toInt(); + } + } + + //now catch stupidity and bring it to defaults + if (params.mask == 0) params.mask = 0xFFFFFFFF; + if (fabs(params.scale) < 0.0001f) params.scale = 1.0f; + if (params.stride < 1) params.stride = 1; + +} diff --git a/newgraphdialog.h b/newgraphdialog.h index 07589f1..6559d01 100644 --- a/newgraphdialog.h +++ b/newgraphdialog.h @@ -2,6 +2,7 @@ #define NEWGRAPHDIALOG_H #include +#include "graphingwindow.h" namespace Ui { class NewGraphDialog; @@ -14,6 +15,11 @@ class NewGraphDialog : public QDialog public: explicit NewGraphDialog(QWidget *parent = 0); ~NewGraphDialog(); + void getParams(GraphParams &); + +private slots: + void addButtonClicked(); + void colorSwatchClick(); private: Ui::NewGraphDialog *ui; diff --git a/newgraphdialog.ui b/newgraphdialog.ui index 7a2032f..1eec111 100644 --- a/newgraphdialog.ui +++ b/newgraphdialog.ui @@ -76,50 +76,47 @@ - + - + - + - + - + - + - + - - - - - 0 - 0 - - - - - 16777215 - 40 - + + + + Add this graph - - + + + + true + - Add this graph + CHANGE + + + true diff --git a/utility.h b/utility.h index bcd3d37..07d9574 100644 --- a/utility.h +++ b/utility.h @@ -32,7 +32,11 @@ public: } return temp; + } + static int ParseStringToNum(QString input) + { + return ParseStringToNum(input.toUtf8()); } static long GetTimeMS()