Display print for auto integers

This commit is contained in:
Patrick Haßel 2024-12-29 09:29:10 +01:00
parent 8d99d033fe
commit 93a0ee8a86
2 changed files with 47 additions and 28 deletions

View File

@ -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;

View File

@ -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);
}
};