-
Notifications
You must be signed in to change notification settings - Fork 8
/
gcode.js
170 lines (154 loc) · 3.72 KB
/
gcode.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
var stream = require('stream');
var util = require('util');
var parser = require('./parser');
var fs = require('fs');
var byline = require('byline');
// Strips spaces out of all incoming g-code except for comments
function GCodeScrubber() {
this.in_comment = false;
stream.Transform.call(this);
}
util.inherits(GCodeScrubber, stream.Transform);
GCodeScrubber.prototype._transform = function(s, enc, done) {
try {
for(var result = [], i=0, j=0; i<s.length; i++) {
var c = String.fromCharCode(s[i]);
var keep_result = true;
switch(c) {
case ' ':
case '\t':
keep_result = false;
break;
case '(':
if(this.in_comment) {
// ERROR
} else {
this.in_comment = true;
keep_result=false;
}
break;
case ')':
if(this.in_comment) {
this.in_comment = false;
keep_result=false;
} else {
// ERROR
}
default:
if(this.in_comment) { keep_result = false;}
break;
}
if(keep_result) {
result[j++] = c;
}
}
var output = result.join('');
this.push(output);
done();
} catch(e) {
console.log(e);
done();
}
}
function GCodeParser(options) {
if (!options) options = {};
options.objectMode = true;
stream.Transform.call(this, options);
}
util.inherits(GCodeParser, stream.Transform);
GCodeParser.prototype._transform = function (object, encoding, done) {
if(encoding === 'buffer') {
object = object.toString('utf8')
}
this.push(parser.parse(object));
done();
};
var Interpreter = function(options) {}
Interpreter.prototype._exec = function(cmd, args) {
if((cmd in this) && (typeof this[cmd] == 'function')) {
f = this[cmd].bind(this);
return f(args);
} else {
return this._(cmd, args);
}
}
Interpreter.prototype._ = function(cmd, args) {
return;
}
Interpreter.prototype._handle_line = function(line) {
var words = {}
line.words.forEach(function(word) {
var letter = word[0];
var arg = word[1];
switch(letter) {
case 'G':
case 'M':
break;
default:
words[letter] = arg;
break;
}
}.bind(this));
line.words.forEach(function(word) {
var letter = word[0];
var arg = word[1];
switch(letter) {
case 'G':
case 'M':
func = (letter + arg).replace('.','_').trim();
this._exec(func, words)
break;
default:
break;
}
}.bind(this));
}
Interpreter.prototype.interpretStream = function(st, callback) {
st.pipe(new GCodeScrubber())
.pipe(byline())
.pipe(new GCodeParser())
.on('data', function(line) {
this._handle_line(line);
}.bind(this))
.on('end', function() {
if(callback && typeof callback === 'function') {
callback.bind(this)(null);
}
}.bind(this));
}
Interpreter.prototype.interpretFile = function(file, callback) {
this.interpretStream(fs.createReadStream(file), callback);
}
Interpreter.prototype.interpretString = function(s, callback) {
var st = new stream.Readable();
st._read = function noop() {}; // redundant? see update below
st.push(s);
st.push(null);
return this.interpretStream(st, callback);
}
var parseStream = function(st, callback) {
var results = [];
st.pipe(new GCodeScrubber())
.pipe(byline())
.pipe(new GCodeParser())
.on('data', function(line) {
results.push(line);
})
.on('end', function() {
typeof callback === 'function' && callback(null, results);
});
}
var parseFile = function(file, callback) {
return parseStream(fs.createReadStream(file), callback)
}
var parseString = function(s, callback) {
var st = new stream.Readable();
st._read = function noop() {}; // redundant? see update below
st.push(s);
st.push(null);
return parseStream(st, callback)
}
module.exports.parseStream = parseStream;
module.exports.parseString = parseString;
module.exports.parseFile = parseFile;
module.exports.Interpreter = Interpreter;