Moved checksum algorithm

This commit is contained in:
NeoTheFox 2015-03-13 14:49:08 +03:00
parent d007eb32d9
commit 314f853e40
2 changed files with 55 additions and 41 deletions

View File

@ -69,6 +69,8 @@ MainWindow::MainWindow(QWidget *parent) :
readyRecieve = 1; readyRecieve = 1;
lastRecieved = 0; lastRecieved = 0;
userHistoryPos = 0; userHistoryPos = 0;
totalLineNum = 0;
resendLineNum = -1;
userHistory.append(""); userHistory.append("");
//Update serial ports //Update serial ports
@ -171,21 +173,7 @@ void MainWindow::parseFile(QString filename)
while (!in.atEnd()) while (!in.atEnd())
{ {
QString line = in.readLine(); QString line = in.readLine();
if(!line.startsWith(";")) if(!line.startsWith(";")) gcode.append(line);
{
if(sendingChecksum)
{
//Checksum algorithm from RepRap wiki
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);
}
} }
gfile.close(); gfile.close();
ui->fileBox->setEnabled(true); ui->fileBox->setEnabled(true);
@ -198,9 +186,20 @@ void MainWindow::parseFile(QString filename)
bool MainWindow::sendLine(QString line) bool MainWindow::sendLine(QString line)
{ {
if(printer.isOpen()) if(printer.isOpen())
{ {
if(sendingChecksum)
{
if(line.contains("M110")) totalLineNum = 0;
//Checksum algorithm from RepRap wiki
line = "N"+QString::number(totalLineNum)+line+"*";
int cs = 0;
for(int i = 0; line.at(i) != '*'; i++) cs = cs ^ line.at(i).toLatin1();
cs &= 0xff;
line += QString::number(cs);
totalLineNum++;
}
if(printer.write(line.toUtf8()+'\n')) if(printer.write(line.toUtf8()+'\n'))
{ {
if(echo) printMsg(line + '\n'); if(echo) printMsg(line + '\n');
@ -208,6 +207,7 @@ bool MainWindow::sendLine(QString line)
} }
else return false; else return false;
} }
else return false; else return false;
} }
@ -579,36 +579,47 @@ void MainWindow::printMsg(QString text)
void MainWindow::sendNext() void MainWindow::sendNext()
{ {
if(!userCommands.isEmpty() && printer.isWritable() && readyRecieve > 0) //Inject user command if(printer.isWritable())
{ {
sendLine(userCommands.dequeue()); if(resendLineNum != -1)
readyRecieve--;
return;
}
else if(sending && !paused && readyRecieve > 0 && !sdprinting && printer.isWritable()) //Send line of gcode
{
if(currentLine >= gcode.size()) //check if we are at the end of array
{ {
sending = false; if(gcode.isEmpty()) sendLine("M110 N0");
currentLine = 0; else sendLine(gcode.at(resendLineNum));
ui->sendBtn->setText("Send"); resendLineNum = -1;
ui->pauseBtn->setDisabled(true);
ui->filelines->setText(QString::number(gcode.size())
+ QString("/")
+ QString::number(currentLine)
+ QString(" Lines"));
if(sendingChecksum) injectCommand("M110 N0");
return; return;
} }
sendLine(gcode.at(currentLine)); if(!userCommands.isEmpty() && readyRecieve > 0) //Inject user command
currentLine++; {
readyRecieve--; sendLine(userCommands.dequeue());
readyRecieve--;
return;
}
else if(sending && !paused && readyRecieve > 0 && !sdprinting) //Send line of gcode
{
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);
ui->filelines->setText(QString::number(gcode.size())
+ QString("/")
+ QString::number(currentLine)
+ QString(" Lines"));
if(sendingChecksum) injectCommand("M110 N0");
return;
}
sendLine(gcode.at(currentLine));
currentLine++;
readyRecieve--;
ui->filelines->setText(QString::number(gcode.size()) ui->filelines->setText(QString::number(gcode.size())
+ QString("/") + QString("/")
+ QString::number(currentLine) + QString::number(currentLine)
+ QString(" Lines")); + QString(" Lines"));
ui->progressBar->setValue(((float)currentLine/gcode.size()) * 100); ui->progressBar->setValue(((float)currentLine/gcode.size()) * 100);
}
} }
} }
@ -882,6 +893,7 @@ void MainWindow::recievedSDDone()
void MainWindow::recievedResend(int num) void MainWindow::recievedResend(int num)
{ {
if(!sendingChecksum) currentLine--; if(!sendingChecksum) currentLine--;
else resendLineNum = num;
} }
bool MainWindow::eventFilter(QObject *obj, QEvent *event) bool MainWindow::eventFilter(QObject *obj, QEvent *event)

View File

@ -75,6 +75,8 @@ private:
long int currentLine; long int currentLine;
unsigned long int lastRecieved; unsigned long int lastRecieved;
int readyRecieve; int readyRecieve;
unsigned long int totalLineNum;
long int resendLineNum;
int userHistoryPos; int userHistoryPos;
unsigned long int sdBytes; unsigned long int sdBytes;