Files
LED-Matrix/main/LEDMatrix.cpp
2024-11-30 16:48:48 +01:00

69 lines
3.9 KiB
C++

#include "LEDMatrix.h"
#include "font.h"
LEDMatrix::LEDMatrix(uint8_t width, uint8_t height, uint16_t numLeds, uint16_t lastVisibleLed, CRGB* leds)
: kMatrixWidth(width), kMatrixHeight(height), NUM_LEDS(numLeds), LAST_VISIBLE_LED(lastVisibleLed), leds(leds) {
static const int table[] = {
248, 247, 232, 231, 216, 215, 200, 199, 184, 183, 168, 167, 152, 151, 136, 135, 120, 119, 104, 103, 88, 87, 72, 71, 56, 55, 40, 39, 24, 23, 8, 7,
249, 246, 233, 230, 217, 214, 201, 198, 185, 182, 169, 166, 153, 150, 137, 134, 121, 118, 105, 102, 89, 86, 73, 70, 57, 54, 41, 38, 25, 22, 9, 6,
250, 245, 234, 229, 218, 213, 202, 197, 186, 181, 170, 165, 154, 149, 138, 133, 122, 117, 106, 101, 90, 85, 74, 69, 58, 53, 42, 37, 26, 21, 10, 5,
251, 244, 235, 228, 219, 212, 203, 196, 187, 180, 171, 164, 155, 148, 139, 132, 123, 116, 107, 100, 91, 84, 75, 68, 59, 52, 43, 36, 27, 20, 11, 4,
252, 243, 236, 227, 220, 211, 204, 195, 188, 179, 172, 163, 156, 147, 140, 131, 124, 115, 108, 99, 92, 83, 76, 67, 60, 51, 44, 35, 28, 19, 12, 3,
253, 242, 237, 226, 221, 210, 205, 194, 189, 178, 173, 162, 157, 146, 141, 130, 125, 114, 109, 98, 93, 82, 77, 66, 61, 50, 45, 34, 29, 18, 13, 2,
254, 241, 238, 225, 222, 209, 206, 193, 190, 177, 174, 161, 158, 145, 142, 129, 126, 113, 110, 97, 94, 81, 78, 65, 62, 49, 46, 33, 30, 17, 14, 1,
255, 240, 239, 224, 223, 208, 207, 192, 191, 176, 175, 160, 159, 144, 143, 128, 127, 112, 111, 96, 95, 80, 79, 64, 63, 48, 47, 32, 31, 16, 15, 0,
256, 271, 272, 287, 288, 303, 304, 319, 320, 335, 336, 351, 352, 367, 368, 383, 384, 399, 400, 415, 416, 431, 432, 447, 448, 463, 464, 479, 480, 495, 496, 511,
257, 270, 273, 286, 289, 302, 305, 318, 321, 334, 337, 350, 353, 366, 369, 382, 385, 398, 401, 414, 417, 430, 433, 446, 449, 462, 465, 478, 481, 494, 497, 510,
258, 269, 274, 285, 290, 301, 306, 317, 322, 333, 338, 349, 354, 365, 370, 381, 386, 397, 402, 413, 418, 429, 434, 445, 450, 461, 466, 477, 482, 493, 498, 509,
259, 268, 275, 284, 291, 300, 307, 316, 323, 332, 339, 348, 355, 364, 371, 380, 387, 396, 403, 412, 419, 428, 435, 444, 451, 460, 467, 476, 483, 492, 499, 508,
260, 267, 276, 283, 292, 299, 308, 315, 324, 331, 340, 347, 356, 363, 372, 379, 388, 395, 404, 411, 420, 427, 436, 443, 452, 459, 468, 475, 484, 491, 500, 507,
261, 266, 277, 282, 293, 298, 309, 314, 325, 330, 341, 346, 357, 362, 373, 378, 389, 394, 405, 410, 421, 426, 437, 442, 453, 458, 469, 474, 485, 490, 501, 506,
262, 265, 278, 281, 294, 297, 310, 313, 326, 329, 342, 345, 358, 361, 374, 377, 390, 393, 406, 409, 422, 425, 438, 441, 454, 457, 470, 473, 486, 489, 502, 505,
263, 264, 279, 280, 295, 296, 311, 312, 327, 328, 343, 344, 359, 360, 375, 376, 391, 392, 407, 408, 423, 424, 439, 440, 455, 456, 471, 472, 487, 488, 503, 504
};
XYTable = table;
}
uint16_t LEDMatrix::XY(uint8_t x, uint8_t y) {
// any out of bounds address maps to the first hidden pixel
if ((x >= kMatrixWidth) || (y >= kMatrixHeight)) {
return (LAST_VISIBLE_LED + 1);
}
int i = (y * kMatrixWidth) + x;
int j = XYTable[i];
return j;
}
void LEDMatrix::drawPixel(uint8_t x, uint8_t y, CRGB color) {
int index = XY(x, y);
if (index <= LAST_VISIBLE_LED) {
leds[index] = color;
}
}
void LEDMatrix::drawChar(char c, int x, int y, CRGB color) {
if (c < 32 || c > 127) return; // Ignore non-printable characters
for (int i = 0; i < 5; i++) {
uint8_t line = font[(c - 32) * 5 + i]; // Access the font array as a one-dimensional array
for (int j = 0; j < 7; j++) {
if (line & 0x1) {
drawPixel(x + i, y + j, color);
}
line >>= 1;
}
}
}
void LEDMatrix::drawText(const char* text, int x, int y, CRGB color) {
while (*text) {
drawChar(*text++, x, y, color);
x += 6; // Move to the next character position
if (x >= kMatrixWidth) { // Handle overflow to the next screen
x = 0;
y += 8; // Move to the next line
}
}
}