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 8 -> 32-28 = 4.Bit
// bei 2 -> 32-20 = 2.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)); 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 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> template <size_t N>
uint8_t charWidth(const uint8_t *charMatrix); uint8_t charWidth(const uint8_t *charMatrix);
// Text Scrolling
int8_t scrollPosition = 0;
unsigned long lastScrollTime = 0;
uint8_t getTextWidth(const char *text);
public: public:
LedMatrix(uint8_t h = 64, uint8_t w = 64); LedMatrix(uint8_t h = 64, uint8_t w = 64);
@@ -31,8 +37,13 @@ public:
void drawPixel(int x, int y, CRGB color); void drawPixel(int x, int y, CRGB color);
uint8_t drawChar(int x, int y, char c, CRGB color); uint8_t drawChar(int x, int y, char c, CRGB color);
void clearRow(uint8_t y);
// Draw text string // Draw text string
void drawText(int x, int y, const char *text, CRGB color); 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 #endif

View File

@@ -1,6 +1,6 @@
#include "mathematics.h" #include "mathematics.h"
vector<uint16_t> number_to_bitarray_msb(u_int16_t number, int bits, boolean four_bits) vector<uint16_t> number_to_bitarray_msb(uint16_t number, int bits, boolean four_bits)
{ {
Serial.printf("s-o-n: %d", sizeof(number)); Serial.printf("s-o-n: %d", sizeof(number));
Serial.println(); Serial.println();
@@ -27,4 +27,4 @@ vector<uint16_t> number_to_bitarray_msb(u_int16_t number, int bits, boolean four
Serial.println(); Serial.println();
return two_bytes; return two_bytes;
} }

View File

@@ -54,7 +54,7 @@ void playWithBlinkLed()
Serial.println("PinStatus: " + led_status); Serial.println("PinStatus: " + led_status);
} }
void playWithMatrix() void setupMatrix()
{ {
matrix.setBrightness(255); matrix.setBrightness(255);
@@ -63,10 +63,10 @@ void playWithMatrix()
// Draw static "HELLO WORLD" // Draw static "HELLO WORLD"
matrix.drawText(1, 1, "Hello World!", CRGB::Red); matrix.drawText(1, 1, "Hello World!", CRGB::Red);
matrix.drawText(1, 9, "<°|`¶>", CRGB::Yellow); // matrix.drawText(1, 9, "<°|`¶>", CRGB::Yellow);
matrix.drawText(1, 17, "Bottom", CRGB::Yellow); // matrix.drawText(1, 17, "Bottom", CRGB::Yellow);
matrix.drawText(1, 25, "ABCDEFGHIJ.", CRGB::Yellow); // matrix.drawText(1, 25, "ABCDEFGHIJ.", CRGB::Yellow);
matrix.drawText(1, 33, "A", CRGB::Yellow); // matrix.drawText(1, 33, "A", CRGB::Yellow);
matrix.show(); matrix.show();
} }
@@ -81,14 +81,15 @@ void setup()
delay(10); delay(10);
} }
// playWithMatrix(); setupMatrix();
printCharMatrixSimple('A'); // printCharMatrixSimple('A');
number_to_bitarray_msb(0x11FF, 16, true); // number_to_bitarray_msb(0x11FF, 16, true);
// playWithBlinkLed(); // playWithBlinkLed();
} }
void loop() void loop()
{ {
delay(1000); matrix.drawHorizontalScrollText(41, "scrolling...", CRGB::Wheat);
delay(10);
} }