aboutsummaryrefslogtreecommitdiff
path: root/examples/GraphicsLayer/Layer.cpp
blob: 29ea97f95812efe0676d38f2ae0ebb3c5b964887 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/**
 * Experimental layer class to do play with pixel in an off-screen buffer before painting to the DMA 
 *
 * Requires FastLED
 *
 * Faptastic 2020
 **/

#include "Layer.h"

// For adafruit
void Layer::drawPixel(int16_t x, int16_t y, uint16_t color) {

	// 565 color conversion
	uint8_t r = ((((color >> 11)  & 0x1F) * 527) + 23) >> 6;
	uint8_t g = ((((color >> 5)   & 0x3F) * 259) + 33) >> 6;
	uint8_t b = (((color & 0x1F)  * 527) + 23) >> 6;

	drawPixel(x, y, CRGB(r,g,b));
}

void Layer::drawPixel(int16_t x, int16_t y, int r, int g, int b) {
	drawPixel(x, y, CRGB(r,g,b));
}

void Layer::drawPixel(int16_t x, int16_t y, CRGB color) {

	if( x >= LAYER_WIDTH 	|| x < 0) return; // 0;
	if( y >= LAYER_HEIGHT 	|| y < 0) return; // 0;
	
	pixels->data[y][x] = color;
}

/**
 * Dim all the pixels in the display.
 */
void Layer::dim(byte value)  {   

		// nscale8 max value is 255, or it'll flip back to 0 
		// (documentation is wrong when it says x/256), it's actually x/255
		for (int y = 0; y < LAYER_HEIGHT; y++) {
			for (int x = 0; x < LAYER_WIDTH; x++) {
				pixels->data[y][x].nscale8(value);
		}}
}

void Layer::clear() { 

	memset(pixels, BLACK_BACKGROUND_PIXEL_COLOUR, sizeof(layerPixels) );
}

/**
 * Send the layer to the display device.
 */
void Layer::display() { 

	CRGB _pixel = 0 ;
	for (int y = 0; y < LAYER_HEIGHT; y++) {
		for (int x = 0; x < LAYER_WIDTH; x++)
		{
			//_pixel = pixel[XY(x, y)];
			_pixel = pixels->data[y][x];

			matrix->drawPixelRGB888( x, y, _pixel.r, _pixel.g, _pixel.b);

			/*
			if ( !transparency_enabled ){
				matrix->drawPixelRGB888( x, y, _pixel.r, _pixel.g, _pixel.b);
			} else {
				if (_pixel != transparency_colour) {
					matrix->drawPixelRGB888( x, y, _pixel.r, _pixel.g, _pixel.b);
				}
			}
			*/
		} // end loop to copy fast led to the dma matrix
	}

} // display

void Layer::overridePixelColor(int r, int g, int b) {
	CRGB _pixel = 0 ;
	for (int y = 0; y < LAYER_HEIGHT; y++) {
		for (int x = 0; x < LAYER_WIDTH; x++)
		{
			//_pixel = pixel[XY(x, y)];
			_pixel = pixels->data[y][x];
			
			if (_pixel != transparency_colour) {
				matrix->drawPixelRGB888( x, y, _pixel.r, _pixel.g, _pixel.b);
			}

		} // end loop to copy fast led to the dma matrix
	}
}


