From 04a1d317be1e43c7a3273448b74218fedc8dce37 Mon Sep 17 00:00:00 2001 From: NeoTheFox Date: Sat, 28 Feb 2015 18:12:34 +0300 Subject: [PATCH] initial commit --- Wizard.pro | 26 ++ main.cpp | 16 + mainwindow.cpp | 453 +++++++++++++++++++++++++ mainwindow.h | 90 +++++ mainwindow.ui | 827 +++++++++++++++++++++++++++++++++++++++++++++ settings.qrc | 1 + settings.ui | 151 +++++++++ settingswindow.cpp | 27 ++ settingswindow.h | 29 ++ settingswindow.ui | 125 +++++++ 10 files changed, 1745 insertions(+) create mode 100644 Wizard.pro create mode 100644 main.cpp create mode 100644 mainwindow.cpp create mode 100644 mainwindow.h create mode 100644 mainwindow.ui create mode 100644 settings.qrc create mode 100644 settings.ui create mode 100644 settingswindow.cpp create mode 100644 settingswindow.h create mode 100644 settingswindow.ui diff --git a/Wizard.pro b/Wizard.pro new file mode 100644 index 0000000..2c550b3 --- /dev/null +++ b/Wizard.pro @@ -0,0 +1,26 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2015-02-26T16:14:20 +# +#------------------------------------------------- + +QT += core gui serialport + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = Wizard +TEMPLATE = app + + +SOURCES += main.cpp\ + mainwindow.cpp \ + settingswindow.cpp + +HEADERS += mainwindow.h \ + settingswindow.h + +FORMS += mainwindow.ui \ + settingswindow.ui + +RESOURCES += \ + settings.qrc diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..284447b --- /dev/null +++ b/main.cpp @@ -0,0 +1,16 @@ +#include "mainwindow.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + + QCoreApplication::setOrganizationName("NeoTheFox"); + QCoreApplication::setOrganizationDomain("https://github.com/NeoTheFox"); + QCoreApplication::setApplicationName("RepRaptor"); + + MainWindow w; + w.show(); + + return a.exec(); +} diff --git a/mainwindow.cpp b/mainwindow.cpp new file mode 100644 index 0000000..45faf39 --- /dev/null +++ b/mainwindow.cpp @@ -0,0 +1,453 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow) +{ + ui->setupUi(this); + + ui->fileBox->setDisabled(true); + ui->sendBtn->setDisabled(true); + ui->pauseBtn->setDisabled(true); + ui->progressBar->setValue(0); + ui->controlBox->setDisabled(true); + ui->consoleGroup->setDisabled(true); + ui->pauseBtn->setDisabled("true"); + + //ui->extruderlcd->setSegmentStyle(); + + ui->baudbox->addItem(QString::number(4800)); + ui->baudbox->addItem(QString::number(9600)); + ui->baudbox->addItem(QString::number(115200)); + ui->baudbox->addItem(QString::number(128000)); + ui->baudbox->addItem(QString::number(230400)); + ui->baudbox->addItem(QString::number(250000)); + ui->baudbox->addItem(QString::number(460800)); + ui->baudbox->addItem(QString::number(500000)); + ui->baudbox->setCurrentIndex(2); + + ui->extruderlcd->setPalette(Qt::red); + ui->bedlcd->setPalette(Qt::red); + + sending = false; + paused = false; + commandDone = false; + checkingTemperature = true; + currentLine = 0; + + serialupdate(); + + connect(&printer, SIGNAL(readyRead()), this, SLOT(readSerial())); + connect(&statusTimer, SIGNAL(timeout()), this, SLOT(checkStatus())); + connect(&sendTimer, SIGNAL(timeout()), this, SLOT(sendNext())); + + if(settings.value("core/statusinterval").toInt()) statusTimer.setInterval(settings.value("core/statusinterval").toInt()); + else statusTimer.setInterval(3000); + statusTimer.start(); + + if(settings.value("core/senderinterval").toInt()) sendTimer.setInterval(settings.value("core/senderinterval").toInt()); + else sendTimer.setInterval(50); + sendTimer.start(); +} + +MainWindow::~MainWindow() +{ + if(gfile.isOpen()) gfile.close(); + if(printer.isOpen()) printer.close(); + + settings.beginWriteArray("user/recentfiles"); + for(int i = 0; i < recentFiles.size(); i++) + { + settings.setArrayIndex(i); + settings.setValue("user/recentfile", recentFiles.at(i)); + } + settings.endArray(); + + delete ui; +} + +void MainWindow::open() +{ + QString filename; + filename = QFileDialog::getOpenFileName(this, tr("Open GCODE"), "/home/", tr("GCODE (*.g *.gcode *.nc)")); + + gfile.setFileName(filename); + if(!recentFiles.contains(filename)) + { + if(recentFiles.size() < 5) + { + recentFiles.append(filename); + } + else + { + recentFiles.push_front(filename); + } + } + + parseFile(gfile); +} + +void MainWindow::parseFile(QFile &file) +{ + gcode.clear(); + if (file.open(QIODevice::ReadOnly)) + { + QTextStream in(&file); + while (!in.atEnd()) + { + QString line = in.readLine(); + if(!line.startsWith(";")) + { + gcode.append(line); + } + + } + file.close(); + ui->fileBox->setEnabled(true); + ui->filename->setText(file.fileName().split("/").last()); + ui->filelines->setText(QString::number(gcode.size()) + QString("/0 lines")); + } +} + +bool MainWindow::sendLine(QString line) +{ + if(printer.isOpen()) + { + if(printer.write(line.toUtf8()+'\n')) + { + printMsg(line); + return true; + } + else + { + return false; + } + } + else return false; +} + +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() +{ + if(!printer.isOpen()) + { + foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) + { + if(info.portName() == ui->serialBox->currentText()) + { + printerinfo = info; + break; + } + } + + printer.setPort(printerinfo); + printer.setFlowControl(QSerialPort::HardwareControl); + + switch(ui->baudbox->currentText().toInt()) + { + case 4800: + printer.setBaudRate(QSerialPort::Baud4800); + break; + + case 9600: + printer.setBaudRate(QSerialPort::Baud9600); + break; + + case 115200: + printer.setBaudRate(QSerialPort::Baud115200); + break; + + default: + printer.setBaudRate(QSerialPort::Baud115200); + break; + } + + if(printer.open(QIODevice::ReadWrite)) + { + ui->connectBtn->setText("Disconnect"); + ui->sendBtn->setDisabled(false); + ui->pauseBtn->setDisabled(false); + ui->progressBar->setValue(0); + ui->controlBox->setDisabled(false); + ui->consoleGroup->setDisabled(false); + } + } + + else if(printer.isOpen()) + { + printer.close(); + + ui->connectBtn->setText("Connect"); + ui->sendBtn->setDisabled(true); + ui->pauseBtn->setDisabled(true); + ui->progressBar->setValue(0); + ui->controlBox->setDisabled(true); + ui->consoleGroup->setDisabled(true); + } +} + +//Buttons start +void MainWindow::xplus() +{ + QString command = "G91\n G1 X" + ui->stepspin->text() + "\n G90"; + sendLine(command); +} + +void MainWindow::xminus() +{ + QString command = "G91\n G1 X-" + ui->stepspin->text() + "\n G90"; + sendLine(command); +} + +void MainWindow::xhome() +{ + sendLine("G28 X0"); +} + +void MainWindow::yplus() +{ + QString command = "G91\n G1 Y" + ui->stepspin->text() + "\n G90"; + sendLine(command); +} + +void MainWindow::yminus() +{ + QString command = "G91\n G1 Y-" + ui->stepspin->text() + "\n G90"; + sendLine(command); +} + +void MainWindow::yhome() +{ + sendLine("G28 Y0"); +} + +void MainWindow::zplus() +{ + QString command = "G91\n G1 Z" + ui->stepspin->text() + "\n G90"; + sendLine(command); +} + +void MainWindow::zminus() +{ + QString command = "G91\n G1 Z-" + ui->stepspin->text() + "\n G90"; + sendLine(command); +} + +void MainWindow::zhome() +{ + sendLine("G28 Z0"); +} + +void MainWindow::eplus() +{ + QString command = "G91\n G1 E" + ui->estepspin->text() + "\n G90"; + sendLine(command); +} + +void MainWindow::eminus() +{ + QString command = "G91\n G1 E-" + ui->estepspin->text() + "\n G90"; + sendLine(command); +} + +void MainWindow::ezero() +{ + sendLine("G92 E0"); +} + +void MainWindow::homeall() +{ + sendLine("G28"); +} + +void MainWindow::on_sendbtn_clicked() +{ + sendLine(ui->sendtext->text()); +} + +void MainWindow::on_fanonbtn_clicked() +{ + sendLine("M106"); +} + +void MainWindow::on_fanoffbtn_clicked() +{ + sendLine("M107"); +} + +void MainWindow::on_atxonbtn_clicked() +{ + sendLine("M80"); +} + +void MainWindow::on_atxoffbtn_clicked() +{ + sendLine("M81"); +} + +void MainWindow::on_etmpset_clicked() +{ + QString command = "M104 S" + ui->etmpspin->text(); + sendLine(command); +} + +void MainWindow::on_etmpoff_clicked() +{ + sendLine("M104 S0"); +} + +void MainWindow::on_btmpset_clicked() +{ + QString command = "M140 S" + ui->btmpspin->text(); + sendLine(command); +} + +void MainWindow::on_btmpoff_clicked() +{ + sendLine("M140 S0"); +} +//Buttons end + +void MainWindow::readSerial() +{ + if(printer.canReadLine()) + { + QByteArray data = printer.readLine(); + if(data.startsWith("T:")) + { + QString extmp = ""; + QString btmp = ""; + for(int i = 2; data.at(i) != '/'; i++) + { + extmp+=data.at(i); + } + for(int i = data.indexOf("B:")+2; data.at(i) != '/'; i++) + { + btmp+=data.at(i); + } + + ui->extruderlcd->display(extmp.toDouble()); + ui->bedlcd->display(btmp.toDouble()); + } + else if(data.startsWith("ok") || data.startsWith("wait")) commandDone = true; + printMsg(QString(data)); + } +} + +void MainWindow::printMsg(const char* text) +{ + QTextCursor cursor = ui->terminal->textCursor(); + cursor.movePosition(QTextCursor::End); + + //QTextBlockFormat bf; + //QTextCharFormat cf; + + //cursor.insertBlock(bf, cf); + cursor.insertText(text); + cursor.insertText("\n"); + + ui->terminal->setTextCursor(cursor); +; +} + +void MainWindow::printMsg(QString text) +{ + QTextCursor cursor = ui->terminal->textCursor(); + cursor.movePosition(QTextCursor::End); + + //QTextBlockFormat bf; + //QTextCharFormat cf; + + //cursor.insertBlock(bf, cf); + cursor.insertText(text); + cursor.insertText("\n"); + + ui->terminal->setTextCursor(cursor); +} + +void MainWindow::on_sendBtn_clicked() +{ + if(sending) + { + sending = false; + ui->sendBtn->setText("Send"); + ui->pauseBtn->setDisabled("true"); + } + else if(!sending) + { + sending=true; + ui->sendBtn->setText("Stop"); + ui->pauseBtn->setEnabled("true"); + } + + ui->progressBar->setValue(0); + currentLine = 0; +} + +void MainWindow::sendNext() +{ + if(sending && !paused && printer.bytesToWrite() < 100 && commandDone) + { + if(currentLine >= gcode.size()) //check if we are at the end of array + { + sending = false; + currentLine = 0; + ui->sendBtn->setText("Send"); + ui->pauseBtn->setDisabled("true"); + return; + } + sendLine(gcode.at(currentLine)); + currentLine++; + commandDone = false; + + ui->filelines->setText(QString::number(gcode.size()) + QString("/") + QString::number(currentLine) + QString(" Lines")); + ui->progressBar->setValue(((float)currentLine/gcode.size()) * 100); + } +} + +void MainWindow::on_pauseBtn_clicked() +{ + if(paused) + { + paused = false; + ui->pauseBtn->setText("Pause"); + } + else + { + paused = true; + ui->pauseBtn->setText("Resume"); + } +} + +void MainWindow::checkStatus() +{ + if(checkingTemperature) sendLine("M105"); +} + +void MainWindow::on_checktemp_stateChanged(int arg1) +{ + if(arg1) checkingTemperature = true; + else checkingTemperature = false; +} + +void MainWindow::on_actionSettings_triggered() +{ + SettingsWindow settingswindow(this); + + settingswindow.exec(); +} + +void MainWindow::on_releasebtn_clicked() +{ + sendLine("M84"); +} diff --git a/mainwindow.h b/mainwindow.h new file mode 100644 index 0000000..1bbded6 --- /dev/null +++ b/mainwindow.h @@ -0,0 +1,90 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "settingswindow.h" + + +namespace Ui { +class MainWindow; +} + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = 0); + ~MainWindow(); + + QFile gfile; + QVector gcode; + QTimer sendTimer; + QTimer statusTimer; + QSettings settings; + QStringList recentFiles; + +private: + Ui::MainWindow *ui; + + void parseFile(QFile &file); + QSerialPort printer; + QSerialPortInfo printerinfo; + bool sending; + bool paused; + bool commandDone; + bool checkingTemperature; + int currentLine; + + +private slots: + void open(); + void serialconnect(); + void serialupdate(); + bool sendLine(QString line); + + void readSerial(); + void printMsg(QString text); + void printMsg(const char* text); + void sendNext(); + void checkStatus(); + + void xplus(); + void yplus(); + void zplus(); + void eplus(); + void xminus(); + void yminus(); + void zminus(); + void eminus(); + void xhome(); + void yhome(); + void zhome(); + void ezero(); + void homeall(); + void on_sendbtn_clicked(); + void on_fanonbtn_clicked(); + void on_fanoffbtn_clicked(); + void on_atxonbtn_clicked(); + void on_atxoffbtn_clicked(); + void on_etmpset_clicked(); + void on_etmpoff_clicked(); + void on_btmpset_clicked(); + void on_btmpoff_clicked(); + void on_sendBtn_clicked(); + void on_pauseBtn_clicked(); + void on_checktemp_stateChanged(int arg1); + void on_actionSettings_triggered(); + void on_releasebtn_clicked(); +}; + +#endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui new file mode 100644 index 0000000..dd61b44 --- /dev/null +++ b/mainwindow.ui @@ -0,0 +1,827 @@ + + + MainWindow + + + + 0 + 0 + 768 + 498 + + + + RepRaptor + + + + + + 10 + 10 + 189 + 94 + + + + Serial + + + + + + Ports + + + + + + + + + + Connect + + + + + + + + + + + + 10 + 120 + 190 + 136 + + + + File + + + + + + + 0 + 0 + + + + Filename: + + + + + + + Lines: + + + + + + + 24 + + + + + + + Send + + + + + + + Pause + + + + + + + + + 480 + 10 + 280 + 371 + + + + Console + + + + + + false + + + + + + + Clear + + + + + + + + + + Send + + + + + + + + + 210 + 10 + 261 + 311 + + + + Control + + + + + + + 0 + 0 + + + + H X + + + + + + + X- + + + + + + + X+ + + + + + + + H Y + + + + + + + H Z + + + + + + + 1.000000000000000 + + + 150.000000000000000 + + + + + + + E tmp + + + + + + + SET + + + + + + + OFF + + + + + + + B tmp + + + + + + + SET + + + + + + + OFF + + + + + + + FAN ON + + + + + + + FAN OFF + + + + + + + ATX ON + + + + + + + ATX OFF + + + + + + + E+ + + + + + + + Z+ + + + + + + + E0 + + + + + + + C + + + + + + + Y+ + + + + + + + Y- + + + + + + + E- + + + + + + + Z- + + + + + + + 500 + + + + + + + 500 + + + + + + + 1.000000000000000 + + + 100.000000000000000 + + + + + + + Step + + + + + + + E Step + + + + + + + Home All + + + + + + + Release + + + + + + + + + 10 + 270 + 166 + 121 + + + + Status + + + + + + Extruder + + + + + + + QLCDNumber::Outline + + + + + + + Bed + + + + + + + QLCDNumber::Outline + + + + + + + Check temperature + + + true + + + false + + + + + + + + + + 0 + 0 + 768 + 20 + + + + + File + + + + + + + + + + + + + TopToolBarArea + + + false + + + + + + toolBar + + + TopToolBarArea + + + false + + + + + Open... + + + Ctrl+O + + + + + Recent + + + + + Exit + + + Ctrl+Q + + + + + Settings + + + + + + + + actionOpen + triggered() + MainWindow + open() + + + -1 + -1 + + + 247 + 217 + + + + + portsBtn + clicked() + MainWindow + serialupdate() + + + 61 + 88 + + + 247 + 217 + + + + + actionExit + triggered() + MainWindow + close() + + + -1 + -1 + + + 247 + 217 + + + + + connectBtn + clicked() + MainWindow + serialconnect() + + + 61 + 117 + + + 247 + 217 + + + + + homexbtn + clicked() + MainWindow + xhome() + + + 434 + 88 + + + 383 + 248 + + + + + clearbtn + clicked() + terminal + clear() + + + 624 + 335 + + + 654 + 275 + + + + + xplusbtn + clicked() + MainWindow + xplus() + + + 327 + 117 + + + 383 + 248 + + + + + sendtext + returnPressed() + sendbtn + click() + + + 540 + 369 + + + 548 + 397 + + + + + yminusbtn + clicked() + MainWindow + yminus() + + + 292 + 146 + + + 383 + 248 + + + + + yplusbtn + clicked() + MainWindow + yplus() + + + 292 + 88 + + + 383 + 248 + + + + + xminusbtn + clicked() + MainWindow + xminus() + + + 256 + 120 + + + 189 + 43 + + + + + zplusbtn + clicked() + MainWindow + zplus() + + + 359 + 82 + + + 356 + -10 + + + + + zminusbtn + clicked() + MainWindow + zminus() + + + 362 + 156 + + + 209 + 151 + + + + + ezerobtn + clicked() + MainWindow + ezero() + + + 399 + 123 + + + 220 + 117 + + + + + homezbtn + clicked() + MainWindow + zhome() + + + 438 + 144 + + + 209 + 199 + + + + + homeybtn + clicked() + MainWindow + yhome() + + + 434 + 117 + + + 383 + 248 + + + + + eplusbtn + clicked() + MainWindow + eplus() + + + 397 + 93 + + + 397 + 42 + + + + + eminusbtn + clicked() + MainWindow + eminus() + + + 406 + 142 + + + 475 + 173 + + + + + + open() + serialupdate() + serialconnect() + xhome() + homeall() + xplus() + xminus() + yplus() + yminus() + zplus() + zminus() + zhome() + eplus() + eminus() + ezero() + yhome() + + diff --git a/settings.qrc b/settings.qrc new file mode 100644 index 0000000..7646d2b --- /dev/null +++ b/settings.qrc @@ -0,0 +1 @@ + diff --git a/settings.ui b/settings.ui new file mode 100644 index 0000000..c61b3a4 --- /dev/null +++ b/settings.ui @@ -0,0 +1,151 @@ + + + Dialog + + + + 0 + 0 + 400 + 300 + + + + Dialog + + + + + 10 + 10 + 181 + 65 + + + + Hardware + + + + + + Bed Size + + + + + + + 200 + + + + + + + X + + + + + + + 200 + + + + + + + + + 220 + 270 + 80 + 23 + + + + Save + + + + + + 310 + 270 + 80 + 23 + + + + Cancel + + + + + + 10 + 90 + 181 + 96 + + + + Intervals + + + + + + Sender + + + + + + + 9999 + + + 50 + + + + + + + ms + + + + + + + Status + + + + + + + 9999 + + + 3000 + + + + + + + ms + + + + + + + + + diff --git a/settingswindow.cpp b/settingswindow.cpp new file mode 100644 index 0000000..ce6e441 --- /dev/null +++ b/settingswindow.cpp @@ -0,0 +1,27 @@ +#include "settingswindow.h" +#include "ui_settingswindow.h" + +SettingsWindow::SettingsWindow(QWidget *parent) : + QDialog(parent), + ui(new Ui::SettingsWindow) +{ + ui->setupUi(this); + + if(settings.value("core/senderinterval").toInt()) ui->senderbox->setValue(settings.value("core/senderinterval").toInt()); + else ui->senderbox->setValue(50); + + if(settings.value("core/statusinterval").toInt()) ui->statusbox->setValue(settings.value("core/statusinterval").toInt()); + else ui->senderbox->setValue(3000); + +} + +SettingsWindow::~SettingsWindow() +{ + delete ui; +} + +void SettingsWindow::on_buttonBox_accepted() +{ + settings.setValue("core/senderinterval", ui->senderbox->value()); + settings.setValue("core/statusinterval", ui->statusbox->value()); +} diff --git a/settingswindow.h b/settingswindow.h new file mode 100644 index 0000000..1851a00 --- /dev/null +++ b/settingswindow.h @@ -0,0 +1,29 @@ +#ifndef SETTINGSWINDOW_H +#define SETTINGSWINDOW_H + +#include +#include + +namespace Ui { +class SettingsWindow; +} + +class SettingsWindow : public QDialog +{ + Q_OBJECT + +public: + explicit SettingsWindow(QWidget *parent = 0); + + QSettings settings; + + ~SettingsWindow(); + +private slots: + void on_buttonBox_accepted(); + +private: + Ui::SettingsWindow *ui; +}; + +#endif // SETTINGSWINDOW_H diff --git a/settingswindow.ui b/settingswindow.ui new file mode 100644 index 0000000..fbd25c3 --- /dev/null +++ b/settingswindow.ui @@ -0,0 +1,125 @@ + + + SettingsWindow + + + + 0 + 0 + 400 + 300 + + + + Settings + + + + + 30 + 260 + 341 + 32 + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + 10 + 10 + 144 + 96 + + + + Intervals + + + + + + Sender + + + + + + + 999 + + + + + + + ms + + + + + + + Status + + + + + + + 9999 + + + + + + + ms + + + + + + + + + + buttonBox + accepted() + SettingsWindow + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + SettingsWindow + reject() + + + 316 + 260 + + + 286 + 274 + + + + +