From 55d0902bb19940e1284e257505fce5869318c66d Mon Sep 17 00:00:00 2001 From: David Todd Date: Mon, 4 Mar 2019 19:22:29 -0600 Subject: [PATCH] first attempt I basically copied all my custom functions into the cpp file and put them in a namespace, then followed the arduino library tutorial to create the header file. This does not compile because IDK how to create an instance of Adafruit_DotStarMatrix (called matrix) --- pixo_displaylib.cpp | 237 ++++++++++++++++++++++++++++++++++++++++++++ pixo_displaylib.h | 71 +++++++++++++ 2 files changed, 308 insertions(+) create mode 100644 pixo_displaylib.cpp create mode 100644 pixo_displaylib.h diff --git a/pixo_displaylib.cpp b/pixo_displaylib.cpp new file mode 100644 index 0000000..1521dfe --- /dev/null +++ b/pixo_displaylib.cpp @@ -0,0 +1,237 @@ +#include "pixo_displaylib.h" +#include + +// Constructor for the library. Use this for init-logic, +// and setting of private variables +pixo_displaylib::pixo_displaylib(int max_brightness) : Adafruit_DotStarMatrix::Adafruit_DotStarMatrix(int w, int h, uint8_t d, uint8_t c, uint8_t matrixType, uint8_t ledType) +{ + BRIGHTNESS = max_brightness; + + Adafruit_DotStarMatrix matrix = Adafruit_DotStarMatrix(ROWS, ROWS, DATAPIN, CLOCKPIN, MCONFIG, BGR) +} + +void pixo_displaylib::begin() { + matrix.begin(); + matrix.setBrightness(BRIGHTNESS); + + pixo_displaylib::PXL pxl; + pixo_displaylib::PIXOLS pixols; + + // Initialize our pixel structure + for (int cnt = 0; cnt < TOTALPXLS; cnt++) { + pixols[cnt].active = false; + pixols[cnt].id = cnt; + } +} + +// Returns the number of pixols who's active state is true +int pixo_displaylib::activePixelCount() { + int totalActive = 0; + for (int cnt = 0; cnt < TOTALPXLS; cnt++) { + if (pixols[cnt].active == true) totalActive++; + } + return totalActive; +} + +// Sets the active state of all pixols to false +void pixo_displaylib::resetActiveState() { + for (int i = 0; i < TOTALPXLS; i++) + pixols[i].active = false; +} + +/* + * showPxl, displays a Pixol to the display with no animation + * args: + * - pxl[TOTALPXLS][3] - Standard Pixol image + * - dlytime - The amount of time (in ms) to keep the image on the display + * - color (optional) - Whether or not to set the active pixel's color to its stored value + */ +void pixo_displaylib::showPxl(int pxl[TOTALPXLS][3], int dlytime, bool color=false) { + resetActiveState(); + + for (int i=0; i < TOTALPXLS; i++){ + if (pxl[i][0] != 0 && pxl[i][1] != 0 && pxl[i][2] != 0) { + pixols[i].active = true; + } + + if (color && pixols[i].active) { + matrix.setPassTrhuColor(matrix.color(pixols[i].r, + pixols[i].g, + pixols[i].b)) + } + + if (!color) { + matrix.setPixelColor(i, pxl[i][0], pxl[i][1], pxl[i][2]); + }else { + if (pixols[i].active) { + + + matrix.setPixelColor(i, pxl[i][], + pxl[i][pixols[i].g], + pxl[i][pixols[i].b]); + } + + } + } + + matrix.show(); + delay(dlytime); + + matrix.fillScreen(0); + matrix.show(); +} + +// Notification functions + +/* + * notifyPopupPxl, draws a Pixol that scrolls into view, and back down + * args: + * - pxl[TOTALPXLS][3] - Standard Pixol image + * - dlytime - The amount of time (in ms) to keep the image on the display + */ +void pixo_displaylib::notifyPopupPxl(int pxl[TOTALPXLS][3], int dlytime) { + for (int j = 15; j >= 0; j--) { + for (int i = 0; i < TOTALPXLS; i++) { + matrix.setPixelColor(i + (j * 16), + pxl[i][0], pxl[i][1], pxl[i][2]); + } + matrix.show(); + delay(50); + } + + delay(dlytime); + + for (int j = 0; j <= 16; j++) { + for (int i = 0; i < TOTALPXLS; i++) { + matrix.setPixelColor(i + (j * 16), + pxl[i][0], pxl[i][1], pxl[i][2]); + } + matrix.show(); + delay(50); + } + + delay(100); +} + +/* + * notifyScrollPxl, draws a Pixol that scrolls upward into view, and upwards out; Loop this to scroll X times + * args: + * - pxl[TOTALPXLS][3] - Standard Pixol image + * - dlytime - The amount of time (in ms) to keep the image on the display + * - loops (optional) - The total amount of times this should loop through + */ +void pixo_displaylib::notifyScrollPxl(int pxl[TOTALPXLS][3], int dlytime, int loops=0) { + for (int x=0; x<=loops; x++) { + for (int j = 15; j >= 0; j--) { + for (int i = 0; i < TOTALPXLS; i++) { + matrix.setPixelColor(i + (j * 16), + pxl[i][0], pxl[i][1], pxl[i][2]); + } + matrix.show(); + delay(15); + } + + delay(dlytime); + + for (int j = 0; j >= -16; j--) { + for (int i = 0; i < TOTALPXLS; i++) { + matrix.setPixelColor(i + (j * 16), + pxl[i][0], pxl[i][1], pxl[i][2]); + } + matrix.show(); + delay(15); + } + + delay(100); + } +} + +/* + * notifyBlinkPxl, flash a Pixol on screen repeatedly + * args: + * - pxl[TOTALPXLS][3] - Standard Pixol image, displayed all at once + * - dlytime - The amount of time (in ms) to keep the image on the display + * - loops (optional) - The total amount of times this should blink + * - color (optional) - Whether or not to set the active pixel's color to its stored value + */ +void pixo_displaylib::notifyBlinkPxl(int pxl[TOTALPXLS][3], int dlytime, int loops=0, bool color=false) { + for (int i = 0; i <= loops; i++) { + showPxl(pxl, dlytime, color); + showPxl(darkness, dlytime, color); + } +} + +// "Animation" or picture transition functions + +/* + * scanChangePxl, Change from one Pixol to another with a scanline appearence + * args: + * - src[TOTALPXLS][3] - Standard Pixo-Style icon that we change from, displayed all at once + * - dst[TOTALPXLS][3] - Standard Pixo-Style icon that we change to, displayed one pixel at a time + * - dlytime - The amount of time (in ms) to keep the image on the display + * - anitime - The amount of time (in ms) between drawing each pixel during the transition; the lower this number, the faster the transition + */ +void pixo_displaylib::scanChangePxl(int src[TOTALPXLS][3], int dst[TOTALPXLS][3], int dlytime, int anitime) { + for (int i=0; i= TOTALPXLS) break; + } + + delay(dlytime); +} \ No newline at end of file diff --git a/pixo_displaylib.h b/pixo_displaylib.h new file mode 100644 index 0000000..1c75c0b --- /dev/null +++ b/pixo_displaylib.h @@ -0,0 +1,71 @@ +/* + Pixo-Pixel Display Library + The MIT License (MIT) + Copyright (c) 2019 c0de (c0de@c0defox.es) + + 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. + + ----------------------------------------------------------------------------- + + This is a wrapper for the DotStar libraries, and makes a lot of assumptions + that I think are fine. Just in case they aren't, I'm open to feedback + and/or pull requests to help improve this library. +*/ + +#ifndef pixo_displaylib_h +#define pixo_displaylib_h + +// Load GFX and DotStar libraries +#include "Arduino.h" +#include + +// Pre-assuming these values as this is the Pixo-Pixel configuration +#define DATAPIN 19 +#define CLOCKPIN 18 +#define TOTALPXLS 256 // The total number of LEDs +#define ROWS 16 +#define MCONFIG 0x00000000 +#define BGR (2 | (1 << 2) | (0 << 4)) + +class pixo_displaylib : public Adafruit_DotStarMatrix { + public: + pixo_displaylib(int max_brightness); + void begin(); // Initializes the pixol structure + int activePixelCount(); // Returns the number of active pixels + void resetActiveState(); // Sets all pixels to inactive + void showPxl(int pxl[TOTALPXLS][3], int dlytime, bool color=false); // Displays the image on screen + void notifyPopupPxl(int pxl[TOTALPXLS][3], int dlytime); // Pops an image up and back down + void notifyScrollPxl(int pxl[TOTALPXLS][3], int dlytime, int loops=0); // Makes an image scroll upwards + void notifyBlinkPxl(int pxl[TOTALPXLS][3], int dlytime, int loops=0, bool color=false); // Makes an image blink + void scanChangePxl(int src[TOTALPXLS][3], int dst[TOTALPXLS][3], int dlytime, int anitime); // Transition from one image to another, one pixel at a time + void randChangePxl(int src[TOTALPXLS][3], int dst[TOTALPXLS][3], int dlytime, int anitime); // Transition from one image to another, one pixel at a time, randomly + private: + int BRIGHTNESS; // Sets the brightness + struct PXL { + byte id: 1; + byte r: 1; + byte g: 1; + byte b: 1; + byte active: 1; + }; // Structure that contains settings of each pixel + struct PXL PIXOLS[TOTALPXLS]; // Structure that contains all pixels + Adafruit_DotStarMatrix matrix; +}; + +#endif \ No newline at end of file