From 32be4065efeafe781e007ca6ff47bd5a3fc22184 Mon Sep 17 00:00:00 2001 From: Scott Lahteine <thinkyhead@users.noreply.github.com> Date: Wed, 31 May 2023 16:47:05 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=91=E2=80=8D=F0=9F=92=BB=20numtostr=20?= =?UTF-8?q?use=20functions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Marlin/src/libs/numtostr.cpp | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/Marlin/src/libs/numtostr.cpp b/Marlin/src/libs/numtostr.cpp index 2938229a7ae..c34faf68ff7 100644 --- a/Marlin/src/libs/numtostr.cpp +++ b/Marlin/src/libs/numtostr.cpp @@ -25,14 +25,25 @@ #include "../inc/MarlinConfigPre.h" #include "../core/utility.h" -char conv[9] = { 0 }; +constexpr char DIGIT(const uint8_t n) { return '0' + n; } -#define DIGIT(n) ('0' + (n)) -#define DIGIMOD(n, f) DIGIT(((n)/(f)) % 10) -#define RJDIGIT(n, f) ((n) >= (f) ? DIGIMOD(n, f) : ' ') -#define MINUSOR(n, alt) (n >= 0 ? (alt) : (n = -n, '-')) -#define INTFLOAT(V,N) (((V) * 10 * pow(10, N) + ((V) < 0 ? -5: 5)) / 10) // pow10? -#define UINTFLOAT(V,N) INTFLOAT((V) < 0 ? -(V) : (V), N) +template <typename T1, typename T2> +constexpr char DIGIMOD(const T1 n, const T2 f) { return DIGIT((n / f) % 10); } + +template <typename T1, typename T2> +constexpr char RJDIGIT(const T1 n, const T2 f) { return (n >= f ? DIGIMOD(n, f) : ' '); } + +template <typename T> +constexpr char MINUSOR(T &n, const char alt) { return (n >= 0) ? alt : (n = -n) ? '-' : '-'; } + +constexpr long INTFLOAT(const float V, const int N) { + return long((V * 10.0f * pow(10.0f, N) + (V < 0.0f ? -5.0f : 5.0f)) / 10.0f); +} +constexpr long UINTFLOAT(const float V, const int N) { + return INTFLOAT(V < 0.0f ? -V : V, N); +} + +char conv[9] = { 0 }; // Format uint8_t (0-100) as rj string with 123% / _12% / __1% format const char* pcttostrpctrj(const uint8_t i) {