Skip to content

Commit

Permalink
interpeter: allow comments that print params, to set the float format
Browse files Browse the repository at this point in the history
printting out number to users often uses excessive decimals.
now &d,%f or %.xf (x being a number) is interpeted to be a float format
for params.

-add test for printing formatted params
-add docs on formatting of parameter numbers in comments
  • Loading branch information
c-morley committed Jul 27, 2022
1 parent 4eee9ab commit 2698cc7
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 2 deletions.
20 changes: 20 additions & 0 deletions docs/src/gcode/overview.adoc
Expand Up @@ -1075,6 +1075,26 @@ are replaced by the value of the named parameter. Named parameters
will have white space removed from them. So, '\#<named parameter>'
will be converted to '#<namedparameter>'.

Parameter numbers can be formatted. eg:

----
(DEBUG, value = %d#<some_value>)
----
will print the value rounded to an integer.

* %lf is default if there is no formatting string
* %d = 0 decimals
* %f = four decimals
* %.xf = x (0-9) number of decimals

The formatting will be preformed on all parameters in the same line unless
changed. ie. multiple formatting is allowed in one line.

The formatting string does not need to be right beside the parameter.

If the formatting string is created with the wrong pattern it will be
printed as characters.

[[gcode:file-requirements]]
== File Requirements(((File Requirements)))

Expand Down
63 changes: 61 additions & 2 deletions src/emc/rs274ngc/interp_convert.cc
Expand Up @@ -1277,6 +1277,7 @@ int Interp::convert_param_comment(char *comment, char *expanded, int len)
FORCE_LC_NUMERIC_C;
int i;
char param[LINELEN+1];
char format[5] = "%lf";
int paramNumber;
int stat;
double value;
Expand All @@ -1286,7 +1287,65 @@ int Interp::convert_param_comment(char *comment, char *expanded, int len)

while(*comment)
{
if(*comment == '#')

if(*comment == '%')
{
// skip over the '%'
comment++;

// convenient integer looking
if(*comment == 'd')
{
comment++;
strcpy(format, "%.0f");
}
// convenient 4 position float
else if(*comment == 'f')
{
comment++;
strcpy(format, "%.4f");
}
// arbitrary 0-9 position float
else if(*comment == '.')
{
comment++;
if(isdigit(*comment))
{
// forward to the (hopefully) letter f
comment++;
if(*comment == 'f')
{
// back up to get the digit into format
comment--;
format[0] = '%';
format[1] = '.';
format[2] = *comment;
format[3] = 'f';
format[4] = 0;
comment++;
comment++;
}
else
{
// not a format string so,
// back up to the digit to continue processing
comment--;
*expanded++ = '.';

}
}
else
{
*expanded++ = '.';
}

}
else
{
*expanded++ = '%';
}
}
else if(*comment == '#')
{
found = 0;
logDebug("a parameter");
Expand Down Expand Up @@ -1378,7 +1437,7 @@ int Interp::convert_param_comment(char *comment, char *expanded, int len)
{
// avoid -0.0/0.0 issues
double pvalue = equal(value, 0.0) ? 0.0 : value;
int n = snprintf(valbuf, VAL_LEN, "%lf", pvalue);
int n = snprintf(valbuf, VAL_LEN, format, pvalue);
bool fail = (n >= VAL_LEN || n < 0);
if(fail)
rtapi_strxcpy(valbuf, "######");
Expand Down
23 changes: 23 additions & 0 deletions tests/interp/magic_comments/param_format_printing/expected
@@ -0,0 +1,23 @@
N..... USE_LENGTH_UNITS(CANON_UNITS_MM)
N..... SET_G5X_OFFSET(1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000)
N..... SET_G92_OFFSET(0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000)
N..... SET_XY_ROTATION(0.0000)
N..... SET_FEED_REFERENCE(CANON_XYZ)
N..... ON_RESET()
N..... MESSAGE(" native value = 1.123457")
N..... MESSAGE(" Test round to integer: 1")
N..... MESSAGE(" Test round to 4 decimals: 1.1235")
N..... MESSAGE(" Test format separate from param: The value is: 1.1235")
N..... MESSAGE(" Test round to 7 decimals: 1.1234568")
N..... MESSAGE(" native value2 = 2.345679")
N..... MESSAGE(" Test2 round all params in line to 4 decimals: The values are: 1.1235, 2.3457")
N..... MESSAGE(" Test2 only round last value to integer: The values are: 1.123457, 2")
N..... SET_G5X_OFFSET(1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000)
N..... SET_XY_ROTATION(0.0000)
N..... SET_FEED_MODE(0, 0)
N..... SET_FEED_RATE(0.0000)
N..... STOP_SPINDLE_TURNING(0)
N..... SET_SPINDLE_MODE(0 0.0000)
N..... PROGRAM_END()
N..... ON_RESET()
N..... ON_RESET()
13 changes: 13 additions & 0 deletions tests/interp/magic_comments/param_format_printing/test.ngc
@@ -0,0 +1,13 @@
#<value> = 1.123456789

(DEBUG, native value = #<value>)
(DEBUG, Test round to integer: %d#<value>)
(DEBUG, Test round to 4 decimals: %f#<value>)
(DEBUG, Test format separate from param: %f The value is: #<value>)
(DEBUG, Test round to 7 decimals: %.7f#<value>)

#<value2> = 2.3456789
(DEBUG, native value2 = #<value2>)
(DEBUG, Test2 round all params in line to 4 decimals: %f The values are: #<value>, #<value2>)
(DEBUG, Test2 only round last value to integer: The values are: #<value>, %d#<value2>)
M2
3 changes: 3 additions & 0 deletions tests/interp/magic_comments/param_format_printing/test.sh
@@ -0,0 +1,3 @@
#!/bin/bash
rs274 -g test.ngc | awk '{$1=""; print}'
exit ${PIPESTATUS[0]}

0 comments on commit 2698cc7

Please sign in to comment.