Skip to content

Commit c07d631

Browse files
committed
Moved message substitution code from gcode.c to ngc_expr.c.
1 parent 236a40c commit c07d631

File tree

5 files changed

+173
-138
lines changed

5 files changed

+173
-138
lines changed

changelog.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
## grblHAL changelog
22

3+
<a name="20241019">Build 20241019
4+
5+
Core:
6+
7+
* Moved message substitution code from _gcode.c_ to _ngc_expr.c_.
8+
9+
Drivers:
10+
11+
* ESP32: fixed tests for ESP32-S3 conditional code, added overridable symbols for UART0 RX/TX pins.
12+
13+
* STM32F4xx: fixed regression in spindle sync code. Ref. [discussion #612](https://github.com/grblHAL/core/discussions/612).
14+
15+
Plugins:
16+
17+
* Spindle: fixed name typo. Ref. [issue #34](https://github.com/grblHAL/Plugins_spindle/issues/34).
18+
19+
---
20+
321
<a name="20241016">Build 20241016
422

523
Core:
@@ -10,7 +28,7 @@ Core:
1028

1129
Plugins:
1230

13-
* Spindle: Fixed compiler warning. Ref. [issue #33](https://github.com/grblHAL/Plugins_spindle/issues/33)
31+
* Spindle: fixed compiler warning. Ref. [issue #33](https://github.com/grblHAL/Plugins_spindle/issues/33).
1432

1533
---
1634

gcode.c

+20-136
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,16 @@
3030
#include "protocol.h"
3131
#include "state_machine.h"
3232

33+
#if NGC_PARAMETERS_ENABLE
34+
#include "ngc_params.h"
35+
#endif
36+
3337
#if NGC_EXPRESSIONS_ENABLE
3438
#include "ngc_expr.h"
35-
#include "ngc_params.h"
3639
#include "ngc_flowctrl.h"
40+
#ifndef NGC_N_ASSIGN_PARAMETERS_PER_BLOCK
41+
#define NGC_N_ASSIGN_PARAMETERS_PER_BLOCK 10
42+
#endif
3743
#endif
3844

3945
// NOTE: Max line number is defined by the g-code standard to be 99999. It seems to be an
@@ -497,134 +503,6 @@ void gc_output_message (char *message)
497503
}
498504
}
499505

500-
#if NGC_EXPRESSIONS_ENABLE
501-
502-
#define NGC_N_ASSIGN_PARAMETERS_PER_BLOCK 10
503-
504-
static ngc_param_t ngc_params[NGC_N_ASSIGN_PARAMETERS_PER_BLOCK];
505-
506-
static int8_t get_format (char c, int8_t pos, uint8_t *decimals)
507-
{
508-
static uint8_t d;
509-
510-
// lcaps c?
511-
512-
switch(pos) {
513-
514-
case 1:
515-
516-
switch(c) {
517-
518-
case 'd':
519-
*decimals = 0;
520-
pos = -2;
521-
break;
522-
523-
case 'f':
524-
*decimals = ngc_float_decimals();
525-
pos = -2;
526-
break;
527-
528-
case '.':
529-
pos = 2;
530-
break;
531-
532-
default:
533-
pos = 0;
534-
break;
535-
}
536-
break;
537-
538-
case 2:
539-
if(c >= '0' && c <= '9') {
540-
d = c - '0';
541-
pos = 3;
542-
} else
543-
pos = 0;
544-
break;
545-
546-
default:
547-
if(c == 'f') {
548-
*decimals = d;
549-
pos = -4;
550-
} else
551-
pos = 0;
552-
break;
553-
}
554-
555-
return pos;
556-
}
557-
558-
static void substitute_parameters (char *comment, char **message)
559-
{
560-
size_t len = 0;
561-
float value;
562-
char *s, c;
563-
uint_fast8_t char_counter = 0;
564-
int8_t parse_format = 0;
565-
uint8_t decimals = ngc_float_decimals(); // LinuxCNC is 1 (or l?)
566-
567-
// Trim leading spaces
568-
while(*comment == ' ')
569-
comment++;
570-
571-
// Calculate length of substituted string
572-
while((c = comment[char_counter++])) {
573-
if(parse_format) {
574-
if((parse_format = get_format(c, parse_format, &decimals)) < 0) {
575-
len -= parse_format;
576-
parse_format = 0;
577-
}
578-
} else if(c == '%')
579-
parse_format = 1;
580-
else if(c == '#') {
581-
char_counter--;
582-
if(ngc_read_parameter(comment, &char_counter, &value, true) == Status_OK)
583-
len += strlen(decimals ? ftoa(value, decimals) : trim_float(ftoa(value, decimals)));
584-
else
585-
len += 3; // "N/A"
586-
} else
587-
len++;
588-
}
589-
590-
// Perform substitution
591-
if((s = *message = malloc(len + 1))) {
592-
593-
char fmt[5] = {0};
594-
595-
*s = '\0';
596-
char_counter = 0;
597-
598-
while((c = comment[char_counter++])) {
599-
if(parse_format) {
600-
fmt[parse_format] = c;
601-
if((parse_format = get_format(c, parse_format, &decimals)) < 0)
602-
parse_format = 0;
603-
else if(parse_format == 0) {
604-
strcat(s, fmt);
605-
s = strchr(s, '\0');
606-
continue;
607-
}
608-
} else if(c == '%') {
609-
parse_format = 1;
610-
fmt[0] = c;
611-
} else if(c == '#') {
612-
char_counter--;
613-
if(ngc_read_parameter(comment, &char_counter, &value, true) == Status_OK)
614-
strcat(s, decimals ? ftoa(value, decimals) : trim_float(ftoa(value, decimals)));
615-
else
616-
strcat(s, "N/A");
617-
s = strchr(s, '\0');
618-
} else {
619-
*s++ = c;
620-
*s = '\0';
621-
}
622-
}
623-
}
624-
}
625-
626-
#endif // NGC_EXPRESSIONS_ENABLE
627-
628506
#if NGC_PARAMETERS_ENABLE
629507

630508
static parameter_words_t g65_words = {0};
@@ -722,16 +600,16 @@ char *gc_normalize_block (char *block, char **message)
722600
if(!strncmp(comment, "(DEBUG,", 7)) { // Debug message string substitution
723601
if(settings.flags.ngc_debug_out) {
724602
comment += 7;
725-
substitute_parameters(comment, message);
603+
ngc_substitute_parameters(comment, message);
726604
}
727605
*comment = '\0'; // Do not generate grbl.on_gcode_comment event!
728606
} else if(!strncmp(comment, "(PRINT,", 7)) { // Print message string substitution
729607
comment += 7;
730-
substitute_parameters(comment, message);
608+
ngc_substitute_parameters(comment, message);
731609
*comment = '\0'; // Do not generate grbl.on_gcode_comment event!
732610
} else if(!strncmp(comment, "(MSG,", 5)) {
733611
comment += 5;
734-
substitute_parameters(comment, message);
612+
ngc_substitute_parameters(comment, message);
735613
}
736614
}
737615
#else
@@ -844,6 +722,8 @@ status_code_t gc_execute_block (char *block)
844722
.o = On
845723
};
846724

