Skip to content

Instantly share code, notes, and snippets.

@patrickelectric
Created January 23, 2017 18:48
Show Gist options
  • Save patrickelectric/7b34ccc748bde57f650c75392d64dc4c to your computer and use it in GitHub Desktop.
Save patrickelectric/7b34ccc748bde57f650c75392d64dc4c to your computer and use it in GitHub Desktop.
diff --git a/src/atcore.cpp b/src/atcore.cpp
index 3d47405..712de87 100644
--- a/src/atcore.cpp
+++ b/src/atcore.cpp
@@ -53,6 +53,7 @@ AtCore::AtCore(QObject *parent) :
QObject(parent),
d(new AtCorePrivate)
{
+ qRegisterMetaType<PrinterState>("PrinterState");
setState(DISCONNECTED);
for (const auto &path : AtCoreDirectories::pluginDir) {
@@ -254,14 +255,17 @@ void AtCore::print(const QString &fileName)
connect(printThread, &PrintThread::finished, thread, &QThread::quit);
connect(printThread, &PrintThread::finished, printThread, &PrintThread::deleteLater);
connect(thread, &QThread::finished, printThread, &PrintThread::deleteLater);
- thread->start();
+ if(!thread->isRunning())
+ thread->start();
}
void AtCore::pushCommand(const QString &comm)
{
if (!pluginLoaded()) {
+ qDebug() << "Plugin not Loaded !";
serial()->pushCommand(comm.toLocal8Bit());
} else {
+ qDebug() << "Plugin Loaded !";
serial()->pushCommand(plugin()->translate(comm));
}
}
diff --git a/src/plugins/repetierplugin.cpp b/src/plugins/repetierplugin.cpp
index b67ae97..3e93c82 100644
--- a/src/plugins/repetierplugin.cpp
+++ b/src/plugins/repetierplugin.cpp
@@ -60,9 +60,8 @@ void RepetierPlugin::validateCommand(const QString &lastMessage)
{
if (lastMessage.contains(_extruderTemp) || lastMessage.contains(_bedTemp)) {
extractTemp(lastMessage);
- } else if (lastMessage.contains(_ok) || lastMessage.contains(_wait)) {
- emit readyForCommand();
}
+ emit readyForCommand();
}
QByteArray RepetierPlugin::translate(const QString &command)
diff --git a/src/printThread.cpp b/src/printThread.cpp
index 324d724..f1736ed 100644
--- a/src/printThread.cpp
+++ b/src/printThread.cpp
@@ -3,6 +3,7 @@
#include "gcodecommands.h"
#include <QDebug>
+#include <QMutexLocker>
class PrintThreadPrivate
{
@@ -17,6 +18,7 @@ public:
PrintThread::PrintThread(AtCore *parent, QString fileName) : d(new PrintThreadPrivate)
{
+ QMutexLocker locker(&mutex1);
d->core = parent;
//QString cline;
QFile file(fileName);
@@ -24,6 +26,7 @@ PrintThread::PrintThread(AtCore *parent, QString fileName) : d(new PrintThreadPr
d->totalSize = file.bytesAvailable();
d->stillSize = d->totalSize;
d->gcodestream = new QTextStream(&file);
+ qDebug() << d->gcodestream->readLine();
}
void PrintThread::start()
@@ -38,6 +41,7 @@ void PrintThread::start()
void PrintThread::commandReady()
{
+ QMutexLocker locker(&mutex1);
qDebug() << "Command Ready!";
switch (d->core->state()) {
case STARTPRINT:
@@ -45,9 +49,14 @@ void PrintThread::commandReady()
case BUSY:
d->core->setState(BUSY);
nextLine();
+ while(d->cline.isEmpty())
+ nextLine();
+ qDebug() << "after nextline";
if (!d->cline.isEmpty()) {
+ qDebug() << "cline not empty";
d->core->pushCommand(d->cline);
}
+ qDebug() << "busy break";
break;
case ERROR:
@@ -68,22 +77,36 @@ void PrintThread::commandReady()
qDebug() << tr("Unknown State");
break;
}
+
if (d->gcodestream->atEnd()) {
+ qDebug() << "atEnd";
disconnect(d->core->plugin(), &IFirmware::readyForCommand, this, &PrintThread::commandReady);
d->core->setState(FINISHEDPRINT);
d->core->setState(IDLE);
emit finished();
}
+ else
+ {
+ qDebug() << "run it again";
+ commandReady();
+ }
}
void PrintThread::nextLine()
{
- d->cline = d->gcodestream->readLine();
+ qDebug() << "nextline";
+ QMutexLocker locker(&mutex2);
+ qDebug() << d->gcodestream;
+ QTextStream *gcodestream = d->gcodestream;
+ d->cline = gcodestream->readLine();
+ qDebug() << d->cline;
d->stillSize -= d->cline.size() + 1; //remove read chars
+ qDebug() << d->stillSize;
d->printProgress = float(d->totalSize - d->stillSize) * 100.0 / float(d->totalSize);
emit(printProgressChanged(d->printProgress));
d->cline = d->cline.simplified();
if (d->cline.contains(QChar::fromLatin1(';'))) {
d->cline.resize(d->cline.indexOf(QChar::fromLatin1(';')));
}
+ qDebug() << "end nextline";
}
diff --git a/src/printThread.h b/src/printThread.h
index 328122f..555e39e 100644
--- a/src/printThread.h
+++ b/src/printThread.h
@@ -20,6 +20,7 @@
#include <QTextStream>
#include <QFile>
+#include <QMutex>
#include "atcore.h"
class PrintThreadPrivate;
@@ -41,5 +42,7 @@ private slots:
void commandReady();
private:
+ mutable QMutex mutex1;
+ mutable QMutex mutex2;
PrintThreadPrivate *d;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment