Changeset View
Changeset View
Standalone View
Standalone View
src/core/atcore.cpp
Show First 20 Lines • Show All 105 Lines • ▼ Show 20 Line(s) | |||||
106 | #if defined GIT_REVISION | 106 | #if defined GIT_REVISION | ||
107 | if (!QStringLiteral(GIT_REVISION).isEmpty()) { | 107 | if (!QStringLiteral(GIT_REVISION).isEmpty()) { | ||
108 | versionString.append(QString::fromLatin1("-%1").arg(QStringLiteral(GIT_REVISION))); | 108 | versionString.append(QString::fromLatin1("-%1").arg(QStringLiteral(GIT_REVISION))); | ||
109 | } | 109 | } | ||
110 | #endif | 110 | #endif | ||
111 | return versionString; | 111 | return versionString; | ||
112 | } | 112 | } | ||
113 | 113 | | |||
114 | SerialLayer *AtCore::serial() const | | |||
115 | { | | |||
116 | return d->serial; | | |||
117 | } | | |||
118 | | ||||
119 | IFirmware *AtCore::firmwarePlugin() const | 114 | IFirmware *AtCore::firmwarePlugin() const | ||
120 | { | 115 | { | ||
121 | return d->firmwarePlugin; | 116 | return d->firmwarePlugin; | ||
122 | } | 117 | } | ||
123 | 118 | | |||
124 | void AtCore::close() | 119 | void AtCore::close() | ||
125 | { | 120 | { | ||
126 | exit(0); | 121 | exit(0); | ||
▲ Show 20 Lines • Show All 68 Lines • ▼ Show 20 Line(s) | 189 | if (!d->pluginLoader.load()) { | |||
195 | //Plugin was not loaded, Provide some debug info. | 190 | //Plugin was not loaded, Provide some debug info. | ||
196 | qCDebug(ATCORE_CORE) << "Plugin Loading: Failed."; | 191 | qCDebug(ATCORE_CORE) << "Plugin Loading: Failed."; | ||
197 | qCDebug(ATCORE_CORE) << d->pluginLoader.errorString(); | 192 | qCDebug(ATCORE_CORE) << d->pluginLoader.errorString(); | ||
198 | setState(AtCore::STATES::CONNECTING); | 193 | setState(AtCore::STATES::CONNECTING); | ||
199 | } else { | 194 | } else { | ||
200 | //Plugin was loaded successfully. | 195 | //Plugin was loaded successfully. | ||
201 | d->firmwarePlugin = qobject_cast<IFirmware *>(d->pluginLoader.instance()); | 196 | d->firmwarePlugin = qobject_cast<IFirmware *>(d->pluginLoader.instance()); | ||
202 | firmwarePlugin()->init(this); | 197 | firmwarePlugin()->init(this); | ||
203 | disconnect(serial(), &SerialLayer::receivedCommand, this, &AtCore::findFirmware); | 198 | disconnect(d->serial, &SerialLayer::receivedCommand, this, &AtCore::findFirmware); | ||
204 | connect(serial(), &SerialLayer::receivedCommand, this, &AtCore::newMessage); | 199 | connect(d->serial, &SerialLayer::receivedCommand, this, &AtCore::newMessage); | ||
205 | connect(firmwarePlugin(), &IFirmware::readyForCommand, this, &AtCore::processQueue); | 200 | connect(firmwarePlugin(), &IFirmware::readyForCommand, this, &AtCore::processQueue); | ||
206 | d->ready = true; // ready on new firmware load | 201 | d->ready = true; // ready on new firmware load | ||
207 | if (firmwarePlugin()->name() != QStringLiteral("Grbl")) { | 202 | if (firmwarePlugin()->name() != QStringLiteral("Grbl")) { | ||
208 | connect(d->tempTimer, &QTimer::timeout, this, &AtCore::checkTemperature); | 203 | connect(d->tempTimer, &QTimer::timeout, this, &AtCore::checkTemperature); | ||
209 | d->tempTimer->start(); | 204 | d->tempTimer->start(); | ||
210 | } | 205 | } | ||
211 | setState(IDLE); | 206 | setState(IDLE); | ||
212 | } | 207 | } | ||
213 | } else { | 208 | } else { | ||
214 | qCDebug(ATCORE_CORE) << "Plugin:" << fwName << ": Not found."; | 209 | qCDebug(ATCORE_CORE) << "Plugin:" << fwName << ": Not found."; | ||
215 | emit atcoreMessage(tr("No plugin found for %1.").arg(fwName)); | 210 | emit atcoreMessage(tr("No plugin found for %1.").arg(fwName)); | ||
216 | } | 211 | } | ||
217 | } | 212 | } | ||
218 | 213 | | |||
219 | bool AtCore::initSerial(const QString &port, int baud, bool disableROC) | 214 | bool AtCore::initSerial(const QString &port, int baud, bool disableROC) | ||
220 | { | 215 | { | ||
221 | if (disableROC) { | 216 | if (disableROC) { | ||
222 | disableResetOnConnect(port); | 217 | disableResetOnConnect(port); | ||
223 | } | 218 | } | ||
224 | 219 | | |||
225 | d->serial = new SerialLayer(port, baud); | 220 | d->serial = new SerialLayer(port, baud); | ||
226 | connect(serial(), &SerialLayer::serialError, this, &AtCore::handleSerialError); | 221 | connect(d->serial, &SerialLayer::serialError, this, &AtCore::handleSerialError); | ||
227 | if (serialInitialized() && d->serial->isWritable()) { | 222 | if (serialInitialized() && d->serial->isWritable()) { | ||
228 | setState(AtCore::STATES::CONNECTING); | 223 | setState(AtCore::CONNECTING); | ||
229 | connect(serial(), &SerialLayer::receivedCommand, this, &AtCore::findFirmware); | 224 | connect(d->serial, &SerialLayer::pushedCommand, this, &AtCore::newCommand); | ||
225 | connect(d->serial, &SerialLayer::receivedCommand, this, &AtCore::findFirmware); | ||||
230 | d->serialTimer->stop(); | 226 | d->serialTimer->stop(); | ||
231 | return true; | 227 | return true; | ||
232 | } else { | 228 | } else { | ||
233 | qCDebug(ATCORE_CORE) << "Failed to open device for Read / Write."; | 229 | qCDebug(ATCORE_CORE) << "Failed to open device for Read / Write."; | ||
234 | emit atcoreMessage(tr("Failed to open device in read/write mode.")); | 230 | emit atcoreMessage(tr("Failed to open device in read/write mode.")); | ||
235 | return false; | 231 | return false; | ||
236 | } | 232 | } | ||
237 | } | 233 | } | ||
238 | 234 | | |||
239 | bool AtCore::serialInitialized() const | 235 | bool AtCore::serialInitialized() const | ||
240 | { | 236 | { | ||
241 | if (!d->serial) { | 237 | if (!d->serial) { | ||
242 | return false; | 238 | return false; | ||
243 | } | 239 | } | ||
244 | return d->serial->isOpen(); | 240 | return d->serial->isOpen(); | ||
245 | } | 241 | } | ||
246 | 242 | | |||
247 | QString AtCore::connectedPort() const | 243 | QString AtCore::connectedPort() const | ||
248 | { | 244 | { | ||
249 | return serial()->portName(); | 245 | return d->serial->portName(); | ||
250 | } | 246 | } | ||
251 | 247 | | |||
252 | QStringList AtCore::serialPorts() const | 248 | QStringList AtCore::serialPorts() const | ||
253 | { | 249 | { | ||
254 | QStringList ports; | 250 | QStringList ports; | ||
255 | QList<QSerialPortInfo> serialPortInfoList = QSerialPortInfo::availablePorts(); | 251 | QList<QSerialPortInfo> serialPortInfoList = QSerialPortInfo::availablePorts(); | ||
256 | if (!serialPortInfoList.isEmpty()) { | 252 | if (!serialPortInfoList.isEmpty()) { | ||
257 | for (const QSerialPortInfo &serialPortInfo : serialPortInfoList) { | 253 | for (const QSerialPortInfo &serialPortInfo : serialPortInfoList) { | ||
▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Line(s) | 304 | { | |||
317 | 313 | | |||
318 | //Check if have temperature info and decode it | 314 | //Check if have temperature info and decode it | ||
319 | if (d->lastMessage.contains("T:") || d->lastMessage.contains("B:")) { | 315 | if (d->lastMessage.contains("T:") || d->lastMessage.contains("B:")) { | ||
320 | temperature().decodeTemp(message); | 316 | temperature().decodeTemp(message); | ||
321 | } | 317 | } | ||
322 | emit receivedMessage(d->lastMessage); | 318 | emit receivedMessage(d->lastMessage); | ||
323 | } | 319 | } | ||
324 | 320 | | |||
321 | void AtCore::newCommand(const QByteArray &command) | ||||
322 | { | ||||
323 | emit pushedCommand(command); | ||||
324 | } | ||||
325 | | ||||
325 | void AtCore::setRelativePosition() | 326 | void AtCore::setRelativePosition() | ||
326 | { | 327 | { | ||
327 | pushCommand(GCode::toCommand(GCode::GCommands::G91)); | 328 | pushCommand(GCode::toCommand(GCode::GCommands::G91)); | ||
328 | } | 329 | } | ||
329 | 330 | | |||
330 | void AtCore::setAbsolutePosition() | 331 | void AtCore::setAbsolutePosition() | ||
331 | { | 332 | { | ||
332 | pushCommand(GCode::toCommand(GCode::GCommands::G90)); | 333 | pushCommand(GCode::toCommand(GCode::GCommands::G90)); | ||
▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Line(s) | 389 | { | |||
389 | if (serialInitialized()) { | 390 | if (serialInitialized()) { | ||
390 | if (AtCore::state() == AtCore::STATES::BUSY && !d->sdCardPrinting) { | 391 | if (AtCore::state() == AtCore::STATES::BUSY && !d->sdCardPrinting) { | ||
391 | //We have to clean up the print job if printing from the host. | 392 | //We have to clean up the print job if printing from the host. | ||
392 | //However disconnecting while printing from sd card should not affect the print job. | 393 | //However disconnecting while printing from sd card should not affect the print job. | ||
393 | setState(AtCore::STATES::STOP); | 394 | setState(AtCore::STATES::STOP); | ||
394 | } | 395 | } | ||
395 | if (firmwarePluginLoaded()) { | 396 | if (firmwarePluginLoaded()) { | ||
396 | disconnect(firmwarePlugin(), &IFirmware::readyForCommand, this, &AtCore::processQueue); | 397 | disconnect(firmwarePlugin(), &IFirmware::readyForCommand, this, &AtCore::processQueue); | ||
397 | disconnect(serial(), &SerialLayer::receivedCommand, this, &AtCore::newMessage); | 398 | disconnect(d->serial, &SerialLayer::receivedCommand, this, &AtCore::newMessage); | ||
398 | if (firmwarePlugin()->name() != QStringLiteral("Grbl")) { | 399 | if (firmwarePlugin()->name() != QStringLiteral("Grbl")) { | ||
399 | disconnect(d->tempTimer, &QTimer::timeout, this, &AtCore::checkTemperature); | 400 | disconnect(d->tempTimer, &QTimer::timeout, this, &AtCore::checkTemperature); | ||
400 | d->tempTimer->stop(); | 401 | d->tempTimer->stop(); | ||
401 | } | 402 | } | ||
402 | //Attempt to unload the firmware plugin. | 403 | //Attempt to unload the firmware plugin. | ||
403 | QString name = firmwarePlugin()->name(); | 404 | QString name = firmwarePlugin()->name(); | ||
404 | QString msg = d->pluginLoader.unload() ? QStringLiteral("closed.") : QStringLiteral("Failed to close."); | 405 | QString msg = d->pluginLoader.unload() ? QStringLiteral("closed.") : QStringLiteral("Failed to close."); | ||
405 | qCDebug(ATCORE_CORE) << QStringLiteral("Firmware plugin %1 %2").arg(name, msg); | 406 | qCDebug(ATCORE_CORE) << QStringLiteral("Firmware plugin %1 %2").arg(name, msg); | ||
406 | } | 407 | } | ||
407 | //Do not reset the connect on disconnect when closing this will cause a reset on connect for the next connection. | 408 | //Do not reset the connect on disconnect when closing this will cause a reset on connect for the next connection. | ||
408 | disconnect(serial(), &SerialLayer::serialError, this, &AtCore::handleSerialError); | 409 | disconnect(d->serial, &SerialLayer::serialError, this, &AtCore::handleSerialError); | ||
409 | serial()->close(); | 410 | disconnect(d->serial, &SerialLayer::pushedCommand, this, &AtCore::newMessage); | ||
411 | d->serial->close(); | ||||
410 | //Clear our copy of the sdcard filelist | 412 | //Clear our copy of the sdcard filelist | ||
411 | clearSdCardFileList(); | 413 | clearSdCardFileList(); | ||
412 | setState(AtCore::STATES::DISCONNECTED); | 414 | setState(AtCore::STATES::DISCONNECTED); | ||
413 | d->serialTimer->start(); | 415 | d->serialTimer->start(); | ||
414 | } | 416 | } | ||
415 | } | 417 | } | ||
416 | 418 | | |||
417 | AtCore::STATES AtCore::state(void) | 419 | AtCore::STATES AtCore::state(void) | ||
Show All 39 Lines | 454 | { | |||
457 | d->commandQueue.clear(); | 459 | d->commandQueue.clear(); | ||
458 | if (AtCore::state() == AtCore::STATES::BUSY) { | 460 | if (AtCore::state() == AtCore::STATES::BUSY) { | ||
459 | if (!d->sdCardPrinting) { | 461 | if (!d->sdCardPrinting) { | ||
460 | //Stop our running print thread | 462 | //Stop our running print thread | ||
461 | setState(AtCore::STATES::STOP); | 463 | setState(AtCore::STATES::STOP); | ||
462 | } | 464 | } | ||
463 | } | 465 | } | ||
464 | //push command through serial to bypass atcore's queue. | 466 | //push command through serial to bypass atcore's queue. | ||
465 | serial()->pushCommand(GCode::toCommand(GCode::MCommands::M112).toLocal8Bit()); | 467 | d->serial->pushCommand(GCode::toCommand(GCode::MCommands::M112).toLocal8Bit()); | ||
466 | } | 468 | } | ||
467 | 469 | | |||
468 | void AtCore::stopSdPrint() | 470 | void AtCore::stopSdPrint() | ||
469 | { | 471 | { | ||
470 | //Stop an SdCard Print. | 472 | //Stop an SdCard Print. | ||
471 | pushCommand(GCode::toCommand(GCode::MCommands::M25)); | 473 | pushCommand(GCode::toCommand(GCode::MCommands::M25)); | ||
472 | d->sdCardFileName = QString(); | 474 | d->sdCardFileName = QString(); | ||
473 | pushCommand(GCode::toCommand(GCode::MCommands::M23, d->sdCardFileName)); | 475 | pushCommand(GCode::toCommand(GCode::MCommands::M23, d->sdCardFileName)); | ||
474 | AtCore::setState(AtCore::STATES::FINISHEDPRINT); | 476 | AtCore::setState(AtCore::STATES::FINISHEDPRINT); | ||
475 | AtCore::setState(AtCore::STATES::IDLE); | 477 | AtCore::setState(AtCore::STATES::IDLE); | ||
476 | } | 478 | } | ||
477 | 479 | | |||
478 | void AtCore::requestFirmware() | 480 | void AtCore::requestFirmware() | ||
479 | { | 481 | { | ||
480 | if (serialInitialized()) { | 482 | if (serialInitialized()) { | ||
481 | qCDebug(ATCORE_CORE) << "Sending " << GCode::description(GCode::MCommands::M115); | 483 | qCDebug(ATCORE_CORE) << "Sending " << GCode::description(GCode::M115); | ||
482 | serial()->pushCommand(GCode::toCommand(GCode::MCommands::M115).toLocal8Bit()); | 484 | d->serial->pushCommand(GCode::toCommand(GCode::MCommands::M115).toLocal8Bit()); | ||
483 | } else { | 485 | } else { | ||
484 | qCDebug(ATCORE_CORE) << "There is no open device to send commands"; | 486 | qCDebug(ATCORE_CORE) << "There is no open device to send commands"; | ||
485 | } | 487 | } | ||
486 | } | 488 | } | ||
487 | 489 | | |||
488 | bool AtCore::firmwarePluginLoaded() const | 490 | bool AtCore::firmwarePluginLoaded() const | ||
489 | { | 491 | { | ||
490 | if (firmwarePlugin()) { | 492 | if (firmwarePlugin()) { | ||
▲ Show 20 Lines • Show All 172 Lines • ▼ Show 20 Line(s) | 658 | { | |||
663 | if (!serialInitialized()) { | 665 | if (!serialInitialized()) { | ||
664 | qCDebug(ATCORE_PLUGIN) << "Can't process queue ! Serial not initialized."; | 666 | qCDebug(ATCORE_PLUGIN) << "Can't process queue ! Serial not initialized."; | ||
665 | return; | 667 | return; | ||
666 | } | 668 | } | ||
667 | 669 | | |||
668 | QString text = d->commandQueue.takeAt(0); | 670 | QString text = d->commandQueue.takeAt(0); | ||
669 | 671 | | |||
670 | if (firmwarePluginLoaded()) { | 672 | if (firmwarePluginLoaded()) { | ||
671 | serial()->pushCommand(firmwarePlugin()->translate(text)); | 673 | d->serial->pushCommand(firmwarePlugin()->translate(text)); | ||
672 | } else { | 674 | } else { | ||
673 | serial()->pushCommand(text.toLocal8Bit()); | 675 | d->serial->pushCommand(text.toLocal8Bit()); | ||
674 | } | 676 | } | ||
675 | d->ready = false; | 677 | d->ready = false; | ||
676 | } | 678 | } | ||
677 | 679 | | |||
678 | void AtCore::checkTemperature() | 680 | void AtCore::checkTemperature() | ||
679 | { | 681 | { | ||
680 | //One request for the temperature in the queue at a time. | 682 | //One request for the temperature in the queue at a time. | ||
681 | if (d->commandQueue.contains(GCode::toCommand(GCode::MCommands::M105))) { | 683 | if (d->commandQueue.contains(GCode::toCommand(GCode::MCommands::M105))) { | ||
Show All 18 Lines | 697 | { | |||
700 | case AtCore::UNITS::IMPERIAL: | 702 | case AtCore::UNITS::IMPERIAL: | ||
701 | pushCommand(GCode::toCommand(GCode::GCommands::G20)); | 703 | pushCommand(GCode::toCommand(GCode::GCommands::G20)); | ||
702 | break; | 704 | break; | ||
703 | } | 705 | } | ||
704 | } | 706 | } | ||
705 | 707 | | |||
706 | QStringList AtCore::portSpeeds() const | 708 | QStringList AtCore::portSpeeds() const | ||
707 | { | 709 | { | ||
708 | return serial()->validBaudRates(); | 710 | return d->serial->validBaudRates(); | ||
709 | } | 711 | } | ||
710 | 712 | | |||
711 | void AtCore::disableMotors(uint delay) | 713 | void AtCore::disableMotors(uint delay) | ||
712 | { | 714 | { | ||
713 | //Disables motors | 715 | //Disables motors | ||
714 | if (delay) { | 716 | if (delay) { | ||
715 | pushCommand(GCode::toCommand(GCode::MCommands::M84, QString::number(delay))); | 717 | pushCommand(GCode::toCommand(GCode::MCommands::M84, QString::number(delay))); | ||
716 | } else { | 718 | } else { | ||
▲ Show 20 Lines • Show All 138 Lines • Show Last 20 Lines |