-
Notifications
You must be signed in to change notification settings - Fork 1
/
lb_clusters.c
455 lines (367 loc) · 11.2 KB
/
lb_clusters.c
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
/*
lb_clusters.c - plugin for unpacking LightBurn cluster encoded engraving moves.
*** EXPERIMENTAL ***
Part of grblHAL
Copyright (c) 2022-2023 Terje Io
Grbl is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Grbl is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Grbl. If not, see <http://www.gnu.org/licenses/>.
*/
#include "driver.h"
#if LB_CLUSTERS_ENABLE
#include "grbl/hal.h"
#include "grbl/gcode.h"
#include "grbl/protocol.h"
#include <string.h>
#ifndef LB_CLUSTER_SIZE
#define LB_CLUSTER_SIZE 16
#endif
#ifndef LB_SVALUE_SCALING
#define LB_SVALUE_SCALING 0 // Change to 1 if S-values is to be multiplied by $30 value (max RPM).
#endif
static struct {
char block[LINE_BUFFER_SIZE];
char *s;
char *cmd;
char eol;
uint_fast16_t length;
} input = {0};
static struct {
char block[34];
char param[24];
char sval[LB_CLUSTER_SIZE][10];
char *s;
char *cmd;
uint_fast16_t count;
uint_fast16_t next;
} cluster;
static stream_read_ptr file_read = NULL, stream_read = NULL;
static on_stream_changed_ptr on_stream_changed;
static on_report_handlers_init_ptr on_report_handlers_init;
static status_message_ptr status_message = NULL;
static on_report_options_ptr on_report_options;
static on_reset_ptr on_reset;
static inline char *get_value (char *v, uint_fast8_t *offset, uint_fast16_t scale)
{
float val;
read_float(v, offset, &val);
return ftoa(val / (float)scale, 8);
}
#if LB_SVALUE_SCALING
static inline char *get_s_value (char *v)
{
char *s;
float val;
uint_fast8_t cc = 0;
read_float(v, &cc, &val);
s = ftoa(val * settings.spindle.rpm_max, 0);
*strchr(s, '.') = input.eol;
return s;
}
#endif
// File stream decoder
static inline void file_fill_buffer (void)
{
int16_t c;
if(cluster.count == 0) {
char *s = input.block;
input.s = s;
input.length = 0;
while((c = file_read()) != SERIAL_NO_DATA) {
*s++ = (char)c;
input.length++;
if((char)c == '\n' || (char)c == '\r') {
if(input.length == 1 && input.eol && input.eol != (char)c) {
input.eol = '\0';
input.s = s;
input.length = 0;
}
input.eol = c;
break;
}
}
*s = '\0';
if(input.length > 5 && !strncasecmp(input.block, "G1", 2) && strchr(input.block, ':')) {
char *s2 = input.block, *s3;
uint_fast8_t params = 0;
s = cluster.block;
while((c = *s2++)) {
if(c != ' ')
*s++ = c;
if(c == 'S')
break;
}
*s = '\0';
cluster.s = s;
cluster.cmd = cluster.block;
cluster.count = cluster.next = 0;
s3 = s2;
while((c = *s2)) {
if(c == ':') {
#if LB_SVALUE_SCALING
*s2++ = '\0';
strcpy(cluster.sval[cluster.count++], get_s_value(s3));
#else
*s2++ = input.eol;
c = *s2;
*s2 = '\0';
strcpy(cluster.sval[cluster.count++], s3);
*s2 = (char)c;
#endif
if(cluster.count == LB_CLUSTER_SIZE) {
cluster.count = input.length = 0;
return;
}
s3 = s2;
}
s2++;
}
#if LB_SVALUE_SCALING
strcpy(cluster.sval[cluster.count++], get_s_value(s3));
#else
*s2 = '\0';
strcpy(cluster.sval[cluster.count++], s3);
#endif
s = cluster.cmd + 3;
s2 = get_value(s, ¶ms, cluster.count);
if(strchr(s, '\0') != s + params + 1) {
strcpy(cluster.param, s + params);
*strchr(cluster.param, 'S') = input.eol;
*strchr(cluster.sval[0], input.eol) = '\0';
}
else
*cluster.param = '\0';
strcpy(s, s2);
cluster.s = strchr(s, '\0');
while(*(cluster.s - 1) == '0')
*(--cluster.s) = '\0';
strcat(cluster.s++, "S");
}
}
if(cluster.count) {
strcpy(cluster.s, cluster.sval[cluster.next++]);
if(*cluster.param) {
strcat(cluster.s, cluster.param);
*cluster.param = '\0';
}
// if(cluster.next == 2) !! oddly this slows down the parser
// cluster.cmd += 2;
if(cluster.next == cluster.count)
cluster.count = 0;
input.s = cluster.cmd;
input.length = strlen(cluster.cmd);
}
}
static int16_t file_decoder (void)
{
int16_t c;
if(input.length == 0)
file_fill_buffer();
if(input.length) {
c = *input.s++;
input.length--;
} else
c = SERIAL_NO_DATA;
return c;
}
// "Normal" stream decoder
static int16_t stream_fill_buffer (void)
{
static char *s = NULL;
int16_t c;
if(s == NULL || input.s == NULL) {
input.s = s = input.block;
cluster.count = input.length = 0;
}
if(cluster.count == 0) {
c = stream_read();
if(c == SERIAL_NO_DATA || c == ASCII_CAN) {
if(ABORTED) {
cluster.count = input.length = 0;
s = NULL;
}
return c;
}
if(++input.length >= LINE_BUFFER_SIZE - 1) {
s = NULL;
return SERIAL_NO_DATA;
}
*s++ = (char)c;
if((char)c == '\n' || (char)c == '\r') {
if(input.length == 1 && input.eol && input.eol != (char)c) {
input.eol = '\0';
input.length = 0;
s--;
return SERIAL_NO_DATA;
}
input.eol = (char)c;
} else
return SERIAL_NO_DATA;
*s = '\0';
if(input.length > 5 && !strncasecmp(input.block, "G1", 2) && strchr(input.block, ':')) {
char *s2 = input.block, *s3;
uint_fast8_t params = 0;
s = cluster.block;
while((c = *s2++)) {
if(c != ' ')
*s++ = c;
if(c == 'S')
break;
}
*s = '\0';
cluster.s = s;
cluster.cmd = cluster.block;
strcpy(cluster.param, cluster.block);
cluster.count = cluster.next = 0;
s3 = s2;
while((c = *s2)) {
if(c == ':') {
#if LB_SVALUE_SCALING
*s2++ = '\0';
strcpy(cluster.sval[cluster.count++], get_s_value(s3));
#else
*s2++ = input.eol;
c = *s2;
*s2 = '\0';
strcpy(cluster.sval[cluster.count++], s3);
*s2 = (char)c;
#endif
if(cluster.count == LB_CLUSTER_SIZE) {
s = NULL;
return SERIAL_NO_DATA;
}
s3 = s2;
}
s2++;
}
#if LB_SVALUE_SCALING
strcpy(cluster.sval[cluster.count++], get_s_value(s3));
#else
*s2 = '\0';
strcpy(cluster.sval[cluster.count++], s3);
#endif
s = cluster.cmd + 3;
s2 = get_value(s, ¶ms, cluster.count);
if(strchr(s, '\0') != s + params + 1) {
strcpy(cluster.param, s + params);
*strchr(cluster.param, 'S') = input.eol;
*strchr(cluster.sval[0], input.eol) = '\0';
}
else
*cluster.param = '\0';
strcpy(s, s2);
cluster.s = strchr(s, '\0');
while(*(cluster.s - 1) == '0')
*(--cluster.s) = '\0';
strcat(cluster.s++, "S");
} else
s = NULL;
}
if(cluster.count) {
strcpy(cluster.s, cluster.sval[cluster.next++]);
if(*cluster.param) {
strcat(cluster.s, cluster.param);
*cluster.param = '\0';
}
// if(cluster.next == 2) !! oddly this slows down the parser
// cluster.cmd += 2;
if(cluster.next == cluster.count) {
s = NULL;
cluster.count = 0;
}
input.s = cluster.cmd;
input.length = strlen(cluster.cmd);
}
return 0;
}
static int16_t stream_decoder (void)
{
static bool buffering = true;
int16_t c;
if(buffering) {
while((c = stream_fill_buffer()) != 0) {
if(ABORTED)
break;
return c;
}
buffering = false;
}
if(input.length) {
c = *input.s++;
input.length--;
} else {
buffering = true;
c = SERIAL_NO_DATA;
}
return c;
}
// Only respond with a single "ok" message for each cluster
// or terminate cluster unpacking if error status reported.
static status_code_t cluster_status_message (status_code_t status_code)
{
if(status_code != Status_OK) {
status_message(status_code);
if(cluster.next) {
input.s = NULL;
cluster.count = cluster.next = input.length = 0;
}
} else if(cluster.count == 0)
status_message(status_code);
return status_code;
}
static void stream_changed (stream_type_t type)
{
if(on_stream_changed)
on_stream_changed(type);
if(type == StreamType_File) {
file_read = hal.stream.read;
hal.stream.read = file_decoder;
} else if(hal.stream.read != stream_decoder) {
stream_read = hal.stream.read;
hal.stream.read = stream_decoder;
}
cluster.count = cluster.next = input.length = 0;
}
static void cluster_reset (void)
{
if(on_reset)
on_reset();
cluster.count = cluster.next = input.length = 0;
}
static void cluster_report (void)
{
if(on_report_handlers_init)
on_report_handlers_init();
status_message = grbl.report.status_message;
grbl.report.status_message = cluster_status_message;
}
static void report_options (bool newopt)
{
if(!newopt) {
hal.stream.write("[CLUSTER:");
hal.stream.write(uitoa(LB_CLUSTER_SIZE));
hal.stream.write("]" ASCII_EOL);
hal.stream.write("[PLUGIN:LightBurn clusters v0.06]" ASCII_EOL);
}
on_report_options(newopt);
}
void lb_clusters_init (void)
{
on_stream_changed = grbl.on_stream_changed;
grbl.on_stream_changed = stream_changed;
on_report_options = grbl.on_report_options;
grbl.on_report_options = report_options;
on_reset = grbl.on_reset;
grbl.on_reset = cluster_reset;
on_report_handlers_init = grbl.on_report_handlers_init;
grbl.on_report_handlers_init = cluster_report;
stream_changed(hal.stream.type);
}
#endif