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
use crate::hwa;
use crate::control::{GCode, S, XYZ, XYZEFS, XYZW};
use crate::helpers;
use alloc::string::String;
use futures::Stream;
#[cfg_attr(not(feature="with-defmt"), derive(Debug))]
pub enum GCodeLineParserError {
ParseError(u32),
GCodeNotImplemented(u32, String),
#[allow(unused)]
FatalError,
}
#[cfg(feature = "with-defmt")]
impl defmt::Format for GCodeLineParserError {
fn format(&self, fmt: defmt::Formatter) {
match self {
GCodeLineParserError::ParseError(ln) => {
defmt::write!(fmt, "ParseError(line={})", ln);
}
GCodeLineParserError::GCodeNotImplemented(ln, string) => {
defmt::write!(fmt, "GCodeNotImplemented(line={}, gcode={})", ln, string.as_str());
}
GCodeLineParserError::FatalError => {
defmt::write!(fmt, "FatalError");
}
}
}
}
// dyn trait could reduce code size a lot with the penalty of the indirection
pub struct GCodeLineParser<STREAM>
where STREAM: Stream<Item = Result<u8, async_gcode::Error>> + Unpin
{
raw_parser: async_gcode::Parser<STREAM, async_gcode::Error>,
current_line: u32,
}
#[allow(unused)]
impl<STREAM> GCodeLineParser<STREAM>
where STREAM: Stream<Item = Result<u8, async_gcode::Error>> + Unpin
{
pub fn new(stream: STREAM) -> Self {
Self {
raw_parser: async_gcode::Parser::new(stream),
current_line: 0,
}
}
#[allow(unused)]
pub fn current_line(&self) -> u32 {
self.current_line
}
pub async fn next_gcode(&mut self) -> Result<Option<GCode>, GCodeLineParserError> {
let mut current_gcode_value = None;
let mut current_line_number = None;
let mut skip_gcode = false;
let mut raw_gcode_spec: Option<(char, Option<(i32, u8)>)> = None;
loop {
match self.raw_parser.next().await {
None => {
hwa::warn!("EOF reading from stream");
return Ok(None);
}
Some(result) => {
match result {
Ok(g) => {
match g {
// FIXME Undo this hacky temporary solution in async-gcode.
// A sub-protocol approach is preferred
#[cfg(feature = "grbl-compat")]
async_gcode::GCode::StatusCommand => {
return Ok(Some(GCode::STATUS))
}
async_gcode::GCode::BlockDelete => {
skip_gcode = true;
//crate::debug!("BlockDelete");
}
async_gcode::GCode::LineNumber(n) => {
//println!("got LN");
current_line_number = Some(n);
}
async_gcode::GCode::Word(ch, fv) => {
if !skip_gcode {
let frx = match fv {
async_gcode::RealValue::Literal(async_gcode::Literal::RealNumber(f)) => {
Some((f.integer_part(), f.scale()))
},
_ => {
None
},
};
match &mut current_gcode_value {
None => {
raw_gcode_spec.replace((ch.to_ascii_uppercase(), frx));
hwa::debug!("GC: {} {:?}", ch, frx);
current_gcode_value = match (ch, frx) {
#[cfg(feature = "grbl-compat")]
('$', None) => {
Some(GCode::GRBLCMD)
},
('g', None) => {
Some(GCode::G)
},
('g', Some((0, 0))) => {
Some(GCode::G0(XYZ {
ln: current_line_number.clone(),
f: None,
x: None,
y: None,
z: None,
}))
}
('g', Some((1, 0))) => {
Some(GCode::G1(XYZEFS {
ln: current_line_number.clone(),
e: None,
f: None,
s: None,
x: None,
y: None,
z: None,
}))
}
('g', Some((4, 0))) => {
Some(GCode::G4)
}
('g', Some((10, 0))) => {
Some(GCode::G10)
}
('g', Some((17, 0))) => {
Some(GCode::G17)
}
('g', Some((21, 0))) => {
Some(GCode::G21)
}
('g', Some((28, 0))) => {
Some(GCode::G28(XYZW {
ln: current_line_number.clone(),
x: None,
y: None,
z: None,
w: None,
}))
}
('g', Some((29, 0))) => {
Some(GCode::G29)
}
('g', Some((31, 0))) => {
Some(GCode::G31)
}
('g', Some((32, 0))) => {
Some(GCode::G32)
}
('g', Some((80, 0))) => {
Some(GCode::G80)
}
('g', Some((90, 0))) => {
Some(GCode::G90)
}
('g', Some((91, 0))) => {
Some(GCode::G91)
}
('g', Some((92, 0))) => {
Some(GCode::G92)
}
('g', Some((94, 0))) => {
Some(GCode::G94)
}
('g', Some((291, 1))) => {
Some(GCode::G29_1)
}
('g', Some((292, 1))) => {
Some(GCode::G29_2)
}
('m', None) => {
Some(GCode::M)
}
('m', Some((3, 0))) => {
Some(GCode::M3)
}
('m', Some((5, 0))) => {
Some(GCode::M5)
}
('m', Some((20, 0))) => {
Some(GCode::M20(None))
}
('m', Some((23, 0))) => {
Some(GCode::M23(None))
}
('m', Some((24, 0))) => {
Some(GCode::M24)
}
('m', Some((25, 0))) => {
Some(GCode::M25)
}
('m', Some((73, 0))) => {
Some(GCode::M73)
}
('m', Some((79, 0))) => {
Some(GCode::M79)
}
('m', Some((80, 0))) => {
Some(GCode::M80)
}
('m', Some((81, 0))) => {
Some(GCode::M81)
}
('m', Some((83, 0))) => {
Some(GCode::M83)
}
('m', Some((84, 0))) => {
Some(GCode::M84)
}
('m', Some((100, 0))) => {
Some(GCode::M100)
}
('m', Some((104, 0))) => {
Some(GCode::M104(S{ln: None, s: None}))
}
('m', Some((105, 0))) => {
Some(GCode::M105)
}
('m', Some((106, 0))) => {
Some(GCode::M106)
}
('m', Some((107, 0))) => {
Some(GCode::M107)
}
('m', Some((109, 0))) => {
Some(GCode::M109(
S {
ln: current_line_number.clone(),
s: None
})
)
}
('m', Some((114, 0))) => {
Some(GCode::M114)
}
('m', Some((115, 0))) => {
Some(GCode::M115)
}
('m', Some((117, 0))) => {
Some(GCode::M117)
}
('m', Some((119, 0))) => {
Some(GCode::M119)
}
('m', Some((140, 0))) => {
Some(GCode::M140(S{ln: None, s: None}))
}
('m', Some((190, 0))) => {
Some(GCode::M190)
}
('m', Some((201, 0))) => {
Some(GCode::M201)
}
('m', Some((203, 0))) => {
Some(GCode::M203)
}
('m', Some((204, 0))) => {
Some(GCode::M204)
}
('m', Some((205, 0))) => {
Some(GCode::M205)
}
('m', Some((220, 0))) => {
Some(GCode::M220(S{ ln: None, s: None }))
}
('m', Some((221, 0))) => {
Some(GCode::M221(S{ ln: None, s: None }))
}
('m', Some((502, 0))) => {
Some(GCode::M502)
}
('m', Some((8621, 1))) => {
Some(GCode::M862_1)
}
('m', Some((8623, 1))) => {
Some(GCode::M862_3)
}
('m', Some((900, 0))) => {
Some(GCode::M900)
}
('m', Some((907, 0))) => {
Some(GCode::M907)
}
_ => {
skip_gcode = true;
None
}
};
}
Some(current_gcode) => {
match current_gcode {
#[cfg(feature = "grbl-compat")]
GCode::STATUS => {
match (ch, frx) {
('I', Some(val)) => {
hwa::warn!("TODO!!");
},
_ => {}
}
}
GCode::G0(coord) => {
match (ch, frx) {
('x', Some(val)) => {
coord.x.replace(helpers::to_fixed(val));
},
('y', Some(val)) => {
coord.y.replace(helpers::to_fixed(val));
},
('z', Some(val)) => {
coord.z.replace(helpers::to_fixed(val));
},
('f', Some(val)) => {
coord.f.replace(helpers::to_fixed(val));
},
_ => {}
}
}
GCode::G1(coord) => {
match (ch, frx) {
('x', Some(val)) => {
coord.x.replace(helpers::to_fixed(val));
},
('y', Some(val)) => {
coord.y.replace(helpers::to_fixed(val));
},
('z', Some(val)) => {
coord.z.replace(helpers::to_fixed(val));
},
('e', Some(val)) => {
coord.e.replace(helpers::to_fixed(val));
}
_ => {}
}
}
GCode::G28(coord) => {
match (ch, frx) {
('x', Some(val)) => {
coord.x.replace(helpers::to_fixed(val));
},
('y', Some(val)) => {
coord.y.replace(helpers::to_fixed(val));
},
('z', Some(val)) => {
coord.z.replace(helpers::to_fixed(val));
},
_ => {}
}
}
GCode::M20(path) => {
if ch == 'f' {
if let async_gcode::RealValue::Literal(async_gcode::Literal::String(mstr)) = fv {
path.replace(mstr);
}
}
}
GCode::M23(file) => {
if ch == 'f' {
if let async_gcode::RealValue::Literal(async_gcode::Literal::String(mstr)) = fv {
file.replace(mstr);
}
}
}
GCode::M104(coord) | GCode::M109(coord) | GCode::M140(coord) | GCode::M220(coord) => {
match (ch, frx) {
('s', Some(val)) => {
coord.s.replace(helpers::to_fixed(val));
},
_ => {}
}
}
_ => {}
}
}
}
} else {
//crate::debug!("I'm skipping words")
}
}
async_gcode::GCode::Execute => {
self.current_line += 1;
match current_gcode_value {
None => {
match raw_gcode_spec {
None => continue, // Empty line. Just ignore
Some(rgs) => {
// Nice to report all this kind of things, but avoidable to save code size
let num_part = match rgs.1 {
None => {String::new()}
Some(frv) => {
let dv = 10i32.pow(frv.1.into());
let p1 = frv.0.abs() / dv;
let p2 = frv.0.abs() - (p1 * dv);
alloc::format!("{}{}.{}", if frv.0 < 0 {"-"} else {""}, p1, p2)
}
};
let s = alloc::format!("{}{}", rgs.0, num_part);
return Err(GCodeLineParserError::GCodeNotImplemented(self.current_line, s));
}
}
},
Some(cgv) => return Ok(Some(cgv)),
}
}
_ => {
return Err(GCodeLineParserError::GCodeNotImplemented(self.current_line, alloc::string::String::from("N/A")))
}
}
}
Err(error) => {
match error {
async_gcode::Error::UnexpectedByte(b) => {
hwa::warn!("Unexpected byte: {} ({})", b, char::from(b));
}
async_gcode::Error::NumberOverflow => {
hwa::warn!("Number overflow");
}
async_gcode::Error::BadNumberFormat => {
hwa::warn!("Bad number format");
}
_e => {
#[cfg(feature = "native")]
hwa::error!("Parse error {:?}", _e);
hwa::error!("Parse error");
}
}
return Err(GCodeLineParserError::ParseError(self.current_line))
}
}
}
}
}
}
#[allow(unused)]
pub async fn close(&mut self) {
}
}
/// This trait is just a hack to give backward compatibility with unpatched async-gcode 0.3.0
/// Normally, won't be used as it is patched in cargo.toml
/// Purpose of this trait and impl is just to be able to publish printhor in crates.io
trait FixedAdaptor {
fn integer_part(&self) -> i32;
fn scale(&self) -> u8;
}
impl FixedAdaptor for f64 {
fn integer_part(&self) -> i32 {
panic!("Please, use patched async-gcode instead")
}
fn scale(&self) -> u8 {
panic!("Please, use patched async-gcode instead")
}
}