// default value is in definition
void Layer::drawCentreText(const char *buf, textPosition textPos, const GFXfont *f, CRGB color, int yadjust) 
{
			int16_t x1, y1;
			uint16_t w, h;

			setTextWrap(false);
			
			if (f) {          // Font struct pointer passed in?
					setFont((GFXfont *)f);
			} else { // NULL passed.  Current font struct defined?
					setFont(); // use default
			}

			// getTextBounds isn't correct for variable width fonts
			getTextBounds(buf, 0, 0, &x1, &y1, &w, &h); //calc width of new string

			//Serial.printf("The width of the text is %d pixels, the height is %d pixels.\n", w,h);

			/*

			 From: https://learn.adafruit.com/adafruit-gfx-graphics-library/using-fonts
		
			 For example, whereas the cursor position when printing with the classic font identified 
			 the top-left corner of the character cell, with new fonts the cursor position indicates the baseline — 
			 the bottom-most row — of subsequent text. Characters may vary in size and width, and don’t 
			 necessarily begin at the exact cursor column (as in below, this character starts one pixel 
			 left of the cursor, but others may be on or to the right of it).
			 */
			
			if (!f) { 
				if (textPos == TOP) {
					setCursor((LAYER_WIDTH - w) / 2, 0); // top 
				} else if (textPos == BOTTOM) {
					setCursor((LAYER_WIDTH - w) / 2, LAYER_HEIGHT - h);
				} else { // middle
					setCursor((LAYER_WIDTH - w) / 2, (LAYER_HEIGHT - h) / 2); // top 
				}
			}
			else // custom font
			/* As we can't reliable know what is the actual FIRST and last 'lit' pixel, we need to check what was printed to the layer.*/
			{				
				int wstart = 0;

				if (w > 42) wstart = (LAYER_WIDTH - w) / 2;
				else wstart = (LAYER_WIDTH - w) / 2;

				if (textPos == TOP) {
					setCursor(wstart, h+yadjust); // top 
				} else if (textPos == BOTTOM) {
					setCursor(wstart+1, (LAYER_HEIGHT-1)+yadjust);
				} else { // middle
					setCursor( wstart, ((LAYER_HEIGHT/2) + (h/2)) + yadjust);
				}	
				
				//Serial.printf("Layer: x1: %d, y1: %d, w: %d, h: %d.\n", x1, y1, w, h);			
			}

		//	setCursor(0,16);
			setTextColor(this->color565(color.r, color.g, color.b)); // Need to confirm from FastLed CRGB to adafruit 565
			print(buf);

} // end drawCentreText


  // Move the contents of the screen left (-ve) or right (+ve)
  void Layer::moveX(int offset) 
  {
		if(offset > 0) { // move right
		//	Sprintln("Moving right");

			for(int x = LAYER_WIDTH - 1; x >= 0; x--){ // 63 to 0
				for(int y = 0; y < LAYER_HEIGHT; y++){ // 0 to 31
					if (x - offset >= 0) 
					{
						//  Serial.printf("setting y %d x %d to y %d x %d\n", y, x, y, x-offset);
						pixels->data[y][x] = pixels->data[y][x-offset];
					}
					else {
						pixels->data[y][x] = BLACK_BACKGROUND_PIXEL_COLOUR;            
					}
				}    
			}
		} else { // move left

		//	Sprintln("Moving Left");
			for(int x = 0; x <=LAYER_WIDTH - 1; x++){
				for(int y = 0; y < LAYER_HEIGHT; y++){
					if ( x > (LAYER_WIDTH-1)+offset )
					{
						pixels->data[y][x] = BLACK_BACKGROUND_PIXEL_COLOUR;                    
						//Serial.println("eh?");
					}
					else
					{
						pixels->data[y][x] = pixels->data[y][x-offset]; 
						//	Serial.println("eh?");
					}
				}    
			}         
		} 
  } 
  
/**
 * Centre the contents of the layer based on the leftmost and rightmost pixels.
 * Useful if you want to make sure text / graphics IS in the centre of the display.
 */
  void Layer::autoCenterX()
  {
	  	int leftmost_x = 0, rightmost_x = 0, adjusted_leftmost_x = 0;

		// Find leftmost
		for(int x = 0; x < LAYER_WIDTH; x++) { 
			for(int y = 0; y < LAYER_HEIGHT; y++) {
				if (pixels->data[y][x] != BLACK_BACKGROUND_PIXEL_COLOUR)
				{
					leftmost_x = x;
					//Serial.printf("Left most x pixel is %d\n", leftmost_x);
					goto rightmost;
				}
			}
		}

		rightmost:
		for(int x = LAYER_WIDTH-1; x >= 0; x--) { 
			for(int y = 0; y < LAYER_HEIGHT; y++) {
				if (pixels->data[y][x] != BLACK_BACKGROUND_PIXEL_COLOUR)
				{
					rightmost_x = x+1;
					//Serial.printf("Right most x pixel is %d\n", rightmost_x);					
					goto centreit;
				}
			}
		}

		centreit:
			adjusted_leftmost_x = ( LAYER_WIDTH - (rightmost_x - leftmost_x))/2;
			//Serial.printf("Adjusted: %d, Moving x coords by %d pixels.\n", adjusted_leftmost_x, adjusted_leftmost_x-leftmost_x);
			moveX(adjusted_leftmost_x-leftmost_x);
  } // end autoCentreX
  
  void Layer::moveY(int delta)
  {
	// Not implemented
  }


