Advertisement
Guest User

Natural Blob

a guest
Jun 11th, 2014
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.08 KB | None | 0 0
  1. /*
  2.                 Funky Clouds
  3.  
  4.               Toy Collection For
  5.                 Matrix Effects
  6.                  @BM 2014(?!)
  7.  
  8.        www.stefan-petrick.de/wordpress_beta
  9.  
  10.           Keep the code alive by evolving
  11. ...and then do whatever you love with the source.
  12.  
  13. */
  14.  
  15. #include <FastLED.h>
  16.  
  17. // Matrix Size
  18.  
  19. const uint8_t WIDTH  = 16;
  20. const uint8_t HEIGHT = 16;
  21.  
  22. // LED Setup
  23.  
  24. #define LED_PIN     23
  25. #define COLOR_ORDER GRB
  26. #define CHIPSET     WS2812
  27. #define BRIGHTNESS  255
  28. #define NUM_LEDS (WIDTH * HEIGHT)
  29. CRGB    leds[NUM_LEDS];
  30. CRGB    buffer[NUM_LEDS];
  31.  
  32. byte count[4];  // counters for the wave functions
  33.  
  34. void setup()
  35. {
  36.   FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  37.   FastLED.setBrightness(BRIGHTNESS);
  38. }
  39.  
  40. void loop()
  41. {
  42.   // since I found out that integers are casted impicit that part became easy...
  43.   // so just have 4 sinewaves with differnt speeds running
  44.   count[0] = count[0] + 7;
  45.   count[1] = count[1] + 5;
  46.   count[2] = count[2] + 3;
  47.   count[3] = count[3] + 2;
  48.    
  49.   // first plant the seed into the buffer
  50.   //buffer[XY(sin8(count[0])/20, cos8(count[1])/17)] = CHSV (count[0] , 255, 255);
  51.  
  52.   //buffer[XY(sin8(count[2])/17, cos8(count[3])/17)] = CHSV (count[1] , 255, 255);
  53.  
  54.   Line(((sin8(count[0])/17)+(sin8(count[2])/17))/2, ((sin8(count[1])/17)+(sin8(count[3])/17))/2,         // combine 2 wavefunctions for more variety of x
  55.     ((sin8(count[3])/17)+(sin8(count[2])/17))/2, ((sin8(count[1])/17)+(sin8(count[2])/17))/2, count[0]); // and y, color just simple linear ramp
  56.  
  57.   //   just some try and error:
  58.   //Line(sin8(count[3])/17, cos8(count[1])/17, sin8(count[1])/17, cos8(count[2])/17, count[3]);
  59.   //buffer[XY(quadwave8(count)/17, 4)] = CHSV (0 , 255, 255); // lines following different wave fonctions
  60.   //buffer[XY(cubicwave8(count)/17, 6)] = CHSV (40 , 255, 255);
  61.   //buffer[XY(triwave8(count)/17, 8)] = CHSV (80 , 255, 255);
  62.  
  63.   // duplicate the seed in the buffer
  64.   //Caleidoscope4();
  65.  
  66.   // add buffer to leds
  67.   ShowBuffer();
  68.  
  69.   // clear buffer
  70.   ClearBuffer();
  71.  
  72.   // rotate leds
  73.   Spiral(7,7,8,110);
  74.  
  75.   //DimmAll(230);
  76.  
  77.   FastLED.show();
  78.  
  79.   // do not delete the current leds, just fade them down for the tail effect
  80.   //DimmAll(220);
  81.  
  82. }
  83.  
  84. // finds the right index for a S shaped matrix
  85. // ...maybe you need a different mapping for your setup
  86. int XY(int x, int y) {
  87.   if(y > HEIGHT) { y = HEIGHT; }
  88.   if(y < 0) { y = 0; }
  89.   if(x > WIDTH) { x = WIDTH;}
  90.   if(x < 0) { x = 0; }
  91.   if(x % 2 == 1) {  
  92.   return (x * (WIDTH) + (HEIGHT - y -1));
  93.   } else {
  94.     // use that line only, if you have all rows beginning at the same side
  95.     return (x * (WIDTH) + y);  
  96.   }
  97. }
  98.  
  99. // scale the brightness of the screenbuffer down
  100. void DimmAll(byte value)  
  101. {
  102.   for(int i = 0; i < NUM_LEDS; i++)
  103.   {
  104.     leds[i].nscale8(value);
  105.   }
  106. }
  107.  
  108. /*
  109. Caleidoscope1 mirrors from source to A, B and C
  110.  
  111. y
  112.  
  113. |       |
  114. |   B   |   C
  115. |_______________
  116. |       |
  117. |source |   A
  118. |_______________ x
  119.  
  120. */
  121. void Caleidoscope1() {
  122.   for(int x = 0; x < WIDTH / 2 ; x++) {
  123.     for(int y = 0; y < HEIGHT / 2; y++) {
  124.       leds[XY( WIDTH - 1 - x, y )] = leds[XY( x, y )];              // copy to A
  125.       leds[XY( x, HEIGHT - 1 - y )] = leds[XY( x, y )];             // copy to B
  126.       leds[XY( WIDTH - 1 - x, HEIGHT - 1 - y )] = leds[XY( x, y )]; // copy to C
  127.      
  128.     }
  129.   }
  130. }
  131.  
  132. /*
  133. Caleidoscope2 rotates from source to A, B and C
  134.  
  135. y
  136.  
  137. |       |
  138. |   C   |   B
  139. |_______________
  140. |       |
  141. |source |   A
  142. |_______________ x
  143.  
  144. */
  145. void Caleidoscope2() {
  146.   for(int x = 0; x < WIDTH / 2 ; x++) {
  147.     for(int y = 0; y < HEIGHT / 2; y++) {
  148.       leds[XY( WIDTH - 1 - x, y )] = leds[XY( y, x )];    // rotate to A
  149.       leds[XY( WIDTH - 1 - x, HEIGHT - 1 - y )] = leds[XY( x, y )];    // rotate to B
  150.       leds[XY( x, HEIGHT - 1 - y )] = leds[XY( y, x )];    // rotate to C
  151.      
  152.      
  153.     }
  154.   }
  155. }
  156.  
  157. // adds the color of one quarter to the other 3
  158. void Caleidoscope3() {
  159.   for(int x = 0; x < WIDTH / 2 ; x++) {
  160.     for(int y = 0; y < HEIGHT / 2; y++) {
  161.       leds[XY( WIDTH - 1 - x, y )] += leds[XY( y, x )];    // rotate to A
  162.       leds[XY( WIDTH - 1 - x, HEIGHT - 1 - y )] += leds[XY( x, y )];    // rotate to B
  163.       leds[XY( x, HEIGHT - 1 - y )] += leds[XY( y, x )];    // rotate to C
  164.      
  165.      
  166.     }
  167.   }
  168. }
  169.  
  170. // add the complete buffer 3 times while rotating
  171. void Caleidoscope4() {
  172.   for(int x = 0; x < WIDTH ; x++) {
  173.     for(int y = 0; y < HEIGHT ; y++) {
  174.       buffer[XY( WIDTH - 1 - x, y )] += buffer[XY( y, x )];    // rotate to A
  175.       buffer[XY( WIDTH - 1 - x, HEIGHT - 1 - y )] += buffer[XY( x, y )];    // rotate to B
  176.       buffer[XY( x, HEIGHT - 1 - y )] += buffer[XY( y, x )];    // rotate to C
  177.      
  178.      
  179.     }
  180.   }
  181. }
  182.  
  183. void ShowBuffer() {
  184.   for(int i = 0; i < NUM_LEDS ; i++) {
  185.     leds[i] += buffer[i];
  186.   }
  187. }
  188.  
  189. void ClearBuffer() {
  190.   for(int i = 0; i < NUM_LEDS ; i++) {
  191.     buffer[i] = 0;
  192.   }
  193. }
  194.  
  195. //  creates a little twister for softening
  196. void Spiral(int x,int y, int r, byte dimm) {  
  197.   for(int d = r; d >= 0; d--) {                // from the outside to the inside
  198.     for(int i = x-d; i <= x+d; i++) {
  199.        leds[XY(i,y-d)] += leds[XY(i+1,y-d)];   // lowest row to the right
  200.        leds[XY(i,y-d)].nscale8( dimm );}
  201.     for(int i = y-d; i <= y+d; i++) {
  202.        leds[XY(x+d,i)] += leds[XY(x+d,i+1)];   // right colum up
  203.        leds[XY(x+d,i)].nscale8( dimm );}
  204.     for(int i = x+d; i >= x-d; i--) {
  205.        leds[XY(i,y+d)] += leds[XY(i-1,y+d)];   // upper row to the left
  206.        leds[XY(i,y+d)].nscale8( dimm );}
  207.     for(int i = y+d; i >= y-d; i--) {
  208.        leds[XY(x-d,i)] += leds[XY(x-d,i-1)];   // left colum down
  209.        leds[XY(x-d,i)].nscale8( dimm );}
  210.   }
  211. }
  212.  
  213. //  Bresenham line algorythm
  214. void Line(int x0, int y0, int x1, int y1, byte color)
  215. {
  216.   int dx =  abs(x1-x0), sx = x0<x1 ? 1 : -1;
  217.   int dy = -abs(y1-y0), sy = y0<y1 ? 1 : -1;
  218.   int err = dx+dy, e2;
  219.   for(;;){  
  220.     buffer[XY(x0, y0)] += CHSV(color, 255, 255);
  221.     if (x0==x1 && y0==y1) break;
  222.     e2 = 2*err;
  223.     if (e2 > dy) { err += dy; x0 += sx; }
  224.     if (e2 < dx) { err += dx; y0 += sy; }
  225.   }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement