-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwidget.js
306 lines (261 loc) · 13 KB
/
widget.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
requirejs.config({
paths: {
Three: '//threejs.org/build/three.min'
}
});
// Test this element. This code is auto-removed by the chilipeppr.load()
cprequire_test(["inline:com-chilipeppr-widget-touchplate"], function (touchplate) {
console.log("test running of " + touchplate.id);
touchplate.init();
$('body').css('padding', '20px');
} /*end_test*/ );
cpdefine("inline:com-chilipeppr-widget-touchplate", ["chilipeppr_ready", 'Three'], function () {
return {
id: "com-chilipeppr-widget-touchplate",
url: "(auto fill by runme.js)", // The final URL of the working widget as a single HTML file with CSS and Javascript inlined. You can let runme.js auto fill this if you are using Cloud9.
fiddleurl: "(auto fill by runme.js)", // The edit URL. This can be auto-filled by runme.js in Cloud9 if you'd like, or just define it on your own to help people know where they can edit/fork your widget
githuburl: "(auto fill by runme.js)", // The backing github repo
testurl: "(auto fill by runme.js)", // The standalone working widget so can view it working by itself
name: "Widget / GRBL Touch Plate",
desc: "This widget helps you use a touch plate to create your Z zero offset.",
publish: {
},
subscribe: {
},
foreignPublish: {
},
foreignSubscribe: {
},
isInitted: false, // keep track of our one-time init
offset: 0,
width: 400,
height: 500,
init: function () {
this.setupUiFromLocalStorage();
this.btnSetup();
this.forkSetup();
this.isInitted = true;
console.log(this.name + " done loading.");
},
gcodeCtr: 0,
isRunning: false,
onRun: function(evt) {
// when user clicks the run button
console.log("user clicked run button. evt:", evt);
if (this.isRunning) {
// we need to stop
// swap button to stop
$('#com-chilipeppr-widget-touchplate .btn-touchplaterun').removeClass("btn-danger").text("Run");
this.isRunning = false;
} else {
// we need to run the whole darn process
this.isRunning = true;
// swap button to stop
$('#com-chilipeppr-widget-touchplate .btn-touchplaterun').addClass("btn-danger").text("Stop");
// get user feedrate
var fr = $('#com-chilipeppr-widget-touchplate .frprobe').val();
//get user depth
var depth = $('#com-chilipeppr-widget-touchplate .depthprobe').val();
//set plate offset for use in probe response
this.offset = $('#com-chilipeppr-widget-touchplate .heightplate').val();
// now start watching for cnc widget response
chilipeppr.subscribe("/com-chilipeppr-interface-cnccontroller/proberesponse", this, this.probeResponse,1);
// send the probe command to start the movement
var id = "tp" + this.gcodeCtr++;
var gcode = "G21 G90 (Use mm and abs coords)\n";
chilipeppr.publish("/com-chilipeppr-widget-serialport/jsonSend", {Id: id, D: gcode});
//Clear any previous tool offsets
id = "tp" + this.gcodeCtr++;
gcode = "G49\n";
chilipeppr.publish("/com-chilipeppr-widget-serialport/jsonSend", {Id: id, D: gcode});
//Zero out z axis in case user has not, or incase removing preious tool offset has changed Z zero position.
id = "tp" + this.gcodeCtr++;
gcode = "G92 Z0\n";
chilipeppr.publish("/com-chilipeppr-widget-serialport/jsonSend", {Id: id, D: gcode});
//send probe command
id = "tp" + this.gcodeCtr++;
gcode = "G38.2 Z"+ depth + " F" + fr + "\n";
chilipeppr.publish("/com-chilipeppr-widget-serialport/jsonSend", {Id: id, D: gcode});
}
},
probeResponse: function(position) {
if(position === "alarm" || position.status === "0"){
console.log("Touchplate: PROBE Failure");
// swap button to stop
$('#com-chilipeppr-widget-touchplate .btn-touchplaterun').addClass("btn-danger").text("Alarm!!");
return false;
}
console.log("Touchplate: Probe responded with Z position: " + position.z);
console.log("Touchplate: G43.1 tool offset to: " + (position.z - this.offset).toString());
var id = "tp" + this.gcodeCtr++;
var gcode = "G43.1 Z" + (position.z - this.offset).toString() + "\n";
chilipeppr.publish("/com-chilipeppr-widget-serialport/jsonSend", {Id: id, D: gcode});
//now back off 2 mm to allow touch plate removal
id = "tp" + this.gcodeCtr++;
gcode = "G91 G0 Z2\n"
chilipeppr.publish("/com-chilipeppr-widget-serialport/jsonSend", {Id: id, D: gcode});
id = "tp" + this.gcodeCtr++;
gcode = "G90\n"
chilipeppr.publish("/com-chilipeppr-widget-serialport/jsonSend", {Id: id, D: gcode});
// switch stop button back to "run"
$('#com-chilipeppr-widget-touchplate .btn-touchplaterun').removeClass("btn-danger").text("Run");
//unsubscribe now that we have our probe response
chilipeppr.unsubscribe("/com-chilipeppr-interface-cnccontroller/proberesponse", this.probeResponse);
this.offset = 0; //set offset back to zero for next run (should user change, we want to capture that
return false;
},
onresize: function() {
this.width = $('#com-chilipeppr-widget-touchplate .panel-body').width();
this.setSize(this.width, this.height);
},
dispatch: function ( array, event ) {
for ( var i = 0, l = array.length; i < l; i ++ ) {
array[ i ]( event );
}
},
request: null,
isHidden: false,
unactivateWidget: function() {
if (!this.isHidden) {
// unsubscribe from everything
console.log("unactivateWidget. unsubscribing.");
//chilipeppr.unsubscribe("/com-chilipeppr-interface-cnccontroller/axes", this, this.onAxes);
//chilipeppr.unsubscribe("/com-chilipeppr-widget-serialport/ws/recv", this, this.onWsRecvLaser);
this.isHidden = true;
}
},
activateWidget: function() {
if (!this.isInitted) {
this.init();
}
if (this.isHidden) {
// resubscribe
console.log("activateWidget. resubscribing.");
//chilipeppr.subscribe("/com-chilipeppr-interface-cnccontroller/axes", this, this.onAxes);
//chilipeppr.subscribe("/com-chilipeppr-widget-serialport/ws/recv", this, this.onWsRecvLaser);
this.isHidden = false;
}
},
options: null,
setupUiFromLocalStorage: function() {
// read vals from cookies
var options = localStorage.getItem('com-chilipeppr-widget-touchplate-options');
if (options) {
options = $.parseJSON(options);
console.log("just evaled options: ", options);
} else {
options = {
showBody: true,
frprobe: 50,
heightplate: 3.75,
depthprobe: -10
};
}
// check z
//if (!('z' in options)) options.z = 1.0;
this.options = options;
console.log("options:", options);
// show/hide body
if (options.showBody) {
this.showBody();
} else {
this.hideBody();
}
// setup textboxes
$('#com-chilipeppr-widget-touchplate .frprobe').val(this.options.frprobe);
$('#com-chilipeppr-widget-touchplate .heightplate').val(this.options.heightplate);
$('#com-chilipeppr-widget-touchplate .depthprobe').val(this.options.depthprobe);
// attach onchange
$('#com-chilipeppr-widget-touchplate input').change(this.saveOptionsLocalStorage.bind(this));
},
saveOptionsLocalStorage: function() {
//var options = {
// showBody: this.options.showBody
//};
// grab text vals
this.options.frprobe = $('#com-chilipeppr-widget-touchplate .frprobe').val();
this.options.heightplate = $('#com-chilipeppr-widget-touchplate .heightplate').val();
this.options.depthprobe = $('#com-chilipeppr-widget-touchplate .depthprobe').val();
var options = this.options;
var optionsStr = JSON.stringify(options);
console.log("saving options:", options, "json.stringify:", optionsStr);
// store cookie
localStorage.setItem('com-chilipeppr-widget-touchplate-options', optionsStr);
},
showBody: function(evt) {
$('#com-chilipeppr-widget-touchplate .panel-body').removeClass('hidden');
//$('#com-chilipeppr-widget-touchplate .panel-footer').removeClass('hidden');
$('#com-chilipeppr-widget-touchplate .hidebody span').addClass('glyphicon-chevron-up');
$('#com-chilipeppr-widget-touchplate .hidebody span').removeClass('glyphicon-chevron-down');
if ((evt !== null)) {
this.options.showBody = true;
this.saveOptionsLocalStorage();
}
},
hideBody: function(evt) {
$('#com-chilipeppr-widget-touchplate .panel-body').addClass('hidden');
//$('#com-chilipeppr-widget-touchplate .panel-footer').addClass('hidden');
$('#com-chilipeppr-widget-touchplate .hidebody span').removeClass('glyphicon-chevron-up');
$('#com-chilipeppr-widget-touchplate .hidebody span').addClass('glyphicon-chevron-down');
if ((evt !== null)) {
this.options.showBody = false;
this.saveOptionsLocalStorage();
}
},
btnSetup: function() {
// chevron hide body
var that = this;
$('#com-chilipeppr-widget-touchplate .hidebody').click(function(evt) {
console.log("hide/unhide body");
if ($('#com-chilipeppr-widget-touchplate .panel-body').hasClass('hidden')) {
// it's hidden, unhide
that.showBody(evt);
} else {
// hide
that.hideBody(evt);
}
});
$('#com-chilipeppr-widget-touchplate .btn-toolbar .btn').popover({
delay: 500,
animation: true,
placement: "auto",
trigger: "hover",
container: 'body'
});
// setup run button
$('#com-chilipeppr-widget-touchplate .btn-touchplaterun').click(this.onRun.bind(this));
},
statusEl: null, // cache the status element in DOM
status: function(txt) {
console.log("status. txt:", txt);
if (this.statusEl === null) this.statusEl = $('#com-chilipeppr-widget-touchplate-status');
var len = this.statusEl.val().length;
if (len > 30000) {
console.log("truncating status area text");
this.statusEl.val(this.statusEl.val().substring(len-5000));
}
this.statusEl.val(this.statusEl.val() + txt + "\n");
this.statusEl.scrollTop(
this.statusEl[0].scrollHeight - this.statusEl.height()
);
},
forkSetup: function() {
var topCssSelector = '#' + this.id;
$(topCssSelector + ' .panel-title').popover({
title: this.name,
content: this.desc,
html: true,
delay: 1000,
animation: true,
trigger: 'hover',
placement: 'auto'
});
var that = this;
chilipeppr.load("http://raw.githubusercontent.com/chilipeppr/widget-pubsubviewer/master/auto-generated-widget.html", function() {
require(['inline:com-chilipeppr-elem-pubsubviewer'], function(pubsubviewer) {
pubsubviewer.attachTo($(topCssSelector + ' .panel-heading .dropdown-menu'), that);
});
});
},
};
});