Changeset View
Changeset View
Standalone View
Standalone View
src/printthread.cpp
Show All 16 Lines | 4 | Authors: | |||
---|---|---|---|---|---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
18 | Lesser General Public License for more details. | 18 | Lesser General Public License for more details. | ||
19 | 19 | | |||
20 | You should have received a copy of the GNU Lesser General Public | 20 | You should have received a copy of the GNU Lesser General Public | ||
21 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | 21 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
22 | */ | 22 | */ | ||
23 | #include <QTime> | 23 | #include <QTime> | ||
24 | #include <QLoggingCategory> | 24 | #include <QLoggingCategory> | ||
25 | #include <QCommandLineOption> | ||||
26 | #include <QCommandLineParser> | ||||
25 | 27 | | |||
26 | #include "printthread.h" | 28 | #include "printthread.h" | ||
27 | 29 | | |||
28 | Q_LOGGING_CATEGORY(PRINT_THREAD, "org.kde.atelier.core.printThread") | 30 | Q_LOGGING_CATEGORY(PRINT_THREAD, "org.kde.atelier.core.printThread") | ||
29 | /** | 31 | /** | ||
30 | * @brief The PrintThreadPrivate class | 32 | * @brief The PrintThreadPrivate class | ||
31 | */ | 33 | */ | ||
32 | class PrintThreadPrivate | 34 | class PrintThreadPrivate | ||
33 | { | 35 | { | ||
34 | public: | 36 | public: | ||
35 | AtCore *core = nullptr; //!<@param core: Pointer to AtCore | 37 | AtCore *core = nullptr; //!<@param core: Pointer to AtCore | ||
36 | QTextStream *gcodestream = nullptr; //!<@param gcodestream: Steam the job is read from | 38 | QTextStream *gcodestream = nullptr; //!<@param gcodestream: Steam the job is read from | ||
37 | float printProgress = 0; //!<@param printProgress: Progress of the print job | 39 | float printProgress = 0; //!<@param printProgress: Progress of the print job | ||
38 | qint64 totalSize = 0; //!<@param totalSize: total file size | 40 | qint64 totalSize = 0; //!<@param totalSize: total file size | ||
39 | qint64 stillSize = 0; //!<@param stillSize: remaining file | 41 | qint64 stillSize = 0; //!<@param stillSize: remaining file | ||
40 | QString cline; //!<@param cline: current line | 42 | QString cline; //!<@param cline: current line | ||
41 | AtCore::STATES state = AtCore::IDLE;//!<@param state: printer state | 43 | AtCore::STATES state = AtCore::IDLE;//!<@param state: printer state | ||
42 | QFile *file = nullptr; //!<@param file: gcode File to stream from | 44 | QFile *file = nullptr; //!<@param file: gcode File to stream from | ||
45 | QList<QCommandLineOption> options = { | ||||
46 | {QCommandLineOption(QStringLiteral("pause"))}, | ||||
47 | {QCommandLineOption(QStringLiteral("extruder temperature"))}, | ||||
48 | {QCommandLineOption(QStringLiteral("bed temperature"))}, | ||||
49 | {QCommandLineOption(QStringLiteral("print speed"))}, | ||||
50 | {QCommandLineOption(QStringLiteral("fan speed"))}, | ||||
51 | {QCommandLineOption(QStringLiteral("flow rate"))}, | ||||
52 | {QCommandLineOption(QStringLiteral("message"))}, | ||||
53 | {QCommandLineOption(QStringLiteral("command"))} | ||||
54 | }; //!<@param options: injectable commands. | ||||
43 | }; | 55 | }; | ||
44 | 56 | | |||
45 | PrintThread::PrintThread(AtCore *parent, QString fileName) : d(new PrintThreadPrivate) | 57 | PrintThread::PrintThread(AtCore *parent, QString fileName) : d(new PrintThreadPrivate) | ||
46 | { | 58 | { | ||
47 | d->core = parent; | 59 | d->core = parent; | ||
48 | d->state = d->core->state(); | 60 | d->state = d->core->state(); | ||
49 | d->file = new QFile(fileName); | 61 | d->file = new QFile(fileName); | ||
50 | d->file->open(QFile::ReadOnly); | 62 | d->file->open(QFile::ReadOnly); | ||
Show All 24 Lines | 81 | { | |||
75 | case AtCore::STARTPRINT: | 87 | case AtCore::STARTPRINT: | ||
76 | case AtCore::IDLE: | 88 | case AtCore::IDLE: | ||
77 | case AtCore::BUSY: | 89 | case AtCore::BUSY: | ||
78 | setState(AtCore::BUSY); | 90 | setState(AtCore::BUSY); | ||
79 | nextLine(); | 91 | nextLine(); | ||
80 | while (d->cline.isEmpty() && !d->gcodestream->atEnd()) { | 92 | while (d->cline.isEmpty() && !d->gcodestream->atEnd()) { | ||
81 | nextLine(); | 93 | nextLine(); | ||
82 | } | 94 | } | ||
83 | if (!d->cline.isEmpty()) { | 95 | if (!d->cline.isEmpty() && d->core->state() != AtCore::PAUSE) { | ||
84 | qCDebug(PRINT_THREAD) << "cline:" << d->cline; | 96 | qCDebug(PRINT_THREAD) << "cline:" << d->cline; | ||
85 | emit nextCommand(d->cline); | 97 | emit nextCommand(d->cline); | ||
86 | } | 98 | } | ||
87 | break; | 99 | break; | ||
88 | 100 | | |||
89 | case AtCore::ERRORSTATE: | 101 | case AtCore::ERRORSTATE: | ||
90 | qCDebug(PRINT_THREAD) << "Error State"; | 102 | qCDebug(PRINT_THREAD) << "Error State"; | ||
91 | break; | 103 | break; | ||
92 | 104 | | |||
93 | case AtCore::STOP: { | 105 | case AtCore::STOP: { | ||
94 | endPrint(); | 106 | endPrint(); | ||
95 | break; | 107 | break; | ||
96 | } | 108 | } | ||
97 | 109 | | |||
98 | case AtCore::PAUSE: | 110 | case AtCore::PAUSE: | ||
111 | if (d->cline.startsWith(QStringLiteral(";-"))) { | ||||
112 | nextLine(); | ||||
113 | } | ||||
99 | break; | 114 | break; | ||
100 | 115 | | |||
101 | default: | 116 | default: | ||
102 | qCDebug(PRINT_THREAD) << "Unknown State"; | 117 | qCDebug(PRINT_THREAD) << "Unknown State"; | ||
103 | break; | 118 | break; | ||
104 | } | 119 | } | ||
105 | } | 120 | } | ||
106 | 121 | | |||
Show All 14 Lines | |||||
121 | { | 136 | { | ||
122 | d->cline = d->gcodestream->readLine(); | 137 | d->cline = d->gcodestream->readLine(); | ||
123 | qCDebug(PRINT_THREAD) << "Nextline:" << d->cline; | 138 | qCDebug(PRINT_THREAD) << "Nextline:" << d->cline; | ||
124 | d->stillSize -= d->cline.size() + 1; //remove read chars | 139 | d->stillSize -= d->cline.size() + 1; //remove read chars | ||
125 | d->printProgress = float(d->totalSize - d->stillSize) * 100.0 / float(d->totalSize); | 140 | d->printProgress = float(d->totalSize - d->stillSize) * 100.0 / float(d->totalSize); | ||
126 | qCDebug(PRINT_THREAD) << "progress:" << QString::number(d->printProgress); | 141 | qCDebug(PRINT_THREAD) << "progress:" << QString::number(d->printProgress); | ||
127 | emit(printProgressChanged(d->printProgress)); | 142 | emit(printProgressChanged(d->printProgress)); | ||
128 | if (d->cline.contains(QChar::fromLatin1(';'))) { | 143 | if (d->cline.contains(QChar::fromLatin1(';'))) { | ||
144 | if (d->cline.startsWith(QStringLiteral(";-"))) { | ||||
145 | injectCommand(d->cline); | ||||
146 | } | ||||
129 | d->cline.resize(d->cline.indexOf(QChar::fromLatin1(';'))); | 147 | d->cline.resize(d->cline.indexOf(QChar::fromLatin1(';'))); | ||
130 | } | 148 | } | ||
131 | d->cline = d->cline.simplified(); | 149 | d->cline = d->cline.simplified(); | ||
132 | } | 150 | } | ||
133 | 151 | | |||
134 | void PrintThread::setState(const AtCore::STATES &newState) | 152 | void PrintThread::setState(const AtCore::STATES &newState) | ||
135 | { | 153 | { | ||
136 | if (d->state == AtCore::STATES::DISCONNECTED && | 154 | if (d->state == AtCore::STATES::DISCONNECTED && | ||
137 | ( | 155 | ( | ||
138 | newState == AtCore::STATES::PAUSE || | 156 | newState == AtCore::STATES::PAUSE || | ||
139 | newState == AtCore::STATES::STOP | 157 | newState == AtCore::STATES::STOP | ||
140 | ) | 158 | ) | ||
141 | ) { | 159 | ) { | ||
142 | qCDebug(PRINT_THREAD) << "Serial not connected !"; | 160 | qCDebug(PRINT_THREAD) << "Serial not connected !"; | ||
143 | return; | 161 | return; | ||
144 | } | 162 | } | ||
145 | if (newState != d->state) { | 163 | if (newState != d->state) { | ||
146 | qCDebug(PRINT_THREAD) << "State Changed from [" << d->state << "] to [" << newState << ']'; | 164 | qCDebug(PRINT_THREAD) << "State Changed from [" << d->state << "] to [" << newState << ']'; | ||
147 | disconnect(d->core, &AtCore::stateChanged, this, &PrintThread::setState); | 165 | disconnect(d->core, &AtCore::stateChanged, this, &PrintThread::setState); | ||
148 | d->state = newState; | 166 | d->state = newState; | ||
149 | emit(stateChanged(d->state)); | 167 | emit(stateChanged(d->state)); | ||
150 | connect(d->core, &AtCore::stateChanged, this, &PrintThread::setState, Qt::QueuedConnection); | 168 | connect(d->core, &AtCore::stateChanged, this, &PrintThread::setState, Qt::QueuedConnection); | ||
151 | } | 169 | } | ||
152 | } | 170 | } | ||
171 | | ||||
172 | void PrintThread::injectCommand(QString command) | ||||
patrickelectric: This function should be used ir our default gcode handle also, I would be nice to use it in… | |||||
this is our default gcode handing for AtCore Any gcode pased thur atcore will use this function. rizzitello: this is our default gcode handing for AtCore
Any gcode pased thur atcore will use this… | |||||
173 | { | ||||
174 | //remove the ; | ||||
175 | command.remove(0, 1); | ||||
176 | command.prepend(QStringLiteral("0:")); | ||||
177 | QStringList cmd = command.split(QLatin1Char(':')); | ||||
178 | cmd.replace(1, cmd.at(1).simplified().toLower()); | ||||
179 | cmd.replace(2, cmd.at(2).simplified()); | ||||
180 | | ||||
181 | static QCommandLineParser parser; | ||||
182 | if (!parser.optionNames()) { | ||||
183 | parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions); | ||||
184 | parser.addOptions(d->options); | ||||
static QCommandLineParser parser; if(!parser.optionNames()) { parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions); parser.addOptions(d->options); } patrickelectric: ```
static QCommandLineParser parser;
if(!parser.optionNames()) {
parser. | |||||
185 | } | ||||
186 | | ||||
187 | qCDebug(PRINT_THREAD) << "attempting to inject " << cmd; | ||||
188 | parser.process(cmd); | ||||
189 | | ||||
190 | if (parser.isSet(QStringLiteral("pause"))) { | ||||
191 | d->core->pause(parser.positionalArguments().at(0)); | ||||
192 | } else if (parser.isSet(QStringLiteral("extruder temperature"))) { | ||||
bool wait = !QString::compare(args.at(2).simplified(), QStringLiteral("true"), Qt::CaseInsensitive); ? patrickelectric: `bool wait = !QString::compare(args.at(2).simplified(), QStringLiteral("true"), Qt… | |||||
rizzitello: If the string is True bool should be True .. | |||||
What are you saying does not make sense with the code, shouldn't it be without the ! patrickelectric: What are you saying does not make sense with the code, shouldn't it be without the ! | |||||
yea you would think (i thought so too) . then it didn't work and i read the docs... this is why i had == 0 before but, if ! 0 is true and if ! other ints is false so it works. rizzitello: yea you would think (i thought so too) . then it didn't work and i read the docs...
https://doc. | |||||
193 | QStringList args = parser.positionalArguments().at(0).split(QLatin1Char(',')); | ||||
194 | bool wait = !QString::compare(args.at(2).simplified(), QStringLiteral("true"), Qt::CaseInsensitive); | ||||
195 | d->core->setExtruderTemp(args.at(0).toInt(), args.at(1).toInt(), wait); | ||||
196 | } else if (parser.isSet(QStringLiteral("bed temperature"))) { | ||||
197 | QStringList args = parser.positionalArguments().at(0).split(QLatin1Char(',')); | ||||
198 | bool wait = !QString::compare(args.at(1).simplified(), QStringLiteral("true"), Qt::CaseInsensitive); | ||||
199 | d->core->setBedTemp(args.at(0).toInt(), wait); | ||||
patrickelectric: first().toInt() will make it more legible | |||||
rizzitello: I would rather use `at()` to be consistant. | |||||
200 | } else if (parser.isSet(QStringLiteral("print speed"))) { | ||||
201 | d->core->setPrinterSpeed(parser.positionalArguments().at(0).toInt()); | ||||
202 | } else if (parser.isSet(QStringLiteral("fan speed"))) { | ||||
203 | d->core->setFanSpeed(parser.positionalArguments().at(0).toInt(), parser.positionalArguments().at(1).toInt()); | ||||
204 | } else if (parser.isSet(QStringLiteral("flow rate"))) { | ||||
205 | d->core->setFlowRate(parser.positionalArguments().at(0).toInt()); | ||||
206 | } else if (parser.isSet(QStringLiteral("message"))) { | ||||
207 | d->core->showMessage(parser.positionalArguments().at(0)); | ||||
208 | } else if (parser.isSet(QStringLiteral("command"))) { | ||||
209 | d->core->pushCommand(parser.positionalArguments().at(0)); | ||||
210 | } else { | ||||
211 | qCDebug(PRINT_THREAD) << "Attempted to inject unknown command: " << parser.positionalArguments(); | ||||
212 | } | ||||
213 | } |
This function should be used ir our default gcode handle also, I would be nice to use it in text gui gcode entry.