diff options
author | Daniel Garcia <danielgarcia@gmail.com> | 2014-04-22 08:09:58 +0400 |
---|---|---|
committer | Daniel Garcia <danielgarcia@gmail.com> | 2014-04-22 08:09:58 +0400 |
commit | d0563d953698195f238498f5ebd861799eda1d24 (patch) | |
tree | f0b880db5a0384c90cbf6319bc506c87d7a0db44 /examples | |
parent | 57a603d7e0db73e4089c86e6d1cd797c2ad851b9 (diff) | |
parent | 6f790f8a40804ccfc43a08da8edd923ed866676f (diff) |
Merging from FastLED2.1
Diffstat (limited to 'examples')
-rw-r--r-- | examples/ColorTemperature/ColorTemperature.ino | 85 | ||||
-rw-r--r-- | examples/Fire2012/Fire2012.ino | 96 | ||||
-rw-r--r-- | examples/XYMatrix/XYMatrix.ino | 197 |
3 files changed, 378 insertions, 0 deletions
diff --git a/examples/ColorTemperature/ColorTemperature.ino b/examples/ColorTemperature/ColorTemperature.ino new file mode 100644 index 00000000..66093e06 --- /dev/null +++ b/examples/ColorTemperature/ColorTemperature.ino @@ -0,0 +1,85 @@ +#include <FastLED.h> + +#define LED_PIN 3 + +// Information about the LED strip itself +#define NUM_LEDS 60 +#define CHIPSET WS2811 +#define COLOR_ORDER GRB +CRGB leds[NUM_LEDS]; + +#define BRIGHTNESS 128 + + +// FastLED v2.1 provides two color-management controls: +// (1) color correction settings for each LED strip, and +// (2) master control of the overall output 'color temperature' +// +// THIS EXAMPLE demonstrates the second, "color temperature" control. +// It shows a simple rainbow animation first with one temperature profile, +// and a few seconds later, with a different temperature profile. +// +// The first pixel of the strip will show the color temperature. +// +// HELPFUL HINTS for "seeing" the effect in this demo: +// * Don't look directly at the LED pixels. Shine the LEDs aganst +// a white wall, table, or piece of paper, and look at the reflected light. +// +// * If you watch it for a bit, and then walk away, and then come back +// to it, you'll probably be able to "see" whether it's currently using +// the 'redder' or the 'bluer' temperature profile, even not counting +// the lowest 'indicator' pixel. +// +// +// FastLED provides these pre-conigured incandescent color profiles: +// Candle, Tungsten40W, Tungsten100W, Halogen, CarbonArc, +// HighNoonSun, DirectSunlight, OvercastSky, ClearBlueSky, +// FastLED provides these pre-configured gaseous-light color profiles: +// WarmFluorescent, StandardFluorescent, CoolWhiteFluorescent, +// FullSpectrumFluorescent, GrowLightFluorescent, BlackLightFluorescent, +// MercuryVapor, SodiumVapor, MetalHalide, HighPressureSodium, +// FastLED also provides an "Uncorrected temperature" profile +// UncorrectedTemperature; + +#define TEMPERATURE_1 Tungsten100W +#define TEMPERATURE_2 OvercastSky + +// How many seconds to show each temperature before switching +#define DISPLAYTIME 20 +// How many seconds to show black between switches +#define BLACKTIME 3 + +void loop() +{ + // draw a generic, no-name rainbow + static uint8_t starthue = 0; + fill_rainbow( leds + 5, NUM_LEDS - 5, --starthue, 20); + + // Choose which 'color temperature' profile to enable. + uint8_t secs = (millis() / 1000) % (DISPLAYTIME * 2); + if( secs < DISPLAYTIME) { + FastLED.setTemperature( TEMPERATURE_1 ); // first temperature + leds[0] = TEMPERATURE_1; // show indicator pixel + } else { + FastLED.setTemperature( TEMPERATURE_2 ); // second temperature + leds[0] = TEMPERATURE_2; // show indicator pixel + } + + // Black out the LEDs for a few secnds between color changes + // to let the eyes and brains adjust + if( (secs % DISPLAYTIME) < BLACKTIME) { + memset8( leds, 0, NUM_LEDS * sizeof(CRGB)); + } + + FastLED.show(); + FastLED.delay(8); +} + +void setup() { + delay( 3000 ); // power-up safety delay + // It's important to set the color correction for your LED strip here, + // so that colors can be more accurately rendered through the 'temperature' profiles + FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalSMD5050 ); + FastLED.setBrightness( BRIGHTNESS ); +} + diff --git a/examples/Fire2012/Fire2012.ino b/examples/Fire2012/Fire2012.ino new file mode 100644 index 00000000..73703426 --- /dev/null +++ b/examples/Fire2012/Fire2012.ino @@ -0,0 +1,96 @@ +#include <FastLED.h> + +#define LED_PIN 5 +#define COLOR_ORDER GRB +#define CHIPSET WS2811 +#define NUM_LEDS 30 + +#define BRIGHTNESS 200 +#define FRAMES_PER_SECOND 60 + +CRGB leds[NUM_LEDS]; + +void setup() { + delay(3000); // sanity delay + FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip ); + FastLED.setBrightness( BRIGHTNESS ); +} + +void loop() +{ + // Add entropy to random number generator; we use a lot of it. + random16_add_entropy( random()); + + Fire2012(); // run simulation frame + + FastLED.show(); // display this frame + FastLED.delay(1000 / FRAMES_PER_SECOND); +} + + +// Fire2012 by Mark Kriegsman, July 2012 +// as part of "Five Elements" shown here: http://youtu.be/knWiGsmgycY +//// +// This basic one-dimensional 'fire' simulation works roughly as follows: +// There's a underlying array of 'heat' cells, that model the temperature +// at each point along the line. Every cycle through the simulation, +// four steps are performed: +// 1) All cells cool down a little bit, losing heat to the air +// 2) The heat from each cell drifts 'up' and diffuses a little +// 3) Sometimes randomly new 'sparks' of heat are added at the bottom +// 4) The heat from each cell is rendered as a color into the leds array +// The heat-to-color mapping uses a black-body radiation approximation. +// +// Temperature is in arbitrary units from 0 (cold black) to 255 (white hot). +// +// This simulation scales it self a bit depending on NUM_LEDS; it should look +// "OK" on anywhere from 20 to 100 LEDs without too much tweaking. +// +// I recommend running this simulation at anywhere from 30-100 frames per second, +// meaning an interframe delay of about 10-35 milliseconds. +// +// Looks best on a high-density LED setup (60+ pixels/meter). +// +// +// There are two main parameters you can play with to control the look and +// feel of your fire: COOLING (used in step 1 above), and SPARKING (used +// in step 3 above). +// +// COOLING: How much does the air cool as it rises? +// Less cooling = taller flames. More cooling = shorter flames. +// Default 50, suggested range 20-100 +#define COOLING 55 + +// SPARKING: What chance (out of 255) is there that a new spark will be lit? +// Higher chance = more roaring fire. Lower chance = more flickery fire. +// Default 120, suggested range 50-200. +#define SPARKING 120 + + +void Fire2012() +{ +// Array of temperature readings at each simulation cell + static byte heat[NUM_LEDS]; + + // Step 1. Cool down every cell a little + for( int i = 0; i < NUM_LEDS; i++) { + heat[i] = qsub8( heat[i], random8(0, ((COOLING * 10) / NUM_LEDS) + 2)); + } + + // Step 2. Heat from each cell drifts 'up' and diffuses a little + for( int k= NUM_LEDS - 3; k > 0; k--) { + heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3; + } + + // Step 3. Randomly ignite new 'sparks' of heat near the bottom + if( random8() < SPARKING ) { + int y = random8(7); + heat[y] = qadd8( heat[y], random8(160,255) ); + } + + // Step 4. Map from heat cells to LED colors + for( int j = 0; j < NUM_LEDS; j++) { + leds[j] = HeatColor( heat[j]); + } +} + diff --git a/examples/XYMatrix/XYMatrix.ino b/examples/XYMatrix/XYMatrix.ino new file mode 100644 index 00000000..ff0c5d14 --- /dev/null +++ b/examples/XYMatrix/XYMatrix.ino @@ -0,0 +1,197 @@ +#include <FastLED.h> + +#define LED_PIN 3 + +#define COLOR_ORDER GRB +#define CHIPSET WS2811 + +#define BRIGHTNESS 64 + +// Helper functions for an two-dimensional XY matrix of pixels. +// Simple 2-D demo code is included as well. +// +// XY(x,y) takes x and y coordinates and returns an LED index number, +// for use like this: leds[ XY(x,y) ] == CRGB::Red; +// No error checking is performed on the ranges of x and y. +// +// XYsafe(x,y) takes x and y coordinates and returns an LED index number, +// for use like this: leds[ XY(x,y) ] == CRGB::Red; +// Error checking IS performed on the ranges of x and y, and an +// index of "-1" is returned. Special instructions below +// explain how to use this without having to do your own error +// checking every time you use this function. +// This is a slightly more advanced technique, and +// it REQUIRES SPECIAL ADDITIONAL setup, described below. + + +// Params for width and height +const uint8_t kMatrixWidth = 16; +const uint8_t kMatrixHeight = 16; + +// Param for different pixel layouts +const bool kMatrixSerpentineLayout = true; +// Set 'kMatrixSerpentineLayout' to false if your pixels are +// laid out all running the same way, like this: +// +// 0 > 1 > 2 > 3 > 4 +// | +// .----<----<----<----' +// | +// 5 > 6 > 7 > 8 > 9 +// | +// .----<----<----<----' +// | +// 10 > 11 > 12 > 13 > 14 +// | +// .----<----<----<----' +// | +// 15 > 16 > 17 > 18 > 19 +// +// Set 'kMatrixSerpentineLayout' to true if your pixels are +// laid out back-and-forth, like this: +// +// 0 > 1 > 2 > 3 > 4 +// | +// | +// 9 < 8 < 7 < 6 < 5 +// | +// | +// 10 > 11 > 12 > 13 > 14 +// | +// | +// 19 < 18 < 17 < 16 < 15 +// +// Bonus vocabulary word: anything that goes one way +// in one row, and then backwards in the next row, and so on +// is call "boustrophedon", meaning "as the ox plows." + + +// This function will return the right 'led index number' for +// a given set of X and Y coordinates on your matrix. +// IT DOES NOT CHECK THE COORDINATE BOUNDARIES. +// That's up to you. Don't pass it bogus values. +// +// Use the "XY" function like this: +// +// for( uint8_t x = 0; x < kMatrixWidth; x++) { +// for( uint8_t y = 0; y < kMatrixHeight; y++) { +// +// // Here's the x, y to 'led index' in action: +// leds[ XY( x, y) ] = CHSV( random8(), 255, 255); +// +// } +// } +// +// +uint16_t XY( uint8_t x, uint8_t y) +{ + uint16_t i; + + if( kMatrixSerpentineLayout == false) { + i = (y * kMatrixWidth) + x; + } + + if( kMatrixSerpentineLayout == true) { + if( y & 0x01) { + // Odd rows run backwards + uint8_t reverseX = (kMatrixWidth - 1) - x; + i = (y * kMatrixWidth) + reverseX; + } else { + // Even rows run forwards + i = (y * kMatrixWidth) + x; + } + } + + return i; +} + + +// Once you've gotten the basics working (AND NOT UNTIL THEN!) +// here's a helpful technique that can be tricky to set up, but +// then helps you avoid the needs for sprinkling array-bound-checking +// throughout your code. +// +// It requires a careful attention to get it set up correctly, but +// can potentially make your code smaller and faster. +// +// Suppose you have an 8 x 5 matrix of 40 LEDs. Normally, you'd +// delcare your leds array like this: +// CRGB leds[40]; +// But instead of that, declare an LED buffer with one extra pixel in +// it, "leds_plus_safety_pixel". Then declare "leds" as a pointer to +// that array, but starting with the 2nd element (id=1) of that array: +// CRGB leds_with_safety_pixel[41]; +// const CRGB* leds( leds_plus_safety_pixel + 1); +// Then you use the "leds" array as you normally would. +// Now "leds[0..N]" are aliases for "leds_plus_safety_pixel[1..(N+1)]", +// AND leds[-1] is now a legitimate and safe alias for leds_plus_safety_pixel[0]. +// leds_plus_safety_pixel[0] aka leds[-1] is now your "safety pixel". +// +// Now instead of using the XY function above, use the one below, "XYsafe". +// +// If the X and Y values are 'in bounds', this function will return an index +// into the visible led array, same as "XY" does. +// HOWEVER -- and this is the trick -- if the X or Y values +// are out of bounds, this function will return an index of -1. +// And since leds[-1] is actually just an alias for leds_plus_safety_pixel[0], +// it's a totally safe and legal place to access. And since the 'safety pixel' +// falls 'outside' the visible part of the LED array, anything you write +// there is hidden from view automatically. +// Thus, this line of code is totally safe, regardless of the actual size of +// your matrix: +// leds[ XYsafe( random8(), random8() ) ] = CHSV( random8(), 255, 255); +// +// The only catch here is that while this makes it safe to read from and +// write to 'any pixel', there's really only ONE 'safety pixel'. No matter +// what out-of-bounds coordinates you write to, you'll really be writing to +// that one safety pixel. And if you try to READ from the safety pixel, +// you'll read whatever was written there last, reglardless of what coordinates +// were supplied. + +#define NUM_LEDS (kMatrixWidth * kMatrixHeight) +CRGB leds_plus_safety_pixel[ NUM_LEDS + 1]; +CRGB* leds( leds_plus_safety_pixel + 1); + +uint16_t XYsafe( uint8_t x, uint8_t y) +{ + if( x >= kMatrixWidth) return -1; + if( y >= kMatrixHeight) return -1; + return XY(x,y); +} + + +// Demo that USES "XY" follows code below + +void loop() +{ + uint32_t ms = millis(); + int32_t yHueDelta32 = ((int32_t)cos16( ms * (27/1) ) * (350 / kMatrixWidth)); + int32_t xHueDelta32 = ((int32_t)cos16( ms * (39/1) ) * (310 / kMatrixHeight)); + DrawOneFrame( ms / 65536, yHueDelta32 / 32768, xHueDelta32 / 32768); + if( ms < 5000 ) { + FastLED.setBrightness( scale8( BRIGHTNESS, (ms * 256) / 5000)); + } else { + FastLED.setBrightness(BRIGHTNESS); + } + FastLED.show(); +} + +void DrawOneFrame( byte startHue8, int8_t yHueDelta8, int8_t xHueDelta8) +{ + byte lineStartHue = startHue8; + for( byte y = 0; y < kMatrixHeight; y++) { + lineStartHue += yHueDelta8; + byte pixelHue = lineStartHue; + for( byte x = 0; x < kMatrixWidth; x++) { + pixelHue += xHueDelta8; + leds[ XY(x, y)] = CHSV( pixelHue, 255, 255); + } + } +} + + +void setup() { + FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalSMD5050); + FastLED.setBrightness( BRIGHTNESS ); +} + |