725+
static ngc_param_t ngc_params[NGC_N_ASSIGN_PARAMETERS_PER_BLOCK];
726+
847727
uint_fast8_t ngc_param_count = 0;
848728

849729
// NOTE: this array has to match the parameter_words_t order!
@@ -1038,13 +918,17 @@ status_code_t gc_execute_block (char *block)
1038918
} else
1039919
FAIL(status);
1040920
} else if(block[char_counter] == '<') {
1041-
/* char o_label[NGC_MAX_PARAM_LENGTH + 1];
1042-
if((status = ngc_read_name(block, &char_counter, o_label)) != Status_OK)
921+
#if 0
922+
char o_slabel[NGC_MAX_PARAM_LENGTH + 1];
923+
if((status = ngc_read_name(block, &char_counter, o_slabel)) != Status_OK)
1043924
FAIL(status);
1044925
gc_block.words.o = On;
1045-
gc_block.values.o = 0xFFFFFFFE;
1046-
continue;*/
926+
if(gc_block.values.o = string_register_set_name(o_slabel) == 0)
927+
FAIL(Status_FlowControlOutOfMemory);
928+
continue;
929+
#else
1047930
FAIL(Status_GcodeUnsupportedCommand); // [For now...]
931+
#endif
1048932
}
1049933
}
1050934

grbl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
#else
4343
#define GRBL_VERSION "1.1f"
4444
#endif
45-
#define GRBL_BUILD 20241016
45+
#define GRBL_BUILD 20241019
4646

4747
#define GRBL_URL "https://github.com/grblHAL"
4848

ngc_expr.c

