v.0.0.9 drawHorizontalScrollText

This commit is contained in:
tiijay
2025-11-06 12:11:32 +01:00
parent 7bdd67f3c3
commit f0614c9a4a
4 changed files with 81 additions and 12 deletions

View File

@@ -31,7 +31,7 @@ uint8_t LedMatrix::charWidth(const uint8_t *charMatrix)
// bei 8 -> 32-28 = 4.Bit
// bei 2 -> 32-20 = 2.Bit
Serial.printf("max_val: [0x%04X] -> %d\n\r", max_val, (32 - __builtin_clz(max_val)));
// Serial.printf("max_val: [0x%04X] -> %d\n\r", max_val, (32 - __builtin_clz(max_val)));
return (max_val == 0) ? 0 : (32 - __builtin_clz(max_val));
}
@@ -88,3 +88,60 @@ void LedMatrix::drawText(int x, int y, const char *text, CRGB color)
cursorX += (chr_width + 1); // 5 pixels width + 1 pixel spacing
}
}
uint8_t LedMatrix::getTextWidth(const char *text)
{
uint8_t totalWidth = 0;
for (uint8_t i = 0; text[i] != '\0'; i++)
{
const uint8_t *fontData = getFontChar_5x7(text[i]);
totalWidth += charWidth<7>(fontData) + 1; // char width + spacing
}
return totalWidth;
}
void LedMatrix::clearRow(uint8_t y)
{
const u_int8_t font_height = fontHeight_5x7();
Serial.printf("clearRow: %dx%d@%d\n\r", font_height, width, y);
for (uint8_t line = y; line < (y + font_height); line++)
{
Serial.printf("\tline: %d\n\r", line);
for (uint8_t x = 0; x < width; x++)
{
drawPixel(x, line, CRGB::Black);
}
}
}
void LedMatrix::drawHorizontalScrollText(uint8_t y, const char *text, CRGB color, uint8_t scrollSpeed)
{
unsigned long currentTime = millis();
uint8_t txtWidth = getTextWidth(text);
// Update scroll position based on time
if (currentTime - lastScrollTime > scrollSpeed)
{
scrollPosition--;
lastScrollTime = currentTime;
// Reset when text has completely scrolled off screen
if (scrollPosition < -txtWidth)
{
scrollPosition = width;
}
}
// Clear the row where text will be drawn
clearRow(y);
// Draw text at current scroll position
drawText(scrollPosition, y, text, color);
show();
// Serial.printf("drawHorizontalScrollText: %s @[%d; %d]", text, scrollPosition, y);
// Serial.println();
}

View File

@@ -24,6 +24,12 @@ private:
template <size_t N>
uint8_t charWidth(const uint8_t *charMatrix);
// Text Scrolling
int8_t scrollPosition = 0;
unsigned long lastScrollTime = 0;
uint8_t getTextWidth(const char *text);
public:
LedMatrix(uint8_t h = 64, uint8_t w = 64);
@@ -31,8 +37,13 @@ public:
void drawPixel(int x, int y, CRGB color);
uint8_t drawChar(int x, int y, char c, CRGB color);
void clearRow(uint8_t y);
// Draw text string
void drawText(int x, int y, const char *text, CRGB color);
void drawHorizontalScrollText(uint8_t y, const char *text, CRGB color, uint8_t scrollSpeed = 100);
};
#endif