From ef341ea7ead1e0a076ac72833534a2293f166a3c Mon Sep 17 00:00:00 2001 From: tiijay Date: Tue, 4 Nov 2025 11:29:09 +0100 Subject: [PATCH] v.0.0.3 LedMatrix::drawChar --- lib/Fonts/src/font_5x7.cpp | 5 +--- lib/LedMatrix/src/LedMatrix.cpp | 49 ++++++++++++++------------------- lib/LedMatrix/src/LedMatrix.h | 5 ++-- src/main.cpp | 7 +++-- 4 files changed, 27 insertions(+), 39 deletions(-) diff --git a/lib/Fonts/src/font_5x7.cpp b/lib/Fonts/src/font_5x7.cpp index 6e28cf5..0583e85 100644 --- a/lib/Fonts/src/font_5x7.cpp +++ b/lib/Fonts/src/font_5x7.cpp @@ -24,9 +24,8 @@ uint8_t getCharWidth(char c) case ';': case '\'': case '|': - return 1; case ' ': - return 2; + return 1; case 'I': case 'i': case 'j': @@ -46,8 +45,6 @@ uint8_t getCharWidth(char c) case 'e': case 'f': case 'k': - case 's': - case 't': case '<': case '>': case '`': diff --git a/lib/LedMatrix/src/LedMatrix.cpp b/lib/LedMatrix/src/LedMatrix.cpp index f65eb98..9efdf1d 100644 --- a/lib/LedMatrix/src/LedMatrix.cpp +++ b/lib/LedMatrix/src/LedMatrix.cpp @@ -19,44 +19,34 @@ void LedMatrix::drawPixel(int x, int y, CRGB color) } } -void LedMatrix::drawChar(int x, int y, char c, CRGB color) +uint8_t LedMatrix::drawChar(int x, int y, char c, CRGB color) { u_int8_t char_width_pixel = getCharWidth(c); - // Serial.printf("Char '%c' -> width %d\n\r", c, char_width_pixel); const uint8_t *fontData = getFontChar(c); for (int row = 0; row < 7; row++) { uint8_t line = fontData[row]; - for (int col = 0; col < 5 /*char_width_pixel*/; col++) + uint8_t mtx_col_offset = 0; + // Start Column in der Zeichen-Matrix + // Abhängig von Zeichen-Breite + // Bsp: Font hat Breite von 5, Zeichen hat Breite 3 + // --> dann brauchen wir nur die Spalten 2-4 + // denn die Spalten 0 u. 1 haben keine gesetzten Bits + uint8_t start_col = 5 - char_width_pixel; + for (int col = start_col; col < 5; col++) { - drawPixel(x + col, y + row, CRGB::SlateGrey); + drawPixel(x + mtx_col_offset, y + row, CRGB::SlateGrey); if (line & (1 << (4 - col))) { - drawPixel(x + col, y + row, color); + drawPixel(x + mtx_col_offset, y + row, color); } + + mtx_col_offset++; } } -} -void LedMatrix::_drawChar_(int x, int y, char c, CRGB color) -{ - u_int8_t char_width_pixel = getCharWidth(c); - // Serial.printf("Char '%c' -> width %d\n\r", c, char_width_pixel); - - const uint8_t *fontData = getFontChar(c); - for (int row = 0; row < 7; row++) - { - uint8_t line = fontData[row]; - for (int col = 5 - char_width_pixel; col < 5; col++) - { - drawPixel(x + col, y + row, CRGB::SlateGrey); - if (line & (1 << (4 - col))) - { - drawPixel(x + col, y + row, CRGB::GhostWhite); - } - } - } + return char_width_pixel; } // Draw text string @@ -66,12 +56,13 @@ void LedMatrix::drawText(int x, int y, const char *text, CRGB color) for (int i = 0; text[i] != '\0'; i++) { drawChar(cursorX, y, text[i], color); - _drawChar_(cursorX, 8 + y, text[i], color); - cursorX += 6; // 5 pixels width + 1 pixel spacing + uint8_t chr_width = drawChar(cursorX, 8 + y, text[i], color); + // cursorX += 6; // 5 pixels width + 1 pixel spacing + cursorX += (chr_width + 1); // 5 pixels width + 1 pixel spacing } } -vector LedMatrix::number_to_bitarray_msb(uint16_t number, int bits) +vector LedMatrix::number_to_bitarray_msb(uint16_t number, int bits, boolean four_bits) { /** Convert 8/16-bit number to bit array (MSB first) */ std::vector byte; @@ -83,13 +74,13 @@ vector LedMatrix::number_to_bitarray_msb(uint16_t number, int bits) } // Debug, Ausgabe des Bytes - Serial.printf("0x%02X\n\r", number); + Serial.printf("[0x%02X] ", number); uint8_t idx = 0; for (auto bit : byte) { Serial.printf("%d", bit); idx++; - if (!(idx % 4)) + if (four_bits && !(idx % 4)) Serial.printf(" "); } Serial.println(); diff --git a/lib/LedMatrix/src/LedMatrix.h b/lib/LedMatrix/src/LedMatrix.h index 3ab18c1..50f1bbf 100644 --- a/lib/LedMatrix/src/LedMatrix.h +++ b/lib/LedMatrix/src/LedMatrix.h @@ -25,12 +25,11 @@ public: ~LedMatrix(); void drawPixel(int x, int y, CRGB color); - void drawChar(int x, int y, char c, CRGB color); - void _drawChar_(int x, int y, char c, CRGB color); + uint8_t drawChar(int x, int y, char c, CRGB color); // Draw text string void drawText(int x, int y, const char *text, CRGB color); - vector number_to_bitarray_msb(uint16_t number, int bits = 8); + vector number_to_bitarray_msb(uint16_t number, int bits = 8, boolean four_bits = false); }; #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 64aa8f6..1aa50c2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -71,11 +71,12 @@ void setup() // matrix.drawText(35, 1, "World", CRGB::Blue); // matrix.drawText(1, 20, "<°|`¶>", CRGB::Yellow); // matrix.drawText(1, 30, "Bottom", CRGB::Yellow); - matrix.drawText(1, 40, "ABCDEFGHIJ.", CRGB::Yellow); + // matrix.drawText(1, 40, "ABCDEFGHIJ.", CRGB::Yellow); + matrix.drawText(1, 40, "Moin u stupid", CRGB::Yellow); matrix.show(); - printCharMatrixSimple('S'); - matrix.number_to_bitarray_msb(0x8F); + // printCharMatrixSimple('S'); + // matrix.number_to_bitarray_msb(0x8F); // playWithBlinkLed(); }