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