Skip to content

Commit

Permalink
Command script regex fix.
Browse files Browse the repository at this point in the history
Command queue fix.
pull the Experimental (Denvi#534)
  • Loading branch information
odaki committed Jun 21, 2022
1 parent cdfec92 commit c49df23
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 31 deletions.
70 changes: 41 additions & 29 deletions src/candle/frmmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -899,26 +899,31 @@ void frmMain::openPort()
}
}

void frmMain::sendCommand(QString command, int tableIndex, bool showInConsole, bool queue)
frmMain::SendCommandResult frmMain::sendCommand(QString command, int tableIndex, bool showInConsole, bool wait)
{
if (!m_serialPort.isOpen() || !m_resetCompleted) return;
if (!m_serialPort.isOpen() || !m_resetCompleted) return SendDone;

// Commands queue
if (queue || (bufferLength() + command.length() + 1) > BUFFERLENGTH) {
CommandQueue cq;
// Check command
if (command.isEmpty()) return SendEmpty;

cq.command = command;
cq.tableIndex = tableIndex;
cq.showInConsole = showInConsole;
cq.queue = queue;

m_queue.append(cq);
return;
// Place to queue on 'wait' flag
if (wait) {
m_queue.append(CommandQueue(command, tableIndex, showInConsole));
return SendQueue;
}

// Evaluate scripts in command
if (tableIndex < 0) command = evaluateCommand(command);

// Check evaluated command
if (command.isEmpty()) return SendEmpty;

// Place to queue if command buffer is full
if ((bufferLength() + command.length() + 1) > BUFFERLENGTH) {
m_queue.append(CommandQueue(command, tableIndex, showInConsole));
return SendQueue;
}

command = command.toUpper();

CommandAttributes ca;
Expand Down Expand Up @@ -956,6 +961,8 @@ void frmMain::sendCommand(QString command, int tableIndex, bool showInConsole, b
}

m_serialPort.write((command + "\r").toLatin1());

return SendDone;
}

void frmMain::grblReset()
Expand Down Expand Up @@ -1436,18 +1443,20 @@ void frmMain::onSerialPortReadyRead()
emit responseReceived(ca.command, ca.tableIndex, response);

// Check queue
if (m_queue.length() > 0) {
CommandQueue cq = m_queue.takeFirst();
while (true) {
if ((bufferLength() + cq.command.length() + 1) <= BUFFERLENGTH) {
if (!cq.command.isEmpty()) sendCommand(cq.command, cq.tableIndex, cq.showInConsole);
if (!cq.command.isEmpty() && (m_queue.isEmpty() || cq.queue)) break;
else cq = m_queue.takeFirst();
} else {
m_queue.insert(0, cq);
break;
}
}
static bool processingQueue = false;
if (m_queue.length() > 0 && !processingQueue) {
processingQueue = true;
while (m_queue.length() > 0) {
CommandQueue cq = m_queue.takeFirst();
SendCommandResult r = sendCommand(cq.command, cq.tableIndex, cq.showInConsole);
if (r == SendDone) {
break;
} else if (r == SendQueue) {
m_queue.prepend(m_queue.takeLast());
break;
}
}
processingQueue = false;
}

// Add response to table, send next program commands
Expand Down Expand Up @@ -3990,14 +3999,17 @@ void frmMain::jogStep()
QString frmMain::evaluateCommand(QString command)
{
// Evaluate script
QRegExp sx("\\{([^\\}]+)\\}");
static QRegularExpression rx("\\{(?:(?>[^\\{\\}])|(?0))*\\}");
QRegularExpressionMatch m;
QScriptValue v;
QString vs;
while (sx.indexIn(command) != -1) {
v = m_scriptEngine.evaluate(sx.cap(1));

while ((m = rx.match(command)).hasMatch()) {
v = m_scriptEngine.evaluate(m.captured(0));
vs = v.isUndefined() ? "" : v.isNumber() ? QString::number(v.toNumber(), 'f', 4) : v.toString();
command.replace(sx.cap(0), vs);
command.replace(m.captured(0), vs);
}

return command;
}

Expand Down
28 changes: 26 additions & 2 deletions src/candle/frmmain.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,31 @@ struct CommandAttributes {
int consoleIndex;
int tableIndex;
QString command;

CommandAttributes() {
}

CommandAttributes(int len, int consoleIdx, int tableIdx, QString cmd) {
length = len;
consoleIndex = consoleIdx;
tableIndex = tableIdx;
command = cmd;
}
};

struct CommandQueue {
QString command;
int tableIndex;
bool showInConsole;
bool queue;

CommandQueue() {
}

CommandQueue(QString cmd, int idx, bool show) {
command = cmd;
tableIndex = idx;
showInConsole = show;
}
};

class CancelException : public std::exception {
Expand All @@ -92,7 +110,6 @@ class frmMain : public QMainWindow
explicit frmMain(QWidget *parent = 0);
~frmMain();

Q_INVOKABLE void sendCommand(QString command, int tableIndex = -1, bool showInConsole = true, bool queue = false);
Q_INVOKABLE void applySettings();

double toolZPosition();
Expand Down Expand Up @@ -234,6 +251,12 @@ private slots:
private:
static const int BUFFERLENGTH = 127;

enum SendCommandResult {
SendDone = 0,
SendEmpty = 1,
SendQueue = 2
};

Ui::frmMain *ui;

GcodeViewParse m_viewParser;
Expand Down Expand Up @@ -368,6 +391,7 @@ private slots:
bool saveChanges(bool heightMapMode);
void updateControlsState();
void openPort();
SendCommandResult sendCommand(QString command, int tableIndex = -1, bool showInConsole = true, bool wait = false);
QString evaluateCommand(QString command);
void grblReset();
int bufferLength();
Expand Down

0 comments on commit c49df23

Please sign in to comment.