Skip to content

Commit

Permalink
printf fixes
Browse files Browse the repository at this point in the history
- fixes printf custom function
- resets parameters after each formated arg
- fixes byte printing
  • Loading branch information
Paciente8159 committed Nov 13, 2024
1 parent 3282b6b commit 91a2942
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
Binary file modified docs/mcumap_gen.xlsx
Binary file not shown.
47 changes: 28 additions & 19 deletions uCNC/src/interface/grbl_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ size_t prt_byte(void *out, size_t maxlen, const uint8_t *data, uint8_t flags)
maxlen = prt_putc(out, maxlen, '0');
maxlen = prt_putc(out, maxlen, 'x');
}
for (uint8_t i = 0; i < size; i++)
for (uint8_t i = size; i !=0;)
{
i--;
uint8_t up = data[i] >> 4;
uint8_t c = (up > 9) ? (hexchar + up - 10) : ('0' + up);
maxlen = prt_putc(out, maxlen, c);
Expand Down Expand Up @@ -189,32 +190,34 @@ size_t prt_ip(void *out, size_t maxlen, uint32_t ip)

size_t prt_fmtva(void *out, size_t maxlen, const char *fmt, va_list *args)
{
char c = 0, cval = 0;
uint8_t lcount = 2;
#ifndef PRINT_FTM_MINIMAL
const char *s;
bool hexflags = HEX_NONE;
void *pt = NULL;
#endif
int32_t li = 0;
float f, *f_ptr = NULL;

char *ptr = (char *)out;
char **memref;
if (maxlen != PRINT_CALLBACK)
{
memref = &ptr;
out = memref;
}
uint8_t elems = 0;

char c = 0;

do
{
char cval = 0;
uint8_t lcount = 2;
#ifndef PRINT_FTM_MINIMAL
const char *s;
uint8_t hexflags = HEX_NONE;
void *pt = NULL;
#endif
int32_t li = 0;
float f, *f_ptr = NULL;
uint8_t elems = 0;
#ifndef PRINTF_FTM_CUSTOM_PRECISION
uint8_t precision = (!g_settings.report_inches) ? 3 : 5;
uint8_t precision = (!g_settings.report_inches) ? 3 : 5;
#else
uint8_t precision = PRINTF_FTM_CUSTOM_PRECISION;
uint8_t precision = PRINTF_FTM_CUSTOM_PRECISION;
#endif

do
{
c = prtf_getc(fmt++);
if (c == '%')
{
Expand All @@ -223,7 +226,8 @@ size_t prt_fmtva(void *out, size_t maxlen, const char *fmt, va_list *args)
{
#ifndef PRINT_FTM_MINIMAL
case '#':
hexflags = HEX_PREFIX;
hexflags |= HEX_PREFIX;
c = prtf_getc(fmt++);
__FALL_THROUGH__
case '0':
// ignores zero padding
Expand All @@ -235,7 +239,7 @@ size_t prt_fmtva(void *out, size_t maxlen, const char *fmt, va_list *args)
if (c == '.' || (c >= '1' && c <= '9'))
{
fmt--;
cval = prt_atof((void*)str_read_romchar, (const char **)&fmt, &f);
cval = prt_atof((void *)str_read_romchar, (const char **)&fmt, &f);
if (cval != ATOF_NUMBER_UNDEF)
{
elems = (uint8_t)f;
Expand Down Expand Up @@ -311,6 +315,7 @@ size_t prt_fmtva(void *out, size_t maxlen, const char *fmt, va_list *args)
break;
}
#ifndef PRINT_FTM_MINIMAL
pt = &li;
elems = 1;
}
do
Expand All @@ -319,7 +324,11 @@ size_t prt_fmtva(void *out, size_t maxlen, const char *fmt, va_list *args)
{
case 'x':
case 'X':
#ifndef PRINT_FTM_MINIMAL
maxlen = prt_byte(out, maxlen, (const uint8_t *)pt, (hexflags | lcount));
#else
maxlen = prt_byte(out, maxlen, (const uint8_t *)&li, (hexflags | lcount));
#endif
break;
case 'I':
maxlen = prt_ip(out, maxlen, li);
Expand All @@ -340,7 +349,7 @@ size_t prt_fmtva(void *out, size_t maxlen, const char *fmt, va_list *args)
{
maxlen = prt_putc(out, maxlen, ',');
}
pt += (1 << lcount);
pt += (1 << (lcount - 1));
} while (elems);
#endif
/* code */
Expand Down
4 changes: 2 additions & 2 deletions uCNC/src/interface/grbl_protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ WEAK_EVENT_HANDLER(proto_gcode_modes)
* all other implementations can use the formated print helper
*/

#define proto_itoa(value) prt_int((void*)proto_putc, PRINT_CALLBACK, (uint32_t)(value), 0)
#define proto_ftoa(value) prt_flt((void*)proto_putc, PRINT_CALLBACK, (float)(value), ((!g_settings.report_inches) ? 3 : 5))
// #define proto_itoa(value) prt_int((void*)proto_putc, PRINT_CALLBACK, (uint32_t)(value), 0)
// #define proto_ftoa(value) prt_flt((void*)proto_putc, PRINT_CALLBACK, (float)(value), ((!g_settings.report_inches) ? 3 : 5))

void proto_puts(const char *str)
{
Expand Down
2 changes: 2 additions & 0 deletions uCNC/src/interface/grbl_protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ extern "C"
void proto_feedback_fmt(const char *fmt, ...);
#define proto_feedback(__s) proto_print(MSG_FEEDBACK_START __s MSG_FEEDBACK_END)
#define proto_info(__s, ...) proto_feedback_fmt(__romstr__(__s), ##__VA_ARGS__)
#define proto_itoa(value) prt_int((void*)proto_putc, PRINT_CALLBACK, (uint32_t)(value), 0)
#define proto_ftoa(value) prt_flt((void*)proto_putc, PRINT_CALLBACK, (float)(value), ((!g_settings.report_inches) ? 3 : 5))
void proto_probe_result(uint8_t val);
void proto_gcode_coordsys(void);
void proto_gcode_modes(void);
Expand Down

0 comments on commit 91a2942

Please sign in to comment.