Skip to content

Commit

Permalink
Translate gcode while sending a job
Browse files Browse the repository at this point in the history
  • Loading branch information
cheton committed Apr 21, 2017
1 parent 84d38e4 commit d6825ec
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 45 deletions.
28 changes: 14 additions & 14 deletions src/app/controllers/Grbl/GrblController.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class GrblController {

// Feeder
this.feeder = new Feeder();
this.feeder.on('data', (command = '', context = {}) => {
this.feeder.on('data', (line = '', context = {}) => {
if (this.isClose()) {
log.error(`[Grbl] Serial port "${this.options.port}" is not accessible`);
return;
Expand All @@ -174,7 +174,7 @@ class GrblController {
return;
}

let line = String(command).trim();
line = String(line).trim();
if (line.length === 0) {
return;
}
Expand All @@ -194,7 +194,7 @@ class GrblController {
// Deduct the length of periodic commands ('$G\n', '?') to prevent from buffer overrun
bufferSize: (128 - 8) // The default buffer size is 128 bytes
});
this.sender.on('data', (gcode = '', context = {}) => {
this.sender.on('data', (line = '', context = {}) => {
if (this.isClose()) {
log.error(`[Grbl] Serial port "${this.options.port}" is not accessible`);
return;
Expand All @@ -205,11 +205,18 @@ class GrblController {
return;
}

gcode = ('' + gcode).trim();
if (gcode.length > 0) {
this.serialport.write(gcode + '\n');
dbg(`[Grbl] > ${gcode}`);
line = String(line).trim();
if (line.length === 0) {
log.warn(`[Grbl] Expected non-empty line: N=${this.sender.state.sent}`);
return;
}

// Example
// "G0 X[posx - 8] Y[ymax]" -> "G0 X2 Y50"
line = this.translateWithContext(line, context);

this.serialport.write(line + '\n');
dbg(`[Grbl] > ${line}`);
});

// Workflow
Expand Down Expand Up @@ -641,13 +648,6 @@ class GrblController {
context = {};
}

// TODO: This will move to sender in a future release
if (Object.keys(context).length > 0) {
// Example
// "G0 X[posx - 8] Y[ymax]" -> "G0 X2 Y50"
gcode = this.translateWithContext(gcode, context);
}

const ok = this.sender.load(name, gcode, context);
if (!ok) {
callback(new Error(`Invalid G-code: name=${name}`));
Expand Down
28 changes: 14 additions & 14 deletions src/app/controllers/Smoothie/SmoothieController.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class SmoothieController {

// Feeder
this.feeder = new Feeder();
this.feeder.on('data', (command = '', context = {}) => {
this.feeder.on('data', (line = '', context = {}) => {
if (this.isClose()) {
log.error(`[Smoothie] Serial port "${this.options.port}" is not accessible`);
return;
Expand All @@ -173,7 +173,7 @@ class SmoothieController {
return;
}

let line = String(command).trim();
line = String(line).trim();
if (line.length === 0) {
return;
}
Expand All @@ -193,7 +193,7 @@ class SmoothieController {
// Deduct the length of periodic commands ('$G\n', '?') to prevent from buffer overrun
bufferSize: (128 - 8) // The default buffer size is 128 bytes
});
this.sender.on('data', (gcode = '', context = {}) => {
this.sender.on('data', (line = '', context = {}) => {
if (this.isClose()) {
log.error(`[Smoothie] Serial port "${this.options.port}" is not accessible`);
return;
Expand All @@ -204,11 +204,18 @@ class SmoothieController {
return;
}

gcode = ('' + gcode).trim();
if (gcode.length > 0) {
this.serialport.write(gcode + '\n');
dbg(`[Smoothie] > ${gcode}`);
line = String(line).trim();
if (line.length === 0) {
log.warn(`[Smoothie] Expected non-empty line: N=${this.sender.state.sent}`);
return;
}

// Example
// "G0 X[posx - 8] Y[ymax]" -> "G0 X2 Y50"
line = this.translateWithContext(line, context);

this.serialport.write(line + '\n');
dbg(`[Smoothie] > ${line}`);
});

// Workflow
Expand Down Expand Up @@ -621,13 +628,6 @@ class SmoothieController {
context = {};
}

// TODO: This will move to sender in a future release
if (Object.keys(context).length > 0) {
// Example
// "G0 X[posx - 8] Y[ymax]" -> "G0 X2 Y50"
gcode = this.translateWithContext(gcode, context);
}

const ok = this.sender.load(name, gcode, context);
if (!ok) {
callback(new Error(`Invalid G-code: name=${name}`));
Expand Down
35 changes: 18 additions & 17 deletions src/app/controllers/TinyG/TinyGController.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class TinyGController {

// Feeder
this.feeder = new Feeder();
this.feeder.on('data', (command = '', context = {}) => {
this.feeder.on('data', (line = '', context = {}) => {
if (this.isClose()) {
log.error(`[TinyG] Serial port "${this.options.port}" is not accessible`);
return;
Expand All @@ -166,7 +166,7 @@ class TinyGController {
return;
}

let line = String(command).trim();
line = String(line).trim();
if (line.length === 0) {
return;
}
Expand All @@ -183,7 +183,7 @@ class TinyGController {

// Sender
this.sender = new Sender(SP_TYPE_SEND_RESPONSE);
this.sender.on('data', (gcode = '', context = {}) => {
this.sender.on('data', (line = '', context = {}) => {
if (this.isClose()) {
log.error(`[TinyG] Serial port "${this.options.port}" is not accessible`);
return;
Expand All @@ -194,16 +194,24 @@ class TinyGController {
return;
}

// Remove blanks to reduce the amount of bandwidth
line = String(line).replace(/\s+/g, '');
if (line.length === 0) {
log.warn(`[TinyG] Expected non-empty line: N=${this.sender.state.sent}`);
return;
}

// Example
// "G0 X[posx - 8] Y[ymax]" -> "G0 X2 Y50"
line = this.translateWithContext(line, context);

// Replace line numbers with the number of lines sent
const n = this.sender.state.sent;
gcode = ('' + gcode).replace(/^N[0-9]*/, '');
gcode = ('N' + n + ' ' + gcode);

// Remove blanks to reduce the amount of bandwidth
gcode = ('' + gcode).replace(/\s+/g, '');
line = ('' + line).replace(/^N[0-9]*/, '');
line = ('N' + n + line);

this.serialport.write(gcode + '\n');
dbg(`[TinyG] > SEND: n=${n}, gcode="${gcode}"`);
this.serialport.write(line + '\n');
dbg(`[TinyG] > SEND: n=${n}, line="${line}"`);
});

// Workflow
Expand Down Expand Up @@ -630,13 +638,6 @@ class TinyGController {
context = {};
}

// TODO: This will move to sender in a future release
if (Object.keys(context).length > 0) {
// Example
// "G0 X[posx - 8] Y[ymax]" -> "G0 X2 Y50"
gcode = this.translateWithContext(gcode, context);
}

const ok = this.sender.load(name, gcode, context);
if (!ok) {
callback(new Error(`Invalid G-code: name=${name}`));
Expand Down

0 comments on commit d6825ec

Please sign in to comment.