Skip to content

Commit

Permalink
feat: add support for host commands (e.g. action:pause) and use suspe…
Browse files Browse the repository at this point in the history
…nd/resume instead of !/~ (#515)
  • Loading branch information
cheton committed Oct 24, 2021
1 parent 46cc507 commit cc84323
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 6 deletions.
38 changes: 32 additions & 6 deletions src/server/controllers/Smoothie/SmoothieController.js
Expand Up @@ -455,6 +455,32 @@ class SmoothieController {
this.emit('serialport:read', res.raw);
});

// Action commands
//
// ```
// //action:<command>
// ```
this.runner.on('action', (res) => {
log.debug(`action command: action:${res.message}`);

if (res.message === 'pause') {
this.workflow.pause({ data: 'action:pause' });
return;
}

if (res.message === 'resume') {
this.workflow.resume({ data: 'action:resume' });
return;
}

if (res.message === 'cancel') {
this.workflow.stop();
return;
}

log.error(`Unknown action command: action:${res.message}`);
});

this.runner.on('parserstate', (res) => {
this.actionMask.queryParserState.state = false;
this.actionMask.queryParserState.reply = true;
Expand Down Expand Up @@ -1004,7 +1030,7 @@ class SmoothieController {

const activeState = _.get(this.state, 'status.activeState', '');
if (activeState === SMOOTHIE_ACTIVE_STATE_HOLD) {
this.write('~'); // resume
this.writeln('resume'); // resume
}
},
'pause': () => {
Expand All @@ -1016,7 +1042,7 @@ class SmoothieController {

this.workflow.pause();

this.write('!');
this.writeln('suspend');
},
'resume': () => {
log.warn(`Warning: The "${cmd}" command is deprecated and will be removed in a future release.`);
Expand All @@ -1025,7 +1051,7 @@ class SmoothieController {
'gcode:resume': () => {
this.event.trigger('gcode:resume');

this.write('~');
this.writeln('resume');

this.workflow.resume();
},
Expand All @@ -1037,7 +1063,7 @@ class SmoothieController {
if (this.workflow.state === WORKFLOW_STATE_RUNNING) {
return;
}
this.write('~');
this.writeln('resume');
this.feeder.unhold();
this.feeder.next();
},
Expand All @@ -1047,12 +1073,12 @@ class SmoothieController {
'feedhold': () => {
this.event.trigger('feedhold');

this.write('!');
this.writeln('suspend');
},
'cyclestart': () => {
this.event.trigger('cyclestart');

this.write('~');
this.writeln('resume');
},
'statusreport': () => {
this.write('?');
Expand Down
4 changes: 4 additions & 0 deletions src/server/controllers/Smoothie/SmoothieLineParser.js
Expand Up @@ -2,6 +2,7 @@ import _ from 'lodash';
import SmoothieLineParserResultStatus from './SmoothieLineParserResultStatus';
import SmoothieLineParserResultOk from './SmoothieLineParserResultOk';
import SmoothieLineParserResultError from './SmoothieLineParserResultError';
import SmoothieLineParserResultAction from './SmoothieLineParserResultAction';
import SmoothieLineParserResultAlarm from './SmoothieLineParserResultAlarm';
import SmoothieLineParserResultParserState from './SmoothieLineParserResultParserState';
import SmoothieLineParserResultParameters from './SmoothieLineParserResultParameters';
Expand All @@ -19,6 +20,9 @@ class SmoothieLineParser {
// error:x
SmoothieLineParserResultError,

// action:<command>
SmoothieLineParserResultAction,

// ALARM:
SmoothieLineParserResultAlarm,

Expand Down
22 changes: 22 additions & 0 deletions src/server/controllers/Smoothie/SmoothieLineParserResultAction.js
@@ -0,0 +1,22 @@
class SmoothieLineParserResultAction {
static parse(line) {
// handle action commands from the host
// see https://reprap.org/wiki/G-code#Replies_from_the_RepRap_machine_to_the_host_computer
// '// action:{pause,resume,cancel}\r\n'
const r = line.match(/^\/\/ action:(.+)$/);
if (!r) {
return null;
}

const payload = {
message: r[1]
};

return {
type: SmoothieLineParserResultAction,
payload: payload
};
}
}

export default SmoothieLineParserResultAction;
5 changes: 5 additions & 0 deletions src/server/controllers/Smoothie/SmoothieRunner.js
Expand Up @@ -4,6 +4,7 @@ import SmoothieLineParser from './SmoothieLineParser';
import SmoothieLineParserResultStatus from './SmoothieLineParserResultStatus';
import SmoothieLineParserResultOk from './SmoothieLineParserResultOk';
import SmoothieLineParserResultError from './SmoothieLineParserResultError';
import SmoothieLineParserResultAction from './SmoothieLineParserResultAction';
import SmoothieLineParserResultAlarm from './SmoothieLineParserResultAlarm';
import SmoothieLineParserResultParserState from './SmoothieLineParserResultParserState';
import SmoothieLineParserResultParameters from './SmoothieLineParserResultParameters';
Expand Down Expand Up @@ -99,6 +100,10 @@ class SmoothieRunner extends events.EventEmitter {
this.emit('error', payload);
return;
}
if (type === SmoothieLineParserResultAction) {
this.emit('action', payload);
return;
}
if (type === SmoothieLineParserResultAlarm) {
this.emit('alarm', payload);
return;
Expand Down

0 comments on commit cc84323

Please sign in to comment.