diff options
| author | mrfaptastic <12006953+mrfaptastic@users.noreply.github.com> | 2020-12-07 22:42:37 +0000 |
|---|---|---|
| committer | mrfaptastic <12006953+mrfaptastic@users.noreply.github.com> | 2020-12-07 22:42:37 +0000 |
| commit | 76351960b80ebbace056fad6b1107f92bb461703 (patch) | |
| tree | 7ebf115b76b00f61b5ecb8e7bc480f5476280613 /examples/GraphicsLayer/Layer.h | |
| parent | 7683b1d8f3540f90baf0052ef0d82b3c2e88fbd2 (diff) | |
Added GraphicsLayer example.
Diffstat (limited to 'examples/GraphicsLayer/Layer.h')
| -rw-r--r-- | examples/GraphicsLayer/Layer.h | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/examples/GraphicsLayer/Layer.h b/examples/GraphicsLayer/Layer.h new file mode 100644 index 0000000..69a5886 --- /dev/null +++ b/examples/GraphicsLayer/Layer.h @@ -0,0 +1,122 @@ +/** + * Experimental layer class to do play with pixel in an off-screen buffer before painting to the DMA + * + * Requires FastLED + * + * Faptastic 2020 + **/ + +#ifndef DISPLAY_MATRIX_LAYER +#define DISPLAY_MATRIX_LAYER + + +/* Use GFX_Root (https://github.com/mrfaptastic/GFX_Root) instead of + * Adafruit_GFX library. No real benefit unless you don't want Bus_IO & Wire.h library dependencies. + */ +#define USE_GFX_ROOT 1 + + +#ifdef USE_GFX_ROOT + #include "GFX.h" // Adafruit GFX core class -> https://github.com/mrfaptastic/GFX_Root +#else + #include "Adafruit_GFX.h" // Adafruit class with all the other stuff +#endif + +#include <FastLed.h> +#include <ESP32-RGB64x32MatrixPanel-I2S-DMA.h> + +/* + * Set the width and height of the layer buffers. This should match exactly that of your output display, or virtual display. + */ +#define LAYER_WIDTH 64 +#define LAYER_HEIGHT 32 + + +#define HALF_WHITE_COLOUR 0x8410 +#define BLACK_BACKGROUND_PIXEL_COLOUR CRGB(0,0,0) + +enum textPosition { TOP, MIDDLE, BOTTOM }; + + +/* To help with direct pixel referencing by width and height */ +struct layerPixels { + CRGB data[LAYER_HEIGHT][LAYER_WIDTH]; +}; + +#ifdef USE_GFX_ROOT +class Layer : public GFX { +#else +class Layer : public Adafruit_GFX { +#endif +// class Layer : public GFX // use GFX Root for now +{ + public: + + // Static allocation of memory for layer + //CRGB pixels[LAYER_WIDTH][LAYER_HEIGHT] = {{0}}; + + Layer(RGB64x32MatrixPanel_I2S_DMA &disp) : GFX (LAYER_WIDTH, LAYER_HEIGHT) { + matrix = &disp; + } + + inline void init() + { + // https://stackoverflow.com/questions/5914422/proper-way-to-initialize-c-structs + pixels = new layerPixels(); + + //pixels = (layerPixels *) malloc(sizeof(layerPixels)); + // pixel = (CRGB *) &pixels[0]; + //Serial.printf("Allocated %d bytes of memory for standard CRGB (24bit) layer.\r\n", NUM_PIXELS*sizeof(CRGB)); + Serial.printf("Allocated %d bytes of memory for layerPixels.\r\n", sizeof(layerPixels)); + + } // end Layer + + void drawPixel(int16_t x, int16_t y, uint16_t color); // overwrite adafruit implementation + void drawPixel(int16_t x, int16_t y, int r, int g, int b); // Layer implementation + void drawPixel(int16_t x, int16_t y, CRGB color); // Layer implementation + + // Font Stuff + //https://forum.arduino.cc/index.php?topic=642749.0 + void drawCentreText(const char *buf, textPosition textPos = BOTTOM, const GFXfont *f = NULL, CRGB color = 0x8410, int yadjust = 0); // 128,128,128 RGB @ bottom row by default + + + void dim(byte value); + void clear(); + void display(); // flush to display / LED matrix + + // override the color of all pixels that aren't the transparent color + void overridePixelColor(int r, int g, int b); + + inline uint16_t color565(uint8_t r, uint8_t g, uint8_t b) { + return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3); + } + + inline void setTransparency(bool t) { transparency_enabled = t; } + + // Effects + void moveX(int delta); + void autoCenterX(); + void moveY(int delta); + + // For layer composition - accessed publically + CRGB transparency_colour = BLACK_BACKGROUND_PIXEL_COLOUR; + bool transparency_enabled = true; + layerPixels *pixels; + + // Release Memory + ~Layer(void); + + private: + RGB64x32MatrixPanel_I2S_DMA *matrix = NULL; +}; + + +/* Merge FastLED layers into a super layer and display. */ +namespace LayerCompositor +{ + void Stack(RGB64x32MatrixPanel_I2S_DMA &disp, const Layer &_bgLayer, const Layer &_fgLayer, bool writeToBgLayer = false); + void Siloette(RGB64x32MatrixPanel_I2S_DMA &disp, const Layer &_bgLayer, const Layer &_fgLayer); + void Blend(RGB64x32MatrixPanel_I2S_DMA &disp, const Layer &_bgLayer, const Layer &_fgLayer, uint8_t ratio); +} + +#endif |
