v.0.0.3 LedMatrix::drawChar

This commit is contained in:
tiijay
2025-11-04 11:29:09 +01:00
parent d138cb9763
commit ef341ea7ea
4 changed files with 27 additions and 39 deletions

View File

@@ -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 '`':

View File

@@ -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);
}
}
}
}
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);
mtx_col_offset++;
}
}
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<uint8_t> LedMatrix::number_to_bitarray_msb(uint16_t number, int bits)
vector<uint8_t> 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<uint8_t> byte;
@@ -83,13 +74,13 @@ vector<uint8_t> 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();

View File

@@ -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<uint8_t> number_to_bitarray_msb(uint16_t number, int bits = 8);
vector<uint8_t> number_to_bitarray_msb(uint16_t number, int bits = 8, boolean four_bits = false);
};
#endif

View File

@@ -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();
}