Guest User

FastLED lookup table example

a guest
Apr 28th, 2016
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.20 KB | None | 0 0
  1. #include<FastLED.h>
  2.  
  3. #define DATA_PIN    7
  4. #define CLK_PIN     14
  5. #define BRIGHTNESS  64  //3
  6. #define LED_TYPE    LPD8806
  7. #define COLOR_ORDER BRG
  8. #define FRAMES_PER_SECOND  40
  9.  
  10. ///////////////////////////////////////////////////////////
  11. ///////////////////////////////////////////////////////////
  12. //     adjust these values to reflect the dimensions of your lookup table
  13. const uint8_t kMatrixWidth  = 12; //18
  14. const uint8_t kMatrixHeight = 15; //25
  15. const bool    kMatrixSerpentineLayout = true;
  16.  
  17. ///////////////////////////////////////////////////////////
  18. ///////////////////////////////////////////////////////////
  19. //     here is the lookup table, nice an evenly spaced
  20. uint8_t LEDvanLookUp[] = {
  21.   144,   0,   0, 109, 110, 111, 112, 113, 114,   0,   0, 115,
  22.   143,   0,   0,   0, 108, 107, 106, 105,   0,   0,   0, 116,
  23.   142,   0,   0,  28,  29,  52,  53,  76,  77,   0,   0, 117,
  24.   141,   0,   0,  27,  30,  51,  54,  75,  78,   0,   0, 118,
  25.   140,   8,   9,  26,  31,  50,  55,  74,  79,  96,  97, 119,
  26.   139,   7,  10,  25,  32,  49,  56,  73,  80,  95,  98, 120,
  27.   138,   6,  11,  24,  33,  48,  57,  72,  81,  94,  99, 121,
  28.   137,   5,  12,  23,  34,  47,  58,  71,  82,  93, 100, 122,
  29.   136,   4,  13,  22,  35,  46,  59,  70,  83,  92, 101, 123,
  30.   135,   3,  14,  21,  36,  45,  60,  69,  84,  91, 102, 124,
  31.   134,   2,  15,  20,  37,  44,  61,  68,  85,  90, 103, 125,
  32.   133,   1,  16,  19,  38,  43,  62,  67,  86,  89, 104, 126,
  33.     0,   0,   0,  18,  39,  42,  63,  66,  87,   0,   0,   0,
  34.     0,   0,   0,  17,  40,  41,  64,  65,  88,   0,   0,   0,
  35.     0,   0,   0, 132, 131, 130, 129, 128, 127,   0,   0,   0
  36. };
  37.  
  38. // This example combines two features of FastLED to produce a remarkable range of
  39. // effects from a relatively small amount of code.  This example combines FastLED's
  40. // color palette lookup functions with FastLED's Perlin/simplex noise generator, and
  41. // the combination is extremely powerful.
  42. //
  43. // You might want to look at the "ColorPalette" and "Noise" examples separately
  44. // if this example code seems daunting.
  45. //
  46. //
  47. // The basic setup here is that for each frame, we generate a new array of
  48. // 'noise' data, and then map it onto the LED matrix through a color palette.
  49. //
  50. // Periodically, the color palette is changed, and new noise-generation parameters
  51. // are chosen at the same time.  In this example, specific noise-generation
  52. // values have been selected to match the given color palettes; some are faster,
  53. // or slower, or larger, or smaller than others, but there's no reason these
  54. // parameters can't be freely mixed-and-matched.
  55. //
  56. // In addition, this example includes some fast automatic 'data smoothing' at
  57. // lower noise speeds to help produce smoother animations in those cases.
  58. //
  59. // The FastLED built-in color palettes (Forest, Clouds, Lava, Ocean, Party) are
  60. // used, as well as some 'hand-defined' ones, and some proceedurally generated
  61. // palettes.
  62.  
  63.  
  64. #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
  65. #define MAX_DIMENSION ((kMatrixWidth>kMatrixHeight) ? kMatrixWidth : kMatrixHeight)
  66.  
  67. // The leds
  68. CRGB leds[kMatrixWidth * kMatrixHeight];
  69.  
  70. // The 16 bit version of our coordinates
  71. static uint16_t x;
  72. static uint16_t y;
  73. static uint16_t z;
  74.  
  75. // We're using the x/y dimensions to map to the x/y pixels on the matrix.  We'll
  76. // use the z-axis for "time".  speed determines how fast time moves forward.  Try
  77. // 1 for a very slow moving effect, or 60 for something that ends up looking like
  78. // water.
  79.  
  80. uint16_t speed = 1; // 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.  
  87. uint16_t scale = 14; // scale is set dynamically once we've started up
  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( LavaColors_p );
  92. uint8_t       colorLoop = 0;
  93.  
  94. void setup() {
  95.   delay(3000);
  96.   FastLED.addLeds<LED_TYPE, DATA_PIN, CLK_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  97.   LEDS.setBrightness(BRIGHTNESS);
  98.  
  99.   RainbowPalette();
  100.   // Initialize our coordinates to some random values
  101.   x = random16();
  102.   y = random16();
  103.   z = random16();
  104. }
  105.  
  106. // Fill the x/y array of 8-bit noise values using the inoise8 function.
  107. void fillnoise8() {
  108.   // If we're runing at a low "speed", some 8-bit artifacts become visible
  109.   // from frame-to-frame.  In order to reduce this, we can do some fast data-smoothing.
  110.   // The amount of data smoothing we're doing depends on "speed".
  111.   uint8_t dataSmoothing = 0;
  112.   if ( speed < 50) {
  113.     dataSmoothing = 200 - (speed * 4);
  114.   }
  115.  
  116.   for (int i = 0; i < MAX_DIMENSION; i++) {
  117.     int ioffset = scale * i;
  118.     for (int j = 0; j < MAX_DIMENSION; j++) {
  119.       int joffset = scale * j;
  120.  
  121.       uint8_t data = inoise8(x + ioffset, y + joffset, z);
  122.  
  123.       // The range of the inoise8 function is roughly 16-238.
  124.       // These two operations expand those values out to roughly 0..255
  125.       // You can comment them out if you want the raw noise data.
  126.       data = qsub8(data, 16);
  127.       data = qadd8(data, scale8(data, 39));
  128.  
  129.       if ( dataSmoothing ) {
  130.         uint8_t olddata = noise[i][j];
  131.         uint8_t newdata = scale8( olddata, dataSmoothing) + scale8( data, 256 - dataSmoothing);
  132.         data = newdata;
  133.       }
  134.  
  135.       noise[i][j] = data;
  136.     }
  137.   }
  138.  
  139.   z += speed * 1;
  140.  
  141.   // apply slow drift to X and Y, just for visual variation.
  142.  
  143.   x -= 10;
  144.   y += 0;
  145. }
  146.  
  147. void mapNoiseToLEDsUsingPalette()
  148. {
  149.   static uint8_t ihue = 0;
  150.  
  151.   for (int i = 0; i < kMatrixWidth; i++) {
  152.     for (int j = 0; j < kMatrixHeight; j++) {
  153.       // ONLY EXECUTE NOISE FUNCTION INSIDE PIXELS
  154.       if ( i < kMatrixWidth ) {
  155.  
  156.        
  157.         ///////////////////////////////////////////////////////////
  158.         ///////////////////////////////////////////////////////////
  159.         //     here is where the lookup table comes into play
  160.         uint8_t lookUpNum = LEDvanLookUp[i + (j * kMatrixWidth)];
  161.         //     make sure there is an LED in the lookup table index (greater than 0)
  162.         if ( lookUpNum > 0 ) {
  163.  
  164.          
  165.           // We use the value at the (i,j) coordinate in the noise
  166.           // array for our brightness, and the flipped value from (j,i)
  167.           // for our pixel's index into the color palette.
  168.           uint8_t index = noise[j][i];
  169.           uint8_t bri =   noise[i][j];
  170.  
  171.           // if this palette is a 'loop', add a slowly-changing base value
  172.           if ( colorLoop) {
  173.             index += ihue;
  174.           }
  175.  
  176.           // brighten up, as the color palette itself often contains the
  177.           // light/dark dynamic range desired
  178.           if ( bri > 127 ) {
  179.             bri = 255;
  180.           } else {
  181.             bri = dim8_raw( bri * 2);
  182.           }
  183.  
  184.           CRGB color = ColorFromPalette( currentPalette, index, bri);
  185.  
  186.           ///////////////////////////////////////////////////////////
  187.           ///////////////////////////////////////////////////////////
  188.           // don't forget to subtract 1 to account for starting counting at 1 and not 0.
  189.           leds[lookUpNum - 1] = color;
  190.  
  191.          
  192.         }
  193.       } else {
  194.         // Nothing
  195.       }
  196.     }
  197.   }
  198.  
  199.   ihue += 1;
  200. }
  201.  
  202. void loop() {
  203.   // generate noise data
  204.   fillnoise8();
  205.  
  206.   // convert the noise data to colors in the LED array
  207.   // using the current palette
  208.   mapNoiseToLEDsUsingPalette();
  209.  
  210.   LEDS.show();
  211.   FastLED.delay(1000 / FRAMES_PER_SECOND);
  212. }
  213.  
  214. void RainbowPalette() {
  215.   currentPalette = RainbowColors_p;
  216. }
  217.  
  218. //
  219. // Mark's xy coordinate mapping code.  See the XYMatrix for more information on it.
  220. //
  221. uint16_t XY( uint8_t x, uint8_t y)
  222. {
  223.   uint16_t i;
  224.   if ( kMatrixSerpentineLayout == false) {
  225.     i = (y * kMatrixWidth) + x;
  226.   }
  227.   if ( kMatrixSerpentineLayout == true) {
  228.     if ( y & 0x01) {
  229.       // Odd rows run backwards
  230.       uint8_t reverseX = (kMatrixWidth - 1) - x;
  231.       i = (y * kMatrixWidth) + reverseX;
  232.     } else {
  233.       // Even rows run forwards
  234.       i = (y * kMatrixWidth) + x;
  235.     }
  236.   }
  237.   return i;
  238. }
Advertisement
Add Comment
Please, Sign In to add comment