-
Notifications
You must be signed in to change notification settings - Fork 2
/
node-xhc.js
594 lines (512 loc) · 17.1 KB
/
node-xhc.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
var HID = require("node-hid");
var WebSocket = require('ws');
var argv = require('optimist').argv;
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var Xpen = function () {
var self = this;
EventEmitter.call(this);
isPaused = false;
isJogging = false;
stepDistance = 0;
distanceTable = [0.1, 0.01, 0.001];
jogMode = "incremental"; //vs "continuous"
velocityMin = 40; //mm/min
calculatedVelocity = velocityMin;
var dateObj = new Date();
//==========================================================
//Macros - These commands are sent when the macro is called.
//==========================================================
this.macro1 = "Macro 1";
this.macro2 = "Macro 2";
this.macro3 = "Macro 3";
this.macro4 = "";
this.macro6 = "Macro 6";
this.macro7 = "Macro 7";
//CONSTANTS
this.vendorId = 4302;
this.productId = 60272;
self.dialSetting = "";
//=====================
// Defines
//=====================
this.INCHES = 0;
this.MM = 1;
this.units = this.MM;
//=====================
//CMD Byte Packet Names
//=====================
CMD_START_BYTE = 0;
CMD_BYTE1 = 1;
CMD_PADDING = 2;
CMD_DIAL_BYTE = 3;
CMD_VELOCITY = 4;
CMD_BYTE2 = 5;
CMDS = [
{
name: "keyup",
value: [0x00, 0x9a]
},
{
name: "reset",
value: [0x17, 0x8d]
},
{
name: "sleep",
value: [0x00, 0x00]
},
{
name: "stop",
value: [0x16, 0x8c]
},
{
name: "arrow1",
value: [0x01, 0x9b]
}, //Set Zero
{
name: "arrow2",
value: [0x09, 0x93]
}, //Go to zero
{
name: "rewind",
value: [0x03, 0x99]
},
{
name: "spindle",
value: [0x0c, 0x96]
},
{
name: "macro_1",
value: [0x0a, 0x90]
},
{
name: "macro_2",
value: [0x0b, 0x91]
},
{
name: "macro_3",
value: [0x05, 0x9f]
},
{
name: "macro_6",
value: [0x0f, 0x95]
},
{
name: "macro_7",
value: [0x10, 0x8a]
},
{
name: "half",
value: [0x06, 0x9c]
},
{
name: "zero",
value: [0x07, 0x9d]
},
{
name: "pause_resume",
value: [0x02, 0x98]
},
{
name: "probez",
value: [0x04, 0x9e]
},
{
name: "safez",
value: [0x08, 0x92]
},
{
name: "step++",
value: [0x0d, 0x97]
},
{
name: "model",
value: [0x0e, 0x94]
},
{
name: "half",
value: [0x03, 0x99]
}
];
//=====================
//DIAL Modes
//=====================
OFF_DIAL = 0x00;
X_AXIS = 0x11;
Y_AXIS = 0x12;
Z_AXIS = 0x13;
A_AXIS = 0x18;
SPINDLE_DIAL = 0x14;
FEED_DIAL = 0x15;
//=====================
//INIT COde
//=====================
this._findAndConnectPendant();
};
util.inherits(Xpen, EventEmitter);
Xpen.prototype._findAndConnectPendant = function () {
var self = this;
try {
self.dev = new HID.HID(this.vendorId, this.productId);
self.dev.on("data", function (data) {
//console.log("DATA: ", data);
//_dial = _getDialSetting(data[CMD_DIAL_BYTE]);
_packet = self.parseDataPacket(data);
if (_packet != null) {
//This emits the type of packet and the packet data to whatever code is
//listening to the xhc module
self.emit(_packet.type, _packet);
}
});
} catch (err) {
console.error("Unable to locate pendant... Check USB pendant is plugged in.");
}
};
var parseCommand = function (data) {
for (i = 0; i < CMDS.length; i++) {
if (data[CMD_BYTE1] == CMDS[i].value[0] && data[CMD_BYTE2] == CMDS[i].value[1]) {
//The Key Up command looks identical to a "jog" dial movement vs.
//The 2nd to last byte if its a not 0 value it makes this key_up command a jog.
if (data[CMD_VELOCITY] != 0x00) {
return ({
name: "jog",
value: [0x00, data[CMD_VELOCITY], 0x9a]
})
}
return (CMDS[i]);
} //eek!
}
return null;
};
var _getDialSetting = function (dialByte) {
// console.warn(X_AXIS);
// console.warn(dialByte);
switch (dialByte) {
case (OFF_DIAL):
return ("DIAL OFF");
case (X_AXIS):
return ("X");
case (Y_AXIS):
return ("Y");
case (Z_AXIS):
return ("Z");
case (A_AXIS):
return ("A");
case (SPINDLE_DIAL):
return ("SPINDLE");
case (FEED_DIAL):
return ("FEED");
default:
return ("Unknown Dial State");
}
};
//=================================================================================================
//
// ---> JOGGING CODE HERE <---
//
//=================================================================================================
var getJogDirectionVelocity = function (data) {
_velocity = data[CMD_VELOCITY];
//We need to figure out if this is a negative move or a positive move
if (_velocity > 0xaa) {
sign = "-";
_velocity = 255 - _velocity; // When rotating counter clockwise the velocity
//Comes in as 0xfe for 1 which we will subtract from 0xff to get a sane number
} else {
sign = ""
}
return ([sign, _velocity]);
};
var _resetVelocities = function () {
calculatedVelocity = velocityMin;
};
//Continuous is the machine will continue to jog as long as there are event dial events coming in.
var calculateContinuousVelocity = function (vel) {
//build our jog command
tmpCalc = (vel * 10) * velocityMin;
if (tmpCalc > calculatedVelocity) {
calculatedVelocity = tmpCalc; //If we are moving faster than previously we will increase our speed.
}
return (calculatedVelocity);
};
Xpen.prototype.getMacroByNumber = function (macroNumber) {
var self = this;
switch (macroNumber) {
case(1):
return (this.macro1);
case(2):
return (this.macro2);
case(3):
return (this.macro3);
case(6):
return (this.macro6);
case(7):
return (this.macro7);
}
};
//var setMacroByNumber = function (macroNumber, funcBody) {
// switch (macroNumber) {
// case(1):
// this.macro1 = funcBody;
// case(2):
// this.macro2 = funcBody;
// case(3):
// this.macro3 = funcBody;
// case(4):
// this.macro4 = funcBody;
// case(6):
// this.macro5 = funcBody;
// case(7):
// this.macro6 = funcBody;
// }
//}
Xpen.prototype.getJogMode = function(){
return(jogMode);
};
Xpen.prototype.parseDataPacket = function (data) {
self = this;
if (data[CMD_START_BYTE] == 0x04) { //0x04 is a constant for this device as the first byte
//We are going to see if we noticed the dial indicator changed
_tmpDial = _getDialSetting(data[CMD_DIAL_BYTE]);
if(self.dialSetting != _tmpDial){
//We are going to emit this to the "change" listener.
self.dialSetting = _tmpDial;
this.emit("change",{"cmd":"dial_indicator","value":_tmpDial})
}
_tmpCmd = parseCommand(data);
if (_tmpCmd && self.dialSetting != "DIAL OFF") {
switch (_tmpCmd.name) {
case ("keyup"):
//keyup is a weird name for this event in regards to jogging but its the same
//as lifting a key after press. So this why we must keep state on jogging.
//If we were jogging we are in incremental mode
//We need to exit this mode now that we are done jogging.
if (isJogging) {
isJogging = false;
//What this does is if you are in continuous mode you will move until
//you stop twisting the dial. This will then issue a feedhold flush command.
_resetVelocities();
//We need to reset the velocity vaules to the min again.
if (jogMode == "continuous") {
return ({
'type': 'jog',
'dialSetting': self.dialSetting,
'cmd': "jog_continuous_finish"
});
break;
} else {
return ({
'type': 'jog',
'dialSetting': self.dialSetting,
'cmd': "jog_incremental_finish"
});
break;
}
}else{
break;
}
case ("jog"):
isJogging = true;
_dirvel = getJogDirectionVelocity(data);
dir = _dirvel[0];
vel = _dirvel[1];
if (jogMode == "incremental") { //Incremental Jog Sent
return ({
'type': 'jog',
'dialSetting': self.dialSetting,
'cmd': "jog_incremental",
'dir': dir,
'value': vel
});
break;
} else { //Continuous Mode Jog
return ({
'type': 'jog',
'dialSetting': self.dialSetting,
'cmd': "jog_continuous",
'dir': _dirvel[0],
'value': calculateContinuousVelocity(vel)
});
break;
}
case ("pause_resume"):
if (isPaused) {
_name = "resume";
} else {
_name = "feedhold";
}
return ({
'type': 'key_press',
'dialSetting': self.dialSetting,
'cmd': _name //feedhold/resume
});
break;
case ("zero"):
return ({
'type': 'key_press',
'dialSetting': self.dialSetting,
'cmd': _tmpCmd.name //zero
});
break;
case ("half"):
return ({
'type': 'key_press',
'dialSetting': self.dialSetting,
'cmd': _tmpCmd.name //zero
});
break;
case ("spindle"):
return ({
'type': 'key_press',
'dialSetting': self.dialSetting,
'cmd': _tmpCmd.name //zero
});
break;
case ("rewind"):
return ({
'type': 'key_press',
'dialSetting': self.dialSetting,
'cmd': _tmpCmd.name //zero
});
break;
case ("reset"):
return ({
'type': 'key_press',
'dialSetting': self.dialSetting,
'cmd': _tmpCmd.name //zero
});
break;
case ("stop"):
return ({
'type': 'key_press',
'dialSetting': self.dialSetting,
'cmd': _tmpCmd.name //zero
});
break;
case ("arrow1"):
return ({
'type': 'key_press',
'dialSetting': self.dialSetting,
'cmd': _tmpCmd.name //zero
});
break;
case ("macro_1"):
return ({
'type': 'key_press',
'dialSetting': self.dialSetting,
'cmd': _tmpCmd.name,
'value': this.getMacroByNumber(1)
});
break;
case ("macro_2"):
return ({
'type': 'key_press',
'dialSetting': self.dialSetting,
'cmd': _tmpCmd.name,
'value': this.getMacroByNumber(2)
});
break;
case ("macro_3"):
return ({
'type': 'key_press',
'dialSetting': self.dialSetting,
'cmd': _tmpCmd.name,
'value': this.getMacroByNumber(3)
});
break;
case ("macro_6"):
return ({
'type': 'key_press',
'dialSetting': self.dialSetting,
'cmd': _tmpCmd.name,
'value': this.getMacroByNumber(6)
});
break;
case ("macro_7"):
return ({
'type': 'key_press',
'dialSetting': self.dialSetting,
'cmd': _tmpCmd.name,
'value': this.getMacroByNumber(7)
});
break;
case ("safez"):
return ({
'type': 'key_press',
'dialSetting': self.dialSetting,
'cmd': _tmpCmd.name //safe_z
});
break;
case ("probez"):
return ({
'type': 'key_press',
'dialSetting': self.dialSetting,
'cmd': _tmpCmd.name //safe_z
});
break;
case ("arrow2"): //Arrow2 is, at least for now go to zero on all axis
return ({
'type': 'key_press',
'dialSetting': self.dialSetting,
'cmd': _tmpCmd.name //arrow2
});
break;
case ("step++"):
//console.log("Changing Step Rate for Incremental Mode");
setStepDistance();
//console.log("\t " + getStepDistance());
return ({
'type': 'change',
'dialSetting': self.dialSetting,
'cmd':"step_distance",
'value': this.getStepDistance() //incremental value
});
break;
case ("model"):
//console.log("-----Changing Jog Modes----");
if (jogMode == "incremental") {
jogMode = "continuous";
} else {
jogMode = "incremental";
}
//console.log("MODE: " + jogMode);
break;
default:
console.log("Un-Caught Case: " + _tmpCmd.name, _tmpCmd.value);
break;
}
} else {
//console.log("DIAL: " + dialSetting + " Command Code Unknown: " + data.toString('hex'));
}
}
};
Xpen.prototype.getStepDistance = function () {
return distanceTable[stepDistance];
};
var setStepDistance = function () {
//We only have 3 values in the array for distanceTable
//So if its 2 lets reset it back to 0
if (stepDistance == 2) {
stepDistance = 0;
} else {
stepDistance = stepDistance + 1;
}
};
Xpen.prototype.setUnits = function (units) {
var self = this;
if (units == self.MM) {
self.units = self.MM;
} else {
self.units = self.INCHES;
}
console.info("Changing Units to: " + this.units);
};
Xpen.prototype.transmit = function(buffer){
var self = this;
console.info("Writing: " + buffer.toString('hex'));
self.dev.write(buffer);
};
Xpen.prototype.test = function (b) {
isPaused = b; //true or false
};
module.exports = Xpen;