pchestna

L3D Cube Color Shift

Dec 27th, 2014
678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.79 KB | None | 0 0
  1. #include <FastLED.h>
  2. #include <LEDMatrix.h>
  3.  
  4. // Change the next 6 defines to match your matrix type and size
  5.  
  6. #define LED_PIN        2
  7. #define COLOR_ORDER    GRB
  8. #define CHIPSET        WS2812B
  9. #define BRIGHTNESS  32
  10.  
  11. #define MATRIX_WIDTH   8
  12. #define MATRIX_HEIGHT  8
  13. #define MATRIX_TYPE    VERTICAL_ZIGZAG_MATRIX
  14.  
  15. const uint8_t kCubeSize = 8;
  16. const bool    kMatrixSerpentineLayout = false;
  17.  
  18. typedef enum {X_LAYER, Y_LAYER, Z_LAYER} LAYER;
  19. typedef enum {UP, DOWN} DIRECTION;
  20. bool bAnimate = false;
  21.  
  22. // Dictates the direction of the frames
  23. DIRECTION fillDirection = UP;
  24. // Which axis are we filling the frames
  25. LAYER layer = Y_LAYER;
  26. // Which frame number gets the new noise data
  27. int nFillFrame;
  28.  
  29. #define NUM_LEDS (kCubeSize * kCubeSize * kCubeSize)
  30. #define MAX_DIMENSION kCubeSize
  31.  
  32. // The leds
  33. CRGB leds[NUM_LEDS];
  34.  
  35. cLEDMatrix<kCubeSize, kCubeSize, MATRIX_TYPE> inputFrame;
  36.  
  37. uint8_t hue;
  38. int16_t counter;
  39.  
  40. void setup()
  41. {
  42.   FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  43.   LEDS.setBrightness(BRIGHTNESS);
  44.   FastLED.clear(true);
  45.   hue = 0;
  46.   counter = 0;
  47.  
  48.   if (fillDirection == UP) nFillFrame = 0; else nFillFrame = kCubeSize-1;
  49. }
  50.  
  51. // Copies pixels from one layer to another
  52. void copyLayer(uint8_t fromLayer, uint8_t toLayer);
  53. void copyLayer(uint8_t fromLayer, uint8_t toLayer)
  54. {
  55.   int nFrom; // from pixel
  56.   int nTo; // to pixel
  57.  
  58.   for (int m = 0; m < kCubeSize; m++)
  59.     for (int n = 0; n < kCubeSize; n++)
  60.     {
  61.       switch (layer)
  62.       {
  63.          case X_LAYER:
  64.            nFrom = XYZ(fromLayer, m, n);
  65.            nTo = XYZ(toLayer, m, n);
  66.          break;
  67.          case Y_LAYER:
  68.            nFrom = XYZ(m, fromLayer, n);
  69.            nTo = XYZ(m, toLayer, n);
  70.          break;
  71.          case Z_LAYER:
  72.            nFrom = XYZ(m, n, fromLayer);
  73.            nTo = XYZ(m, n, toLayer);
  74.          break;
  75.       }
  76.       leds[nTo] = leds[nFrom];
  77.     }
  78. }
  79.  
  80. // Copy frames foward in the plane being used from the fillFrame back
  81. void moveFrameForward()
  82. {
  83.   switch (fillDirection)
  84.   {
  85.     case UP:
  86.     {
  87.       for (int nLayer = kCubeSize-1; nLayer > 0; nLayer--)
  88.       {
  89.         if (bAnimate)
  90.           copyLayer(nLayer-1, nLayer);
  91.         else
  92.           copyLayer(nFillFrame, nLayer);
  93.       }
  94.     }
  95.     break;
  96.    
  97.     case DOWN:
  98.     {
  99.       for (int nLayer = 0; nLayer < kCubeSize; nLayer++)
  100.       {
  101.         if (bAnimate)
  102.           copyLayer(nLayer+1, nLayer);
  103.         else
  104.           copyLayer(nFillFrame, nLayer);
  105.       }
  106.     }
  107.     break;
  108.   }
  109. }
  110.  
  111. void copyFromFrameToLEDs()
  112. {
  113.   for(int i = 0; i < kCubeSize; i++) {
  114.     for(int j = 0; j < kCubeSize; j++) {
  115.  
  116.       static int nTo;
  117.       switch (layer)
  118.       {
  119.          case X_LAYER:
  120.            nTo = XYZ(nFillFrame, i, j);
  121.          break;
  122.          case Y_LAYER:
  123.            nTo = XYZ(i, nFillFrame, j);
  124.          break;
  125.          case Z_LAYER:
  126.            nTo = XYZ(i, j, nFillFrame);
  127.          break;
  128.       }
  129.       leds[nTo] = inputFrame(i, j);
  130.     }
  131.   }
  132. }
  133.  
  134. void loop()
  135. {
  136.   int16_t sx, sy, x, y;
  137.   uint8_t h;
  138.  
  139.   h = hue;
  140.   if (counter < 1125)
  141.   {
  142.     // ** Fill LED's with diagonal stripes
  143.     sx = sy = 0;
  144.     while (sy < MATRIX_HEIGHT)
  145.     {
  146.       x = sx;
  147.       y = sy;
  148.       while ((x >= 0) && (y < MATRIX_HEIGHT))
  149.       {
  150.         inputFrame(x, y) = CHSV(h, 255, 255);
  151.         x--;
  152.         y++;
  153.       }
  154.       h+=16;
  155.       if (sx < (MATRIX_WIDTH - 1))
  156.         sx++;
  157.       else
  158.         sy++;
  159.     }
  160.   }
  161.   else
  162.   {
  163.     // ** Fill LED's with horizontal stripes
  164.     for (y=0; y<MATRIX_HEIGHT; y++)
  165.     {
  166.       for (x=0; x<MATRIX_WIDTH; x++)
  167.         inputFrame(x, y) = CHSV(h, 255, 255);
  168.       h+=16;
  169.     }
  170.   }
  171.   hue+=4;
  172.  
  173.   if (counter < 125)
  174.     ;
  175.   else if (counter < 375)
  176.     inputFrame.HorizontalMirror();
  177.   else if (counter < 625)
  178.     inputFrame.VerticalMirror();
  179.   else if (counter < 875)
  180.     inputFrame.QuadrantMirror();
  181.   else if (counter < 1125)
  182.     inputFrame.QuadrantRotateMirror();
  183.   else if (counter < 1250)
  184.     ;
  185.   else if (counter < 1500)
  186.     inputFrame.TriangleTopMirror();
  187.   else if (counter < 1750)
  188.     inputFrame.TriangleBottomMirror();
  189.   else if (counter < 2000)
  190.     inputFrame.QuadrantTopTriangleMirror();
  191.   else if (counter < 2250)
  192.     inputFrame.QuadrantBottomTriangleMirror();
  193.  
  194.   counter++;
  195.   if (counter >= 2250)
  196.     counter = 0;
  197.    
  198.   copyFromFrameToLEDs();
  199.   if (bAnimate)
  200.   {
  201.     LEDS.show();
  202.     delay(20);
  203.     // Copy frames into time after showing
  204.     moveFrameForward();
  205.   }
  206.   else
  207.   {
  208.     // mirror to other frames before show
  209.     moveFrameForward();
  210.     LEDS.show();
  211.     delay(20);
  212.   }
  213. }
  214.  
  215. //
  216. // xyz coordinate mapping code
  217. //
  218. uint16_t XYZ( uint8_t x, uint8_t y, uint8_t z)
  219. {
  220.   uint16_t i;
  221.  
  222.   i = 8 * ((y * kCubeSize) + x) + z;
  223.  
  224.   if ((i >= 0) && (i <= 512))
  225.     return i;
  226.   else
  227.     return 0;
  228. }
Advertisement
Add Comment
Please, Sign In to add comment