Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <FastLED.h>
- #include <LEDMatrix.h>
- // Change the next 6 defines to match your matrix type and size
- #define LED_PIN 2
- #define COLOR_ORDER GRB
- #define CHIPSET WS2812B
- #define BRIGHTNESS 32
- #define MATRIX_WIDTH 8
- #define MATRIX_HEIGHT 8
- #define MATRIX_TYPE VERTICAL_ZIGZAG_MATRIX
- const uint8_t kCubeSize = 8;
- const bool kMatrixSerpentineLayout = false;
- typedef enum {X_LAYER, Y_LAYER, Z_LAYER} LAYER;
- typedef enum {UP, DOWN} DIRECTION;
- bool bAnimate = false;
- // Dictates the direction of the frames
- DIRECTION fillDirection = UP;
- // Which axis are we filling the frames
- LAYER layer = Y_LAYER;
- // Which frame number gets the new noise data
- int nFillFrame;
- #define NUM_LEDS (kCubeSize * kCubeSize * kCubeSize)
- #define MAX_DIMENSION kCubeSize
- // The leds
- CRGB leds[NUM_LEDS];
- cLEDMatrix<kCubeSize, kCubeSize, MATRIX_TYPE> inputFrame;
- uint8_t hue;
- int16_t counter;
- void setup()
- {
- FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
- LEDS.setBrightness(BRIGHTNESS);
- FastLED.clear(true);
- hue = 0;
- counter = 0;
- if (fillDirection == UP) nFillFrame = 0; else nFillFrame = kCubeSize-1;
- }
- // Copies pixels from one layer to another
- void copyLayer(uint8_t fromLayer, uint8_t toLayer);
- void copyLayer(uint8_t fromLayer, uint8_t toLayer)
- {
- int nFrom; // from pixel
- int nTo; // to pixel
- for (int m = 0; m < kCubeSize; m++)
- for (int n = 0; n < kCubeSize; n++)
- {
- switch (layer)
- {
- case X_LAYER:
- nFrom = XYZ(fromLayer, m, n);
- nTo = XYZ(toLayer, m, n);
- break;
- case Y_LAYER:
- nFrom = XYZ(m, fromLayer, n);
- nTo = XYZ(m, toLayer, n);
- break;
- case Z_LAYER:
- nFrom = XYZ(m, n, fromLayer);
- nTo = XYZ(m, n, toLayer);
- break;
- }
- leds[nTo] = leds[nFrom];
- }
- }
- // Copy frames foward in the plane being used from the fillFrame back
- void moveFrameForward()
- {
- switch (fillDirection)
- {
- case UP:
- {
- for (int nLayer = kCubeSize-1; nLayer > 0; nLayer--)
- {
- if (bAnimate)
- copyLayer(nLayer-1, nLayer);
- else
- copyLayer(nFillFrame, nLayer);
- }
- }
- break;
- case DOWN:
- {
- for (int nLayer = 0; nLayer < kCubeSize; nLayer++)
- {
- if (bAnimate)
- copyLayer(nLayer+1, nLayer);
- else
- copyLayer(nFillFrame, nLayer);
- }
- }
- break;
- }
- }
- void copyFromFrameToLEDs()
- {
- for(int i = 0; i < kCubeSize; i++) {
- for(int j = 0; j < kCubeSize; j++) {
- static int nTo;
- switch (layer)
- {
- case X_LAYER:
- nTo = XYZ(nFillFrame, i, j);
- break;
- case Y_LAYER:
- nTo = XYZ(i, nFillFrame, j);
- break;
- case Z_LAYER:
- nTo = XYZ(i, j, nFillFrame);
- break;
- }
- leds[nTo] = inputFrame(i, j);
- }
- }
- }
- void loop()
- {
- int16_t sx, sy, x, y;
- uint8_t h;
- h = hue;
- if (counter < 1125)
- {
- // ** Fill LED's with diagonal stripes
- sx = sy = 0;
- while (sy < MATRIX_HEIGHT)
- {
- x = sx;
- y = sy;
- while ((x >= 0) && (y < MATRIX_HEIGHT))
- {
- inputFrame(x, y) = CHSV(h, 255, 255);
- x--;
- y++;
- }
- h+=16;
- if (sx < (MATRIX_WIDTH - 1))
- sx++;
- else
- sy++;
- }
- }
- else
- {
- // ** Fill LED's with horizontal stripes
- for (y=0; y<MATRIX_HEIGHT; y++)
- {
- for (x=0; x<MATRIX_WIDTH; x++)
- inputFrame(x, y) = CHSV(h, 255, 255);
- h+=16;
- }
- }
- hue+=4;
- if (counter < 125)
- ;
- else if (counter < 375)
- inputFrame.HorizontalMirror();
- else if (counter < 625)
- inputFrame.VerticalMirror();
- else if (counter < 875)
- inputFrame.QuadrantMirror();
- else if (counter < 1125)
- inputFrame.QuadrantRotateMirror();
- else if (counter < 1250)
- ;
- else if (counter < 1500)
- inputFrame.TriangleTopMirror();
- else if (counter < 1750)
- inputFrame.TriangleBottomMirror();
- else if (counter < 2000)
- inputFrame.QuadrantTopTriangleMirror();
- else if (counter < 2250)
- inputFrame.QuadrantBottomTriangleMirror();
- counter++;
- if (counter >= 2250)
- counter = 0;
- copyFromFrameToLEDs();
- if (bAnimate)
- {
- LEDS.show();
- delay(20);
- // Copy frames into time after showing
- moveFrameForward();
- }
- else
- {
- // mirror to other frames before show
- moveFrameForward();
- LEDS.show();
- delay(20);
- }
- }
- //
- // xyz coordinate mapping code
- //
- uint16_t XYZ( uint8_t x, uint8_t y, uint8_t z)
- {
- uint16_t i;
- i = 8 * ((y * kCubeSize) + x) + z;
- if ((i >= 0) && (i <= 512))
- return i;
- else
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment