Skip to content

Commit 5d6a99c

Browse files
authored
Merge pull request #587 from skasti/master
Enable parameter substitution in MSG comments
2 parents 3ef6763 + 94d15b4 commit 5d6a99c

File tree

1 file changed

+50
-44
lines changed

1 file changed

+50
-44
lines changed

gcode.c

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,51 @@ static status_code_t read_parameter (char *line, uint_fast8_t *char_counter, flo
509509
return status;
510510
}
511511

512+
static void substitute_parameters(char *comment, char **message) {
513+
size_t len = 0;
514+
float value;
515+
char *s3;
516+
uint_fast8_t char_counter = 0;
517+
char c = *(comment + char_counter);
518+
519+
// Trim leading spaces
520+
while(*comment == ' ')
521+
comment++;
522+
523+
// Calculate length of substituted string
524+
while((c = comment[char_counter++])) {
525+
if(c == '#') {
526+
char_counter--;
527+
if(read_parameter(comment, &char_counter, &value) == Status_OK)
528+
len += strlen(trim_float(ftoa(value, ngc_float_decimals())));
529+
else
530+
len += 3; // "N/A"
531+
} else
532+
len++;
533+
}
534+
535+
// Perform substitution
536+
if((s3 = *message = malloc(len + 1))) {
537+
538+
*s3 = '\0';
539+
char_counter = 0;
540+
541+
while((c = comment[char_counter++])) {
542+
if(c == '#') {
543+
char_counter--;
544+
if(read_parameter(comment, &char_counter, &value) == Status_OK)
545+
strcat(s3, trim_float(ftoa(value, ngc_float_decimals())));
546+
else
547+
strcat(s3, "N/A");
548+
s3 = strchr(s3, '\0');
549+
} else {
550+
*s3++ = c;
551+
*s3 = '\0';
552+
}
553+
}
554+
}
555+
}
556+
512557
#endif // NGC_EXPRESSIONS_ENABLE
513558

514559
#if NGC_PARAMETERS_ENABLE
@@ -596,63 +641,24 @@ char *gc_normalize_block (char *block, char **message)
596641

597642
if(message && *message == NULL && !strncmp(comment, "(MSG,", 5) && (*message = malloc(len))) {
598643
comment += 5;
599-
// Trim leading spaces
644+
#if NGC_EXPRESSIONS_ENABLE
645+
substitute_parameters(comment, message);
646+
#else
600647
while(*comment == ' ') {
601648
comment++;
602649
len--;
603650
}
604651
memcpy(*message, comment, len);
652+
#endif
605653
}
606654

607655
#if NGC_EXPRESSIONS_ENABLE
608656
// Debug message string substitution
609657
if(message && *message == NULL && !strncmp(comment, "(DEBUG,", 7)) {
610658

611659
if(settings.flags.ngc_debug_out) {
612-
613-
float value;
614-
char *s3;
615-
uint_fast8_t char_counter = 0;
616-
617-
len = 0;
618660
comment += 7;
619-
620-
// Trim leading spaces
621-
while(*comment == ' ')
622-
comment++;
623-
624-
// Calculate length of substituted string
625-
while((c = comment[char_counter++])) {
626-
if(c == '#') {
627-
char_counter--;
628-
if(read_parameter(comment, &char_counter, &value) == Status_OK)
629-
len += strlen(trim_float(ftoa(value, ngc_float_decimals())));
630-
else
631-
len += 3; // "N/A"
632-
} else
633-
len++;
634-
}
635-
636-
// Perform substitution
637-
if((s3 = *message = malloc(len + 1))) {
638-
639-
*s3 = '\0';
640-
char_counter = 0;
641-
642-
while((c = comment[char_counter++])) {
643-
if(c == '#') {
644-
char_counter--;
645-
if(read_parameter(comment, &char_counter, &value) == Status_OK)
646-
strcat(s3, trim_float(ftoa(value, ngc_float_decimals())));
647-
else
648-
strcat(s3, "N/A");
649-
s3 = strchr(s3, '\0');
650-
} else {
651-
*s3++ = c;
652-
*s3 = '\0';
653-
}
654-
}
655-
}
661+
substitute_parameters(comment, message);
656662
}
657663

658664
*comment = '\0'; // Do not generate grbl.on_gcode_comment event!

0 commit comments

Comments
 (0)