Advertisement
PocketUniverses

FastLED NoisePlusPalette Weather

Apr 28th, 2015
1,353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 26.90 KB | None | 0 0
  1. #include<FastLED.h>
  2.  
  3. #define LED_PIN_A     3
  4. #define LED_PIN_B     5
  5. #define LED_PIN_C     6
  6. #define LED_PIN_D     9
  7. #define FRAME_ADV_PIN 12
  8. #define ANIMATION_ADV_PIN 4
  9. #define BRIGHTNESS  255
  10. #define LED_TYPE    WS2812
  11. #define COLOR_ORDER GRB
  12. #define NUM_STRIPS 4
  13. #define MINUTES_SPEED 1
  14.  
  15. const boolean isTriggered = false;
  16.  
  17. const uint8_t kMatrixWidth  = 16;
  18. const uint8_t kMatrixHeight = 16; // Must be divisible by NUM_STRIPS (4)
  19. const uint8_t ledsPerStrip  = kMatrixWidth * kMatrixHeight / NUM_STRIPS;
  20. const boolean kMatrixSerpentineLayout = true;
  21.  
  22. #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
  23. #define MAX_DIMENSION ((kMatrixWidth>kMatrixHeight) ? kMatrixWidth : kMatrixHeight)
  24.  
  25. CRGB leds[kMatrixWidth * kMatrixHeight];
  26.  
  27. uint8_t mainHue = 128;  // Reference hue for color combinations
  28. // NOTE: 30 degrees on the color wheel is 21.3 on 8-bit hue
  29.  
  30. // Timing controls
  31. int refreshRate = 60; // Hz           // Refresh rate of the LEDs independent of counters
  32. long refreshIncrement = 1000 / refreshRate;   // Refresh rate in milliseconds
  33. long previousMillis = 0;              // will store last time LEDs were updated
  34.  
  35. uint8_t       count8  = 0;
  36. uint8_t       countMode  = 1;
  37. uint16_t      countMinutes  = 0;
  38. boolean modeButtonState = false;
  39.  
  40. uint8_t       countPalette = 0;   // Count which palette to animate from
  41. uint8_t       offsetPalette = 0;  // index offset for when we fill from palette
  42.  
  43.  
  44. // This example combines two features of FastLED to produce a remarkable range of
  45. // effects from a relatively small amount of code.  This example combines FastLED's
  46. // color palette lookup functions with FastLED's Perlin/simplex noise generator, and
  47. // the combination is extremely powerful.
  48. //
  49. // You might want to look at the "ColorPalette" and "Noise" examples separately
  50. // if this example code seems daunting.
  51. //
  52. //
  53. // The basic setup here is that for each frame, we generate a new array of
  54. // 'noise' data, and then map it onto the LED matrix through a color palette.
  55. //
  56. // Periodically, the color palette is changed, and new noise-generation parameters
  57. // are chosen at the same time.  In this example, specific noise-generation
  58. // values have been selected to match the given color palettes; some are faster,
  59. // or slower, or larger, or smaller than others, but there's no reason these
  60. // parameters can't be freely mixed-and-matched.
  61. //
  62. // In addition, this example includes some fast automatic 'data smoothing' at
  63. // lower noise speeds to help produce smoother animations in those cases.
  64. //
  65. // The FastLED built-in color palettes (Forest, Clouds, Lava, Ocean, Party) are
  66. // used, as well as some 'hand-defined' ones, and some proceedurally generated
  67. // palettes.
  68.  
  69.  
  70.  
  71. // The 16 bit version of our coordinates
  72. static uint16_t x;
  73. static uint16_t y;
  74. static uint16_t z;
  75.  
  76. // We're using the x/y dimensions to map to the x/y pixels on the matrix.  We'll
  77. // use the z-axis for "time".  speed determines how fast time moves forward.  Try
  78. // 1 for a very slow moving effect, or 60 for something that ends up looking like
  79. // water.
  80. uint16_t speed = 1; // speed is set dynamically once we've started up
  81.  
  82. // Scale determines how far apart the pixels in our noise matrix are.  Try
  83. // changing these values around to see how it affects the motion of the display.  The
  84. // higher the value of scale, the more "zoomed out" the noise iwll be.  A value
  85. // of 1 will be so zoomed in, you'll mostly see solid colors.
  86. uint16_t scale = 80; // scale is set dynamically once we've started up
  87.  
  88. // This is the array that we keep our computed noise values in
  89. uint8_t noise[MAX_DIMENSION][MAX_DIMENSION];
  90.  
  91. CRGBPalette16 currentPalette( CRGB::Black );
  92. TBlendType    currentBlending;
  93. uint8_t       colorLoop = 1;
  94.  
  95.  
  96. void setup() {
  97.   delay(3000);
  98.  
  99.   // Construct the LED matrix from multiple strips
  100.   // tell FastLED there's 60 leds on pin A, starting at index 0 in the led array
  101.   FastLED.addLeds<LED_TYPE, LED_PIN_A, GRB>(leds, 0, ledsPerStrip);
  102.  
  103.   // tell FastLED there's 60 NEOPIXEL leds on pin 11, starting at index 60 in the led array
  104.   FastLED.addLeds<LED_TYPE, LED_PIN_B, GRB>(leds, ledsPerStrip, ledsPerStrip);
  105.  
  106.   // tell FastLED there's 60 NEOPIXEL leds on pin 12, starting at index 120 in the led array
  107.   FastLED.addLeds<LED_TYPE, LED_PIN_C, GRB>(leds, 2 * ledsPerStrip, ledsPerStrip);
  108.  
  109.   // tell FastLED there's 60 NEOPIXEL leds on pin 12, starting at index 120 in the led array
  110.   FastLED.addLeds<LED_TYPE, LED_PIN_D, GRB>(leds, 3 * ledsPerStrip, ledsPerStrip);
  111.  
  112.  
  113.   //LEDS.addLeds<LED_TYPE,LED_PIN_A,COLOR_ORDER>(leds,NUM_LEDS);
  114.  
  115.   LEDS.setBrightness(BRIGHTNESS);
  116.   currentBlending = BLEND;
  117.  
  118.   // Initialize our coordinates to some random values
  119.   x = random16();
  120.   y = random16();
  121.   z = random16();
  122.  
  123.   Serial.begin(9600);      // Serial Baudrate
  124.  
  125.   pinMode(FRAME_ADV_PIN, INPUT);
  126.   pinMode(ANIMATION_ADV_PIN, INPUT);
  127.  
  128.   if (isTriggered == true) {
  129.     while (digitalRead(FRAME_ADV_PIN) < 1) {
  130.       SetupSunnyCloudsPalette();
  131.       fillnoise8();
  132.       mapNoiseToLEDsUsingPalette();
  133.       LEDS.show();
  134.     }
  135.   }
  136.  
  137. }
  138.  
  139.  
  140.  
  141. void loop() {
  142.  
  143.   // Update the LEDs with the designated refresh rate
  144.   unsigned long currentMillis = millis();
  145.   if (isTriggered == true) {
  146.     // Update when triggered and wait a bit before triggering again
  147.     if(currentMillis - previousMillis > 2000L && digitalRead(FRAME_ADV_PIN) > 0) {
  148.       updateAnimations(currentMillis);
  149.     }
  150.   } else {
  151.     // Continuously update with the refresh rate
  152.     if(currentMillis - previousMillis > refreshRate) {
  153.       updateAnimations(currentMillis);
  154.     }
  155.   }
  156.   // delay(10);
  157. }
  158.  
  159. void updateAnimations(unsigned long currentMillis) {
  160.   for (int updatenum = 0; updatenum < 5; updatenum++) {
  161.       // Update the main 8-bit counnter
  162.       //count8 = (millis() / 20) % 256;
  163.       count8 += 1;
  164.       //if (count8 % 256 == 0) {countMode += 1;}
  165.      
  166.       countMinutes = (countMinutes + MINUTES_SPEED) % 5760;
  167.       // Update the refresh rate counter
  168.       previousMillis = currentMillis;  
  169.      
  170.       // Periodically choose a new palette, speed, and scale
  171.       ChangePaletteAndSettingsPeriodically();
  172.      
  173.    
  174.     // generate noise data slower than the default allows
  175.     fillnoise8();
  176.    
  177.    
  178.     // convert the noise data to colors in the LED array
  179.     // using the current palette
  180.     if ((count8 % 500) == 0 ) {offsetPalette+=1;}
  181.     mapNoiseToLEDsUsingPalette();
  182.     //FillLEDsFromPaletteColors( count8 );
  183.  
  184.     LEDS.show();
  185.     }
  186. }
  187.  
  188.  
  189.  
  190. // Fill the x/y array of 8-bit noise values using the inoise8 function.
  191. void fillnoise8() {
  192.   // If we're runing at a low "speed", some 8-bit artifacts become visible
  193.   // from frame-to-frame.  In order to reduce this, we can do some fast data-smoothing.
  194.   // The amount of data smoothing we're doing depends on "speed".
  195.   uint8_t dataSmoothing = 0;
  196.   if( speed < 50) {
  197.     dataSmoothing = 200 - (speed * 4);
  198.   }
  199.  
  200.   for(int i = 0; i < MAX_DIMENSION; i++) {
  201.     int ioffset = scale * i;
  202.     for(int j = 0; j < MAX_DIMENSION; j++) {
  203.       int joffset = scale * j;
  204.      
  205.       uint8_t data = inoise8(x + ioffset,y + joffset,z);
  206.  
  207.       // The range of the inoise8 function is roughly 16-238.
  208.       // These two operations expand those values out to roughly 0..255
  209.       // You can comment them out if you want the raw noise data.
  210.       data = qsub8(data,16);
  211.       data = qadd8(data,scale8(data,39));
  212.  
  213.       if( dataSmoothing ) {
  214.         uint8_t olddata = noise[i][j];
  215.         uint8_t newdata = scale8( olddata, dataSmoothing) + scale8( data, 256 - dataSmoothing);
  216.         data = newdata;
  217.       }
  218.      
  219.       noise[i][j] = data;
  220.     }
  221.   }
  222.  
  223.   z += speed;
  224.  
  225.   // apply slow drift to X and Y, just for visual variation.
  226.   x += speed / 8;
  227.   y -= speed / 16;
  228. }
  229.  
  230.  
  231. void mapNoiseToLEDsUsingPalette()
  232. {
  233.   static uint8_t ihue=0;
  234.  
  235.   for(int i = 0; i < kMatrixWidth; i++) {
  236.     for(int j = 0; j < kMatrixHeight; j++) {
  237.       // We use the value at the (i,j) coordinate in the noise
  238.       // array for our brightness, and the flipped value from (j,i)
  239.       // for our pixel's index into the color palette.
  240.  
  241.       uint8_t index = noise[j][i];
  242.       uint8_t bri =   noise[i][j];
  243.  
  244.       // if this palette is a 'loop', add a slowly-changing base value
  245.       if( colorLoop) {
  246.         index += offsetPalette;
  247.       }
  248.  
  249.       // brighten up, as the color palette itself often contains the
  250.       // light/dark dynamic range desired
  251.       if( bri > 127 ) {
  252.         bri = 255;
  253.       } else {
  254.         bri = dim8_raw( bri * 2);
  255.       }
  256.  
  257.       CRGB color = ColorFromPalette( currentPalette, index, bri);
  258.       leds[XY(i,j)] = color;
  259.     }
  260.   }
  261.  
  262.   ihue+=1;
  263. }
  264.  
  265. void FillLEDsFromPaletteColors( uint8_t colorIndex)
  266. {
  267.   uint8_t brightness = 255;
  268.  
  269.   for( int i = 0; i < NUM_LEDS; i++) {
  270.     leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
  271.     colorIndex += 3;
  272.   }
  273. }
  274.  
  275.  
  276. // There are several different palettes of colors demonstrated here.
  277. //
  278. // FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p,
  279. // OceanColors_p, CloudColors_p, LavaColors_p, ForestColors_p, and PartyColors_p.
  280. //
  281. // Additionally, you can manually define your own color palettes, or you can write
  282. // code that creates color palettes on the fly.
  283.  
  284. // 1 = 5 sec per palette
  285. // 2 = 10 sec per palette
  286. // etc
  287. #define HOLD_PALETTES_X_TIMES_AS_LONG 4
  288.  
  289. void ChangePaletteAndSettingsPeriodically()
  290. {
  291.   //static uint8_t lastSecond = 99;
  292.   //uint8_t secondHand = ((millis() / 1000) / HOLD_PALETTES_X_TIMES_AS_LONG) % 60;
  293.  
  294.   // Set up a counter to move the reference hue for rainbow animations
  295.   //if ((count8 % 1) == 0) {countPalette += 1; secondHand -= (secondHand % 5); lastSecond = 99;}
  296.   //if ((countPalette % 256) == 0) {countPalette += 1; secondHand -= (secondHand % 5); lastSecond = 99;}
  297.  
  298.   // Manually set the Noise pattern to override te demo loop temporarily
  299.   //secondHand = 5;
  300.  
  301.   countPalette = count8;
  302.  
  303.   if (digitalRead(ANIMATION_ADV_PIN) == HIGH && modeButtonState == false) {
  304.     countMode = (countMode + 1) % 12;
  305.     modeButtonState = true;
  306.   } else if (digitalRead(ANIMATION_ADV_PIN) == LOW) {
  307.     modeButtonState = false;
  308.   }
  309.  
  310.   // Demo code to run different animations in sequence
  311.   if( 4 != 5) {
  312.     //lastSecond = secondHand;
  313.     if( countMode ==  0)  { SetupSunrisePalette(countPalette);              speed =  1; scale =120; colorLoop = 0; }
  314.     if( countMode ==  1)  { SetupSunnyCloudsPalette();                      speed =  1; scale =120; colorLoop = 0; }
  315.     if( countMode ==  2)  { SetupSunsetPalette(countPalette);               speed =  1; scale =120; colorLoop = 0; }
  316.     if( countMode ==  3)  { SetupMoonlightPalette(countPalette);            speed =  1; scale =120; colorLoop = 0; }
  317.     if( countMode ==  4)  { SetupAnalogousPalette(countPalette);            speed =  5; scale = 80; colorLoop = 0; }
  318.     if( countMode ==  5)  { SetupComplementaryPalette(countPalette);        speed =  3; scale =300; colorLoop = 0; }
  319.     if( countMode ==  6)  { SetupSplitComplementaryPalette(countPalette);   speed =  4; scale = 30; colorLoop = 0; }
  320.     if( countMode ==  7)  { SetupDoubleComplementaryPalette(countPalette);  speed =  4; scale = 50; colorLoop = 0; }
  321.     if( countMode ==  8)  { SetupTriadicPalette(countPalette);              speed =  2; scale = 90; colorLoop = 0; }
  322.     if( countMode ==  9)  { AnimateWeatherPalette();                        speed =  1; scale =120; colorLoop = 0; }
  323.     if( countMode == 10)  { SetupNeoPalette();                              speed =  8; scale =120; colorLoop = 1; }
  324.     if( countMode == 11)  { SetupBlackPalette();                            speed = 30; scale = 80; colorLoop = 0; }
  325.   }
  326. }
  327.  
  328.  
  329.  
  330. /*
  331.  
  332. Weather Patterns
  333.  
  334. The goal here is to illuminate the clouds in the foreground
  335. and the sky in the background differently thoughout the day.
  336.  
  337. There are 24*60=1440 minutes per 24hr day,
  338. so we animate a time lapse with 0.25 minutes per frame
  339. for a total of 5760 frames. That's 96 seconds at 60fps.
  340. There are 240 frames per hour in 4 seconds at 60fps.
  341.  
  342. Minutes 0 to 600 - Sunrise
  343. As the sun rises, the sky is dark and the clouds illuminate with yellow
  344. The sky fades in to pale blue and then deepens in saturation
  345. as the clouds fade to dim and then bright white
  346.  
  347. Minutes 601 to 2400 - Daytime
  348. The day is represented with white clouds on a deep blue sky,
  349. but as the day gets cloudier, the sky darkens into a thunderstorm.
  350. Thuderstorms are rendered the same during the day as they are at night.
  351.  
  352. Minutes 2401 to 3000 - Sunset
  353. If the day is only cloudy and not storming,
  354. the sun turns the clouds pink and purple as it sets
  355. and the sky deepens and slowly darkens.
  356.  
  357. Minutes 3001 to 5760 - Moonlight
  358. The clouds are dimly lit by the moon and the sky is black
  359.  
  360. */
  361.  
  362. CRGBPalette16 targetPalette( CRGB::Black );
  363. uint8_t maxChanges = 36;
  364. uint8_t cloudiness = 130;
  365.  
  366. void AnimateWeatherPalette()
  367. {
  368.   static uint8_t cloudSaturation = 0;
  369.   static uint8_t skySaturation = 230;
  370.  
  371.   // First, test the cloudiness.
  372.   // If it's over 192, then animate a thunderstorm
  373.  
  374.   if (cloudiness >= 192) {
  375.     // There is a thunderstorm of 0-63 in severity
  376.     // Severity causes the clouds to darken
  377.     // and the lightning to strike more frequently
  378.    
  379.    
  380.   } else {
  381.     // There are 0-15 clouds evenly dispersed across the sky
  382.    
  383.     // --- Sunrise ---
  384.     // Black out the sky before sunrise
  385.     //if (countMinutes ==   0) { currentPalette = CRGBPalette16( CRGB::Black );                      }
  386.     // Golden sunrise                              clouds            sky                 cloudRandomness
  387.     if (countMinutes ==   3) { SetupWeatherPalette(CHSV( 30,230,140),CHSV( 40,230,120),  40);    }
  388.     // Bring down the sky and keep brightening the clouds
  389.     if (countMinutes == 200) { SetupWeatherPalette(CHSV( 30,230,200),CHSV( 40,230,  0),  40);    }
  390.     if (countMinutes == 220) { SetupWeatherPalette(CHSV( 40,230,200),CHSV(160,230,  0),  30);    }
  391.     // Bring up the sky in blue as the clouds fade back out
  392.     if (countMinutes == 300) { SetupWeatherPalette(CHSV( 50,230, 90),CHSV(160,230, 90),  30);    }
  393.     // Keep bringing up the sky and clouds and bring the clouds to white
  394.     if (countMinutes == 400) { SetupWeatherPalette(CHSV( 50,  0,192),CHSV(160,230,192),  30);    }
  395.     if (countMinutes == 500) { SetupWeatherPalette(CHSV( 90,  0,192),CHSV(160,230,200),  30);    }
  396.     if (countMinutes == 600) { SetupWeatherPalette(CHSV(245,  0,220),CHSV(160,230,200),  30);    }
  397.    
  398.     // --- Sunset ---
  399.     if (countMinutes ==2400) { SetupWeatherPalette(CHSV(245, 90,220),CHSV(160,240,190),  30);    }
  400.     if (countMinutes ==2500) { SetupWeatherPalette(CHSV(245,120,200),CHSV(160,240,180),  30);    }
  401.     if (countMinutes ==2700) { SetupWeatherPalette(CHSV(245,230,150),CHSV(160,240,150),  30);    }
  402.     if (countMinutes ==2900) { SetupWeatherPalette(CHSV(245,230,  0),CHSV(160,240,  0),  30);    }
  403.    
  404.     // --- Moonlight ---
  405.     if (countMinutes ==3100) { SetupWeatherPalette(CHSV(245,  0,  0),CHSV(160,  0,  0),  30);    }
  406.     if (countMinutes ==3300) { SetupWeatherPalette(CHSV(245,  0,120),CHSV(160,  0, 50),  30);    }
  407.    
  408.     if (countMinutes ==5600) { SetupWeatherPalette(CHSV(245,  0,0),CHSV(160,  0, 0),  30);    }
  409.   }
  410.  
  411.   // Slow the rate that the palettes are blending, but let them blend evenly
  412.   if ((countMinutes / MINUTES_SPEED) % 6 == 0) { nblendPaletteTowardPalette( currentPalette, targetPalette, maxChanges);}
  413.  
  414. }
  415.  
  416. void SetupWeatherPalette(CHSV cloudColor, CHSV skyColor, uint8_t cloudRandomness)
  417. {
  418.  
  419.   for (int i = 0; i < 16; i++) {
  420.     if (i >= cloudiness / 16) {
  421.       // Apply sky color
  422.       targetPalette[i] = skyColor;
  423.     } else {
  424.       // Apply cloud color and some randomization
  425.       targetPalette[i] = CHSV( cloudColor.hue, cloudColor.saturation, cloudColor.value);
  426.       //targetPalette[i].hue += random8(cloudRandomness);
  427.     }
  428.   }
  429. }
  430.  
  431.  
  432. void SetupSunnyCloudsPalette()
  433. {
  434.   static uint8_t cloudSaturation = 0;
  435.   static uint8_t skySaturation = 230;
  436.  
  437.   if (cloudiness <= 127) {
  438.     cloudSaturation = 255-cloudiness*1.9;
  439.     skySaturation = 230;
  440.   } else {
  441.     cloudSaturation = 0;
  442.     skySaturation = 230-(cloudiness-127)*1.7;
  443.   }
  444.  
  445.  
  446.   // 'black out' all 16 palette entries...
  447.   fill_solid( currentPalette, 16, CHSV( 160, skySaturation, 255-cloudiness/2 ) );
  448.  
  449.   // 30 degrees on color wheel is about 21.3 out of 256
  450.   currentPalette[0] =  CHSV( 160, cloudSaturation, 255 );
  451.   currentPalette[1] =  CHSV( 160, cloudSaturation, 255 );
  452.  
  453.   currentPalette[2] =  CHSV( 160, cloudSaturation, 255 );
  454.   currentPalette[3] =  CHSV( 160, cloudSaturation, 255 );
  455.  
  456.   currentPalette[4] =  CHSV( 160, cloudSaturation, 255 );
  457.   currentPalette[5] =  CHSV( 160, cloudSaturation, 255 );
  458.  
  459.   currentPalette[6] = CHSV( 160, cloudSaturation, 255 );
  460.   currentPalette[7] = CHSV( 160, cloudSaturation, 255 );
  461.   /*
  462.     // 30 degrees on color wheel is about 21.3 out of 256
  463.   currentPalette[0] =  CHSV( 160, cloudSaturation, 255 );
  464.   currentPalette[1] =  CHSV( 160, cloudSaturation, 255 );
  465.  
  466.   currentPalette[4] =  CHSV( 160, cloudSaturation, 255 );
  467.   currentPalette[5] =  CHSV( 160, cloudSaturation, 255 );
  468.  
  469.   currentPalette[8] =  CHSV( 160, cloudSaturation, 255 );
  470.   currentPalette[9] =  CHSV( 160, cloudSaturation, 255 );
  471.  
  472.   currentPalette[12] = CHSV( 160, cloudSaturation, 255 );
  473.   currentPalette[13] = CHSV( 160, cloudSaturation, 255 );
  474.   */
  475. }
  476.  
  477. void SetupSunsetPalette(uint8_t  time)
  478. {
  479.  
  480.   static uint8_t skyBrightness = 255;
  481.   static uint8_t cloudBrightness = 255;
  482.  
  483.   if ( time <= 201) {
  484.     // The sun is setting, but still out
  485.     cloudBrightness = 255;
  486.     skyBrightness = 255- time/2;
  487.   } else {
  488.     // The sun sinks beneath the horizon
  489.     cloudBrightness = 256-( time-200)*4.6;
  490.     skyBrightness = 180-( time-200)*3;
  491.   }
  492.  
  493.   // 'black out' all 16 palette entries...
  494.   fill_solid( currentPalette, 16, CHSV( 160, 255, skyBrightness ) );
  495.  
  496.   // 30 degrees on color wheel is about 21.3 out of 256
  497.   currentPalette[0] = CHSV( 245+random8(30),  time, cloudBrightness );
  498.   currentPalette[1] = CHSV( 245+random8(30),  time, cloudBrightness );
  499.  
  500.   currentPalette[4] = CHSV( 245+random8(30),  time, cloudBrightness );
  501.   currentPalette[5] = CHSV( 245+random8(30),  time, cloudBrightness );
  502.  
  503.   currentPalette[8] = CHSV( 245+random8(30),  time, cloudBrightness );
  504.   currentPalette[9] = CHSV( 245+random8(30),  time, cloudBrightness );
  505.  
  506.   currentPalette[12] = CHSV( 245+random8(30),  time, cloudBrightness );
  507.   currentPalette[13] = CHSV( 245+random8(30),  time, cloudBrightness );
  508.  
  509. }
  510.  
  511. void SetupSunrisePalette(uint8_t  time)
  512. {
  513.  
  514.   static uint8_t skyBrightness = 0;
  515.   static uint8_t cloudBrightness = 0;
  516.   static uint8_t skySaturation = 255;
  517.  
  518.   if ( time <= 50) {
  519.     // Before the sun rises
  520.     cloudBrightness =  time*4.6;
  521.     skyBrightness =   0;
  522.     skySaturation = 180;
  523.     colorLoop = 0;
  524.     speed =  1;
  525.   } else if ( time <= 100){
  526.     // The Sky is changing colors
  527.     skyBrightness = ( time-50)*4.6;
  528.     cloudBrightness = 230-(( time-50)*1.6);
  529.     skySaturation = 200;
  530.     colorLoop = 1;
  531.     speed =  1;
  532.   } else {
  533.     // Sunrise into day
  534.     cloudBrightness = (( time-10)*1);
  535.     skyBrightness = 240;
  536.     skySaturation = 220;
  537.     speed =  3;
  538.   }
  539.  
  540.   // 'black out' all 16 palette entries...
  541.   fill_solid( currentPalette, 16, CHSV( 160, skySaturation, skyBrightness ) );
  542.  
  543.   // 30 degrees on color wheel is about 21.3 out of 256
  544.   currentPalette[0] = CHSV( 60+random8(10), 256- time, cloudBrightness );
  545.   currentPalette[1] = CHSV( 60+random8(10), 256- time, cloudBrightness );
  546.  
  547.   currentPalette[4] = CHSV( 60+random8(10), 256- time, cloudBrightness );
  548.   currentPalette[5] = CHSV( 60+random8(10), 256- time, cloudBrightness );
  549.  
  550.   currentPalette[8] = CHSV( 60+random8(10), 256- time, cloudBrightness );
  551.   currentPalette[9] = CHSV( 60+random8(10), 256- time, cloudBrightness );
  552.  
  553.   currentPalette[12] = CHSV( 60+random8(10), 256- time, cloudBrightness );
  554.   currentPalette[13] = CHSV( 60+random8(10), 256- time, cloudBrightness );
  555.  
  556. }
  557.  
  558.  
  559. void SetupMoonlightPalette(uint8_t  time)
  560. {
  561.  
  562.   static uint8_t cloudBrightness = 0;
  563.  
  564.   if ( time <= 50) {
  565.     // Darkness before moonlight
  566.     cloudBrightness =  time*3;
  567.   } else if ( time <= 200){
  568.     // Clouds and moonlight
  569.     cloudBrightness = 150;
  570.   } else {
  571.     // Fade out before sunrise
  572.     cloudBrightness = 150-( time-200)*2.6;
  573.   }
  574.  
  575.   // 'black out' all 16 palette entries...
  576.   fill_solid( currentPalette, 16, CHSV( 160, 0, 0 ) );
  577.  
  578.   // 30 degrees on color wheel is about 21.3 out of 256
  579.   currentPalette[0] = CHSV( 60+random8(10), 0, cloudBrightness );
  580.   currentPalette[1] = CHSV( 60+random8(10), 0, cloudBrightness );
  581.  
  582.   currentPalette[4] = CHSV( 60+random8(10), 0, cloudBrightness );
  583.   currentPalette[5] = CHSV( 60+random8(10), 0, cloudBrightness );
  584.  
  585.   currentPalette[8] = CHSV( 60+random8(10), 0, cloudBrightness );
  586.   currentPalette[9] = CHSV( 60+random8(10), 0, cloudBrightness );
  587.  
  588.   currentPalette[12] = CHSV( 60+random8(10), 0, cloudBrightness );
  589.   currentPalette[13] = CHSV( 60+random8(10), 0, cloudBrightness );
  590.  
  591. }
  592.  
  593.  
  594. /*
  595.  
  596. Color Harmony Patterns
  597.  
  598. We've found these useful for trying out and thinking about different color combinations.
  599. They are based on ideas about which colors may be pleasing in combinations.
  600. Of course, this isn't an exact science, so we want to play with brightness and saturation
  601. and some randomization on hue as well.
  602.  
  603. */
  604.  
  605. void SetupAnalogousPalette(uint8_t referenceHue)
  606. {
  607.   // Set all 16 palette entries to reference
  608.   fill_solid( currentPalette, 16, CHSV( referenceHue, 255, 127 ) );
  609.  
  610.   // 30 degrees on color wheel is about 21.3 out of 256
  611.   currentPalette[0] = CHSV( referenceHue-21, 180, 255 );
  612.   currentPalette[1] = CHSV( referenceHue,    255, 255 );
  613.   currentPalette[2] = CHSV( referenceHue+21, 180, 255 );
  614.  
  615.   currentPalette[6] = CHSV( referenceHue+21, 255, 255 );
  616.   currentPalette[7] = CHSV( referenceHue,    180, 255 );
  617.   currentPalette[8] = CHSV( referenceHue-21, 255, 255 );
  618.  
  619.   currentPalette[11] = CHSV( referenceHue+21, 180, 255 );
  620.   currentPalette[12] = CHSV( referenceHue-21, 255, 255 );
  621.   currentPalette[13] = CHSV( referenceHue+21, 180, 255 );
  622. }
  623.  
  624.  
  625. void SetupComplementaryPalette(uint8_t referenceHue)
  626. {
  627.   // 'black out' all 16 palette entries...
  628.   fill_solid( currentPalette, 16, CRGB::Black );
  629.  
  630.   // 30 degrees on color wheel is about 21.3 out of 256
  631.   currentPalette[0] = CHSV( referenceHue, 255, 255 );
  632.   currentPalette[1] = CHSV( referenceHue, 255, 255 );
  633.  
  634.   currentPalette[4] = CHSV( referenceHue+128, 255, 255 );
  635.   currentPalette[5] = CHSV( referenceHue+128, 255, 255 );
  636.  
  637.   currentPalette[8] = CHSV( referenceHue, 200, 255 );
  638.   currentPalette[9] = CHSV( referenceHue, 200, 255 );
  639.  
  640.   currentPalette[12] = CHSV( referenceHue+128, 200, 255 );
  641.   currentPalette[13] = CHSV( referenceHue+128, 200, 255 );
  642.  
  643. }
  644.  
  645. void SetupSplitComplementaryPalette(uint8_t referenceHue)
  646. {
  647.   // 'black out' all 16 palette entries...
  648.   fill_solid( currentPalette, 16, CRGB::Black );
  649.  
  650.   // 30 degrees on color wheel is about 21.3 out of 256
  651.   currentPalette[0] = CHSV( referenceHue-21, 180, 255 );
  652.   currentPalette[1] = CHSV( referenceHue-21, 255, 255 );
  653.   currentPalette[2] = CHSV( referenceHue-21, 127, 255 );
  654.  
  655.   currentPalette[4] = CHSV( referenceHue+128, 255, 255 );
  656.  
  657.   currentPalette[6] = CHSV( referenceHue+21, 255, 255 );
  658.   currentPalette[7] = CHSV( referenceHue+21, 127, 255 );
  659.   currentPalette[8] = CHSV( referenceHue+21, 255, 255 );
  660.  
  661.   currentPalette[10] = CHSV( referenceHue+128, 180, 255 );
  662.  
  663.   currentPalette[12] = CHSV( referenceHue+21, 180, 255 );
  664.   currentPalette[13] = CHSV( referenceHue-21, 150, 255 );
  665.   currentPalette[14] = CHSV( referenceHue+128, 255, 255 );
  666. }
  667.  
  668. void SetupDoubleComplementaryPalette(uint8_t referenceHue)
  669. {
  670.   // 'black out' all 16 palette entries...
  671.   fill_solid( currentPalette, 16, CRGB::Black );
  672.  
  673.   // 30 degrees on color wheel is about 21.3 out of 256
  674.   currentPalette[0] = CHSV( referenceHue, 255, 255 );
  675.   currentPalette[2] = CHSV( referenceHue, 150, 255 );
  676.  
  677.   currentPalette[4] = CHSV( referenceHue+128, 255, 255 );
  678.   currentPalette[6] = CHSV( referenceHue+128, 150, 255 );
  679.  
  680.   currentPalette[8] = CHSV( referenceHue+43, 200, 255 );
  681.   currentPalette[10] = CHSV( referenceHue+43, 150, 255 );
  682.  
  683.   currentPalette[12] = CHSV( referenceHue+171, 200, 255 );
  684.   currentPalette[14] = CHSV( referenceHue+171, 150, 255 );
  685.  
  686. }
  687.  
  688.  
  689. void SetupTriadicPalette(uint8_t referenceHue)
  690. {
  691.   // 'black out' all 16 palette entries...
  692.   fill_solid( currentPalette, 16, CRGB::Black );
  693.  
  694.   // A third around the color wheel is 85 out of 256
  695.   currentPalette[0] = CHSV( referenceHue-85, 200, 255 );
  696.   currentPalette[1] = CHSV( referenceHue-85, 255, 255 );
  697.   currentPalette[2] = CHSV( referenceHue-85, 200, 255 );
  698.  
  699.   currentPalette[6] = CHSV( referenceHue+85, 255, 255 );
  700.   currentPalette[7] = CHSV( referenceHue+85, 200, 255 );
  701.   currentPalette[8] = CHSV( referenceHue+85, 255, 255 );
  702.  
  703.   currentPalette[11] = CHSV( referenceHue, 200, 255 );
  704.   currentPalette[12] = CHSV( referenceHue, 255, 255 );
  705.   currentPalette[13] = CHSV( referenceHue, 200, 255 );
  706. }
  707.  
  708.  
  709.  
  710. // This function generates a random palette that's a gradient
  711. // between four different colors.  The first is a dim hue, the second is
  712. // a bright hue, the third is a bright pastel, and the last is
  713. // another bright hue.  This gives some visual bright/dark variation
  714. // which is more interesting than just a gradient of different hues.
  715. void SetupRandomPalette(uint8_t referenceHue)
  716. {
  717.   currentPalette = CRGBPalette16(
  718.                       CHSV( referenceHue, 255, 90),
  719.                       CHSV( referenceHue+40, 255, 255),
  720.                       CHSV( referenceHue+128, 150, 255),
  721.                       CHSV( referenceHue+168, 255, 255));
  722. }
  723.  
  724.  
  725. void SetupNeoPalette()
  726. {
  727.   currentPalette = CRGBPalette16(
  728.                       CHSV( 96, 255, 0),
  729.                       CHSV( 96, 255, 255),
  730.                       CHSV( 96, 180, 0),
  731.                       CHSV( 96, 255, 128));
  732. }
  733.  
  734. void SetupLightCyclePalette()
  735. {
  736.   // 'black out' all 16 palette entries...
  737.   fill_solid( currentPalette, 16, CRGB::Black);
  738.   // and set every fourth one to white.
  739.   currentPalette[0] = CHSV( 32, 255, 255);
  740.   currentPalette[4] = CHSV( 132, 255, 255);
  741.   currentPalette[8] = CHSV( 32, 230, 255);
  742.   currentPalette[12] = CHSV( 132, 230, 255);
  743. }
  744.  
  745.  
  746. void SetupBlackPalette()
  747. {
  748.   // 'black out' all 16 palette entries...
  749.   fill_solid( currentPalette, 16, CRGB::Black);
  750. }
  751.  
  752. //
  753. // Mark's xy coordinate mapping code.  See the XYMatrix for more information on it.
  754. //
  755. uint16_t XY( uint8_t x, uint8_t y)
  756. {
  757.   uint16_t i;
  758.   if( kMatrixSerpentineLayout == false) {
  759.     i = (y * kMatrixWidth) + x;
  760.   }
  761.   if( kMatrixSerpentineLayout == true) {
  762.     if( y & 0x01) {
  763.       // Odd rows run backwards
  764.       uint8_t reverseX = (kMatrixWidth - 1) - x;
  765.       i = (y * kMatrixWidth) + reverseX;
  766.     } else {
  767.       // Even rows run forwards
  768.       i = (y * kMatrixWidth) + x;
  769.     }
  770.   }
  771.   return i;
  772. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement