RGBMatrixDisplay/src/display/Display.h
2023-01-02 17:17:07 +01:00

138 lines
3.4 KiB
C++

#ifndef DISPLAY_H
#define DISPLAY_H
#include "Pixel.h"
#include "Adafruit_NeoPixel.h"
#include "Vector.h"
#define SYMBOL_COUNT 14
#define DISPLAY_CHAR_WIDTH 3
#define DISPLAY_CHAR_HEIGHT 5
#define FULL 255
#define ____ 0
#define rgb(r, g, b) ((((r << 8) | g) << 8) | b)
#define COLOR_RED (rgb(FULL, ____, ____))
#define COLOR_GREEN (rgb(____, FULL, ____))
#define COLOR_BLUE (rgb(____, ____, FULL))
#define COLOR_YELLOW (rgb(FULL, FULL, ____))
#define COLOR_VIOLET (rgb(FULL, ____, FULL))
#define COLOR_TURQUOISE (rgb(____, FULL, FULL))
#define COLOR_WHITE (rgb(FULL, FULL, FULL))
#define COLOR_BLACK (rgb(____, ____, ____))
extern bool SYMBOLS[SYMBOL_COUNT][DISPLAY_CHAR_WIDTH * DISPLAY_CHAR_HEIGHT];
class Display {
public:
const uint8_t width;
const uint8_t height;
const uint16_t pixelCount;
private:
Adafruit_NeoPixel leds;
public:
Display(uint8_t width, uint8_t height) :
width(width), height(height),
pixelCount(width * height),
leds(pixelCount, GPIO_NUM_13) {
// nothing
}
void setup() {
leds.begin();
leds.setBrightness(8);
clear();
}
void print(uint8_t xPos, uint8_t yPos, uint8_t index, uint8_t r, uint8_t g, uint8_t b) {
print(&xPos, yPos, index, r, g, b);
}
void print(uint8_t xPos, uint8_t yPos, uint8_t index) {
print(&xPos, yPos, index, 255, 255, 255);
}
void print(uint8_t *xPos, uint8_t yPos, uint8_t index) {
print(xPos, yPos, index, 255, 255, 255);
}
void print(uint8_t *xPos, uint8_t yPos, uint8_t index, uint32_t color) {
print(xPos, yPos, index, (uint8_t) (color >> 16), (uint8_t) (color >> 8), (uint8_t) color);
}
void print(uint8_t *xPos, uint8_t yPos, uint8_t index, uint8_t r, uint8_t g, uint8_t b) {
if (index >= SYMBOL_COUNT) {
Serial.printf("Cannot print symbol #%u.\n", index);
return;
}
bool *symbolBit = SYMBOLS[index];
for (uint8_t y = 0; y < DISPLAY_CHAR_HEIGHT; ++y) {
for (uint8_t x = 0; x < DISPLAY_CHAR_WIDTH; ++x) {
if (*(symbolBit++)) {
set(*xPos + x, yPos + y, r, g, b);
} else {
set(*xPos + x, yPos + y, 0, 0, 0);
}
}
}
*xPos += 3;
}
void set(uint8_t x, uint8_t y, uint32_t color) {
setPixelColor(x, y, color);
}
void set(uint8_t x, uint8_t y, uint8_t r, uint8_t g, uint8_t b) {
setPixelColor(x, y, (((r << 8) | g) << 8) | b);
}
void set(Vector *pos, uint32_t color) {
setPixelColor((uint8_t) round(pos->x), (uint8_t) round(pos->y), color);
}
void set(Vector *pos, uint8_t r, uint8_t g, uint8_t b) {
setPixelColor((uint8_t) round(pos->x), (uint8_t) round(pos->y), (((r << 8) | g) << 8) | b);
}
void setPixelColor(uint8_t x, uint8_t y, uint32_t color) {
if (x >= width || y >= height) {
return;
}
if ((y % 2) != 0) {
x = width - x - 1;
}
leds.setPixelColor(y * width + x, color);
}
void clear() {
leds.clear();
}
void loop() {
leds.show();
}
void setBrightness(int brightness) {
leds.setBrightness(max(0, min(brightness, 255)));
}
int getBrightness() {
return leds.getBrightness();
}
void setIndex(uint16_t index, uint8_t r, uint8_t g, uint8_t b) {
leds.setPixelColor(index, r, g, b);
}
};
#endif