Bug fixes, turn some connections into QT5 syntax, more implementation of scripting interface.

This commit is contained in:
Collin Kidder
2015-12-02 15:53:26 -05:00
parent f6b5db9617
commit c2e5df6087
6 changed files with 67 additions and 18 deletions
+3 -2
View File
@@ -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();
}
+24 -3
View File
@@ -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
}
}
+4 -1
View File
@@ -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;
+30 -9
View File
@@ -4,6 +4,8 @@
#include <QFile>
#include <QFileDialog>
#include "mainwindow.h"
ScriptingWindow::ScriptingWindow(const QVector<CANFrame> *frames, QWidget *parent) :
QDialog(parent),
ui(new Ui::ScriptingWindow)
@@ -12,15 +14,13 @@ ScriptingWindow::ScriptingWindow(const QVector<CANFrame> *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;
+3
View File
@@ -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;
+2 -2
View File
@@ -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);
}