+131
Original file line numberDiff line numberDiff line change
@@ -881,4 +881,135 @@ status_code_t ngc_eval_expression (char *line, uint_fast8_t *pos, float *value)
881881
return Status_OK;
882882
}
883883

884+
/**/
885+
886+
static int8_t get_format (char c, int8_t pos, uint8_t *decimals)
887+
{
888+
static uint8_t d;
889+
890+
// lcaps c?
891+
892+
switch(pos) {
893+
894+
case 1:
895+
896+
switch(c) {
897+
898+
case 'd':
899+
*decimals = 0;
900+
pos = -2;
901+
break;
902+
903+
case 'f':
904+
*decimals = ngc_float_decimals();
905+
pos = -2;
906+
break;
907+
908+
case '.':
909+
pos = 2;
910+
break;
911+
912+
default:
913+
pos = 0;
914+
break;
915+
}
916+
break;
917+
918+
case 2:
919+
if(c >= '0' && c <= '9') {
920+
d = c - '0';
921+
pos = 3;
922+
} else
923+
pos = 0;
924+
break;
925+
926+
default:
927+
if(c == 'f') {
928+
*decimals = d;
929+
pos = -4;
930+
} else
931+
pos = 0;
932+
break;
933+
}
934+
935+
return pos;
936+
}
937+
938+
/*! \brief Substitute references to parameters and expressions in a string with their values.
939+
940+
_NOTE:_ The returned string must be freed by the caller.
941+
942+
\param comment pointer to the original comment string.
943+
\param message pointer to a char pointer to receive the resulting string.
944+
*/
945+
char *ngc_substitute_parameters (char *comment, char **message)
946+
{
947+
size_t len = 0;
948+
float value;
949+
char *s, c;
950+
uint_fast8_t char_counter = 0;
951+
int8_t parse_format = 0;
952+
uint8_t decimals = ngc_float_decimals(); // LinuxCNC is 1 (or l?)
953+
954+
// Trim leading spaces
955+
while(*comment == ' ')
956+
comment++;
957+
958+
// Calculate length of substituted string
959+
while((c = comment[char_counter++])) {
960+
if(parse_format) {
961+
if((parse_format = get_format(c, parse_format, &decimals)) < 0) {
962+
len -= parse_format;
963+
parse_format = 0;
964+
}
965+
} else if(c == '%')
966+
parse_format = 1;
967+
else if(c == '#') {
968+
char_counter--;
969+
if(ngc_read_parameter(comment, &char_counter, &value, true) == Status_OK)
970+
len += strlen(decimals ? ftoa(value, decimals) : trim_float(ftoa(value, decimals)));
971+
else
972+
len += 3; // "N/A"
973+
} else
974+
len++;
975+
}
976+
977+
// Perform substitution
978+
if((s = *message = malloc(len + 1))) {
979+
980+
char fmt[5] = {0};
981+
982+
*s = '\0';
983+
char_counter = 0;
984+
985+
while((c = comment[char_counter++])) {
986+
if(parse_format) {
987+
fmt[parse_format] = c;
988+
if((parse_format = get_format(c, parse_format, &decimals)) < 0)
989+
parse_format = 0;
990+
else if(parse_format == 0) {
991+
strcat(s, fmt);
992+
s = strchr(s, '\0');
993+
continue;
994+
}
995+
} else if(c == '%') {
996+
parse_format = 1;
997+
fmt[0] = c;
998+
} else if(c == '#') {
999+
char_counter--;
1000+
if(ngc_read_parameter(comment, &char_counter, &value, true) == Status_OK)
1001+
strcat(s, decimals ? ftoa(value, decimals) : trim_float(ftoa(value, decimals)));
1002+
else
1003+
strcat(s, "N/A");
1004+
s = strchr(s, '\0');
1005+
} else {
1006+
*s++ = c;
1007+
*s = '\0';
1008+
}
1009+
}
1010+
}
1011+
1012+
return *message;
1013+
}
1014+
8841015
#endif

ngc_expr.h

+2
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ status_code_t ngc_read_integer_value(char *line, uint_fast8_t *pos, int32_t *val
99
status_code_t ngc_read_integer_unsigned (char *line, uint_fast8_t *pos, uint32_t *value);
1010
status_code_t ngc_read_parameter (char *line, uint_fast8_t *pos, float *value, bool check);
1111
status_code_t ngc_eval_expression (char *line, uint_fast8_t *pos, float *value);
12+
/**/
13+
char *ngc_substitute_parameters (char *comment, char **message);
1214

1315
#endif

0 commit comments

Comments
 (0)