Skip to content

Instantly share code, notes, and snippets.

@ScienceElectronicsFun
Last active June 20, 2022 01:56
Show Gist options
  • Save ScienceElectronicsFun/b6ab566fddc12b0768612b0ea64133b2 to your computer and use it in GitHub Desktop.
Save ScienceElectronicsFun/b6ab566fddc12b0768612b0ea64133b2 to your computer and use it in GitHub Desktop.
/*
*
* USING CODE FROM SmartMatrix and Linked List https://github.com/ivanseidel/LinkedList
* Modified for Matrix Falling Code effect
*
* This example shows how to display bitmaps that are exported from GIMP and
* compiled into the sketch and stored in the Teensy's Flash memory
* See more details here:
* http://docs.pixelmatix.com/SmartMatrix/library.html#smartmatrix-library-how-to-use-the-library-drawing-raw-bitmaps
*
* This example uses only the SmartMatrix Background layer
*/
// uncomment one line to select your MatrixHardware configuration - configuration header needs to be included before <SmartMatrix.h>
//#include <MatrixHardware_Teensy3_ShieldV4.h> // SmartLED Shield for Teensy 3 (V4)
#include <MatrixHardware_Teensy4_ShieldV5.h> // SmartLED Shield for Teensy 4 (V5)
//#include <MatrixHardware_Teensy3_ShieldV1toV3.h> // SmartMatrix Shield for Teensy 3 V1-V3
//#include <MatrixHardware_Teensy4_ShieldV4Adapter.h> // Teensy 4 Adapter attached to SmartLED Shield for Teensy 3 (V4)
//#include <MatrixHardware_ESP32_V0.h> // This file contains multiple ESP32 hardware configurations, edit the file to define GPIOPINOUT (or add #define GPIOPINOUT with a hardcoded number before this #include)
//#include "MatrixHardware_Custom.h" // Copy an existing MatrixHardware file to your Sketch directory, rename, customize, and you can include it like this
#include <SmartMatrix.h>
#include <LinkedList.h>
#include "gimpbitmap.h"
#define COLOR_DEPTH 24 // Choose the color depth used for storing pixels in the layers: 24 or 48 (24 is good for most sketches - If the sketch uses type `rgb24` directly, COLOR_DEPTH must be 24)
const uint16_t kMatrixWidth = 64; // Set to the width of your display, must be a multiple of 8
const uint16_t kMatrixHeight = 64; // Set to the height of your display
const uint8_t kRefreshDepth = 36; // Tradeoff of color quality vs refresh rate, max brightness, and RAM usage. 36 is typically good, drop down to 24 if you need to. On Teensy, multiples of 3, up to 48: 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48. On ESP32: 24, 36, 48
const uint8_t kDmaBufferRows = 4; // known working: 2-4, use 2 to save RAM, more to keep from dropping frames and automatically lowering refresh rate. (This isn't used on ESP32, leave as default)
const uint8_t kPanelType = SM_PANELTYPE_HUB75_64ROW_MOD32SCAN; // Choose the configuration that matches your panels. See more details in MatrixCommonHub75.h and the docs: https://github.com/pixelmatix/SmartMatrix/wiki
const uint32_t kMatrixOptions = (SM_HUB75_OPTIONS_NONE); // see docs for options: https://github.com/pixelmatix/SmartMatrix/wiki
const uint8_t kBackgroundLayerOptions = (SM_BACKGROUND_OPTIONS_NONE);
SMARTMATRIX_ALLOCATE_BUFFERS(matrix, kMatrixWidth, kMatrixHeight, kRefreshDepth, kDmaBufferRows, kPanelType, kMatrixOptions);
SMARTMATRIX_ALLOCATE_BACKGROUND_LAYER(backgroundLayer, kMatrixWidth, kMatrixHeight, COLOR_DEPTH, kBackgroundLayerOptions);
int led = -1; // Set to -1 to disable LED flash, only needed for debugging purposes
//int led = 13; // builtin LED pin on the Teensy, interferes with refresh on Teensy 4
class pix_ {
// pix class represents a pixel with x, y coordinates and r g b colors.
public:
int x;
int y;
int r;
int g;
int b;
int ticks;
int max_ticks;
};
LinkedList<pix_*> PList = LinkedList<pix_*>();
void setup() {
matrix.addLayer(&backgroundLayer);
matrix.begin();
matrix.setBrightness(128);
if(led >= 0) pinMode(led, OUTPUT);
}
void loop() {
int theSize = PList.size();
//At each time tick, add a new falling pixel. Max, 1000.
if (theSize < 1000) {
pix_ *a = new pix_();
a->x = 0;
a->y = random(1,64);
a->r = random(1,10);
a->g = random(250,255);
a->b = random(1,10);
a->ticks = random(1,5); //counter that you'd decrement
a->max_ticks = a->ticks; //what you reset to when counter == 0
// Add pix to list
PList.add(a);
}
//Clear the screen
backgroundLayer.fillScreen({0,0,0});
//Pointer to pixel object
pix_ *pix_;
//Draw all the pixels in the list. Include tailing effect.
//Increment x
//If x == 64, remove the object from the list. Important to delete to avoid memory leak.
for(int i = 0; i < PList.size(); i++){
pix_ = PList.get(i);
SM_RGB pixel = { pix_->r,
pix_->g,
pix_->b};
backgroundLayer.drawPixel(pix_->x, pix_->y, pixel);
SM_RGB pixel2 = { pix_->r,
100,
pix_->b};
backgroundLayer.drawPixel(pix_->x-1, pix_->y, pixel2);
SM_RGB pixel3 = { pix_->r,
50,
pix_->b};
backgroundLayer.drawPixel(pix_->x-2, pix_->y, pixel3);
//Increment pixel x coordinate value.
pix_->ticks -= 1;
if (pix_->ticks == 0){
pix_->x += 1;
pix_->ticks = pix_->max_ticks;
}
//If a pixel at end of display, delete it.
int z = pix_->x;
if (z == 64){
pix_ = PList.remove(i);
delete pix_;
}
}
backgroundLayer.swapBuffers();
delay(7.5);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment