diff --git a/src/atcore.h b/src/atcore.h --- a/src/atcore.h +++ b/src/atcore.h @@ -53,7 +53,8 @@ { Q_OBJECT Q_PROPERTY(QStringList availableFirmwarePlugins READ availableFirmwarePlugins) - Q_PROPERTY(QStringList serialPorts READ serialPorts) + Q_PROPERTY(quint16 serialTimerInterval READ serialTimerInterval WRITE setSerialTimerInterval) + Q_PROPERTY(QStringList serialPorts READ serialPorts NOTIFY portsChanged) Q_PROPERTY(QStringList portSpeeds READ portSpeeds) Q_PROPERTY(QString connectedPort READ connectedPort) Q_PROPERTY(AtCore::STATES state READ state WRITE setState NOTIFY stateChanged) @@ -187,6 +188,11 @@ */ Temperature &temperature() const; + /** + * @brief Return the amount of miliseconds the serialTimer is set to. 0 = Disabled + */ + quint16 serialTimerInterval() const; + signals: /** @@ -209,6 +215,11 @@ */ void stateChanged(AtCore::STATES newState); + /** + * @brief Available serialports Changed + */ + void portsChanged(QStringList); + public slots: /** @@ -347,6 +358,12 @@ */ void setUnits(AtCore::UNITS units); + /** + * @brief Set the time between checks for new serialPorts (0 is default) + * @param newTime: Milliseconds between checks. 0 will Disable Checks. + */ + void setSerialTimerInterval(const quint16 &newTime); + private slots: /** * @brief processQueue send commands from the queue. @@ -371,6 +388,11 @@ */ void findFirmware(const QByteArray &message); + /** + * @brief Search for new serial ports + */ + void locateSerialPort(); + private: /** * @brief True if a firmware plugin is loaded diff --git a/src/atcore.cpp b/src/atcore.cpp --- a/src/atcore.cpp +++ b/src/atcore.cpp @@ -59,6 +59,8 @@ float percentage; //!< @param percentage: print job percent QByteArray posString; //!< @param posString: stored string from last M114 return AtCore::STATES printerState; //!< @param printerState: State of the Printer + QStringList serialPorts; //!< @param seralPorts: Detected serial Ports + QTimer *serialTimer = nullptr; //!< @param serialTimer: Timer connected to locateSerialPorts }; AtCore::AtCore(QObject *parent) : @@ -241,9 +243,43 @@ #endif } } + return ports; } +void AtCore::locateSerialPort() +{ + QStringList ports = serialPorts(); + if (d->serialPorts != ports) { + d->serialPorts = ports; + emit portsChanged(d->serialPorts); + } +} + +quint16 AtCore::serialTimerInterval() const +{ + if (d->serialTimer != nullptr) { + return d->serialTimer->interval(); + } + return 0; +} + +void AtCore::setSerialTimerInterval(const quint16 &newTime) +{ + if (newTime == 0) { + if (d->serialTimer) { + disconnect(d->serialTimer, &QTimer::timeout, this, &AtCore::locateSerialPort); + delete d->serialTimer; + } + return; + } + if (!d->serialTimer) { + d->serialTimer = new QTimer(); + connect(d->serialTimer, &QTimer::timeout, this, &AtCore::locateSerialPort); + } + d->serialTimer->start(newTime); +} + void AtCore::newMessage(const QByteArray &message) { d->lastMessage = message; diff --git a/testclient/mainwindow.h b/testclient/mainwindow.h --- a/testclient/mainwindow.h +++ b/testclient/mainwindow.h @@ -178,7 +178,6 @@ Ui::MainWindow *ui; AtCore *core; QTemporaryFile *logFile; - QStringList serialPortList; QTime *printTime; QTimer *printTimer; // Define max number of fans @@ -189,7 +188,7 @@ * @brief Locate serial port * */ - void locateSerialPort(); + void locateSerialPort(const QStringList &ports); /** * @brief Return string with actual time diff --git a/testclient/mainwindow.cpp b/testclient/mainwindow.cpp --- a/testclient/mainwindow.cpp +++ b/testclient/mainwindow.cpp @@ -56,7 +56,7 @@ ui->moveDockContents->layout()->addWidget(axisControl); addLog(tr("Attempting to locate Serial Ports")); - + core->setSerialTimerInterval(1000); populateCBs(); //Icon for actionQuit @@ -67,8 +67,6 @@ //hide the printing progress bar. ui->printLayout->setVisible(false); - locateSerialPort(); - ui->statusBar->addWidget(ui->statusBarWidget); printTime = new QTime(); printTimer = new QTimer(); @@ -98,13 +96,7 @@ connect(ui->stopPB, &QPushButton::clicked, core, &AtCore::stop); connect(ui->emergencyStopPB, &QPushButton::clicked, core, &AtCore::emergencyStop); connect(axisControl, &AxisControl::clicked, this, &MainWindow::axisControlClicked); - - //We love solid, but we need a release :/ - QTimer *timer = new QTimer(); - timer->setInterval(1e3); - timer->start(); - connect(timer, &QTimer::timeout, this, &MainWindow::locateSerialPort); - + connect(core, &AtCore::portsChanged, this, &MainWindow::locateSerialPort); connect(core, &AtCore::printProgressChanged, this, &MainWindow::printProgressChanged); connect(&core->temperature(), &Temperature::bedTemperatureChanged, [ = ](float temp) { @@ -274,21 +266,13 @@ * Locate all active serial ports on the computer and add to the list * of serial ports */ -void MainWindow::locateSerialPort() +void MainWindow::locateSerialPort(const QStringList &ports) { - if (!core->serialPorts().isEmpty()) { - if (core->serialPorts() == serialPortList) { - return; - } else { - serialPortList.clear(); - serialPortList = core->serialPorts(); - ui->serialPortCB->clear(); - ui->serialPortCB->addItems(serialPortList); - addLog(tr("Found %1 Ports").arg(QString::number(serialPortList.count()))); - } + ui->serialPortCB->clear(); + if (!ports.isEmpty()) { + ui->serialPortCB->addItems(ports); + addLog(tr("Found %1 Ports").arg(QString::number(ports.count()))); } else { - serialPortList.clear(); - ui->serialPortCB->clear(); QString portError(tr("No available ports! Please connect a serial device to continue!")); if (! ui->logTE->toPlainText().endsWith(portError)) { addLog(portError);