diff --git a/lib/LedMatrix/src/LedMatrix.cpp b/lib/LedMatrix/src/LedMatrix.cpp index 0f2904e..e6328f0 100644 --- a/lib/LedMatrix/src/LedMatrix.cpp +++ b/lib/LedMatrix/src/LedMatrix.cpp @@ -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(); +} \ No newline at end of file diff --git a/lib/LedMatrix/src/LedMatrix.h b/lib/LedMatrix/src/LedMatrix.h index b1f7b33..ecacd4c 100644 --- a/lib/LedMatrix/src/LedMatrix.h +++ b/lib/LedMatrix/src/LedMatrix.h @@ -24,6 +24,12 @@ private: template 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 \ No newline at end of file diff --git a/lib/Utils/src/mathematics.cpp b/lib/Utils/src/mathematics.cpp index 47ab657..c24d853 100644 --- a/lib/Utils/src/mathematics.cpp +++ b/lib/Utils/src/mathematics.cpp @@ -1,6 +1,6 @@ #include "mathematics.h" -vector number_to_bitarray_msb(u_int16_t number, int bits, boolean four_bits) +vector number_to_bitarray_msb(uint16_t number, int bits, boolean four_bits) { Serial.printf("s-o-n: %d", sizeof(number)); Serial.println(); @@ -27,4 +27,4 @@ vector number_to_bitarray_msb(u_int16_t number, int bits, boolean four Serial.println(); return two_bytes; -} \ No newline at end of file +} diff --git a/src/main.cpp b/src/main.cpp index 9e5a130..dd789e5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -54,7 +54,7 @@ void playWithBlinkLed() Serial.println("PinStatus: " + led_status); } -void playWithMatrix() +void setupMatrix() { matrix.setBrightness(255); @@ -63,10 +63,10 @@ void playWithMatrix() // Draw static "HELLO WORLD" matrix.drawText(1, 1, "Hello World!", CRGB::Red); - matrix.drawText(1, 9, "<°|`¶>", CRGB::Yellow); - matrix.drawText(1, 17, "Bottom", CRGB::Yellow); - matrix.drawText(1, 25, "ABCDEFGHIJ.", CRGB::Yellow); - matrix.drawText(1, 33, "A", CRGB::Yellow); + // matrix.drawText(1, 9, "<°|`¶>", CRGB::Yellow); + // matrix.drawText(1, 17, "Bottom", CRGB::Yellow); + // matrix.drawText(1, 25, "ABCDEFGHIJ.", CRGB::Yellow); + // matrix.drawText(1, 33, "A", CRGB::Yellow); matrix.show(); } @@ -81,14 +81,15 @@ void setup() delay(10); } - // playWithMatrix(); - printCharMatrixSimple('A'); - number_to_bitarray_msb(0x11FF, 16, true); + setupMatrix(); + // printCharMatrixSimple('A'); + // number_to_bitarray_msb(0x11FF, 16, true); // playWithBlinkLed(); } void loop() { - delay(1000); + matrix.drawHorizontalScrollText(41, "scrolling...", CRGB::Wheat); + delay(10); } \ No newline at end of file