From 51bae4e7f37e1b18ef261fc5d29908db9838cb78 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Wed, 29 Mar 2023 16:01:03 +0200 Subject: [PATCH] Removed again lots of arduino bullshit --- src/widgets/vumeter.cpp | 77 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/src/widgets/vumeter.cpp b/src/widgets/vumeter.cpp index 6b2c511..739f0d5 100644 --- a/src/widgets/vumeter.cpp +++ b/src/widgets/vumeter.cpp @@ -2,10 +2,10 @@ // system includes #include +#include // 3rdparty lib includes #include -#include #include // local includes @@ -104,6 +104,81 @@ void VuMeter::start(TftInterface &tft) tft.drawRect(5, 3, 230, 119, TFT_BLACK); // Draw bezel line } +namespace { +char * dtostrf(double number, signed char width, unsigned char prec, char *s) { + bool negative = false; + + if (std::isnan(number)) { + std::strcpy(s, "nan"); + return s; + } + if (std::isinf(number)) { + std::strcpy(s, "inf"); + return s; + } + + char* out = s; + + int fillme = width; // how many cells to fill for the integer part + if (prec > 0) { + fillme -= (prec+1); + } + + // Handle negative numbers + if (number < 0.0) { + negative = true; + fillme--; + number = -number; + } + + // Round correctly so that print(1.999, 2) prints as "2.00" + // I optimized out most of the divisions + double rounding = 2.0; + for (uint8_t i = 0; i < prec; ++i) + rounding *= 10.0; + rounding = 1.0 / rounding; + + number += rounding; + + // Figure out how big our number really is + double tenpow = 1.0; + int digitcount = 1; + while (number >= 10.0 * tenpow) { + tenpow *= 10.0; + digitcount++; + } + + number /= tenpow; + fillme -= digitcount; + + // Pad unused cells with spaces + while (fillme-- > 0) { + *out++ = ' '; + } + + // Handle negative sign + if (negative) *out++ = '-'; + + // Print the digits, and if necessary, the decimal point + digitcount += prec; + int8_t digit = 0; + while (digitcount-- > 0) { + digit = (int8_t)number; + if (digit > 9) digit = 9; // insurance + *out++ = (char)('0' | digit); + if ((digitcount == prec) && (prec > 0)) { + *out++ = '.'; + } + number -= digit; + number *= 10.0; + } + + // make sure the string is terminated + *out = 0; + return s; +} +} + void VuMeter::redraw(TftInterface &tft, float value) { FontRenderer fontRenderer{tft};