forked from grblHAL/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngc_params.c
805 lines (644 loc) · 23.9 KB
/
ngc_params.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
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
/*
ngc_params.c - get/set NGC parameter value by id or name
Part of grblHAL
Copyright (c) 2021-2024 Terje Io
grblHAL 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.
grblHAL 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 grblHAL. If not, see <http://www.gnu.org/licenses/>.
*/
/*
All predefined parameters defined in NIST RS274NGC version 3 (ref section 3.2.1) are implemented.
Most additional predefined parameters defined by LinuxCNC (ref section 5.2.3.1) are implemented.
*/
#include "hal.h"
#if NGC_PARAMETERS_ENABLE
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "system.h"
#include "settings.h"
#include "ngc_params.h"
#ifndef NGC_MAX_CALL_LEVEL
#define NGC_MAX_CALL_LEVEL 10
#endif
#ifndef NGC_MAX_PARAM_LENGTH
#define NGC_MAX_PARAM_LENGTH 20
#endif
typedef float (*ngc_param_get_ptr)(ngc_param_id_t id);
typedef float (*ngc_named_param_get_ptr)(void);
typedef struct {
ngc_param_id_t id_min;
ngc_param_id_t id_max;
ngc_param_get_ptr get;
} ngc_ro_param_t;
typedef struct ngc_rw_param {
void *context;
ngc_param_id_t id;
float value;
struct ngc_rw_param *next;
} ngc_rw_param_t;
typedef struct {
const char *name;
ncg_name_param_id_t id;
ngc_named_param_get_ptr get;
} ngc_named_ro_param_t;
typedef struct ngc_named_rw_param {
void *context;
char name[NGC_MAX_PARAM_LENGTH + 1];
float value;
struct ngc_named_rw_param *next;
} ngc_named_rw_param_t;
typedef struct {
uint32_t level;
void *context;
gc_modal_t *modal_state;
} ngc_param_context_t;
static int32_t call_level = -1;
static void *call_context;
static gc_modal_t *modal_state;
static ngc_param_context_t call_levels[NGC_MAX_CALL_LEVEL];
static ngc_rw_param_t *rw_params = NULL;
static ngc_named_rw_param_t *rw_global_params = NULL;
static float _relative_pos (uint_fast8_t axis)
{
float value;
if(axis < N_AXIS) {
value = sys.position[axis] / settings.axis[axis].steps_per_mm - gc_get_offset(axis, false);
if(settings.flags.report_inches)
value *= 25.4f;
} else
value = 0.0f;
return value;
}
// numbered parameters
static float probe_coord (ngc_param_id_t id)
{
float value = 0.0f;
uint_fast8_t axis = (id % 10) - 1;
coord_system_t data;
if(axis < N_AXIS && (sys.probe_coordsys_id == gc_state.modal.coord_system.id || settings_read_coord_data(sys.probe_coordsys_id, &data.xyz))) {
value = sys.probe_position[axis] / settings.axis[axis].steps_per_mm -
(sys.probe_coordsys_id == gc_state.modal.coord_system.id ? gc_state.modal.coord_system.xyz[axis] : data.xyz[axis]);
if(settings.flags.report_inches)
value *= 25.4f;
}
return value;
}
static float scaling_factors (ngc_param_id_t id)
{
float *factors = gc_get_scaling();
uint_fast8_t axis = id % 10;
return axis <= N_AXIS ? factors[axis - 1] : 0.0f;
}
static float probe_result (ngc_param_id_t id)
{
return sys.flags.probe_succeeded ? 1.0f : 0.0f;
}
/*
static float home_pos (ngc_param_id_t id)
{
uint_fast8_t axis = id % 10;
return axis <= N_AXIS ? sys.home_position[axis - 1] : 0.0f;
}
*/
static float m66_result (ngc_param_id_t id)
{
return (float)sys.var5399;
}
static float tool_number (ngc_param_id_t id)
{
return (float)gc_state.tool->tool_id;
}
static float tool_offset (ngc_param_id_t id)
{
uint_fast8_t axis = id % 10;
return axis <= N_AXIS ? gc_state.tool_length_offset[axis] : 0.0f;
}
static float g28_home (ngc_param_id_t id)
{
float value = 0.0f;
uint_fast8_t axis = id % 10;
coord_system_t data;
if(axis <= N_AXIS && settings_read_coord_data(CoordinateSystem_G28, &data.xyz))
value = data.xyz[axis - 1];
return value;
}
static float g30_home (ngc_param_id_t id)
{
float value = 0.0f;
uint_fast8_t axis = id % 10;
coord_system_t data;
#if COMPATIBILITY_LEVEL > 1
if(id <= CoordinateSystem_G59) {
#endif
if (axis <= N_AXIS && settings_read_coord_data(CoordinateSystem_G30, &data.xyz))
value = data.xyz[axis - 1];
#if COMPATIBILITY_LEVEL > 1
}
#endif
return value;
}
static float coord_system (ngc_param_id_t id)
{
return (float)gc_state.modal.coord_system.id + 1;
}
static float coord_system_offset (ngc_param_id_t id)
{
float value = 0.0f;
uint_fast8_t axis = id % 10;
coord_system_t data;
id = (id - 5220 - axis - (id == 0 ? 10 : 0)) / 20;
if (axis > 0 && axis <= N_AXIS && settings_read_coord_data((coord_system_id_t)id, &data.xyz))
value = data.xyz[axis - 1];
return value;
}
static float g92_offset_applied (ngc_param_id_t id)
{
return (float)gc_state.g92_coord_offset_applied;
}
static float g92_offset (ngc_param_id_t id)
{
uint_fast8_t axis = id % 10;
return axis <= N_AXIS ? gc_state.g92_coord_offset [axis - 1] : 0.0f;
}
static float work_position (ngc_param_id_t id)
{
float value = 0.0f;
uint_fast8_t axis = id % 10;
if(axis < N_AXIS)
value = _relative_pos(axis);
return value;
}
static float debug_output (ngc_param_id_t id)
{
return (float)settings.flags.ngc_debug_out;
}
PROGMEM static const ngc_ro_param_t ngc_ro_params[] = {
{ .id_min = 5061, .id_max = 5069, .get = probe_coord }, // LinuxCNC
{ .id_min = 5070, .id_max = 5070, .get = probe_result }, // LinuxCNC
{ .id_min = 5161, .id_max = 5169, .get = g28_home },
{ .id_min = 5181, .id_max = 5189, .get = g30_home },
{ .id_min = 5191, .id_max = 5199, .get = scaling_factors }, // Mach3
{ .id_min = 5210, .id_max = 5210, .get = g92_offset_applied }, // LinuxCNC
{ .id_min = 5211, .id_max = 5219, .get = g92_offset },
{ .id_min = 5220, .id_max = 5220, .get = coord_system },
{ .id_min = 5221, .id_max = 5230, .get = coord_system_offset },
{ .id_min = 5241, .id_max = 5250, .get = coord_system_offset },
{ .id_min = 5261, .id_max = 5270, .get = coord_system_offset },
{ .id_min = 5281, .id_max = 5290, .get = coord_system_offset },
{ .id_min = 5301, .id_max = 5310, .get = coord_system_offset },
{ .id_min = 5321, .id_max = 5230, .get = coord_system_offset },
{ .id_min = 5341, .id_max = 5350, .get = coord_system_offset },
{ .id_min = 5361, .id_max = 5370, .get = coord_system_offset },
{ .id_min = 5381, .id_max = 5390, .get = coord_system_offset },
{ .id_min = 5399, .id_max = 5399, .get = m66_result }, // LinuxCNC
{ .id_min = 5400, .id_max = 5400, .get = tool_number }, // LinuxCNC
{ .id_min = 5401, .id_max = 5409, .get = tool_offset }, // LinuxCNC
{ .id_min = 5420, .id_max = 5428, .get = work_position }, // LinuxCNC
{ .id_min = 5599, .id_max = 5599, .get = debug_output } // LinuxCNC
};
bool ngc_param_get (ngc_param_id_t id, float *value)
{
bool found = id > 0 && id < ngc_ro_params[0].id_min;
uint_fast8_t idx = sizeof(ngc_ro_params) / sizeof(ngc_ro_param_t);
*value = 0.0f;
if(found) {
void *context = id > (ngc_param_id_t)30 ? NULL : call_context;
ngc_rw_param_t *rw_param = rw_params;
while(rw_param) {
if(rw_param->context == context && rw_param->id == id) {
*value = rw_param->value;
rw_param = NULL;
} else
rw_param = rw_param->next;
}
} else do {
idx--;
if((found = id >= ngc_ro_params[idx].id_min && id <= ngc_ro_params[idx].id_max))
*value = ngc_ro_params[idx].get(id);
} while(idx && !found);
return found;
}
bool ngc_param_is_rw (ngc_param_id_t id)
{
return id > 0 && id < ngc_ro_params[0].id_min;
}
bool ngc_param_exists (ngc_param_id_t id)
{
return id > 0 && id <= ngc_ro_params[(sizeof(ngc_ro_params) / sizeof(ngc_ro_param_t)) - 1].id_max;
}
bool ngc_param_set (ngc_param_id_t id, float value)
{
bool ok = id > 0 && id < ngc_ro_params[0].id_min;
if(ok) {
void *context = id > (ngc_param_id_t)30 ? NULL : call_context;
ngc_rw_param_t *rw_param = rw_params, *rw_param_last = rw_params;
while(rw_param) {
if(rw_param->context == context && rw_param->id == id) {
break;
} else {
rw_param_last = rw_param;
rw_param = rw_param->next;
}
}
if(rw_param == NULL && value != 0.0f && (rw_param = malloc(sizeof(ngc_rw_param_t)))) {
rw_param->id = id;
rw_param->context = context;
rw_param->next = NULL;
if(rw_params == NULL)
rw_params = rw_param;
else
rw_param_last->next = rw_param;
}
if(rw_param)
rw_param->value = value;
else
ok = value == 0.0f;
}
return ok;
}
PROGMEM static const ngc_named_ro_param_t ngc_named_ro_param[] = {
{ .name = "_vmajor", .id = NGCParam_vmajor },
{ .name = "_vminor", .id = NGCParam_vminor },
{ .name = "_line", .id = NGCParam_line },
{ .name = "_motion_mode", .id = NGCParam_motion_mode },
{ .name = "_plane", .id = NGCParam_plane },
{ .name = "_ccomp", .id = NGCParam_ccomp },
{ .name = "_metric", .id = NGCParam_metric },
{ .name = "_imperial", .id = NGCParam_imperial },
{ .name = "_absolute", .id = NGCParam_absolute },
{ .name = "_incremental", .id = NGCParam_incremental },
{ .name = "_inverse_time", .id = NGCParam_inverse_time },
{ .name = "_units_per_minute", .id = NGCParam_units_per_minute },
{ .name = "_units_per_rev", .id = NGCParam_units_per_rev },
{ .name = "_coord_system", .id = NGCParam_coord_system },
{ .name = "_tool_offset", .id = NGCParam_tool_offset },
{ .name = "_retract_r_plane", .id = NGCParam_retract_r_plane },
{ .name = "_retract_old_z", .id = NGCParam_retract_old_z },
{ .name = "_spindle_rpm_mode", .id = NGCParam_spindle_rpm_mode },
{ .name = "_spindle_css_mode", .id = NGCParam_spindle_css_mode },
{ .name = "_ijk_absolute_mode", .id = NGCParam_ijk_absolute_mode },
{ .name = "_lathe_diameter_mode", .id = NGCParam_lathe_diameter_mode },
{ .name = "_lathe_radius_mode", .id = NGCParam_lathe_radius_mode },
{ .name = "_spindle_on", .id = NGCParam_spindle_on },
{ .name = "_spindle_cw", .id = NGCParam_spindle_cw },
{ .name = "_mist", .id = NGCParam_mist },
{ .name = "_flood", .id = NGCParam_flood },
{ .name = "_speed_override", .id = NGCParam_speed_override },
{ .name = "_feed_override", .id = NGCParam_feed_override },
{ .name = "_adaptive_feed", .id = NGCParam_adaptive_feed },
{ .name = "_feed_hold", .id = NGCParam_feed_hold },
{ .name = "_feed", .id = NGCParam_feed },
{ .name = "_rpm", .id = NGCParam_rpm },
{ .name = "_x", .id = NGCParam_x },
{ .name = "_y", .id = NGCParam_y },
{ .name = "_z", .id = NGCParam_z },
{ .name = "_a", .id = NGCParam_a },
{ .name = "_b", .id = NGCParam_b },
{ .name = "_c", .id = NGCParam_c },
{ .name = "_u", .id = NGCParam_u },
{ .name = "_v", .id = NGCParam_v },
{ .name = "_w", .id = NGCParam_w },
{ .name = "_current_tool", .id = NGCParam_current_tool },
{ .name = "_current_pocket", .id = NGCParam_current_pocket },
{ .name = "_selected_tool", .id = NGCParam_selected_tool },
{ .name = "_selected_pocket", .id = NGCParam_selected_pocket },
{ .name = "_call_level", .id = NGCParam_call_level },
};
// Named parameters
float ngc_named_param_get_by_id (ncg_name_param_id_t id)
{
float value;
switch(id) {
case NGCParam_vmajor:
value = 1.1f;
break;
case NGCParam_vminor:
value = 0.0f; // TODO: derive from version letter?
break;
case NGCParam_line:
value = (float)gc_state.line_number;
break;
case NGCParam_motion_mode:
value = (float)(gc_state.modal.motion * 10); // TODO: Fix G38.x
break;
case NGCParam_plane:
value = (float)(170 + gc_state.modal.plane_select * 10);
break;
case NGCParam_ccomp:
value = 400.0f;
break;
case NGCParam_metric:
value = gc_state.modal.units_imperial ? 0.0f : 1.0f;
break;
case NGCParam_imperial:
value = gc_state.modal.units_imperial ? 1.0f : 0.0f;
break;
case NGCParam_absolute:
value = gc_state.modal.distance_incremental ? 0.0f : 1.0f;
break;
case NGCParam_incremental:
value = gc_state.modal.distance_incremental ? 1.0f : 0.0f;
break;
case NGCParam_inverse_time:
value = gc_state.modal.feed_mode == FeedMode_InverseTime ? 1.0f : 0.0f;
break;
case NGCParam_units_per_minute:
value = gc_state.modal.feed_mode == FeedMode_UnitsPerMin ? 1.0f : 0.0f;
break;
case NGCParam_units_per_rev:
value = gc_state.modal.feed_mode == FeedMode_UnitsPerRev ? 1.0f : 0.0f;
break;
case NGCParam_coord_system:
{
uint_fast16_t id = gc_state.modal.coord_system.id * 10;
if(id > (CoordinateSystem_G59 * 10))
id = (CoordinateSystem_G59 * 10) + gc_state.modal.coord_system.id - CoordinateSystem_G59;
value = (float)(540 + id);
}
break;
case NGCParam_tool_offset:
value = gc_state.modal.tool_offset_mode >= ToolLengthOffset_Enable ? 1.0f : 0.0f;
break;
case NGCParam_retract_r_plane:
value = gc_state.modal.retract_mode == CCRetractMode_Previous ? 1.0f : 0.0f;
break;
case NGCParam_retract_old_z:
value = gc_state.modal.retract_mode == CCRetractMode_RPos ? 1.0f : 0.0f;
break;
case NGCParam_spindle_rpm_mode:
value = gc_state.modal.spindle.rpm_mode == SpindleSpeedMode_RPM ? 1.0f : 0.0f;
break;
case NGCParam_spindle_css_mode:
value = gc_state.modal.spindle.rpm_mode == SpindleSpeedMode_CSS ? 1.0f : 0.0f;
break;
case NGCParam_ijk_absolute_mode:
value = 0.0f;
break;
case NGCParam_lathe_diameter_mode:
value = gc_state.modal.diameter_mode ? 1.0f : 0.0f;
break;
case NGCParam_lathe_radius_mode:
value = gc_state.modal.diameter_mode ? 0.0f : 1.0f;
break;
case NGCParam_spindle_on:
value = gc_state.modal.spindle.state.on ? 1.0f : 0.0f;
break;
case NGCParam_spindle_cw:
value = gc_state.modal.spindle.state.ccw ? 1.0f : 0.0f;
break;
case NGCParam_mist:
value = gc_state.modal.coolant.mist ? 1.0f : 0.0f;
break;
case NGCParam_flood:
value = gc_state.modal.coolant.flood ? 1.0f : 0.0f;
break;
case NGCParam_speed_override:
value = gc_state.modal.override_ctrl.spindle_rpm_disable ? 0.0f : 1.0f;
break;
case NGCParam_feed_override:
value = gc_state.modal.override_ctrl.feed_rate_disable ? 0.0f : 1.0f;
break;
case NGCParam_adaptive_feed:
value = 0.0f;
break;
case NGCParam_feed_hold:
value = gc_state.modal.override_ctrl.feed_hold_disable ? 0.0f : 1.0f;
break;
case NGCParam_feed:
value = gc_state.feed_rate;
break;
case NGCParam_rpm:
value = gc_state.spindle.rpm;
break;
case NGCParam_x:
//no break
case NGCParam_y:
//no break
case NGCParam_z:
//no break
case NGCParam_a:
//no break
case NGCParam_b:
//no break
case NGCParam_c:
//no break
case NGCParam_u:
//no break
case NGCParam_v:
//no break
case NGCParam_w:
value = _relative_pos(id - NGCParam_x);
break;
case NGCParam_current_tool:
value = (float)gc_state.tool->tool_id;
break;
case NGCParam_current_pocket:
value = 0.0f;
break;
case NGCParam_selected_tool:
value = gc_state.tool_pending != gc_state.tool->tool_id ? (float)gc_state.tool_pending : -1.0f;
break;
case NGCParam_selected_pocket:
value = 0.0f;
break;
case NGCParam_call_level:
value = (float)ngc_call_level();
break;
default:
value = NAN;
}
return value;
}
// Lowercase name, remove control characters and spaces
static char *ngc_name_tolower (char *s)
{
static char name[NGC_MAX_PARAM_LENGTH + 1];
uint_fast8_t len = 0;
char c, *s1 = s, *s2 = name;
while((c = *s1++) && len < NGC_MAX_PARAM_LENGTH) {
if(c > ' ') {
*s2++ = LCAPS(c);
len++;
}
}
*s2 = '\0';
return name;
}
bool ngc_named_param_get (char *name, float *value)
{
bool found = false;
uint_fast8_t idx = sizeof(ngc_named_ro_param) / sizeof(ngc_named_ro_param_t);
name = ngc_name_tolower(name);
*value = 0.0f;
if(*name == '_') do {
idx--;
if((found = !strcmp(name, ngc_named_ro_param[idx].name)))
*value = ngc_named_param_get_by_id(ngc_named_ro_param[idx].id);
} while(idx && !found);
if(!found) {
void *context = *name == '_' ? NULL : call_context;
ngc_named_rw_param_t *rw_param = rw_global_params;
while(rw_param && !found) {
if((found = rw_param->context == context && !strcmp(rw_param->name, name)))
*value = rw_param->value;
else
rw_param = rw_param->next;
}
}
return found;
}
bool ngc_named_param_exists (char *name)
{
bool ok = false;
uint_fast8_t idx = sizeof(ngc_named_ro_param) / sizeof(ngc_named_ro_param_t);
name = ngc_name_tolower(name);
// Check if name is supplied, return false if not.
if((*name == '_' ? *(name + 1) : *name) == '\0')
return false;
// Check if it is a (read only) predefined parameter.
if(*name == '_') do {
ok = !strcmp(name, ngc_named_ro_param[--idx].name);
} while(idx && !ok);
// If not predefined attempt to find it.
if(!ok && rw_global_params && strlen(name) < NGC_MAX_PARAM_LENGTH) {
void *context = *name == '_' ? NULL : call_context;
ngc_named_rw_param_t *rw_param = rw_global_params;
while(rw_param) {
if((ok = rw_param->context == context && !strcmp(rw_param->name, name)))
break;
rw_param = rw_param->next;
}
}
return ok;
}
bool ngc_named_param_set (char *name, float value)
{
bool ok = false;
uint_fast8_t idx = sizeof(ngc_named_ro_param) / sizeof(ngc_named_ro_param_t);
name = ngc_name_tolower(name);
// Check if name is supplied, return false if not.
if((*name == '_' ? *(name + 1) : *name) == '\0')
return false;
// Check if it is a (read only) predefined parameter.
if(*name == '_') do {
idx--;
ok = !strcmp(name, ngc_named_ro_param[idx].name);
} while(idx && !ok);
// If not predefined attempt to set it.
if(!ok && (ok = strlen(name) < NGC_MAX_PARAM_LENGTH)) {
void *context = *name == '_' ? NULL : call_context;
ngc_named_rw_param_t *rw_param = rw_global_params, *rw_param_last = rw_global_params;
while(rw_param) {
if(rw_param->context == context && !strcmp(rw_param->name, name)) {
break;
} else {
rw_param_last = rw_param;
rw_param = rw_param->next;
}
}
if(rw_param == NULL && (rw_param = malloc(sizeof(ngc_named_rw_param_t)))) {
strcpy(rw_param->name, name);
rw_param->context = context;
rw_param->next = NULL;
if(rw_global_params == NULL)
rw_global_params = rw_param;
else
rw_param_last->next = rw_param;
}
if((ok = rw_param != NULL))
rw_param->value = value;
}
return ok;
}
bool ngc_modal_state_save (gc_modal_t *state, bool auto_restore)
{
gc_modal_t **saved_state = call_level == -1 ? &modal_state : &call_levels[call_level].modal_state;
if(*saved_state == NULL)
*saved_state = malloc(sizeof(gc_modal_t));
if(*saved_state)
memcpy(*saved_state, state, sizeof(gc_modal_t));
return *saved_state != NULL;
}
void ngc_modal_state_invalidate (void)
{
gc_modal_t **saved_state = call_level == -1 ? &modal_state : &call_levels[call_level].modal_state;
if(*saved_state) {
free(*saved_state);
*saved_state = NULL;
}
}
bool ngc_modal_state_restore (void)
{
return gc_modal_state_restore(call_level == -1 ? modal_state : call_levels[call_level].modal_state);
}
bool ngc_call_push (void *context)
{
bool ok;
if((ok = call_level < (NGC_MAX_CALL_LEVEL - 1)))
call_levels[++call_level].context = call_context = context;
return ok;
}
bool ngc_call_pop (void)
{
if(call_level >= 0) {
if(call_context) {
ngc_rw_param_t *rw_param = rw_params, *rw_param_last = rw_params;
while(rw_param) {
if(rw_param->context == call_context) {
ngc_rw_param_t *rw_param_free = rw_param;
rw_param = rw_param->next;
if(rw_param_free == rw_params)
rw_params = rw_param_last = rw_param;
else
rw_param_last->next = rw_param;
free(rw_param_free);
} else {
rw_param_last = rw_param;
rw_param = rw_param->next;
}
}
ngc_named_rw_param_t *rw_named_param = rw_global_params, *rw_named_param_last = rw_global_params;
while(rw_named_param) {
if(rw_named_param->context == call_context) {
ngc_named_rw_param_t *rw_named_param_free = rw_named_param;
rw_named_param = rw_named_param->next;
if(rw_named_param_free == rw_global_params)
rw_global_params = rw_named_param_last = rw_named_param;
else
rw_named_param_last->next = rw_named_param;
free(rw_named_param_free);
} else {
rw_named_param_last = rw_named_param;
rw_named_param = rw_named_param->next;
}
}
}
if(call_levels[call_level].modal_state) {
if(call_levels[call_level].modal_state->auto_restore)
gc_modal_state_restore(call_levels[call_level].modal_state);
free(call_levels[call_level].modal_state);
call_levels[call_level].modal_state = NULL;
}
call_context = --call_level >= 0 ? call_levels[call_level].context : NULL;
}
return call_level >= 0;
}
uint_fast8_t ngc_call_level (void)
{
return (uint_fast8_t)(call_level + 1);
}
uint8_t ngc_float_decimals (void)
{
return settings.flags.report_inches ? N_DECIMAL_COORDVALUE_INCH : N_DECIMAL_COORDVALUE_MM;
}
#endif // NGC_PARAMETERS_ENABLE