diff --git a/connectionwindow.cpp b/connectionwindow.cpp index d9237e6..69b42c6 100644 --- a/connectionwindow.cpp +++ b/connectionwindow.cpp @@ -1,13 +1,18 @@ #include +#include #include "connectionwindow.h" #include "ui_connectionwindow.h" #include "connections/canconfactory.h" +#define FALSE 0 +#define TRUE 1 + ConnectionWindow::ConnectionWindow(CANFrameModel *cModel, QWidget *parent) : QDialog(parent), - ui(new Ui::ConnectionWindow) + ui(new Ui::ConnectionWindow), + mRefreshReqOngoing(FALSE) { ui->setupUi(this); @@ -75,6 +80,11 @@ ConnectionWindow::ConnectionWindow(CANFrameModel *cModel, QWidget *parent) : mTicker.setInterval(500); /*tick twice a second */ mTicker.setSingleShot(false); mTicker.start(); + + /*test*/ + /* retrieve pointer on method */ + int methodIndex = metaObject()->indexOfMethod(QMetaObject::normalizedSignature("refreshCanList()")); + mRefreshM = metaObject()->method(methodIndex); } ConnectionWindow::~ConnectionWindow() @@ -104,17 +114,33 @@ void ConnectionWindow::showEvent(QShowEvent* event) } void ConnectionWindow::refreshCanList() { - QList::iterator conn_p; - QList& conns = connModel->getConnections(); + QList& conns = connModel->getConnections(); CANFrame* frame_p = NULL; - for (conn_p = conns.begin(); conn_p != conns.end(); ++conn_p) { - while( (frame_p = (*conn_p)->getQueue().peek() ) ) { + foreach (CANConnection* conn_p, conns) + { + while( (frame_p = conn_p->getQueue().peek() ) ) { canModel->addFrame(*frame_p, true); - (*conn_p)->getQueue().dequeue(); + conn_p->getQueue().dequeue(); } } + + /* erase flag (this should be done before we start dequeuing...) */ + mRefreshReqOngoing.store(FALSE); +} + +/*test*/ +void ConnectionWindow::callback(CANCon::cbtype pType) +{ + if(mRefreshReqOngoing.testAndSetRelaxed(FALSE, TRUE)) + { + /* ask for a refresh */ + mRefreshM.invoke(this, Qt::AutoConnection); + } + else { + //qDebug() << "skip event"; + } } void ConnectionWindow::handleNewConn() @@ -272,8 +298,16 @@ void ConnectionWindow::handleOKButton() connect(conn_p, SIGNAL(status(CANCon::status)), this, SLOT(connectionStatus(CANCon::status))); + //conn_p->setCallback(std::bind(&ConnectionWindow::callback, this, std::placeholders::_1)); + /*TODO add return value and checks */ conn_p->start(); + /*{ + QVector flters; + flters.append({0x305, 0xFFFF, true}); + conn_p->setFilters(0, flters, false); + connect(conn_p, SIGNAL(notify()), this, SLOT(refreshCanList())); + }*/ for (int i=0 ; igetNumBuses() ; i++) { /* set bus configuration */ diff --git a/connectionwindow.h b/connectionwindow.h index 066cad0..91f292d 100644 --- a/connectionwindow.h +++ b/connectionwindow.h @@ -8,8 +8,9 @@ #include #include #include -//#include "canconnection_old.h" -//#include "serialworker.h" +/*test*/ +#include +/******/ #include "canconnectionmodel.h" #include "canframemodel.h" @@ -33,6 +34,7 @@ public: QString getPortName(); //name of port to connect to CANCon::type getConnectionType(); bool getSWMode(); + void callback(CANCon::cbtype pType); signals: void updateBusSettings(CANBus *bus); @@ -68,6 +70,11 @@ private: CANFrameModel *canModel; QTimer mTicker; + QAtomicInt mRefreshReqOngoing; + + /*test*/ + QMetaMethod mRefreshM; + void selectSerial(); void selectKvaser(); diff --git a/mainwindow.cpp b/mainwindow.cpp index b456467..01fd54c 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -157,7 +157,7 @@ MainWindow::MainWindow(QWidget *parent) : ui->lbNumFrames->setText("0"); connect(&updateTimer, &QTimer::timeout, this, &MainWindow::tickGUIUpdate); - updateTimer.setInterval(250); + updateTimer.setInterval(500); //test 250); updateTimer.start(); elapsedTime = new QTime;