From 763bc62b2635721ac66353cb3ebbd1e4eae528e0 Mon Sep 17 00:00:00 2001 From: NeoTheFox Date: Fri, 6 Mar 2015 17:44:32 +0300 Subject: [PATCH] Getting ready for checksum support --- mainwindow.cpp | 64 ++++++++++++++++++++++----------- mainwindow.h | 5 +-- settingswindow.cpp | 8 +++-- settingswindow.ui | 90 +++++++++++++++++++++++++++++++++++----------- 4 files changed, 121 insertions(+), 46 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index 46ef5ed..444ee33 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -25,13 +25,14 @@ MainWindow::MainWindow(QWidget *parent) : ui->baudbox->addItem(QString::number(250000)); ui->baudbox->addItem(QString::number(460800)); ui->baudbox->addItem(QString::number(500000)); - if(settings.value("printer/baudrateindex").toInt()) ui->baudbox->setCurrentIndex(settings.value("printer/baudrateindex").toInt()); + if(settings.value("printer/baudrateindex").toInt()) + ui->baudbox->setCurrentIndex(settings.value("printer/baudrateindex").toInt()); else ui->baudbox->setCurrentIndex(2); ui->extruderlcd->setPalette(Qt::red); ui->bedlcd->setPalette(Qt::red); - if(!settings.value("core/firstrun").toBool()) firstrun = true; + firstrun = !settings.value("core/firstrun").toBool(); //firstrun is inverted! checkingTemperature = settings.value("core/checktemperature").toBool(); ui->checktemp->setChecked(checkingTemperature); @@ -43,10 +44,10 @@ MainWindow::MainWindow(QWidget *parent) : else echo = false; autolock = settings.value("core/lockcontrols").toBool(); + sendingChecksum = settings.value("core/checksums").toBool(); sending = false; paused = false; - injectingCommand = false; readingFiles = false; sdprinting = false; sdBytes = 0; @@ -56,11 +57,11 @@ MainWindow::MainWindow(QWidget *parent) : temperatureRegxp.setCaseSensitivity(Qt::CaseInsensitive); temperatureRegxp.setPatternSyntax(QRegExp::RegExp); - temperatureRegxp.setPattern("\\d+\\.\\d+"); + temperatureRegxp.setPattern("\\d+\\.\\d+"); // Find float in string SDStatusRegxp.setCaseSensitivity(Qt::CaseInsensitive); SDStatusRegxp.setPatternSyntax(QRegExp::RegExp); - SDStatusRegxp.setPattern("\\d+"); + SDStatusRegxp.setPattern("\\d+"); //First number serialupdate(); @@ -94,11 +95,12 @@ MainWindow::~MainWindow() if(gfile.isOpen()) gfile.close(); if(printer.isOpen()) printer.close(); + if(firstrun) settings.setValue("core/firstrun", true); //firstrun is inverted! + settings.setValue("printer/baudrateindex", ui->baudbox->currentIndex()); settings.setValue("core/checktemperature", ui->checktemp->isChecked()); settings.setValue("user/extrudertemp", ui->etmpspin->value()); settings.setValue("user/bedtemp", ui->btmpspin->value()); - if(firstrun) settings.setValue("core/firstrun", true); settings.beginWriteArray("user/recentfiles"); for(int i = 0; i < recentFiles.size(); i++) @@ -136,11 +138,21 @@ void MainWindow::parseFile(QFile &file) if (file.open(QIODevice::ReadOnly)) { QTextStream in(&file); + int n = 0; while (!in.atEnd()) { QString line = in.readLine(); if(!line.startsWith(";")) { + if(sendingChecksum) + { + line = "N"+QString::number(n)+line+"*"; + int cs = 0; + for(int i = 0; line.at(i) != '*'; i++) cs = cs ^ line.at(i).toLatin1(); + cs &= 0xff; + line += QString::number(cs); + n++; + } gcode.append(line); } @@ -174,13 +186,13 @@ void MainWindow::serialupdate() ui->serialBox->clear(); QList list = QSerialPortInfo::availablePorts(); for(int i = 0; i < list.size(); i++) - { ui->serialBox->addItem(list.at(i).portName()); - } } void MainWindow::serialconnect() { + userCommands.clear(); + if(!printer.isOpen()) { foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) @@ -224,7 +236,7 @@ void MainWindow::serialconnect() ui->consoleGroup->setDisabled(false); ui->actionPrint_from_SD->setEnabled("true"); ui->actionSet_SD_printing_mode->setEnabled("true"); - if(checkingTemperature) injectCommand("M105"); + //if(checkingTemperature) injectCommand("M105"); } } @@ -415,6 +427,7 @@ void MainWindow::on_flowbutton_clicked() void MainWindow::on_haltbtn_clicked() { if(sending && !paused)ui->pauseBtn->click(); + userCommands.clear(); injectCommand("M112"); } //Buttons end @@ -445,10 +458,21 @@ void MainWindow::readSerial() else if(data.startsWith("wait")) readyRecieve = 1; else if(data.startsWith("Resend")) //Handle resend if requested { - if(currentLine > 0) currentLine -= data.split(':')[1].toInt(); - if(currentLine < 0) currentLine = 0; + if(gcode.isEmpty()) + { + injectCommand("M110"); //This means we rebooted, file is gone, so we need to reset counter + return; + } + int err = data.split(':')[1].toInt(); + if(!sendingChecksum) + { + if(currentLine > 0) currentLine -= err; + if(currentLine < 0) currentLine = 0; + } + else injectCommand(gcode.at(err)); } else if(data.startsWith("Done")) sdprinting = false; + else if(data.startsWith("start") && checkingTemperature) injectCommand("M105"); else if(data.startsWith("SD printing byte") && sdWatcher.isFinished()) { QFuture parseSDThread = QtConcurrent::run(this, &MainWindow::parseSDStatus, data); @@ -488,6 +512,7 @@ void MainWindow::printMsg(QString text) void MainWindow::on_sendBtn_clicked() { + userCommands.clear(); if(sending && !sdprinting) { sending = false; @@ -522,11 +547,10 @@ void MainWindow::on_sendBtn_clicked() void MainWindow::sendNext() { - if(injectingCommand && printer.isWritable() && readyRecieve > 0) + if(!userCommands.isEmpty() && printer.isWritable() && readyRecieve > 0) { - sendLine(userCommand); + sendLine(userCommands.dequeue()); readyRecieve--; - injectingCommand=false; return; } else if(sending && !paused && readyRecieve > 0 && !sdprinting && printer.isWritable()) @@ -609,8 +633,7 @@ void MainWindow::on_actionAbout_triggered() void MainWindow::injectCommand(QString command) { - injectingCommand = true; - userCommand = command; + userCommands.enqueue(command); } void MainWindow::updateRecent() @@ -627,6 +650,8 @@ void MainWindow::serialError(QSerialPort::SerialPortError error) if(sending) paused = true; + userCommands.clear(); + ui->connectBtn->setText("Connect"); ui->sendBtn->setDisabled(true); ui->pauseBtn->setDisabled(true); @@ -700,13 +725,9 @@ TemperatureReadings MainWindow::parseStatus(QByteArray data) TemperatureReadings r; if(temperatureRegxp.indexIn(QString(data)) != -1) - { r.e = temperatureRegxp.cap(0).toDouble(); - } if(temperatureRegxp.indexIn(QString(data), temperatureRegxp.matchedLength()) != -1) - { r.b = temperatureRegxp.cap(0).toDouble(); - } else { r.e = -1; @@ -755,7 +776,8 @@ double MainWindow::parseSDStatus(QByteArray data) } */ - if(SDStatusRegxp.indexIn(QString(data)) != 0) return SDStatusRegxp.cap(0).toDouble(); + if(SDStatusRegxp.indexIn(QString(data)) != 0) + return SDStatusRegxp.cap(0).toDouble(); else return -1; } diff --git a/mainwindow.h b/mainwindow.h index 5221aa4..d27481c 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -39,6 +39,7 @@ public: QFile gfile; QVector gcode; + QQueue userCommands; QTimer sendTimer; QTimer progressSDTimer; QTimer statusTimer; @@ -63,11 +64,11 @@ private: bool sending; bool paused; bool checkingTemperature; - bool injectingCommand; bool readingFiles; bool sdprinting; bool echo; - int currentLine; + bool sendingChecksum; + long int currentLine; int readyRecieve; double sdBytes; QString userCommand; diff --git a/settingswindow.cpp b/settingswindow.cpp index 3e65d5f..8e626c8 100644 --- a/settingswindow.cpp +++ b/settingswindow.cpp @@ -7,10 +7,12 @@ SettingsWindow::SettingsWindow(QWidget *parent) : { ui->setupUi(this); - if(!settings.value("core/firstrun").toBool()) ui->senderbox->setValue(4); + bool firstrun = !settings.value("core/firstrun").toBool(); //firstrun is inverted! + + if(firstrun) ui->senderbox->setValue(4); else ui->senderbox->setValue(settings.value("core/senderinterval").toFloat()); - if(!settings.value("core/firstrun").toBool()) ui->echobox->setChecked(false); + if(firstrun) ui->echobox->setChecked(false); else ui->echobox->setChecked(settings.value("core/echo").toBool()); if(settings.value("core/statusinterval").toInt()) ui->statusbox->setValue(settings.value("core/statusinterval").toInt()); @@ -23,6 +25,7 @@ SettingsWindow::SettingsWindow(QWidget *parent) : else ui->bedybox->setValue(200); ui->lockbox->setChecked(settings.value("core/lockcontrols").toBool()); + ui->checksumbox->setChecked(settings.value("core/checksums").toBool()); } @@ -39,4 +42,5 @@ void SettingsWindow::on_buttonBox_accepted() settings.setValue("printer/bedx", ui->bedxbox->value()); settings.setValue("core/echo", ui->echobox->isChecked()); settings.setValue("core/lockcontrols", ui->lockbox->isChecked()); + settings.setValue("core/checksums", ui->checksumbox->isChecked()); } diff --git a/settingswindow.ui b/settingswindow.ui index 55c54dc..39593c6 100644 --- a/settingswindow.ui +++ b/settingswindow.ui @@ -7,7 +7,7 @@ 0 0 253 - 242 + 330 @@ -24,14 +24,17 @@ Internal - - + + + + Show every sent command in console + - Sender + Echo commands - + A good default is 2, lower = fater, higher = less CPU load. 0 would execute as soon as possible. @@ -51,23 +54,26 @@ 0.100000000000000 + + 2.000000000000000 + - - + + ms - + Status - + A good default is 5000. More = less interruptions, less = better temperature monitoring @@ -84,19 +90,55 @@ - + ms - - - - Show every sent command in console + + + + Lock controls when printing + + + + + + + Sender + + + + + + + false - Echo commands + Checksums + + + + + + + + 0 + 28 + + + + This settings are applied after restart + + + Qt::AutoText + + + true + + + 1 @@ -105,14 +147,20 @@ + + + 0 + 0 + + Printer - - + + - X + Bed size @@ -126,10 +174,10 @@ - - + + - Bed size + X