From d480ccaf51d51a5df549570fd29faa7656a56acb Mon Sep 17 00:00:00 2001 From: skasti Date: Wed, 25 Sep 2024 06:31:34 +0200 Subject: [PATCH] Add ability to insert parameters in MSG comments --- gcode.c | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/gcode.c b/gcode.c index 0c79161..3cfb28e 100644 --- a/gcode.c +++ b/gcode.c @@ -595,13 +595,49 @@ char *gc_normalize_block (char *block, char **message) size_t len = s1 - comment - 4; if(message && *message == NULL && !strncmp(comment, "(MSG,", 5) && (*message = malloc(len))) { + float value; + char *s3; + uint_fast8_t char_counter = 0; + + len = 0; comment += 5; + // Trim leading spaces - while(*comment == ' ') { + while(*comment == ' ') comment++; - len--; + + // Calculate length of substituted string + while((c = comment[char_counter++])) { + if(c == '#') { + char_counter--; + if(read_parameter(comment, &char_counter, &value) == Status_OK) + len += strlen(trim_float(ftoa(value, ngc_float_decimals()))); + else + len += 3; // "N/A" + } else + len++; + } + + // Perform substitution + if((s3 = *message = malloc(len + 1))) { + + *s3 = '\0'; + char_counter = 0; + + while((c = comment[char_counter++])) { + if(c == '#') { + char_counter--; + if(read_parameter(comment, &char_counter, &value) == Status_OK) + strcat(s3, trim_float(ftoa(value, ngc_float_decimals()))); + else + strcat(s3, "N/A"); + s3 = strchr(s3, '\0'); + } else { + *s3++ = c; + *s3 = '\0'; + } + } } - memcpy(*message, comment, len); } #if NGC_EXPRESSIONS_ENABLE