pchestna

Tasmanian Pixel (L3D Cube)

Dec 31st, 2014
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.61 KB | None | 0 0
  1. #include <FastLED.h>
  2.  
  3. // Based on the great example by Mark Kriegsman found here:
  4. // http://pastebin.com/g8Bxi6zW
  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. CRGB leds[NUM_LEDS];
  17.  
  18. int     F16posX = 0; // x position of the pixel
  19. int     F16posY = 0; // y position of the pixel
  20. int     F16posZ = 0; // z position of the pixel
  21. uint8_t hue = 0;
  22.  
  23. void setup() {
  24.   delay(3000); // setup guard
  25.   FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  26.   FastLED.setBrightness(MASTER_BRIGHTNESS);
  27. }
  28.  
  29. uint16_t XYZ( uint8_t x, uint8_t y, uint8_t z)
  30. {
  31.   uint16_t i;
  32.  
  33.   i = kCubeSize * ((y * kCubeSize) + x) + z;
  34.  
  35.   if ((i >= 0) && (i <= 512))
  36.     return i;
  37.   else
  38.     return 0;
  39. }
  40.  
  41. // Draw a pixel on a matrix using fractions of light. Positions are measured in
  42. // sixteenths of a pixel.  Fractional positions are
  43. // rendered using 'anti-aliasing' of pixel brightness.
  44. void drawFractionalPixel()
  45. {
  46.   uint8_t x = F16posX / kFracs; // convert from pos to raw pixel number
  47.   uint8_t y = F16posY / kFracs; // convert from pos to raw pixel number
  48.   uint8_t z = F16posZ / kFracs; // convert from pos to raw pixel number
  49.   uint8_t fracX = F16posX & 0x0F; // extract the 'factional' part of the position
  50.   uint8_t fracY = F16posY & 0x0F; // extract the 'factional' part of the position
  51.   uint8_t fracZ = F16posZ & 0x0F; // extract the 'factional' part of the position
  52.  
  53.   uint8_t px = 255 - (fracX * kFracs);
  54.   uint8_t py = 255 - (fracY * kFracs);
  55.   uint8_t pz = 255 - (fracZ * kFracs);
  56.  
  57.   leds[XYZ(x,y,z)] += CHSV( hue, 255, scale8(px, scale8(py, pz)));
  58.   leds[XYZ(x,y+1,z)] += CHSV( hue, 255, scale8(px, scale8((255-py), pz)));
  59.   leds[XYZ(x,y,z+1)] += CHSV( hue, 255, scale8(px, scale8(py, (255-pz))));
  60.   leds[XYZ(x,y+1,z+1)] += CHSV( hue, 255, scale8(px, scale8((255-py), (255-pz))));
  61.   leds[XYZ(x+1,y,z)] += CHSV( hue, 255, scale8((255-px), scale8(py, pz)));
  62.   leds[XYZ(x+1,y+1,z)] += CHSV( hue, 255, scale8((255-px), scale8((255-py), pz)));
  63.   leds[XYZ(x+1,y,z+1)] += CHSV( hue, 255, scale8((255-px), scale8(py, (255-pz))));
  64.   leds[XYZ(x+1,y+1,z+1)] += CHSV( hue, 255, scale8((255-px), scale8((255-py), (255-pz))));
  65. }
  66.  
  67. void loop()
  68. {
  69.   nscale8(leds, NUM_LEDS, FADE);
  70.  
  71.   F16posX = beatsin16(143, 0, MAX_POS);
  72.   F16posY = beatsin16(91, 0, MAX_POS);
  73.   F16posZ = beatsin16(74, 0, MAX_POS);
  74.  
  75.   drawFractionalPixel();
  76.   hue++;
  77.    
  78.   FastLED.show();
  79. }
Advertisement
Add Comment
Please, Sign In to add comment