Skip to content

Commit

Permalink
minify print lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Paciente8159 committed Sep 22, 2024
1 parent 7a39269 commit 12907ce
Showing 1 changed file with 33 additions and 16 deletions.
49 changes: 33 additions & 16 deletions uCNC/src/interface/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
#include "print.h"
#include "../cnc.h"

#define PRINT_ENABLE_MINIMAL
#ifdef PRINT_ENABLE_MINIMAL
#define PRINT_DISABLE_FMT_PADDING
#define PRINT_DISABLE_FMT_HEX
#define PRINT_DISABLE_FMT_IP
#define PRINT_ENABLE_FMT_FLOAT_FONLY
#endif

#define HEX_NONE 0
#define HEX_PREFIX 128
#define HEX_UPPER 64
Expand Down Expand Up @@ -63,6 +71,7 @@ static void print_romstr(print_putc_cb cb, char **buffer_ref, const char *__s)
}
}

#ifndef PRINT_DISABLE_FMT_HEX
static void print_byte(print_putc_cb cb, char **buffer_ref, const uint8_t *data, uint8_t flags)
{
bool prefix = (flags && HEX_PREFIX);
Expand All @@ -85,6 +94,7 @@ static void print_byte(print_putc_cb cb, char **buffer_ref, const uint8_t *data,
data++;
} while (--size);
}
#endif

static void print_int(print_putc_cb cb, char **buffer_ref, int32_t num)
{
Expand Down Expand Up @@ -159,6 +169,7 @@ void print_flt(print_putc_cb cb, char **buffer_ref, float num)
print_int(cb, buffer_ref, digits);
}

#ifndef PRINT_DISABLE_FMT_IP
void print_ip(print_putc_cb cb, char **buffer_ref, uint32_t ip)
{
uint8_t *ptr = (uint8_t *)&ip;
Expand All @@ -170,6 +181,7 @@ void print_ip(print_putc_cb cb, char **buffer_ref, uint32_t ip)
print_putc(cb, buffer_ref, '.');
print_int(cb, buffer_ref, (int32_t)ptr[0]);
}
#endif

static char itof_getc_dummy(bool peek)
{
Expand Down Expand Up @@ -203,6 +215,7 @@ void print_fmtva(print_putc_cb cb, char *buffer, const char *fmt, va_list *args)
c = printf_getc(fmt);
switch (c)
{
#ifndef PRINT_DISABLE_FMT_PADDING
case '#':
hexflags = HEX_PREFIX;
__FALL_THROUGH__
Expand All @@ -217,11 +230,11 @@ void print_fmtva(print_putc_cb cb, char *buffer, const char *fmt, va_list *args)
fmt++;
}
__FALL_THROUGH__
case '.':
fmt++;
__FALL_THROUGH__
#endif
default:
if (c == '.')
{
fmt++;
}
if (c == '.' || (c >= '1' && c <= '9'))
{
if (print_itof(itof_getc_dummy, (const char **)&fmt, &f))
Expand All @@ -243,16 +256,6 @@ void print_fmtva(print_putc_cb cb, char *buffer, const char *fmt, va_list *args)
fmt++;
}

while (c == 'l' || c == 'h')
{
if (c == 'l')
{
lcount++;
}
c = printf_getc(fmt);
fmt++;
}

switch (c)
{
case 'c':
Expand All @@ -268,16 +271,20 @@ void print_fmtva(print_putc_cb cb, char *buffer, const char *fmt, va_list *args)
s = (const char *)va_arg(*args, const char *);
print_romstr(cb, buffer_ref, s);
break;
#ifndef PRINT_DISABLE_FMT_IP
case 'M':
lcount = 4;
__FALL_THROUGH__
#endif
#ifndef PRINT_DISABLE_FMT_HEX
case 'X':
hexflags |= HEX_UPPER;
__FALL_THROUGH__
case 'x':
#endif
case 'd':
case 'i':
case 'u':
case 'x':
if (elems)
{
pt = va_arg(*args, void *);
Expand Down Expand Up @@ -308,13 +315,17 @@ void print_fmtva(print_putc_cb cb, char *buffer, const char *fmt, va_list *args)
case 'u':
print_int(cb, buffer_ref, ABS(i));
break;
#ifndef PRINT_DISABLE_FMT_HEX
case 'x':
case 'X':
print_byte(cb, buffer_ref, (const uint8_t *)&i, (hexflags | lcount));
break;
#endif
#ifndef PRINT_DISABLE_FMT_IP
case 'M':
print_ip(cb, buffer_ref, i);
break;
#endif
}
if (elems && --elems)
{
Expand All @@ -324,16 +335,20 @@ void print_fmtva(print_putc_cb cb, char *buffer, const char *fmt, va_list *args)
} while (elems);
/* code */
break;
#ifndef PRINT_DISABLE_FMT_HEX
case 'A':
hexflags |= HEX_UPPER;
__FALL_THROUGH__
case 'a':
#endif
case 'f':
case 'F':
#ifndef PRINT_ENABLE_FMT_FLOAT_FONLY
case 'e':
case 'E':
case 'a':
case 'g':
case 'G':
#endif
if (elems)
{
f_ptr = va_arg(*args, float *);
Expand All @@ -347,10 +362,12 @@ void print_fmtva(print_putc_cb cb, char *buffer, const char *fmt, va_list *args)
{
switch (c)
{
#ifndef PRINT_DISABLE_FMT_HEX
case 'a':
case 'A':
print_byte(cb, buffer_ref, (const uint8_t *)f_ptr, (hexflags | VAR_DWORD));
break;
#endif
default:
print_flt(cb, buffer_ref, *f_ptr++);
break;
Expand Down

0 comments on commit 12907ce

Please sign in to comment.