Layer::~Layer(void)
{
  free(pixels);
}
	



/* Merge FastLED layers into a super layer and display. Definition */
namespace LayerCompositor
{
	 	/*
		 * Display the foreground pixels if they're not the background/transparent color.
		 * If not, then fill with whatever is in the background.
		 * 
		 * writeToBg = write the result back to the _bgLayer, and not directly to the output device!
		 * 			   -> no need to do a subsequent bgLayer.display() otherwise.
		 */
        void Stack(RGB64x32MatrixPanel_I2S_DMA &disp, const Layer &_bgLayer, const Layer &_fgLayer, bool writeBackToBg)
		{
				for (int y = 0; y < LAYER_HEIGHT; y++) {
					for (int x = 0; x < LAYER_WIDTH; x++)
					{
						//https://www.educative.io/edpresso/how-to-resolve-the-expression-must-have-class-type-error-in-cpp
						if (_fgLayer.pixels->data[y][x] == _fgLayer.transparency_colour) // foreground is transparent, show the _bgLayer colors
						{
							if (writeBackToBg) // write the foreground to the background layer... perhaps so we can do stuff later with the _fgLayer.
								_bgLayer.pixels->data[y][x]	= _bgLayer.pixels->data[y][x];
							else
								disp.drawPixelRGB888(x,y, _bgLayer.pixels->data[y][x].r, _bgLayer.pixels->data[y][x].g, _bgLayer.pixels->data[y][x].b );

						} // if the foreground is NOT transparent, then print whatever is the bg
						else
						{
							if (writeBackToBg) // write the foreground to the background layer... perhaps so we can do stuff later with the _fgLayer.
								_bgLayer.pixels->data[y][x]	= _fgLayer.pixels->data[y][x];
							else
								disp.drawPixelRGB888(x,y, _fgLayer.pixels->data[y][x].r, _fgLayer.pixels->data[y][x].g, _fgLayer.pixels->data[y][x].b );
						}

					} // end x loop
				} // end y loop
		}  // end stack


	 	/*
		 * Where the foreground pixels are not the background/transparent color, populate with 
		 * whatever is in the background.
		 */
        void Siloette(RGB64x32MatrixPanel_I2S_DMA &disp, const Layer &_bgLayer, const Layer &_fgLayer)
		{
				//const Layer *bg = &_bgLayer;
				//const Layer *fg = &_fgLayer;

				for (int y = 0; y < LAYER_HEIGHT; y++) {
					for (int x = 0; x < LAYER_WIDTH; x++)
					{
						//https://www.educative.io/edpresso/how-to-resolve-the-expression-must-have-class-type-error-in-cpp
						if (_fgLayer.pixels->data[y][x] != _fgLayer.transparency_colour)
						{
							disp.drawPixelRGB888(x,y, _bgLayer.pixels->data[y][x].r, _bgLayer.pixels->data[y][x].g, _bgLayer.pixels->data[y][x].b );
						} // if the foreground is transparent, then print whatever is the bg
						else
						{
							disp.drawPixelRGB888(x,y, 0,0,0);
						}

					} // end x loop
				} // end y loop
		}  // end stack		





		void Blend(RGB64x32MatrixPanel_I2S_DMA &disp, const Layer &_bgLayer, const Layer &_fgLayer, uint8_t ratio)
		{
			CRGB _pixel = 0 ;

			for (int y = 0; y < LAYER_HEIGHT; y++) 
			{
				for (int x = 0; x < LAYER_WIDTH; x++)
				{
						/*
						  	// set the blend ratio for the video cross fade
							// (set ratio to 127 for a constant 50% / 50% blend)
							uint8_t ratio = beatsin8(5);
						*/
						
						_pixel = blend(_bgLayer.pixels->data[y][x], _fgLayer.pixels->data[y][x], ratio);

						// https://gist.github.com/StefanPetrick/0c0d54d0f35ea9cca983
						disp.drawPixelRGB888(x,y, _pixel.r, _pixel.g, _pixel.b );

				} // end x loop
			} // end y loop



		} // end blend
}