From 82326230eb5d9bffabf81b88cd65c059b61a6921 Mon Sep 17 00:00:00 2001 From: mrfaptastic <12006953+mrfaptastic@users.noreply.github.com> Date: Mon, 7 Dec 2020 22:44:56 +0000 Subject: [PATCH] Updated GraphicsLayer example --- examples/GraphicsLayer/GraphicsLayer.ino | 81 ++++++++++++++---------- examples/GraphicsLayer/Layer.cpp | 12 ++-- examples/GraphicsLayer/Layer.h | 18 +++--- 3 files changed, 66 insertions(+), 45 deletions(-) diff --git a/examples/GraphicsLayer/GraphicsLayer.ino b/examples/GraphicsLayer/GraphicsLayer.ino index e8a16d9..fbc33ad 100644 --- a/examples/GraphicsLayer/GraphicsLayer.ino +++ b/examples/GraphicsLayer/GraphicsLayer.ino @@ -1,26 +1,12 @@ /* - * Portions of this code are adapted from Aurora: https://github.com/pixelmatix/aurora - * Copyright (c) 2014 Jason Coon - * - * 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 - * - * 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. + * An example that writes to a CRGB FastLED pixel buffer before being ultimately sent + * to the DMA Display. + * + * Faptastic 2020 + * + * 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 @@ -41,17 +27,20 @@ #define CLK_PIN 14 #define LAT_PIN 15 #define OE_PIN 13 - - -#include -MatrixPanel_I2S_DMA dma_display; -#include + +#include // 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 + 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); } } @@ -105,5 +114,13 @@ void loop() { time_counter = 0; 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 \ No newline at end of file +} // end loop diff --git a/examples/GraphicsLayer/Layer.cpp b/examples/GraphicsLayer/Layer.cpp index 29ea97f..5c419c1 100644 --- a/examples/GraphicsLayer/Layer.cpp +++ b/examples/GraphicsLayer/Layer.cpp @@ -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 ; @@ -329,7 +332,8 @@ namespace LayerCompositor // (set ratio to 127 for a constant 50% / 50% blend) 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 diff --git a/examples/GraphicsLayer/Layer.h b/examples/GraphicsLayer/Layer.h index 69a5886..aa93c8f 100644 --- a/examples/GraphicsLayer/Layer.h +++ b/examples/GraphicsLayer/Layer.h @@ -23,12 +23,12 @@ #endif #include -#include +#include /* * 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