Skip to content

Commit

Permalink
Add macro variables support (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheton committed Mar 18, 2017
1 parent 25646ff commit d47a809
Show file tree
Hide file tree
Showing 11 changed files with 503 additions and 111 deletions.
2 changes: 1 addition & 1 deletion src/app/api/api.gcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const set = (req, res) => {

// Load G-code
const { name = '' } = meta;
controller.command(null, 'load', name, gcode, (err) => {
controller.command(null, 'gcode:load', name, gcode, (err) => {
if (err) {
res.status(500).send({
err: 'Failed to load G-code: ' + err
Expand Down
109 changes: 95 additions & 14 deletions src/app/controllers/Grbl/GrblController.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import _ from 'lodash';
import SerialPort from 'serialport';
import log from '../../lib/log';
import ensureArray from '../../lib/ensure-array';
import EventTrigger from '../../lib/event-trigger';
import Feeder from '../../lib/feeder';
import log from '../../lib/log';
import Sender, { SP_TYPE_CHAR_COUNTING } from '../../lib/sender';
import Workflow, {
WORKFLOW_STATE_IDLE,
Expand All @@ -28,6 +29,26 @@ const dbg = (...args) => {
log.raw.apply(log, ['silly'].concat(args));
};

const replaceVariables = (gcode = '', vars = {}) => {
const interpolationPrefix = '[';
const interpolationSuffix = ']';

if (typeof vars !== 'object') {
return gcode;
}

Object.keys(vars).forEach(key => {
const value = vars[key];
if (value === undefined || value === null) {
return;
}
const re = new RegExp(_.escapeRegExp(interpolationPrefix + key + interpolationSuffix), 'g');
gcode = gcode.replace(re, value);
});

return gcode;
};

class GrblController {
type = GRBL;

Expand Down Expand Up @@ -583,7 +604,7 @@ class GrblController {
}
command(socket, cmd, ...args) {
const handler = {
'load': () => {
'gcode:load': () => {
const [name, gcode, callback = noop] = args;

const ok = this.sender.load(name, gcode);
Expand All @@ -600,7 +621,7 @@ class GrblController {

callback(null, { name: name, gcode: gcode });
},
'unload': () => {
'gcode:unload': () => {
this.workflow.stop();

// Sender
Expand All @@ -609,6 +630,10 @@ class GrblController {
this.event.trigger('gcode:unload');
},
'start': () => {
log.warn(`[Grbl] Warning: The "${cmd}" command is deprecated and will be removed in a future release.`);
this.command(socket, 'gcode:start');
},
'gcode:start': () => {
this.event.trigger('gcode:start');

this.workflow.start();
Expand All @@ -620,6 +645,10 @@ class GrblController {
this.sender.next();
},
'stop': () => {
log.warn(`[Grbl] Warning: The "${cmd}" command is deprecated and will be removed in a future release.`);
this.command(socket, 'gcode:stop');
},
'gcode:stop': () => {
this.event.trigger('gcode:stop');

this.workflow.stop();
Expand All @@ -635,13 +664,21 @@ class GrblController {
}, delay);
},
'pause': () => {
log.warn(`[Grbl] Warning: The "${cmd}" command is deprecated and will be removed in a future release.`);
this.command(socket, 'gcode:pause');
},
'gcode:pause': () => {
this.event.trigger('gcode:pause');

this.workflow.pause();

this.write(socket, '!');
},
'resume': () => {
log.warn(`[Grbl] Warning: The "${cmd}" command is deprecated and will be removed in a future release.`);
this.command(socket, 'gcode:resume');
},
'gcode:resume': () => {
this.event.trigger('gcode:resume');

this.write(socket, '~');
Expand Down Expand Up @@ -736,24 +773,63 @@ class GrblController {
commands.push('G4P' + (duration / 1000));
commands.push('M5S0');
}
this.command(null, 'gcode', commands.join('\n'));
this.command(socket, 'gcode', commands.join('\n'));
},
'lasertest:off': () => {
const commands = [
'M5S0'
];
this.command(null, 'gcode', commands.join('\n'));
this.command(socket, 'gcode', commands.join('\n'));
},
'gcode': () => {
const command = args.join(' ').split('\n');
this.feeder.feed(command);
const [commands, params] = args;
const data = ensureArray(commands)
.filter(line => {
if (typeof line !== 'string') {
return false;
}

return line.trim().length > 0;
});

this.feeder.feed(data, params);

if (!this.feeder.isPending()) {
this.feeder.next();
}
},
'loadmacro': () => {
const [id, callback = noop] = args;
'macro:run': () => {
let [id, vars = {}, callback = noop] = args;
if (typeof vars === 'function') {
callback = vars;
vars = {};
}

const macros = config.get('macros');
const macro = _.find(macros, { id: id });

if (!macro) {
log.error(`[Grbl] Cannot find the macro: id=${id}`);
return;
}

// Replace variables
// G0 X[xmin] -> G0 X0.5
// G0 X[xmax] -> G0 X100.5
const gcode = replaceVariables(macro.content, vars);

this.event.trigger('macro:run');

this.command(socket, 'gcode', gcode);
callback(null);
},
'macro:load': () => {
let [id, vars = {}, callback = noop] = args;
if (typeof vars === 'function') {
callback = vars;
vars = {};
}

const macros = config.get('macros');
const macro = _.find(macros, { id: id });

Expand All @@ -762,11 +838,16 @@ class GrblController {
return;
}

this.event.trigger('loadmacro');
// Replace variables
// G0 X[xmin] -> G0 X0.5
// G0 X[xmax] -> G0 X100.5
const gcode = replaceVariables(macro.content, vars);

this.event.trigger('macro:load');

this.command(null, 'load', macro.name, macro.content, callback);
this.command(socket, 'gcode:load', macro.name, gcode, callback);
},
'loadfile': () => {
'watchdir:load': () => {
const [file, callback = noop] = args;

monitor.readFile(file, (err, data) => {
Expand All @@ -775,9 +856,9 @@ class GrblController {
return;
}

this.event.trigger('loadfile');
this.event.trigger('watchdir:load');

this.command(null, 'load', file, data, callback);
this.command(socket, 'gcode:load', file, data, callback);
});
}
}[cmd];
Expand Down

0 comments on commit d47a809

Please sign in to comment.