Skip to content

Commit

Permalink
Human readable messages for alarms, errors, and settings in Grbl v1.1 (
Browse files Browse the repository at this point in the history
  • Loading branch information
cheton committed Jan 13, 2017
1 parent 8387ea2 commit 4efe073
Show file tree
Hide file tree
Showing 3 changed files with 232 additions and 217 deletions.
14 changes: 7 additions & 7 deletions src/app/controllers/Grbl/Grbl.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,9 +532,9 @@ class GrblLineParserResultSettings {
}

const payload = {
name: r[1],
setting: r[1],
value: r[2],
message: _.trim(r[3], '()')
description: _.trim(r[3], '()')
};

return {
Expand Down Expand Up @@ -698,13 +698,13 @@ class Grbl extends events.EventEmitter {
return;
}
if (type === GrblLineParserResultSettings) {
const { name, value } = payload;
const { settings } = this.state;
settings[name] = value;

const { setting, value } = payload;
const nextState = {
...this.state,
settings: settings
settings: {
...this.state.settings,
[setting]: value
}
};
if (!_.isEqual(this.state.settings, nextState.settings)) {
this.state = nextState; // enforce state change
Expand Down
59 changes: 37 additions & 22 deletions src/app/controllers/Grbl/GrblController.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import {
GRBL,
GRBL_ACTIVE_STATE_RUN,
GRBL_REALTIME_COMMANDS,
GRBL_ERRORS
GRBL_ALARMS,
GRBL_ERRORS,
GRBL_SETTINGS
} from './constants';
import {
SMOOTHIE,
Expand Down Expand Up @@ -193,45 +195,50 @@ class GrblController {

this.grbl.on('error', (res) => {
const code = Number(res.message) || undefined;
const err = _.find(GRBL_ERRORS, { code: code }) || {};
const msg = err ? err.msg : res.message;
const error = _.find(GRBL_ERRORS, { code: code });

// Sender
if (this.workflowState === WORKFLOW_STATE_RUNNING) {
const { lines, received } = this.sender.state;
const line = lines[received] || '';

this.emitAll('serialport:read', `> ${line}`);
this.emitAll('serialport:read', JSON.stringify({
err: {
code: code,
msg: msg,
line: received + 1,
data: line.trim()
}
}));
this.emitAll('serialport:read', `> ${line.trim()} (line=${received + 1})`);
if (error) {
// Grbl v1.1
this.emitAll('serialport:read', `error:${code} (${error.description})`);
} else {
// Grbl v0.9
this.emitAll('serialport:read', res.raw);
}

this.sender.ack();
this.sender.next();
return;
}

this.emitAll('serialport:read', res.raw);
if (code) {
this.emitAll('serialport:read', JSON.stringify({
err: {
code: code,
msg: msg
}
}));
if (error) {
// Grbl v1.1
this.emitAll('serialport:read', `error:${code} (${error.description})`);
} else {
// Grbl v0.9
this.emitAll('serialport:read', res.raw);
}

// Feeder
this.feeder.next();
});

this.grbl.on('alarm', (res) => {
this.emitAll('serialport:read', res.raw);
const code = Number(res.message) || undefined;
const alarm = _.find(GRBL_ALARMS, { code: code });

if (alarm) {
// Grbl v1.1
this.emitAll('serialport:read', `ALARM:${code} (${alarm.description})`);
} else {
// Grbl v0.9
this.emitAll('serialport:read', res.raw);
}
});

this.grbl.on('parserstate', (res) => {
Expand All @@ -254,7 +261,15 @@ class GrblController {
});

this.grbl.on('settings', (res) => {
this.emitAll('serialport:read', res.raw);
const setting = _.find(GRBL_SETTINGS, { setting: res.setting });

if (!res.description && setting) {
// Grbl v1.1
this.emitAll('serialport:read', `${res.setting}=${res.value} (${setting.description}, ${setting.units})`);
} else {
// Grbl v0.9
this.emitAll('serialport:read', res.raw);
}
});

this.grbl.on('startup', (res) => {
Expand Down
Loading

0 comments on commit 4efe073

Please sign in to comment.