pchestna

Tortoise and the hare (L3D Cube)

Dec 31st, 2014
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.69 KB | None | 0 0
  1. #include <FastLED.h>
  2.  
  3. // Derivative work to: http://pastebin.com/h95EgTkT
  4. // Thanks again to Mark Kriegsman and FastLED
  5.  
  6. const uint8_t kCubeSize = 8;
  7. const uint8_t kFracs = 16;
  8.  
  9. #define DATA_PIN 2
  10. #define NUM_LEDS kCubeSize * kCubeSize * kCubeSize
  11. #define MAX_POS (kCubeSize-1)*kFracs
  12. #define LED_TYPE    WS2811
  13. #define COLOR_ORDER GRB
  14. #define MASTER_BRIGHTNESS   255
  15. #define FADE 230
  16. #define NUM_PIXELS 2
  17. CRGB leds[NUM_LEDS];
  18.  
  19. typedef struct {
  20.   int     F16posX = 0; // x position of the pixel
  21.   int     F16posY = 0; // y position of the pixel
  22.   int     F16posZ = 0; // z position of the pixel
  23.   uint8_t hue = 0;
  24.   uint8_t xPosMod = 1;
  25.   uint8_t yPosMod = 1;
  26.   uint8_t zPosMod = 1;
  27. } pixel;
  28.  
  29. pixel pixels[NUM_PIXELS];
  30.  
  31. void initPixelsTaz()
  32. {
  33.   // NUM_PIXELS should be 2 to use only these two values
  34.   pixels[0].hue = random8();
  35.   pixels[0].xPosMod = 143;
  36.   pixels[0].yPosMod = 91;
  37.   pixels[0].zPosMod = 74;
  38.  
  39.   pixels[1].hue = random8();
  40.   pixels[1].xPosMod = 21;
  41.   pixels[1].yPosMod = 17;
  42.   pixels[1].zPosMod = 15;
  43. }
  44.  
  45. void initPixelsRandom()
  46. {
  47.   for (int i=0; i<NUM_PIXELS; i++)
  48.   {
  49.     pixels[i].hue = random8();
  50.     pixels[i].xPosMod = random8();
  51.     pixels[i].yPosMod = random8();
  52.     pixels[i].zPosMod = random8();
  53.   }
  54. }
  55.  
  56. void movePixels()
  57. {
  58.   for (int i=0; i<NUM_PIXELS; i++)
  59.   {
  60.     pixels[i].F16posX = beatsin16(pixels[i].xPosMod, 0, MAX_POS);
  61.     pixels[i].F16posY = beatsin16(pixels[i].yPosMod, 0, MAX_POS);
  62.     pixels[i].F16posZ = beatsin16(pixels[i].zPosMod, 0, MAX_POS);
  63.     pixels[i].hue++;
  64.   }
  65. }
  66.  
  67. void setup() {
  68.   delay(3000); // setup guard
  69.   FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  70.   FastLED.setBrightness(MASTER_BRIGHTNESS);
  71.   initPixelsTaz();
  72.   //initPixelsRandom();
  73. }
  74.  
  75. uint16_t XYZ( uint8_t x, uint8_t y, uint8_t z)
  76. {
  77.   uint16_t i;
  78.  
  79.   i = kCubeSize * ((y * kCubeSize) + x) + z;
  80.  
  81.   if ((i >= 0) && (i <= 512))
  82.     return i;
  83.   else
  84.     return 0;
  85. }
  86.  
  87. // Draw a pixel on a matrix using fractions of light. Positions are measured in
  88. // sixteenths of a pixel.  Fractional positions are
  89. // rendered using 'anti-aliasing' of pixel brightness.
  90. void drawPixels()
  91. {
  92.   for (int i = 0; i < NUM_PIXELS; i++)
  93.   {
  94.     int hue = pixels[i].hue;
  95.    
  96.     uint8_t x = pixels[i].F16posX / kFracs; // convert from pos to raw pixel number
  97.     uint8_t y = pixels[i].F16posY / kFracs; // convert from pos to raw pixel number
  98.     uint8_t z = pixels[i].F16posZ / kFracs; // convert from pos to raw pixel number
  99.     uint8_t fracX = pixels[i].F16posX & 0x0F; // extract the 'factional' part of the position
  100.     uint8_t fracY = pixels[i].F16posY & 0x0F; // extract the 'factional' part of the position
  101.     uint8_t fracZ = pixels[i].F16posZ & 0x0F; // extract the 'factional' part of the position
  102.  
  103.     uint8_t px = 255 - (fracX * kFracs);
  104.     uint8_t py = 255 - (fracY * kFracs);
  105.     uint8_t pz = 255 - (fracZ * kFracs);
  106.    
  107.     leds[XYZ(x,y,z)] += CHSV( hue, 255, scale8(px, scale8(py, pz)));
  108.     leds[XYZ(x,y+1,z)] += CHSV( hue, 255, scale8(px, scale8((255-py), pz)));
  109.     leds[XYZ(x,y,z+1)] += CHSV( hue, 255, scale8(px, scale8(py, (255-pz))));
  110.     leds[XYZ(x,y+1,z+1)] += CHSV( hue, 255, scale8(px, scale8((255-py), (255-pz))));
  111.     leds[XYZ(x+1,y,z)] += CHSV( hue, 255, scale8((255-px), scale8(py, pz)));
  112.     leds[XYZ(x+1,y+1,z)] += CHSV( hue, 255, scale8((255-px), scale8((255-py), pz)));
  113.     leds[XYZ(x+1,y,z+1)] += CHSV( hue, 255, scale8((255-px), scale8(py, (255-pz))));
  114.     leds[XYZ(x+1,y+1,z+1)] += CHSV( hue, 255, scale8((255-px), scale8((255-py), (255-pz))));
  115.   }
  116. }
  117.  
  118. void loop()
  119. {
  120.   // Dim everything
  121.   nscale8(leds, NUM_LEDS, FADE);
  122.  
  123.   movePixels();
  124.   drawPixels();
  125.    
  126.   FastLED.show();
  127. }
Advertisement
Add Comment
Please, Sign In to add comment