Changeset View
Changeset View
Standalone View
Standalone View
src/core/atcore.cpp
Show All 30 Lines | |||||
31 | #include <QTime> | 31 | #include <QTime> | ||
32 | #include <QTimer> | 32 | #include <QTimer> | ||
33 | #include <QThread> | 33 | #include <QThread> | ||
34 | #include <QMetaEnum> | 34 | #include <QMetaEnum> | ||
35 | #include <QProcess> | 35 | #include <QProcess> | ||
36 | 36 | | |||
37 | #include "atcore.h" | 37 | #include "atcore.h" | ||
38 | #include "atcore_version.h" | 38 | #include "atcore_version.h" | ||
39 | #include "copythread.h" | ||||
39 | #include "seriallayer.h" | 40 | #include "seriallayer.h" | ||
40 | #include "gcodecommands.h" | 41 | #include "gcodecommands.h" | ||
41 | #include "printthread.h" | 42 | #include "printthread.h" | ||
42 | #include "atcore_default_folders.h" | 43 | #include "atcore_default_folders.h" | ||
43 | 44 | | |||
44 | Q_LOGGING_CATEGORY(ATCORE_PLUGIN, "org.kde.atelier.core.plugin") | 45 | Q_LOGGING_CATEGORY(ATCORE_PLUGIN, "org.kde.atelier.core.plugin") | ||
45 | Q_LOGGING_CATEGORY(ATCORE_CORE, "org.kde.atelier.core") | 46 | Q_LOGGING_CATEGORY(ATCORE_CORE, "org.kde.atelier.core") | ||
46 | /** | 47 | /** | ||
Show All 14 Lines | 51 | struct AtCore::AtCorePrivate { | |||
61 | float percentage; //!< @param percentage: print job percent | 62 | float percentage; //!< @param percentage: print job percent | ||
62 | QByteArray posString; //!< @param posString: stored string from last M114 return | 63 | QByteArray posString; //!< @param posString: stored string from last M114 return | ||
63 | AtCore::STATES printerState; //!< @param printerState: State of the Printer | 64 | AtCore::STATES printerState; //!< @param printerState: State of the Printer | ||
64 | QStringList serialPorts; //!< @param seralPorts: Detected serial Ports | 65 | QStringList serialPorts; //!< @param seralPorts: Detected serial Ports | ||
65 | QTimer *serialTimer = nullptr; //!< @param serialTimer: Timer connected to locateSerialPorts | 66 | QTimer *serialTimer = nullptr; //!< @param serialTimer: Timer connected to locateSerialPorts | ||
66 | bool sdCardMounted = false; //!< @param sdCardMounted: True if Sd Card is mounted. | 67 | bool sdCardMounted = false; //!< @param sdCardMounted: True if Sd Card is mounted. | ||
67 | bool sdCardReadingFileList = false; //!< @param sdCardReadingFileList: True while getting file names from sd card | 68 | bool sdCardReadingFileList = false; //!< @param sdCardReadingFileList: True while getting file names from sd card | ||
68 | bool sdCardPrinting = false; //!< @param sdCardPrinting: True if currently printing from sd card. | 69 | bool sdCardPrinting = false; //!< @param sdCardPrinting: True if currently printing from sd card. | ||
70 | bool sdWriting = false; //!< @param sdWriting: True if writing to sd card. | ||||
69 | QString sdCardFileName; //!< @param sdCardFileName: name of file being used from sd card. | 71 | QString sdCardFileName; //!< @param sdCardFileName: name of file being used from sd card. | ||
70 | QStringList sdCardFileList; //!< @param sdCardFileList: List of files on sd card. | 72 | QStringList sdCardFileList; //!< @param sdCardFileList: List of files on sd card. | ||
71 | }; | 73 | }; | ||
72 | 74 | | |||
73 | AtCore::AtCore(QObject *parent) : | 75 | AtCore::AtCore(QObject *parent) : | ||
74 | QObject(parent), | 76 | QObject(parent), | ||
75 | d(new AtCorePrivate) | 77 | d(new AtCorePrivate) | ||
76 | { | 78 | { | ||
▲ Show 20 Lines • Show All 683 Lines • ▼ Show 20 Line(s) | 761 | { | |||
760 | pushCommand(GCode::toCommand(GCode::M22, QString::number(slot))); | 762 | pushCommand(GCode::toCommand(GCode::M22, QString::number(slot))); | ||
761 | } | 763 | } | ||
762 | 764 | | |||
763 | bool AtCore::isReadingSdCardList() const | 765 | bool AtCore::isReadingSdCardList() const | ||
764 | { | 766 | { | ||
765 | return d->sdCardReadingFileList; | 767 | return d->sdCardReadingFileList; | ||
766 | } | 768 | } | ||
767 | 769 | | |||
770 | bool AtCore::isSdWriting() const | ||||
771 | { | ||||
772 | return d->sdWriting; | ||||
773 | } | ||||
774 | | ||||
768 | void AtCore::setReadingSdCardList(bool readingList) | 775 | void AtCore::setReadingSdCardList(bool readingList) | ||
769 | { | 776 | { | ||
770 | d->sdCardReadingFileList = readingList; | 777 | d->sdCardReadingFileList = readingList; | ||
771 | } | 778 | } | ||
772 | 779 | | |||
773 | void AtCore::sdCardPrintStatus() | 780 | void AtCore::sdCardPrintStatus() | ||
774 | { | 781 | { | ||
775 | //One request for the Sd Job status in the queue at a time. | 782 | //One request for the Sd Job status in the queue at a time. | ||
Show All 16 Lines | 798 | connect(&process, &QProcess::errorOccurred, this, [this, &process] { | |||
792 | qCDebug(ATCORE_CORE) << "Stty Error:" << process.errorString(); | 799 | qCDebug(ATCORE_CORE) << "Stty Error:" << process.errorString(); | ||
793 | }); | 800 | }); | ||
794 | 801 | | |||
795 | #elif Q_OS_WIN | 802 | #elif Q_OS_WIN | ||
796 | //TODO: Disable hangup on windows. | 803 | //TODO: Disable hangup on windows. | ||
797 | #endif | 804 | #endif | ||
798 | } | 805 | } | ||
799 | 806 | | |||
800 | void AtCore::handleSerialError(QSerialPort::SerialPortError error) | 807 | void AtCore::handleSerialError(QSerialPort::SerialPortError error) | ||
tcanabrava: It’s good to say “could not mount se card” somehow, instead of just returning false. Same for… | |||||
801 | { | 808 | { | ||
802 | QString errorString; | 809 | QString errorString; | ||
803 | 810 | | |||
804 | switch (error) { | 811 | switch (error) { | ||
805 | case (QSerialPort::DeviceNotFoundError): | 812 | case (QSerialPort::DeviceNotFoundError): | ||
806 | errorString = tr("Device not found"); | 813 | errorString = tr("Device not found"); | ||
807 | break; | 814 | break; | ||
tcanabrava: Const | |||||
808 | case (QSerialPort::WriteError): | 815 | case (QSerialPort::WriteError): | ||
809 | errorString = tr("Unable to write to device"); | 816 | errorString = tr("Unable to write to device"); | ||
810 | break; | 817 | break; | ||
811 | case (QSerialPort::ReadError): | 818 | case (QSerialPort::ReadError): | ||
patrickelectric: QUrl::filename | |||||
fileName is a QString and we have no other QUrl in this object. if we add that we should do so in another patch. rizzitello: fileName is a QString and we have no other QUrl in this object. if we add that we should do so… | |||||
812 | errorString = tr("Unable to read from device"); | 819 | errorString = tr("Unable to read from device"); | ||
813 | break; | 820 | break; | ||
814 | case (QSerialPort::ResourceError): | 821 | case (QSerialPort::ResourceError): | ||
815 | case (QSerialPort::TimeoutError): | 822 | case (QSerialPort::TimeoutError): | ||
816 | errorString = tr("The device no longer available"); | 823 | errorString = tr("The device no longer available"); | ||
817 | closeConnection(); | 824 | closeConnection(); | ||
818 | break; | 825 | break; | ||
819 | case (QSerialPort::UnsupportedOperationError): | 826 | case (QSerialPort::UnsupportedOperationError): | ||
820 | errorString = tr("Device does not support opperation"); | 827 | errorString = tr("Device does not support opperation"); | ||
821 | break; | 828 | break; | ||
822 | case (QSerialPort::UnknownError): | 829 | case (QSerialPort::UnknownError): | ||
823 | errorString = tr("Unknown Error"); | 830 | errorString = tr("Unknown Error"); | ||
824 | break; | 831 | break; | ||
825 | default: | 832 | default: | ||
How do we poke the thread to see it’s state? I’d store the variable in the class or something tcanabrava: How do we poke the thread to see it’s state? I’d store the variable in the class or something | |||||
826 | //Not Directly processed errors | 833 | //Not Directly processed errors | ||
827 | //QSerialPort::NoError, No error has happened | 834 | //QSerialPort::NoError, No error has happened | ||
828 | //QSerialPort::PermissionError), Already handled. | 835 | //QSerialPort::PermissionError), Already handled. | ||
This needs to be adjusted to return true after the thread has finished rizzitello: This needs to be adjusted to return true after the thread has finished | |||||
829 | //QSerialPort::OpenError), Already handled. | 836 | //QSerialPort::OpenError), Already handled. | ||
830 | //QSerialPort::NotOpenError, SerialLayer destroyed if not connected. | 837 | //QSerialPort::NotOpenError, SerialLayer destroyed if not connected. | ||
831 | //QSerialPort::ParityError, Obsolete. Qt Docs "We strongly advise against using it in new code." | 838 | //QSerialPort::ParityError, Obsolete. Qt Docs "We strongly advise against using it in new code." | ||
832 | //QSerialPort::FramingError, Obsolete. Qt Docs "We strongly advise against using it in new code." | 839 | //QSerialPort::FramingError, Obsolete. Qt Docs "We strongly advise against using it in new code." | ||
833 | //QSerialPort::BreakConditionError, Obsolete. Qt Docs "We strongly advise against using it in new code." | 840 | //QSerialPort::BreakConditionError, Obsolete. Qt Docs "We strongly advise against using it in new code." | ||
834 | return; | 841 | return; | ||
835 | };//End of Switch | 842 | };//End of Switch | ||
836 | qCDebug(ATCORE_CORE) << "SerialError:" << errorString; | 843 | qCDebug(ATCORE_CORE) << "SerialError:" << errorString; | ||
837 | emit atcoreMessage(QStringLiteral("SerialError: %1").arg(errorString)); | 844 | emit atcoreMessage(QStringLiteral("SerialError: %1").arg(errorString)); | ||
838 | } | 845 | } | ||
846 | | ||||
847 | bool AtCore::writeFiletoSd(const QString &fileName) | ||||
I belive this function shouldn't return bool, tcanabrava: I belive this function shouldn't return bool,
it should return enum class Reason { Success… | |||||
848 | { | ||||
849 | //Do a few sanity checks before attempting to write to SD. | ||||
850 | //Can't write an empty fileName. | ||||
851 | if (fileName.isEmpty()) { | ||||
852 | qCDebug(ATCORE_CORE) << "Sd Write Failed: Empty fileName."; | ||||
patrickelectric: This should be a warning or a critical. | |||||
853 | return false; | ||||
854 | } | ||||
855 | //Can not write if plugin doesn't have Sd Support. | ||||
856 | if (!d->firmwarePlugin->isSdSupported()) { | ||||
857 | qCDebug(ATCORE_CORE) << "Sd Write Failed: Firmware plugin does not support Sd functions."; | ||||
858 | return false; | ||||
859 | } | ||||
860 | | ||||
861 | //If the card is not mounted try to read the file list and mount it. | ||||
862 | if (!d->sdCardMounted) { | ||||
863 | getSDFileList(); | ||||
864 | if (!d->sdCardMounted) { | ||||
865 | qCDebug(ATCORE_CORE) << "Sd Write Failed unable to mount Sd Card"; | ||||
866 | return false; | ||||
867 | } | ||||
868 | } | ||||
869 | // Be sure the printer is IDLE | ||||
870 | if (d->printerState == AtCore::DISCONNECTED) { | ||||
871 | qCDebug(ATCORE_CORE) << "No Device Connected."; | ||||
872 | return false; | ||||
patrickelectric: or not connected | |||||
873 | } else if (d->printerState != AtCore::IDLE) { | ||||
874 | qCDebug(ATCORE_CORE) << "Sd Write Failed: Device is Busy"; | ||||
875 | return false; | ||||
876 | } | ||||
patrickelectric: QFileInfo.fileName() | |||||
877 | | ||||
878 | bool restoreTimer = d->tempTimer->isActive(); | ||||
879 | int timerTime = 0; | ||||
880 | if (restoreTimer) { | ||||
881 | timerTime = d->tempTimer->interval(); | ||||
882 | d->tempTimer->stop(); | ||||
883 | } | ||||
884 | | ||||
885 | //Process without thread. | ||||
886 | QString fname = fileName.mid(fileName.lastIndexOf(QStringLiteral("/")) + 1, fileName.length()); | ||||
patrickelectric: You need to check if file isReadable() | |||||
887 | QFile *file = new QFile(fileName); | ||||
888 | file->open(QFile::ReadOnly); | ||||
patrickelectric: Need to check if was able to open it | |||||
889 | QTextStream *txtStream = new QTextStream(file); | ||||
890 | const qint64 filesize = file->size(); | ||||
891 | float progress = 0; | ||||
892 | qint64 stillSize = filesize; | ||||
893 | QString cline; | ||||
894 | | ||||
patrickelectric: uint64_t | |||||
rizzitello: file->size returns type qint64 | |||||
895 | setState(AtCore::BUSY); | ||||
patrickelectric: int | |||||
896 | d->sdWriting = true; | ||||
patrickelectric: uint64_t | |||||
rizzitello: fileSize of type qint64 so avoid warning. | |||||
897 | emit sdWriteChanged(d->sdWriting); | ||||
898 | pushCommand(GCode::toCommand(GCode::M28, fname)); | ||||
899 | | ||||
900 | while (!txtStream->atEnd()) { | ||||
901 | if (state() == AtCore::STOP) { | ||||
902 | txtStream->setString(new QString()); | ||||
903 | progress = 100; | ||||
904 | } else { | ||||
905 | cline = txtStream->readLine(); | ||||
906 | stillSize -= cline.size() + 1; //remove read chars | ||||
907 | progress = float(filesize - stillSize) * 100 / float(filesize); | ||||
908 | } | ||||
909 | pushCommand(cline); | ||||
910 | emit sdWriteProgressChanged(progress); | ||||
911 | qDebug() << "Progress:" << progress << "Current Line:" << cline; | ||||
912 | } | ||||
913 | | ||||
patrickelectric: Category | |||||
914 | pushCommand(GCode::toCommand(GCode::M29, fname)); | ||||
915 | | ||||
916 | file->close(); | ||||
917 | setState(AtCore::IDLE); | ||||
918 | getSDFileList(); | ||||
919 | | ||||
920 | d->sdWriting = false; | ||||
921 | if (restoreTimer) { | ||||
922 | d->tempTimer->start(timerTime); | ||||
923 | } | ||||
924 | emit sdWriteChanged(d->sdWriting); | ||||
925 | return true; | ||||
926 | } |
It’s good to say “could not mount se card” somehow, instead of just returning false. Same for the other checks