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 715 Lines • ▼ Show 20 Line(s) | 792 | connect(&process, &QProcess::errorOccurred, this, [&process] { | |||
792 | qCDebug(ATCORE_CORE) << "Stty Error:" << process.errorString(); | 793 | qCDebug(ATCORE_CORE) << "Stty Error:" << process.errorString(); | ||
793 | }); | 794 | }); | ||
794 | 795 | | |||
795 | #elif defined(Q_OS_WIN) | 796 | #elif defined(Q_OS_WIN) | ||
796 | //TODO: Disable hangup on windows. | 797 | //TODO: Disable hangup on windows. | ||
797 | #endif | 798 | #endif | ||
798 | } | 799 | } | ||
799 | 800 | | |||
800 | void AtCore::handleSerialError(QSerialPort::SerialPortError error) | 801 | 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 | { | 802 | { | ||
802 | QString errorString; | 803 | QString errorString; | ||
803 | 804 | | |||
804 | switch (error) { | 805 | switch (error) { | ||
805 | case (QSerialPort::DeviceNotFoundError): | 806 | case (QSerialPort::DeviceNotFoundError): | ||
806 | errorString = tr("Device not found"); | 807 | errorString = tr("Device not found"); | ||
807 | break; | 808 | break; | ||
tcanabrava: Const | |||||
808 | case (QSerialPort::WriteError): | 809 | case (QSerialPort::WriteError): | ||
809 | errorString = tr("Unable to write to device"); | 810 | errorString = tr("Unable to write to device"); | ||
810 | break; | 811 | break; | ||
811 | case (QSerialPort::ReadError): | 812 | 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"); | 813 | errorString = tr("Unable to read from device"); | ||
813 | break; | 814 | break; | ||
814 | case (QSerialPort::ResourceError): | 815 | case (QSerialPort::ResourceError): | ||
815 | case (QSerialPort::TimeoutError): | 816 | case (QSerialPort::TimeoutError): | ||
816 | errorString = tr("The device no longer available"); | 817 | errorString = tr("The device no longer available"); | ||
817 | closeConnection(); | 818 | closeConnection(); | ||
818 | break; | 819 | break; | ||
819 | case (QSerialPort::UnsupportedOperationError): | 820 | case (QSerialPort::UnsupportedOperationError): | ||
820 | errorString = tr("Device does not support the operation"); | 821 | errorString = tr("Device does not support the operation"); | ||
821 | break; | 822 | break; | ||
822 | case (QSerialPort::UnknownError): | 823 | case (QSerialPort::UnknownError): | ||
823 | errorString = tr("Unknown Error"); | 824 | errorString = tr("Unknown Error"); | ||
824 | break; | 825 | break; | ||
825 | default: | 826 | 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 | 827 | //Not Directly processed errors | ||
827 | //QSerialPort::NoError, No error has happened | 828 | //QSerialPort::NoError, No error has happened | ||
828 | //QSerialPort::PermissionError), Already handled. | 829 | //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. | 830 | //QSerialPort::OpenError), Already handled. | ||
830 | //QSerialPort::NotOpenError, SerialLayer destroyed if not connected. | 831 | //QSerialPort::NotOpenError, SerialLayer destroyed if not connected. | ||
831 | //QSerialPort::ParityError, Obsolete. Qt Docs "We strongly advise against using it in new code." | 832 | //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." | 833 | //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." | 834 | //QSerialPort::BreakConditionError, Obsolete. Qt Docs "We strongly advise against using it in new code." | ||
834 | return; | 835 | return; | ||
835 | };//End of Switch | 836 | };//End of Switch | ||
836 | qCDebug(ATCORE_CORE) << "SerialError:" << errorString; | 837 | qCDebug(ATCORE_CORE) << "SerialError:" << errorString; | ||
837 | emit atcoreMessage(QStringLiteral("SerialError: %1").arg(errorString)); | 838 | emit atcoreMessage(QStringLiteral("SerialError: %1").arg(errorString)); | ||
838 | } | 839 | } | ||
840 | | ||||
841 | 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… | |||||
842 | { | ||||
843 | //Do a few sanity checks before attempting to write to SD. | ||||
844 | //Can't write an empty fileName. | ||||
845 | if (fileName.isEmpty()) { | ||||
846 | qCDebug(ATCORE_CORE) << "Sd Write Failed: Empty fileName."; | ||||
patrickelectric: This should be a warning or a critical. | |||||
847 | return false; | ||||
848 | } | ||||
849 | //Can not write if plugin doesn't have Sd Support. | ||||
850 | if (!d->firmwarePlugin->isSdSupported()) { | ||||
851 | qCDebug(ATCORE_CORE) << "Sd Write Failed: Firmware plugin does not support Sd functions."; | ||||
852 | return false; | ||||
853 | } | ||||
854 | | ||||
855 | //If the card is not mounted try to read the file list and mount it. | ||||
856 | if (!d->sdCardMounted) { | ||||
857 | getSDFileList(); | ||||
858 | if (!d->sdCardMounted) { | ||||
859 | qCDebug(ATCORE_CORE) << "Sd Write Failed unable to mount Sd Card"; | ||||
860 | return false; | ||||
861 | } | ||||
862 | } | ||||
863 | // Be sure the printer is IDLE | ||||
864 | if (d->printerState == AtCore::DISCONNECTED) { | ||||
865 | qCDebug(ATCORE_CORE) << "No Device Connected."; | ||||
866 | return false; | ||||
patrickelectric: or not connected | |||||
867 | } else if (d->printerState != AtCore::IDLE) { | ||||
868 | qCDebug(ATCORE_CORE) << "Sd Write Failed: Device is Busy"; | ||||
869 | return false; | ||||
870 | } | ||||
patrickelectric: QFileInfo.fileName() | |||||
871 | | ||||
872 | bool restoreTimer = d->tempTimer->isActive(); | ||||
873 | int timerTime = 0; | ||||
874 | if (restoreTimer) { | ||||
875 | timerTime = d->tempTimer->interval(); | ||||
876 | d->tempTimer->stop(); | ||||
877 | } | ||||
878 | | ||||
879 | //Process without thread. | ||||
880 | QString fname = QFileInfo(fileName).fileName(); | ||||
patrickelectric: You need to check if file isReadable() | |||||
881 | QFile *file = new QFile(fileName); | ||||
882 | file->open(QFile::ReadOnly); | ||||
patrickelectric: Need to check if was able to open it | |||||
883 | QTextStream *txtStream = new QTextStream(file); | ||||
884 | const qint64 filesize = file->size(); | ||||
885 | float progress = 0; | ||||
886 | qint64 stillSize = filesize; | ||||
887 | QString cline; | ||||
888 | | ||||
patrickelectric: uint64_t | |||||
rizzitello: file->size returns type qint64 | |||||
889 | setState(AtCore::BUSY); | ||||
patrickelectric: int | |||||
890 | d->sdWriting = true; | ||||
patrickelectric: uint64_t | |||||
rizzitello: fileSize of type qint64 so avoid warning. | |||||
891 | emit sdWriteChanged(d->sdWriting); | ||||
892 | pushCommand(GCode::toCommand(GCode::M28, fname)); | ||||
893 | | ||||
894 | while (!txtStream->atEnd()) { | ||||
895 | if (state() == AtCore::STOP) { | ||||
896 | txtStream->setString(new QString()); | ||||
897 | progress = 100; | ||||
898 | } else { | ||||
899 | cline = txtStream->readLine(); | ||||
900 | stillSize -= cline.size() + 1; //remove read chars | ||||
901 | progress = float(filesize - stillSize) * 100 / float(filesize); | ||||
902 | } | ||||
903 | pushCommand(cline); | ||||
904 | processQueue(); | ||||
905 | serial()->waitForBytesWritten(); | ||||
906 | emit sdWriteProgressChanged(progress); | ||||
907 | qDebug() << "Progress:" << progress << "Current Line:" << cline; | ||||
patrickelectric: Category | |||||
908 | } | ||||
909 | | ||||
910 | pushCommand(GCode::toCommand(GCode::M29, fname)); | ||||
911 | | ||||
912 | file->close(); | ||||
913 | d->sdWriting = false; | ||||
914 | if (restoreTimer) { | ||||
915 | d->tempTimer->start(timerTime); | ||||
916 | } | ||||
917 | emit sdWriteChanged(d->sdWriting); | ||||
918 | serial()->flush(); | ||||
919 | setState(AtCore::IDLE); | ||||
920 | getSDFileList(); | ||||
921 | return true; | ||||
922 | } |
It’s good to say “could not mount se card” somehow, instead of just returning false. Same for the other checks