-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathappcmd.js
More file actions
96 lines (79 loc) · 3.03 KB
/
appcmd.js
File metadata and controls
96 lines (79 loc) · 3.03 KB
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
// 3D Printer Interface
// Command line application to stream .gcode files directly to printer just by using ./modules/core.js module
//
// demos:
// 1- send gcode data to printer from file using command line ARGUMENTS
// $ node appcmd.js ./bin/gcode/demo.gcode
//
// 2- send gcode data to printer from STDIN pipe (file content stream using cat)
// $ cat ./bin/gcode/demo.gcode | node appcmd.js
//
// 3- send gcode data to printer from STDIN - manual insert GCODE on the console + ENTER (interactive/manual control)
// $ node appcmd.js
// (enter commands):
// M104 S200\n
// G28\n
// G90\n
// G21\n
// G92\n
// M82\n
// G1 Z0.200 F7800.000\n
//------------------------------------------------------------------
var configdata = require('config');
var node_env =
process.env.NODE_ENV !== undefined ?
process.env.NODE_ENV :
"default";
console.log("[appcmd.js]:config/%s.json: $s", node_env, JSON.stringify(configdata));
var fs = require('fs'),
path = process.argv[2],
printercore = require('./modules/core.js');
var readableStream;
var readableSize = 4*256;
//------------------------------------------------------------------
// objects initialization/configuration
//------------------------------------------------------------------
printercore.setCbAfterOpenPrinter(delayedmain);
// try interface without real 3d printer by using /dev/null
//printercore.setConfigPrinter({serialport: "/dev/null", baudrate: 115200});
//printercore.setConfigPrinter({serialport: "/dev/tty.usbmodem621", baudrate: 115200});
//printercore.setConfigPrinter({serialport: "/dev/tty.usbmodem622", baudrate: 115200});
// or (with 3d printer hardware) - alternative init method with args
//printercore.initializePrinter({serialport: "/dev/tty.usbmodem621", baudrate: 115200});
var spconfig = {};
spconfig.serialport =
configdata.serialport.serialport !== undefined ?
configdata.serialport.serialport :
"/dev/null";
spconfig.baudrate =
configdata.serialport.baudrate !== undefined ?
configdata.serialport.baudrate :
115200;
printercore.initialize(spconfig);
//------------------------------------------------------------------
// main
//------------------------------------------------------------------
function delayedmain () {
setTimeout(main, 2000);
}
function main () {
console.log("Launching Main();");
// check if .gcode file is inserted via command line arguments
if (path !==undefined ) {
readableStream = fs.createReadStream(path, {encoding: 'utf8', highWaterMark : 8});
//readableStream.pipe(process.stdout);
readableStream.pipe(printercore.iStreamPrinter, {end: false});
printercore.oStreamPrinter.pipe(process.stdout);
readableStream.once('end', function() {
console.log('Readable Stream Ended');
});
}
else {
console.log("Get stream from STDIN pipe...");
// http://docs.nodejitsu.com/articles/advanced/streams/how-to-use-stream-pipe
process.stdin.setEncoding('utf8');
process.stdin.pipe(printercore.iStreamPrinter, { end: false });
printercore.oStreamPrinter.pipe(process.stdout);
}
readableStream.read(readableSize);
}