From 6377983eddc393ebaaa94257830fb613706e21a2 Mon Sep 17 00:00:00 2001 From: tiijay Date: Tue, 4 Nov 2025 14:08:51 +0100 Subject: [PATCH] v.0.0.4 uint8_t charWidth(const uint8_t *charMatrix); --- lib/Fonts/src/font_5x7.cpp | 41 --------------------------------- lib/Fonts/src/font_5x7.h | 1 - lib/LedMatrix/src/LedMatrix.cpp | 16 +++++++++++-- lib/LedMatrix/src/LedMatrix.h | 3 +++ 4 files changed, 17 insertions(+), 44 deletions(-) diff --git a/lib/Fonts/src/font_5x7.cpp b/lib/Fonts/src/font_5x7.cpp index 0583e85..005f1a5 100644 --- a/lib/Fonts/src/font_5x7.cpp +++ b/lib/Fonts/src/font_5x7.cpp @@ -12,44 +12,3 @@ const uint8_t *getFontChar(char c) } return font_5x7[52]; // Return space if not found (index of ' ') } - -// Function to get character width -uint8_t getCharWidth(char c) -{ - switch (c) - { - case '!': - case '.': - case ':': - case ';': - case '\'': - case '|': - case ' ': - return 1; - case 'I': - case 'i': - case 'j': - case 'l': - case '(': - case ')': - case '[': - case ']': - case '{': - case '}': - // Nachher (Korrektur): - case 176: // '°' als ASCII Code - case 149: // '•' als ASCII Code - return 3; - case 'a': - case 'c': - case 'e': - case 'f': - case 'k': - case '<': - case '>': - case '`': - return 4; - default: - return 5; - } -} diff --git a/lib/Fonts/src/font_5x7.h b/lib/Fonts/src/font_5x7.h index c82a774..7d2242e 100644 --- a/lib/Fonts/src/font_5x7.h +++ b/lib/Fonts/src/font_5x7.h @@ -119,6 +119,5 @@ const char font_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0 // Declarations only const uint8_t *getFontChar(char c); -uint8_t getCharWidth(char c); #endif \ No newline at end of file diff --git a/lib/LedMatrix/src/LedMatrix.cpp b/lib/LedMatrix/src/LedMatrix.cpp index 9efdf1d..ec3ef09 100644 --- a/lib/LedMatrix/src/LedMatrix.cpp +++ b/lib/LedMatrix/src/LedMatrix.cpp @@ -10,6 +10,18 @@ LedMatrix::~LedMatrix() { } +template +uint8_t LedMatrix::charWidth(const uint8_t *charMatrix) +{ + uint8_t max_val = 0; + for (size_t i = 0; i < N; i++) + { + if (charMatrix[i] > max_val) + max_val = charMatrix[i]; + } + return (max_val == 0) ? 0 : (32 - __builtin_clz(max_val)); +} + void LedMatrix::drawPixel(int x, int y, CRGB color) { if (x >= 0 && x < 64 && y >= 0 && y < 64) @@ -21,9 +33,9 @@ void LedMatrix::drawPixel(int x, int y, CRGB color) uint8_t LedMatrix::drawChar(int x, int y, char c, CRGB color) { - u_int8_t char_width_pixel = getCharWidth(c); - const uint8_t *fontData = getFontChar(c); + const u_int8_t char_width_pixel = charWidth<7>(fontData); + for (int row = 0; row < 7; row++) { uint8_t line = fontData[row]; diff --git a/lib/LedMatrix/src/LedMatrix.h b/lib/LedMatrix/src/LedMatrix.h index 50f1bbf..0335c88 100644 --- a/lib/LedMatrix/src/LedMatrix.h +++ b/lib/LedMatrix/src/LedMatrix.h @@ -19,6 +19,9 @@ private: uint16_t num_leds; // default 64x64 = 4096 LEDs vector leds; // Automatic memory management + template + uint8_t charWidth(const uint8_t *charMatrix); + public: LedMatrix(uint8_t h = 64, uint8_t w = 64);