diff --git a/src/display/Display.h b/src/display/Display.h index 2b4cb1f..24474c9 100644 --- a/src/display/Display.h +++ b/src/display/Display.h @@ -90,6 +90,51 @@ public: memset(buffer, 0, pixelByteCount); } + enum ALIGN { + LEFT, RIGHT + }; + + uint8_t print(int x, int y, double valueDbl, Color colorPositive, Color colorZero, Color colorNegative, ALIGN align = LEFT) { + const Color color = valueDbl == 0 ? colorZero : (valueDbl < 0 ? colorNegative : colorPositive); + return print(x, y, valueDbl, color, align); + } + + uint8_t print(int x, int y, double valueDbl, Color color, ALIGN align = LEFT) { + if (isnan(valueDbl)) { + x -= 3 * (DISPLAY_CHAR_WIDTH + 1) - 1; + x += print(x, y, SYMBOL_DASH, color, true) + 1; + x += print(x, y, SYMBOL_DASH, color, true) + 1; + x += print(x, y, SYMBOL_DASH, color, true) + 1; + return x; + } + + const int value = (int) round(abs(valueDbl)); + const bool negative = valueDbl < 0; + + const int digitCount = (int) max(1.0, ceil(log10(value))); // log10 is -inf for value==0, hence the max(1.0, ...) + if (align == RIGHT) { + x -= ((negative ? 1 : 0) + digitCount) * (DISPLAY_CHAR_WIDTH + 1) - 1; + } + + int divider = (int) pow(10, digitCount - 1); + if (negative) { + x += print(x, y, SYMBOL_DASH, color, true) + 1; + } + + bool showIfZero = false; +// Serial.printf("x=%d, y=%d, value=%d, align=%s, digitCount=%d, divider=%d\n", x, y, value, align == LEFT ? "LEFT" : "RIGHT", digitCount, divider); + for (int digitPos = 0; digitPos < digitCount; ++digitPos) { + const int digitVal = value / divider % 10; + showIfZero |= digitVal != 0 || (digitPos == digitCount - 1); + x += print(x, y, digitVal, color, showIfZero) + 1; +// Serial.printf(" digitPos=%d, x=%d, y=%d, digitVal=%d, showIfZero=%s, divider=%d\n", digitPos, x, y, digitVal, showIfZero ? "true" : "false", divider); + divider /= 10; + } +// Serial.println(); + + return x; + } + uint8_t print(uint8_t xPos, uint8_t yPos, uint8_t index, Color color, bool showIfZero) { if (index == 0 && !showIfZero) { return DISPLAY_CHAR_WIDTH; diff --git a/src/mode/Electricity/Electricity.h b/src/mode/Electricity/Electricity.h index 254f369..793642c 100644 --- a/src/mode/Electricity/Electricity.h +++ b/src/mode/Electricity/Electricity.h @@ -30,34 +30,8 @@ protected: void draw(Display &display) override { display.clear(); - uint8_t x = 0; - - int pv = (int) round(getPhotovoltaicPowerW()); - int pv100 = (pv / 100) % 10; - int pv10 = (pv / 10) % 10; - int pv1 = pv % 10; - Serial.printf("pv100=%d, pv10=%d, pv1=%d, pv=%f\n", pv100, pv10, pv1, getPhotovoltaicPowerW()); - x += display.print(x, 1, isnan(pv) ? SYMBOL_DASH : pv100, GREEN, pv >= 100) + 1; - x += display.print(x, 1, isnan(pv) ? SYMBOL_DASH : pv10, GREEN, pv >= 10) + 1; - x += display.print(x, 1, isnan(pv) ? SYMBOL_DASH : pv1, GREEN, true) + 1; - - x += 5; - - int grid = (int) round(getGridPowerW()); - Color color = ORANGE; - if (grid < 0) { - color = MAGENTA; - grid = -grid; - } - int grid1000 = (grid / 1000) % 10; - int grid100 = (grid / 100) % 10; - int grid10 = (grid / 10) % 10; - int grid1 = grid % 10; - Serial.printf("grid100=%d, grid10=%d, grid1=%d, grid=%f\n", grid100, grid10, grid1, getGridPowerW()); - x += display.print(x, 1, isnan(grid) ? SYMBOL_DASH : grid1000, color, grid >= 1000) + 1; - x += display.print(x, 1, isnan(grid) ? SYMBOL_DASH : grid100, color, grid >= 100) + 1; - x += display.print(x, 1, isnan(grid) ? SYMBOL_DASH : grid10, color, grid >= 10) + 1; - x += display.print(x, 1, isnan(grid) ? SYMBOL_DASH : grid1, color, true) + 1; + display.print((DISPLAY_CHAR_WIDTH + 1) * 3 - 1, 0, getPhotovoltaicPowerW(), GREEN, Color(), Color(), Display::RIGHT); + display.print(width, 0, getGridPowerW(), ORANGE, WHITE, MAGENTA, Display::RIGHT); } };