Skip to content

Commit d480cca

Browse files
committed
Add ability to insert parameters in MSG comments
1 parent 3ef6763 commit d480cca

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

gcode.c

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -595,13 +595,49 @@ char *gc_normalize_block (char *block, char **message)
595595
size_t len = s1 - comment - 4;
596596

597597
if(message && *message == NULL && !strncmp(comment, "(MSG,", 5) && (*message = malloc(len))) {
598+
float value;
599+
char *s3;
600+
uint_fast8_t char_counter = 0;
601+
602+
len = 0;
598603
comment += 5;
604+
599605
// Trim leading spaces
600-
while(*comment == ' ') {
606+
while(*comment == ' ')
601607
comment++;
602-
len--;
608+
609+
// Calculate length of substituted string
610+
while((c = comment[char_counter++])) {
611+
if(c == '#') {
612+
char_counter--;
613+
if(read_parameter(comment, &char_counter, &value) == Status_OK)
614+
len += strlen(trim_float(ftoa(value, ngc_float_decimals())));
615+
else
616+
len += 3; // "N/A"
617+
} else
618+
len++;
619+
}
620+
621+
// Perform substitution
622+
if((s3 = *message = malloc(len + 1))) {
623+
624+
*s3 = '\0';
625+
char_counter = 0;
626+
627+
while((c = comment[char_counter++])) {
628+
if(c == '#') {
629+
char_counter--;
630+
if(read_parameter(comment, &char_counter, &value) == Status_OK)
631+
strcat(s3, trim_float(ftoa(value, ngc_float_decimals())));
632+
else
633+
strcat(s3, "N/A");
634+
s3 = strchr(s3, '\0');
635+
} else {
636+
*s3++ = c;
637+
*s3 = '\0';
638+
}
639+
}
603640
}
604-
memcpy(*message, comment, len);
605641
}
606642

607643
#if NGC_EXPRESSIONS_ENABLE

0 commit comments

Comments
 (0)