v.0.0.2 number_to_bitarray_msb

This commit is contained in:
tiijay
2025-11-03 19:58:32 +01:00
parent 156ce19ec6
commit d138cb9763
4 changed files with 66 additions and 19 deletions

View File

@@ -18,7 +18,6 @@ uint8_t getCharWidth(char c)
{ {
switch (c) switch (c)
{ {
case 'I':
case '!': case '!':
case '.': case '.':
case ':': case ':':
@@ -26,10 +25,10 @@ uint8_t getCharWidth(char c)
case '\'': case '\'':
case '|': case '|':
return 1; return 1;
case 'i':
case ' ': case ' ':
return 2; return 2;
case 'J': case 'I':
case 'i':
case 'j': case 'j':
case 'l': case 'l':
case '(': case '(':

View File

@@ -21,12 +21,16 @@ void LedMatrix::drawPixel(int x, int y, CRGB color)
void LedMatrix::drawChar(int x, int y, char c, CRGB 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);
const uint8_t *fontData = getFontChar(c); const uint8_t *fontData = getFontChar(c);
for (int row = 0; row < 7; row++) for (int row = 0; row < 7; row++)
{ {
uint8_t line = fontData[row]; uint8_t line = fontData[row];
for (int col = 0; col < 5; col++) for (int col = 0; col < 5 /*char_width_pixel*/; col++)
{ {
drawPixel(x + col, y + row, CRGB::SlateGrey);
if (line & (1 << (4 - col))) if (line & (1 << (4 - col)))
{ {
drawPixel(x + col, y + row, color); drawPixel(x + col, y + row, color);
@@ -35,17 +39,60 @@ void LedMatrix::drawChar(int x, int y, char c, CRGB 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);
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);
}
}
}
}
// Draw text string // Draw text string
void LedMatrix::drawText(int x, int y, const char *text, CRGB color) void LedMatrix::drawText(int x, int y, const char *text, CRGB color)
{ {
int cursorX = x; int cursorX = x;
for (int i = 0; text[i] != '\0'; i++) for (int i = 0; text[i] != '\0'; i++)
{ {
u_int8_t w = getCharWidth(text[i]);
// Debug output
Serial.printf("Char %d: '%c' -> width %d at X=%d\n\r",
i, text[i], w, cursorX);
drawChar(cursorX, y, text[i], color); drawChar(cursorX, y, text[i], color);
_drawChar_(cursorX, 8 + y, text[i], color);
cursorX += 6; // 5 pixels width + 1 pixel spacing cursorX += 6; // 5 pixels width + 1 pixel spacing
} }
} }
vector<uint8_t> LedMatrix::number_to_bitarray_msb(uint16_t number, int bits)
{
/** Convert 8/16-bit number to bit array (MSB first) */
std::vector<uint8_t> byte;
byte.reserve(bits);
for (int i = bits - 1; i >= 0; i--)
{
byte.push_back((number >> i) & 1);
}
// Debug, Ausgabe des Bytes
Serial.printf("0x%02X\n\r", number);
uint8_t idx = 0;
for (auto bit : byte)
{
Serial.printf("%d", bit);
idx++;
if (!(idx % 4))
Serial.printf(" ");
}
Serial.println();
return byte;
}

View File

@@ -26,8 +26,11 @@ public:
void drawPixel(int x, int y, CRGB color); 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);
void _drawChar_(int x, int y, char c, CRGB color);
// 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);
vector<uint8_t> number_to_bitarray_msb(uint16_t number, int bits = 8);
}; };
#endif #endif

View File

@@ -10,10 +10,9 @@ void printCharMatrixSimple(char c)
{ {
const uint8_t *fontData = getFontChar(c); const uint8_t *fontData = getFontChar(c);
Serial.println(); Serial.print("<");
Serial.print("'");
Serial.print(c); Serial.print(c);
Serial.println("':"); Serial.println(">");
String mtrx = ""; String mtrx = "";
for (int row = 0; row < 7; row++) for (int row = 0; row < 7; row++)
@@ -24,16 +23,13 @@ void printCharMatrixSimple(char c)
if (line & (1 << (4 - col))) if (line & (1 << (4 - col)))
{ {
mtrx += ""; mtrx += "";
Serial.print("#"); // Einfaches # für Pixel
} }
else else
{ {
mtrx += "."; mtrx += ".";
Serial.print("."); // Punkt für leere Pixel
} }
} }
mtrx += "\n\r"; mtrx += "\n\r";
Serial.println();
} }
Serial.println(mtrx); Serial.println(mtrx);
} }
@@ -71,13 +67,15 @@ void setup()
matrix.show(); matrix.show();
// Draw static "HELLO WORLD" // Draw static "HELLO WORLD"
matrix.drawText(1, 1, "Hello", CRGB::Red); // matrix.drawText(1, 1, "Hello", CRGB::Red);
matrix.drawText(35, 1, "World", CRGB::Blue); // matrix.drawText(35, 1, "World", CRGB::Blue);
matrix.drawText(1, 20, "<°|`¶>", CRGB::Yellow); // matrix.drawText(1, 20, "<°|`¶>", CRGB::Yellow);
matrix.drawText(1, 30, "Bottom", CRGB::Yellow); // matrix.drawText(1, 30, "Bottom", CRGB::Yellow);
matrix.drawText(1, 40, "ABCDEFGHIJ.", CRGB::Yellow);
matrix.show(); matrix.show();
// printCharMatrixSimple('S'); printCharMatrixSimple('S');
matrix.number_to_bitarray_msb(0x8F);
// playWithBlinkLed(); // playWithBlinkLed();
} }