pchestna

Tortoise and the hare (Spark)

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