From c2e5df60877d2d744ad0da6fcc64ca79aae88640 Mon Sep 17 00:00:00 2001 From: Collin Kidder Date: Wed, 2 Dec 2015 15:53:26 -0500 Subject: [PATCH] Bug fixes, turn some connections into QT5 syntax, more implementation of scripting interface. --- mainwindow.cpp | 5 +++-- scriptcontainer.cpp | 27 ++++++++++++++++++++++++--- scriptcontainer.h | 5 ++++- scriptingwindow.cpp | 39 ++++++++++++++++++++++++++++++--------- scriptingwindow.h | 3 +++ serialworker.cpp | 6 +++--- 6 files changed, 67 insertions(+), 18 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index f656310..24fa362 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -84,7 +84,7 @@ MainWindow::MainWindow(QWidget *parent) : worker = new SerialWorker(model); worker->moveToThread(&serialWorkerThread); connect(&serialWorkerThread, &QThread::finished, worker, &QObject::deleteLater); - connect(&serialWorkerThread, SIGNAL(started()), worker, SLOT(run())); //setup timers within the proper thread + 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(this, &MainWindow::updateBaudRates, worker, &SerialWorker::updateBaudRates, Qt::QueuedConnection); @@ -995,7 +995,7 @@ void MainWindow::showFrameSenderWindow() else frameSenderWindow = new FrameSenderWindow(model->getFilteredListReference()); - connect(frameSenderWindow, SIGNAL(sendCANFrame(const CANFrame*,int)), worker, SLOT(sendFrame(const CANFrame*,int))); + connect(frameSenderWindow, &FrameSenderWindow::sendCANFrame, worker, &SerialWorker::sendFrame); } frameSenderWindow->show(); } @@ -1046,6 +1046,7 @@ void MainWindow::showScriptingWindow() if (!scriptingWindow) { scriptingWindow = new ScriptingWindow(model->getListReference()); + connect(scriptingWindow, &ScriptingWindow::sendCANFrame, worker, &SerialWorker::sendFrame); } scriptingWindow->show(); } diff --git a/scriptcontainer.cpp b/scriptcontainer.cpp index 5fb658f..2e39aba 100644 --- a/scriptcontainer.cpp +++ b/scriptcontainer.cpp @@ -86,18 +86,39 @@ void ScriptContainer::clearFilters() filters.clear(); } -void ScriptContainer::sendFrame(QJSValue id, QJSValue length, QJSValue data) +void ScriptContainer::sendFrame(QJSValue bus, QJSValue id, QJSValue length, QJSValue data) { - qDebug() << "called send frame"; + CANFrame frame; + frame.extended = false; + frame.ID = id.toInt(); + frame.len = length.toInt(); + if (frame.len < 0) frame.len = 0; + if (frame.len > 8) frame.len = 8; + + for (int i = 0; i < frame.len; i++) + { + frame.data[i] = (uint8_t)data.property(0).toInt(); + } + + frame.bus = bus.toInt(); + if (frame.bus < 0) frame.bus = 0; + if (frame.bus > 1) frame.bus = 1; + + if (frame.ID > 0x7FF) frame.extended = true; + + emit sendCANFrame(&frame, frame.bus); } void ScriptContainer::gotFrame(const CANFrame &frame) { + if (!gotFrameFunction.isCallable()) return; //nothing to do if we can't even call the function for (int i = 0; i < filters.length(); i++) { if (filters[i].checkFilter(frame.ID, frame.bus)) { - + QJSValueList args; + args << frame.bus << frame.ID << frame.len << frame.data; + gotFrameFunction.call(args); return; //as soon as one filter matches we jump out } } diff --git a/scriptcontainer.h b/scriptcontainer.h index fc354ba..b570f12 100644 --- a/scriptcontainer.h +++ b/scriptcontainer.h @@ -26,11 +26,14 @@ public slots: void setFilter(QJSValue id, QJSValue mask, QJSValue bus); void setTickInterval(QJSValue interval); void clearFilters(); - void sendFrame(QJSValue id, QJSValue length, QJSValue data); + void sendFrame(QJSValue bus, QJSValue id, QJSValue length, QJSValue data); private slots: void tick(); +signals: + void sendCANFrame(const CANFrame *, int); + private: QJSEngine scriptEngine; QJSValue compiledScript; diff --git a/scriptingwindow.cpp b/scriptingwindow.cpp index 3acc807..748434e 100644 --- a/scriptingwindow.cpp +++ b/scriptingwindow.cpp @@ -4,6 +4,8 @@ #include #include +#include "mainwindow.h" + ScriptingWindow::ScriptingWindow(const QVector *frames, QWidget *parent) : QDialog(parent), ui(new Ui::ScriptingWindow) @@ -12,15 +14,13 @@ ScriptingWindow::ScriptingWindow(const QVector *frames, QWidget *paren modelFrames = frames; - connect(ui->btnLoadScript, SIGNAL(pressed()), this, SLOT(loadNewScript())); - connect(ui->btnNewScript, SIGNAL(pressed()), this, SLOT(createNewScript())); - connect(ui->btnRecompile, SIGNAL(pressed()), this, SLOT(recompileScript())); - connect(ui->btnRemoveScript, SIGNAL(pressed()), this, SLOT(deleteCurrentScript())); - connect(ui->btnRevertScript, SIGNAL(pressed()), this, SLOT(revertScript())); - connect(ui->btnSaveScript, SIGNAL(pressed()), this, SLOT(saveScript())); - - currentScript = new ScriptContainer(); - currentScript->setErrorWidget(ui->listErrors); + connect(ui->btnLoadScript, &QAbstractButton::pressed, this, &ScriptingWindow::loadNewScript); + connect(ui->btnNewScript, &QAbstractButton::pressed, this, &ScriptingWindow::createNewScript); + connect(ui->btnRecompile, &QAbstractButton::pressed, this, &ScriptingWindow::recompileScript); + connect(ui->btnRemoveScript, &QAbstractButton::pressed, this, &ScriptingWindow::deleteCurrentScript); + connect(ui->btnRevertScript, &QAbstractButton::pressed, this, &ScriptingWindow::revertScript); + connect(ui->btnSaveScript, &QAbstractButton::pressed, this, &ScriptingWindow::saveScript); + connect(MainWindow::getReference(), &MainWindow::framesUpdated, this, &ScriptingWindow::updatedFrames); } ScriptingWindow::~ScriptingWindow() @@ -28,6 +28,25 @@ ScriptingWindow::~ScriptingWindow() delete ui; } +void ScriptingWindow::updatedFrames(int numFrames) +{ + CANFrame thisFrame; + //-1 means all frames deleted and -2 means a full refresh, neither of which we care about here. + if (numFrames > 0) + { + //for every new frame pass it on to each script container. The container will determine if it needs to actually + //notify the script and do that if applicable. + for (int i = modelFrames->count() - numFrames; i < modelFrames->count(); i++) + { + thisFrame = modelFrames->at(i); + for (int j = 0; j < scripts.length(); j++) + { + scripts[j]->gotFrame(thisFrame); + } + } + } +} + void ScriptingWindow::loadNewScript() { QString filename; @@ -63,6 +82,7 @@ void ScriptingWindow::loadNewScript() container->scriptText = contents; container->setErrorWidget(ui->listErrors); container->compileScript(); + connect(container, &ScriptContainer::sendCANFrame, this, &ScriptingWindow::sendCANFrame); scripts.append(container); currentScript = container; ui->txtScriptSource->setText(container->scriptText); @@ -82,6 +102,7 @@ void ScriptingWindow::createNewScript() container->filePath = QString(); container->scriptText = QString(); container->setErrorWidget(ui->listErrors); + connect(container, &ScriptContainer::sendCANFrame, this, &ScriptingWindow::sendCANFrame); scripts.append(container); ui->listLoadedScripts->addItem(container->fileName); currentScript = container; diff --git a/scriptingwindow.h b/scriptingwindow.h index 4f22260..0320c19 100644 --- a/scriptingwindow.h +++ b/scriptingwindow.h @@ -27,9 +27,12 @@ private slots: void saveScript(); void revertScript(); void recompileScript(); + void updatedFrames(int); public slots: +signals: + void sendCANFrame(const CANFrame *, int); private: Ui::ScriptingWindow *ui; diff --git a/serialworker.cpp b/serialworker.cpp index 4b4ad23..315a71f 100644 --- a/serialworker.cpp +++ b/serialworker.cpp @@ -164,6 +164,8 @@ void SerialWorker::sendFrame(const CANFrame *frame, int bus = 0) canModel->addFrame(tempFrame, false); gotFrames++; + if (serial == NULL) return; + if (!serial->isOpen()) return; if (!connected) return; ID = frame->ID; @@ -183,8 +185,6 @@ void SerialWorker::sendFrame(const CANFrame *frame, int bus = 0) } buffer[8 + frame->len] = 0; - if (serial == NULL) return; - if (!serial->isOpen()) return; //qDebug() << "writing " << buffer.length() << " bytes to serial port"; serial->write(buffer); } @@ -193,7 +193,7 @@ void SerialWorker::sendFrame(const CANFrame *frame, int bus = 0) //Don't get carried away here. The GVRET firmware only has finite //buffers and besides, the other end will get buried in traffic. void SerialWorker::sendFrameBatch(const QList *frames) -{ +{ for (int i = 0; i < frames->length(); i++) sendFrame(&frames->at(i), frames->at(i).bus); }