aboutsummaryrefslogtreecommitdiff
path: root/examples/GraphicsLayer/Layer.h
blob: aa93c8f0c65667035d8a1fabaf66710144549a8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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-HUB75-MatrixPanel-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(MatrixPanel_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:
		MatrixPanel_I2S_DMA *matrix = NULL;		
};


/* Merge FastLED layers into a super layer and display. */
namespace LayerCompositor
{
        void Stack(MatrixPanel_I2S_DMA &disp, const Layer &_bgLayer, const Layer &_fgLayer, bool writeToBgLayer = false);
        void Siloette(MatrixPanel_I2S_DMA &disp, const Layer &_bgLayer, const Layer &_fgLayer);		
		    void Blend(MatrixPanel_I2S_DMA &disp, const Layer &_bgLayer, const Layer &_fgLayer, uint8_t ratio = 127);
}

#endif