-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXHC-HB04.js
executable file
·624 lines (545 loc) · 17.7 KB
/
XHC-HB04.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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const io = require('socket.io-client');
const jwt = require('jsonwebtoken');
const get = require('lodash.get');
const {config, options} = require('./xhcrc');
const HID = require('node-hid');
// socket to CNCjs
var socket;
// Array used to transmit selected axis
const axischars = "XYZABC";
// initialize buttons to nothing pressed;
var prevButtons = [0, 0];
// Knobs
var feedknob;
var axisknob;
// Create USB transfer buffer
var buff = new Buffer.alloc(21);
// Set fixed headers that never get changed
buff[0] = 0xFE;
buff[1] = 0xFD;
buff[2] = 0x04;
// CNCjs information
var store = {
state: {
status: {
activeState: 'Undefined'
}
},
settings: {}
};
// which com port are we using
var myport;
// Initialize CNCjs client and connect to server
const generateAccessToken = function (payload, secret, expiration) {
const token = jwt.sign(payload, secret, {
expiresIn: expiration
});
return token;
};
// Get secret key from the config file and generate an access token
const getUserHome = function () {
return process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME'];
};
const cncrc = path.resolve(getUserHome(), '.cncrc');
try {
const config = JSON.parse(fs.readFileSync(cncrc, 'utf8'));
options.secret = config.secret;
} catch (err) {
console.error(err);
process.exit(1);
}
const token = generateAccessToken({ id: '', name: 'cncjs-pendant' }, options.secret, options.accessTokenLifetime);
const url = 'ws://' + options.socketAddress + ':' + options.socketPort + '?token=' + token;
socket = io.connect('ws://' + options.socketAddress + ':' + options.socketPort, {
'query': 'token=' + token
});
socket.on('connect', () => {
console.log('Connected to ' + url);
// Get list of ports upon connection
socket.emit('list', null);
});
socket.on('error', (err) => {
console.error('Connection error.');
if (socket) {
socket.destroy();
socket = null;
}
});
socket.on('close', () => {
console.log('Connection closed.');
});
// Ignore raw serialport:open for now
socket.on('serialport:open', function (options) {
options = options || {};
console.log('Connected to port "' + options.port + '" (Baud rate: ' + options.baudrate + ')');
});
// Quite on serial port error
socket.on('serialport:error', function (options) {
console.log('Serial port error');
process.exit(1);
});
// socket.on('serialport:read', function (data) {
// console.log("Serial port read");
// console.log((data || '').trim());
// });
socket.on('serialport:list', function (portlist) {
console.log("Serial port list");
// Check to see if a port is in use
for (const portitem in portlist) {
if (portlist[portitem].inuse) {
myport = portlist[portitem].port;
break;
}
}
// If no port in use is found, default to options.port
if (!myport) myport = options.port;
// baud and controller are required?
socket.emit('open', myport, {
baudrate: Number(options.baudrate),
controllerType: options.controllerType
});
});
// Sender
// socket.on('sender:status', function(data) {
// // console.log('sender:status');
// // console.log(data);
// store.sender.status = data;
// });
// socket.on('serialport:write', function (data) {
// console.log((data || '').trim());
// });
// Grbl
socket.on('Grbl:state', function (state) {
// console.log('Grbl:state');
// console.log(state);
// check to see if the state has changed
if (store.state.status.activeState != state.status.activeState) {
console.log('State changed from ' + store.state.status.activeState + ' to ' + state.status.activeState);
}
store.state = state;
xhc_set_display(state);
// console.log(state.status.wpos);
});
socket.on('Grbl:settings', function (settings) {
// console.log('Grbl:settings');
// console.log(settings.settings);
store.settings = settings;
});
// USB Output device
var dev_USB_OUT;
const devices = HID.devices(config.HID_VID, config.HID_PID);
if (devices.length === 0) {
console.error("Could not find HID device with VID=0x%s and PID=0x%s", config.HID_VID.toString(16), config.HID_PID.toString(16));
process.exit(1);
}
var dev_USB_IN;
if (devices.length > 1) {
// Windows finds multiple HID devices for single XHC-HB04. 1 is input device and other is output device
for (iLooper = 0; iLooper < devices.length; iLooper++) {
// This works for 1 windows setup. Not sure if it is portable
if (devices[iLooper].path.includes("col01")) {
dev_USB_IN = new HID.HID(devices[iLooper].path);
}
if (devices[iLooper].path.includes("col02")) {
dev_USB_OUT = new HID.HID(devices[iLooper].path);
}
}
} else {
// Single device found for both input and output. 1 call to new HID with duplicate reference
dev_USB_IN = new HID.HID(devices[0].path);
dev_USB_OUT = dev_USB_IN;
}
if (!dev_USB_IN) {
console.log('USB Pendant not found');
process.exit(1);
}
if (!dev_USB_OUT) {
console.log('USB Pendant not found');
process.exit(1);
}
dev_USB_IN.on('error', function (error) {
console.log("on error", error);
});
dev_USB_IN.on('end', function () {
console.log("on end");
});
console.log("found XHC-HB04 device");
// Setup callback for data in
dev_USB_IN.on('data', function (data) {
parseButtonData(data);
});
function xhc_encode_float(v, buff_offset) {
// Make integer part fraction into unsigned integer number
var unsigned_v = Math.abs(v);
// Separate into whole and fractional parts
var int_part = Math.trunc(unsigned_v);
// truncateDecimals(unsigned_v,0);
var frac_part = Math.trunc((unsigned_v - int_part) * 10000);
// Write to buffer
xhc_uint16_to_buffer(int_part, buff_offset);
xhc_uint16_to_buffer(frac_part, buff_offset + 2);
// Set negative bit if required
if (v < 0)
buff[buff_offset + 3] = buff[buff_offset + 3] | 0x80;
}
function xhc_uint16_to_buffer(v, offset) {
buff[offset + 1] = v >> 8;
buff[offset] = v & 0xff;
}
function xhc_set_display(state) {
// Format the display data into a buffer
var DispAxis;
if (!state) return;
if (config.WorkPos) {
// quite if wpos is undefined
// if (state.wpos) return;
DispAxis = state.status.wpos;
buff[3] |= 0x80;
} else {
// quite if mpos is undefined
// if (!store.controller.state.mpos) return;
DispAxis = state.status.mpos;
buff[3] &= !0x80;
}
// Set display to step
buff[3] |= 0x01;
if (DispAxis.length < 1) {
return;
}
// Stp, Cont, MPG or nothing [0:1]
// MC 0; WC 1 [7]
//buff[16]=6;
// Update XYZ - assumes axis selector is not axis 4-6
xhc_encode_float(DispAxis['x'], 4);
xhc_encode_float(DispAxis['y'], 8);
xhc_encode_float(DispAxis['z'], 12);
// Packetize buffer
var packets = Buffer.allocUnsafe(8);
packets[0] = 6;
// Send "6" and then 7 bytes of buffer
var iIndex = 0;
for (var iPacket = 0; iPacket < 3; iPacket++) {
// Copy 7 bytes into packets[1:7]
buff.copy(packets, 1, iIndex, iIndex + 7);
// Move index to beginning of next 7 bytes
iIndex += 7;
// send packets
dev_USB_OUT.sendFeatureReport(packets);
}
}
// // Data available parsing function
function parseButtonData(data) {
//console.log("usb data:", data, " len:", data.length);
feedknob = data[4];
// Process axis selector switch
axisknob = data[5] - 0x11;
// If axis selector is "off", clear last buttons and ignore everything else
if (data[5] == 6) {
// Axis selector is off
// Clear all prior button presses
prevButtons = [0, 0];
// Don't process message any further
return;
}
// Create newButtons slice of data buffer
var newButtons = [data[2], data[3]];;
// At least one button was pressed
// Check to see if button 1 was recorded previously
if ((newButtons[0]) && (!prevButtons.includes(newButtons[0]))) {
// Button1 was not recorded previous
// console.log("Button %d is down",newButtons[0]);
// Process button press
doButton(newButtons, 0, data[4]);
}
// Check to see if button 2 was recorded previously
if ((newButtons[1]) && (!prevButtons.includes(newButtons[1]))) {
// Button2 was not recorded previous
// console.log("Button %d is down",newButtons[1]);
// Process button press
doButton(newButtons, 1, data[4]);
}
// Check to see if previous button 1 is release
// if ((prevButtons[0]) && (!newButtons.includes(prevButtons[0]))) {
// Previous Button 1 is released
// console.log("Button %d is up",prevButtons[0]);
// }
// Check to see if previous button 2 is release
// if ((prevButtons[1]) && (!newButtons.includes(prevButtons[1]))) {
// Previous Button 2 is released
// console.log("Button %d is up",prevButtons[1]);
// }
// Record new buttons
prevButtons = newButtons;
// Process jog dial
if (data[6]) {
// data[6] is a int8 need to determine sign
var iJog = (data[6] > 127 ? data[6] - 256 : data[6]);
//console.log("Jog dial is %i", iJog);
switch (data[4]) {
case 13:
// 13 = 0.001
iJog *= 0.001;
break;
case 14:
// 14 = 0.01
iJog *= 0.01;
break;
case 15:
// 15 = 0.1
iJog *= 0.1;
break;
case 16:
// 16 = 1
iJog *= 1;
break;
case 26:
// 26 = 60%
iJog *= 10;
break;
case 27:
// 27 = 100%
iJog *= 100;
break;
case 28:
// 28 = Lead
iJog *= 250;
break;
default:
console.log("Feed select value %d not handled", data[4]);
return;
}
// Send string to socket
var strMaxRate = '$' + (110 + axisknob);
if (typeof store.settings.settings[strMaxRate] == undefined) {
console.log("store.settings.settings for " + axischars[axisknob] + " axis not set");
return;
}
doJog("$J=G21G91" + axischars[axisknob] + iJog.toPrecision(4)
+ "F" + store.settings.settings[strMaxRate]);
}
}
function doButton(newButtons, iButton, feedknob) {
// console.log("Button %d is down",newButtons[iButton]);
switch (newButtons[iButton]) {
case 1:
// Reset button
Send_Button("$X\n");
break;
case 2:
// Stop button
Send_Button("!\n");
break;
case 3:
// Start/pause button
switch (store.state.status.activeState) {
case "Idle":
case "Run":
case "Jog":
Send_Button("!\n");
break;
case "Hold":
Send_Button("~\n");
break;
default:
console.log("Cannot toggle pause/run in %s state", store.state.status.activeState);
}
// console.log("$X\r\n");
break;
case 4:
// Feed+ button
if (newButtons.includes(12)) {
// Function key is pressed.
if (feedknob < 16) {
Send_Button(String.fromCharCode(0x93));
} else {
Send_Button(String.fromCharCode(0x91));
}
} else {
// Do Macro 1
console.log("Macro 1");
}
break;
case 5:
// Feed- button
if (newButtons.includes(12)) {
// Function key is pressed.
if (feedknob < 16) {
Send_Button(String.fromCharCode(0x94));
} else {
Send_Button(String.fromCharCode(0x92));
}
} else {
// Do Macro 2
console.log("Macro 2");
}
break;
case 6:
// Spindle+ button
if (newButtons.includes(12)) {
// Function key is pressed.
if (feedknob < 16) {
Send_Button(String.fromCharCode(0x9C));
} else {
Send_Button(String.fromCharCode(0x9A));
}
} else {
// Do Macro 3
console.log("Macro 3");
}
break;
case 7:
// Spindle- button
if (newButtons.includes(12)) {
// Function key is pressed.
if (feedknob < 16) {
Send_Button(String.fromCharCode(0x9D));
} else {
Send_Button(String.fromCharCode(0x9B));
}
} else {
// Do Macro 4
console.log("Macro 4");
}
break;
case 8:
// M-Home button
if (newButtons.includes(12)) {
switch (store.state.status.activeState) {
case "Idle":
case "Check":
case "Home":
Send_Button("$H\r\n");
break;
default:
console.log("Cannot home in state %s", store.state.status.activeState)
}
} else {
// Do Macro 5
console.log("Macro 5");
}
break;
case 9:
// Safe-Z button
if (newButtons.includes(12)) {
// Function key is pressed.
// Safe Z
if (!store.settings.settings['$27']) {
console.log("!store.settings.settings['$27'] not set");
return;
}
if (!store.settings.settings['$112']) {
console.log("store.settings.settings['$112'] for Z axis not set");
return;
}
Send_Button("$J=G53G21Z-" + store.settings.settings['$27'] + "F" + store.settings.settings['$112']);
} else {
// Do Macro 6
console.log("Macro 6");
}
break;
case 10:
// W-Home button
if (newButtons.includes(12)) {
// Function key is pressed.
switch (store.state.status.activeState) {
case "Idle":
case "Check":
Send_Button("G10 P1 L20 X0 Y0 Z0\n");
break;
default:
console.log("Cannot set workpiece home in state %s", store.state.status.activeState)
}
} else {
// Do Macro 7
console.log("Macro 7");
}
break;
case 11:
// Spindle On/Off button
if (newButtons.includes(12)) {
// Function key is pressed.
// console.log("Spindle Toggle\r\n");
switch (store.state.status.activeState) {
case "Idle":
case "Check":
if (store.state.status.spindle > 0) {
Send_Button("M5");
} else {
Send_Button("M3");
}
break;
default:
console.log("Cannot toggle spindle in state %s", store.state.status.activeState)
}
} else {
// Do Macro 8
console.log("Macro 8");
}
break;
case 13:
// Probe-Z button
if (newButtons.includes(12)) {
// Function key is pressed.
doProbeZ();
} else {
// Do Macro 9
console.log("Macro 9");
}
break;
case 16:
// Do Macro 10
// console.log("Macro 10");
// Toggle between machine and work coordinates
config.WorkPos = !config.WorkPos;
break;
default:
}
}
function Send_Button(myGCode) {
if (config.DryRunButtons) {
console.log(myGCode);
} else {
// Append new line just in case (no harm with empty lines)
console.log(myGCode);
socket.emit('write',myport, myGCode + "\n");
}
}
function doProbeZ() {
// Check to see if current state allows probing
switch (store.state.status.activeState) {
case "Idle":
break;
default:
console.log("Cannot probe in %s state", store.state.status.activeState)
return;
}
if (!config.ProbeZ) {
console.log("No Probe macro defined");
return;
}
if (config.DruRunProbeZ) {
console.log(config.ProbeZ);
} else {
socket.emit("write", myport, config.ProbeZ+'\n');
}
}
function doJog(myGCode) {
// Check to see if current state allows jogging
switch (store.state.status.activeState) {
case "Idle":
case "Jog":
case "Check":
break;
default:
console.log("Cannot jog in %s state", store.state.status.activeState)
return;
}
if (config.DryRunJog) {
console.log(myGCode);
} else {
socket.emit("write", myport, myGCode+'\n');
}
}