Updated GraphicsLayer example
This commit is contained in:
parent
76351960b8
commit
82326230eb
3 changed files with 66 additions and 45 deletions
|
@ -1,26 +1,12 @@
|
|||
/*
|
||||
* Portions of this code are adapted from Aurora: https://github.com/pixelmatix/aurora
|
||||
* Copyright (c) 2014 Jason Coon
|
||||
* An example that writes to a CRGB FastLED pixel buffer before being ultimately sent
|
||||
* to the DMA Display.
|
||||
*
|
||||
* Portions of this code are adapted from LedEffects Plasma by Robert Atkins: https://bitbucket.org/ratkins/ledeffects/src/26ed3c51912af6fac5f1304629c7b4ab7ac8ca4b/Plasma.cpp?at=default
|
||||
* Copyright (c) 2013 Robert Atkins
|
||||
* Faptastic 2020
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
* Note:
|
||||
* * Layers use lots of RAM (3*WIDTH*HEIGHT bytes per layer to be precise), so use at your own risk.
|
||||
* * Make sure LAYER_WIDTH and LAYER_HEIGHT are correctly configured in Layer.h !!!
|
||||
*/
|
||||
|
||||
//#define USE_CUSTOM_PINS // uncomment to use custom pins, then provide below
|
||||
|
@ -43,15 +29,18 @@
|
|||
#define OE_PIN 13
|
||||
|
||||
|
||||
#include <ESP32-HUB75-MatrixPanel-I2S-DMA.h>
|
||||
MatrixPanel_I2S_DMA dma_display;
|
||||
#include <FastLED.h> // FastLED needs to be installed.
|
||||
#include "Layer.h" // Layer Library
|
||||
#include "Fonts/FreeSansBold9pt7b.h" // include adafruit font
|
||||
|
||||
// Object creation
|
||||
MatrixPanel_I2S_DMA dma_display; // Create HUB75 DMA object
|
||||
Layer bgLayer(dma_display); // Create background Layer
|
||||
Layer textLayer(dma_display); // Create foreground Layer
|
||||
|
||||
#include <FastLED.h>
|
||||
|
||||
int time_counter = 0;
|
||||
int cycles = 0;
|
||||
|
||||
|
||||
CRGBPalette16 currentPalette;
|
||||
CRGB currentColor;
|
||||
|
||||
|
@ -80,6 +69,21 @@ void setup() {
|
|||
// Set current FastLED palette
|
||||
currentPalette = RainbowColors_p;
|
||||
|
||||
// Allocate Background Layer
|
||||
bgLayer.init();
|
||||
bgLayer.clear();
|
||||
bgLayer.setTransparency(false);
|
||||
|
||||
// Allocate Canvas Layer
|
||||
textLayer.init();
|
||||
textLayer.clear();
|
||||
|
||||
/* Step 1: Write some pixels to foreground Layer (use custom layer function)
|
||||
* Only need to do this once as we're not changing it ever again in this example.
|
||||
*/
|
||||
textLayer.drawCentreText("COOL!", MIDDLE, &FreeSansBold9pt7b, CRGB(255,255,255));
|
||||
textLayer.autoCenterX(); // because I don't trust AdaFruit to perfectly place the contents in the middle
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
@ -92,8 +96,13 @@ void loop() {
|
|||
v += cos16(y * (128 - wibble) + time_counter);
|
||||
v += sin16(y * x * cos8(-time_counter) / 8);
|
||||
|
||||
currentColor = ColorFromPalette(currentPalette, (v >> 8) + 127); //, brightness, currentBlendType);
|
||||
dma_display.drawPixelRGB888(x, y, currentColor.r, currentColor.g, currentColor.b);
|
||||
currentColor = ColorFromPalette(currentPalette, (v >> 8) + 127); //, brightness, currentBlendType);
|
||||
|
||||
/*
|
||||
* Step 2: Write to Background layer! Don't show it on the screen just yet.
|
||||
* Note: Layer class is designed for FastLED 'CRGB' data type.
|
||||
*/
|
||||
bgLayer.drawPixel(x, y, currentColor);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -106,4 +115,12 @@ void loop() {
|
|||
cycles = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Step 3: Merge foreground and background layers and send to the matrix panel!
|
||||
* Use our special sauce LayerCompositor functions
|
||||
*/
|
||||
LayerCompositor::Siloette(dma_display, bgLayer, textLayer);
|
||||
//LayerCompositor::Stack(dma_display, bgLayer, textLayer);
|
||||
// LayerCompositor::Blend(dma_display, bgLayer, textLayer, 127);
|
||||
|
||||
} // end loop
|
|
@ -138,8 +138,11 @@ void Layer::drawCentreText(const char *buf, textPosition textPos, const GFXfont
|
|||
{
|
||||
int wstart = 0;
|
||||
|
||||
/*
|
||||
if (w > 42) wstart = (LAYER_WIDTH - w) / 2;
|
||||
else wstart = (LAYER_WIDTH - w) / 2;
|
||||
*/
|
||||
wstart = (LAYER_WIDTH - w) / 2;
|
||||
|
||||
if (textPos == TOP) {
|
||||
setCursor(wstart, h+yadjust); // top
|
||||
|
@ -259,7 +262,7 @@ namespace LayerCompositor
|
|||
* 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)
|
||||
void Stack(MatrixPanel_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++)
|
||||
|
@ -290,7 +293,7 @@ namespace LayerCompositor
|
|||
* 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)
|
||||
void Siloette(MatrixPanel_I2S_DMA &disp, const Layer &_bgLayer, const Layer &_fgLayer)
|
||||
{
|
||||
//const Layer *bg = &_bgLayer;
|
||||
//const Layer *fg = &_fgLayer;
|
||||
|
@ -316,7 +319,7 @@ namespace LayerCompositor
|
|||
|
||||
|
||||
|
||||
void Blend(RGB64x32MatrixPanel_I2S_DMA &disp, const Layer &_bgLayer, const Layer &_fgLayer, uint8_t ratio)
|
||||
void Blend(MatrixPanel_I2S_DMA &disp, const Layer &_bgLayer, const Layer &_fgLayer, uint8_t ratio)
|
||||
{
|
||||
CRGB _pixel = 0 ;
|
||||
|
||||
|
@ -330,6 +333,7 @@ namespace LayerCompositor
|
|||
uint8_t ratio = beatsin8(5);
|
||||
*/
|
||||
|
||||
// (set ratio to 127 for a constant 50% / 50% blend)
|
||||
_pixel = blend(_bgLayer.pixels->data[y][x], _fgLayer.pixels->data[y][x], ratio);
|
||||
|
||||
// https://gist.github.com/StefanPetrick/0c0d54d0f35ea9cca983
|
||||
|
|
|
@ -23,12 +23,12 @@
|
|||
#endif
|
||||
|
||||
#include <FastLed.h>
|
||||
#include <ESP32-RGB64x32MatrixPanel-I2S-DMA.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_WIDTH 64
|
||||
#define LAYER_HEIGHT 32
|
||||
|
||||
|
||||
|
@ -44,9 +44,9 @@ struct layerPixels {
|
|||
};
|
||||
|
||||
#ifdef USE_GFX_ROOT
|
||||
class Layer : public GFX {
|
||||
class Layer : public GFX
|
||||
#else
|
||||
class Layer : public Adafruit_GFX {
|
||||
class Layer : public Adafruit_GFX
|
||||
#endif
|
||||
// class Layer : public GFX // use GFX Root for now
|
||||
{
|
||||
|
@ -55,7 +55,7 @@ class Layer : public Adafruit_GFX {
|
|||
// Static allocation of memory for layer
|
||||
//CRGB pixels[LAYER_WIDTH][LAYER_HEIGHT] = {{0}};
|
||||
|
||||
Layer(RGB64x32MatrixPanel_I2S_DMA &disp) : GFX (LAYER_WIDTH, LAYER_HEIGHT) {
|
||||
Layer(MatrixPanel_I2S_DMA &disp) : GFX (LAYER_WIDTH, LAYER_HEIGHT) {
|
||||
matrix = &disp;
|
||||
}
|
||||
|
||||
|
@ -107,16 +107,16 @@ class Layer : public Adafruit_GFX {
|
|||
~Layer(void);
|
||||
|
||||
private:
|
||||
RGB64x32MatrixPanel_I2S_DMA *matrix = NULL;
|
||||
MatrixPanel_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);
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue