Skip to content

Commit

Permalink
#22 Probe settings persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
cheton committed Jan 4, 2016
2 parents d16e732 + aeead1f commit ae4ab4c
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 58 deletions.
2 changes: 1 addition & 1 deletion bower.json
@@ -1,6 +1,6 @@
{
"name": "cncjs",
"version": "0.14.2",
"version": "0.14.3",
"description": "CNC.js is a web-based CNC milling controller for the Arduino running GRBL",
"dependencies": {
"html5shiv": "~3.7.3",
Expand Down
24 changes: 12 additions & 12 deletions dist/assets/app.js

Large diffs are not rendered by default.

20 changes: 9 additions & 11 deletions dist/assets/app.js.map

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "cncjs",
"version": "0.14.2",
"version": "0.14.3",
"description": "CNC.js is a web-based CNC milling controller for the Arduino running GRBL",
"homepage": "https://github.com/cheton/cnc.js",
"author": "Cheton Wu <cheton@gmail.com>",
Expand Down Expand Up @@ -70,7 +70,6 @@
"react": "^0.14.3",
"react-addons-update": "^0.14.3",
"react-bootstrap": "^0.28.1",
"react-cookie": "^0.4.3",
"react-datagrid": "^1.2.13",
"react-dom": "^0.14.3",
"react-dropzone": "^3.3.0",
Expand Down
4 changes: 2 additions & 2 deletions web/components/widgets/axes/ToolbarButton.jsx
Expand Up @@ -17,9 +17,9 @@ class ToolbarButton extends React.Component {

toggleDisplayUnit() {
if (this.props.unit === METRIC_UNIT) {
serialport.writeln('G20'); // G20 specifies Imperial (inch) unit
serialport.writeln('G20'); // G20 specifies Imperial unit
} else {
serialport.writeln('G21'); // G21 specifies Metric (mm) unit
serialport.writeln('G21'); // G21 specifies Metric unit
}
}
handleSelect(target, eventKey) {
Expand Down
4 changes: 2 additions & 2 deletions web/components/widgets/axes/constants.js
@@ -1,7 +1,7 @@
export const IMPERIAL_UNIT = 'inch';
export const IMPERIAL_UNIT = 'in';
export const METRIC_UNIT = 'mm';

// mm (or inch)
// mm (or in)
export const DISTANCE_MIN = 0;
export const DISTANCE_MAX = 10000;
export const DISTANCE_STEP = 0.1;
Expand Down
7 changes: 4 additions & 3 deletions web/components/widgets/connection/Connection.jsx
@@ -1,12 +1,12 @@
import _ from 'lodash';
import cookie from 'react-cookie';
import pubsub from 'pubsub-js';
import React from 'react';
import Select from 'react-select';
import Alert from './Alert';
import i18n from '../../../lib/i18n';
import log from '../../../lib/log';
import socket from '../../../lib/socket';
import store from '../../../store';

class Connection extends React.Component {
state = {
Expand Down Expand Up @@ -56,7 +56,8 @@ class Connection extends React.Component {

this.clearAlert();

let port = cookie.load('port');
let port = store.getState('widgets.connection.port') || '';

if (_.includes(_.pluck(ports, 'port'), port)) {
this.setState({
port: port,
Expand All @@ -81,7 +82,7 @@ class Connection extends React.Component {
pubsub.publish('port', port);

// save the port
cookie.save('port', port);
store.setState('widgets.connection.port', port);

this.setState({
connecting: false,
Expand Down
2 changes: 1 addition & 1 deletion web/components/widgets/gcode/constants.js
@@ -1,4 +1,4 @@
export const IMPERIAL_UNIT = 'inch';
export const IMPERIAL_UNIT = 'in';
export const METRIC_UNIT = 'mm';
export const GCODE_STATUS = {
ERROR: -1,
Expand Down
53 changes: 31 additions & 22 deletions web/components/widgets/probe/Probe.jsx
Expand Up @@ -6,6 +6,7 @@ import i18n from '../../../lib/i18n';
import socket from '../../../lib/socket';
import serialport from '../../../lib/serialport';
import ToolbarButton from './ToolbarButton';
import store from '../../../store';
import {
IMPERIAL_UNIT,
METRIC_UNIT,
Expand All @@ -17,11 +18,11 @@ class Probe extends React.Component {
port: '',
unit: METRIC_UNIT,
activeState: ACTIVE_STATE_IDLE,
probeCommand: 'G38.2',
probeDepth: 10,
probeFeedrate: 20,
tlo: 10,
retractionDistance: 2
probeCommand: store.getState('widgets.probe.probeCommand', 'G38.2'),
probeDepth: store.getState('widgets.probe.probeDepth.mm', 10),
probeFeedrate: store.getState('widgets.probe.probeFeedrate.mm', 20),
tlo: store.getState('widgets.probe.tlo.mm', 10),
retractionDistance: store.getState('widgets.probe.retractionDistance.mm', 2)
}
socketEventListener = {
'grbl:current-status': ::this.socketOnGrblCurrentStatus,
Expand All @@ -39,6 +40,22 @@ class Probe extends React.Component {
shouldComponentUpdate(nextProps, nextState) {
return ! _.isEqual(nextState, this.state);
}
componentDidUpdate() {
store.setState('widgets.probe.probeCommand', this.state.probeCommand);

if (this.state.unit === METRIC_UNIT) {
store.setState('widgets.probe.probeDepth.mm', this.state.probeDepth);
store.setState('widgets.probe.probeFeedrate.mm', this.state.probeFeedrate);
store.setState('widgets.probe.tlo.mm', this.state.tlo);
store.setState('widgets.probe.retractionDistance.mm', this.state.retractionDistance);
}
if (this.state.unit === IMPERIAL_UNIT) {
store.setState('widgets.probe.probeDepth.in', this.state.probeDepth);
store.setState('widgets.probe.probeFeedrate.in', this.state.probeFeedrate);
store.setState('widgets.probe.tlo.in', this.state.tlo);
store.setState('widgets.probe.retractionDistance.in', this.state.retractionDistance);
}
}
subscribe() {
this.pubsubTokens = [];

Expand Down Expand Up @@ -102,18 +119,18 @@ class Probe extends React.Component {

if (unit === METRIC_UNIT) {
return {
probeDepth: 10,
probeFeedrate: 20,
tlo: 10,
retractionDistance: 2
probeDepth: store.getState('widgets.probe.probeDepth.mm', 10),
probeFeedrate: store.getState('widgets.probe.probeFeedrate.mm', 20),
tlo: store.getState('widgets.probe.tlo.mm', 10),
retractionDistance: store.getState('widgets.probe.retractionDistance.mm', 2)
};
}
if (unit === IMPERIAL_UNIT) {
return {
probeDepth: 0.5,
probeFeedrate: 1,
tlo: 0.5,
retractionDistance: 0.1
probeDepth: store.getState('widgets.probe.probeDepth.in', 0.5),
probeFeedrate: store.getState('widgets.probe.probeFeedrate.in', 1),
tlo: store.getState('widgets.probe.tlo.in', 0.5),
retractionDistance: store.getState('widgets.probe.retractionDistance.in', 0.1)
};
}
}
Expand Down Expand Up @@ -374,15 +391,7 @@ class Probe extends React.Component {
{i18n._('Run Z-probe')}
</button>
</div>
<div className="btn-group" role="group">
<button
type="button"
className="btn btn-sm btn-default"
onClick={::this.restoreDefaults}
>
{i18n._('Restore Defaults')}
</button>
</div>
<div className="btn-group" role="group"></div>
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion web/components/widgets/probe/constants.js
@@ -1,4 +1,4 @@
export const IMPERIAL_UNIT = 'inch';
export const IMPERIAL_UNIT = 'in';
export const METRIC_UNIT = 'mm';

// Grbl Active State
Expand Down
1 change: 0 additions & 1 deletion web/i18n/en/resource.json
Expand Up @@ -71,7 +71,6 @@
"88ab0f7ba1277080be5f90a24eb713dcf07c81c0": "Cycle Start",
"89b86ab0e66f527166d98df92ddbcf5416ed58f6": "Language",
"8cfa322f6132ea1c4fa9a502e5b945d428b97dd4": "View Startup Blocks ($N)",
"8e80e07d804d975411800c262997a8cd6582b5f7": "Restore Defaults",
"8f82c5bcb36e47fc4b23303a57b9a67d0caa0edb": "Spindle state:",
"90d48549bcd13abbfaf99474aeff3124549a3273": "Choose a port",
"9255527b8ff8560d22e16e7542a5d02894cd1006": "Decrease step by 0.1 unit",
Expand Down
31 changes: 31 additions & 0 deletions web/store/index.js
@@ -0,0 +1,31 @@
import _ from 'lodash';

let state;

try {
state = _.extend({}, JSON.parse(localStorage.getItem('state') || {}));
}
catch(err) {
state = {};
}

const setState = (key, value) => {
let result = _.set(state, key, value);
localStorage.setItem('state', JSON.stringify(state));
return result;
};

const getState = (key, defaultValue) => {
let value = _.get(state, key);
return (typeof value !== 'undefined') ? value : defaultValue;
};

const clearState = () => {
localStorage.setItem('state', JSON.stringify({}));
};

export default {
setState: setState,
getState: getState,
clearState: clearState
};

0 comments on commit ae4ab4c

Please sign in to comment.