Advertisement
kriegsman

Anti-aliased light bar example (v1.1 "demo")

Apr 16th, 2014
2,600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.84 KB | None | 0 0
  1. #include <FastLED.h>
  2.  
  3. #define DATA_PIN 13
  4. #define NUM_LEDS 60
  5. CRGB leds[NUM_LEDS];
  6.  
  7.  
  8. // Anti-aliased light bar example
  9. //   v1.0 is here http://pastebin.com/yAgKs0Ay
  10. //   v1.1 by Mark Kriegsman <kriegsman@tr.org>, April 16, 2013
  11. //   More of a toy than a side-by-side demo...
  12. //   I tore out all the "Integer bar" code, leaving only the anti-aliased bar
  13. //   Also made it slowly rotate through all the hues.
  14.  
  15. int     F16pos = 0; // position of the "fraction-based bar"
  16. int     F16delta = 1; // how many 16ths of a pixel to move the Fractional Bar
  17. uint16_t Fhue16 = 0; // color for Fractional Bar
  18.  
  19. int Width  = 4; // width of each light bar, in whole pixels
  20.  
  21. int InterframeDelay = 40; //ms
  22.  
  23.  
  24. void setup() {
  25.   delay(3000); // setup guard
  26.   FastLED.addLeds<WS2811, DATA_PIN, GRB>(leds, NUM_LEDS);
  27.   FastLED.setBrightness(128);
  28. }
  29.  
  30.  
  31.  
  32. // Draw a "Fractional Bar" of light starting at position 'pos16', which is counted in
  33. // sixteenths of a pixel from the start of the strip.  Fractional positions are
  34. // rendered using 'anti-aliasing' of pixel brightness.
  35. // The bar width is specified in whole pixels.
  36. // Arguably, this is the interesting code.
  37. void drawFractionalBar( int pos16, int width, uint8_t hue)
  38. {
  39.   int i = pos16 / 16; // convert from pos to raw pixel number
  40.   uint8_t frac = pos16 & 0x0F; // extract the 'factional' part of the position
  41.  
  42.   // brightness of the first pixel in the bar is 1.0 - (fractional part of position)
  43.   // e.g., if the light bar starts drawing at pixel "57.9", then
  44.   // pixel #57 should only be lit at 10% brightness, because only 1/10th of it
  45.   // is "in" the light bar:
  46.   //
  47.   //                       57.9 . . . . . . . . . . . . . . . . . 61.9
  48.   //                        v                                      v
  49.   //  ---+---56----+---57----+---58----+---59----+---60----+---61----+---62---->
  50.   //     |         |        X|XXXXXXXXX|XXXXXXXXX|XXXXXXXXX|XXXXXXXX |  
  51.   //  ---+---------+---------+---------+---------+---------+---------+--------->
  52.   //                   10%       100%      100%      100%      90%        
  53.   //
  54.   // the fraction we get is in 16ths and needs to be converted to 256ths,
  55.   // so we multiply by 16.  We subtract from 255 because we want a high
  56.   // fraction (e.g. 0.9) to turn into a low brightness (e.g. 0.1)
  57.   uint8_t firstpixelbrightness = 255 - (frac * 16);
  58.  
  59.   // if the bar is of integer length, the last pixel's brightness is the
  60.   // reverse of the first pixel's; see illustration above.
  61.   uint8_t lastpixelbrightness  = 255 - firstpixelbrightness;
  62.  
  63.   // For a bar of width "N", the code has to consider "N+1" pixel positions,
  64.   // which is why the "<= width" below instead of "< width".
  65.  
  66.   uint8_t bright;
  67.   for( int n = 0; n <= width; n++) {
  68.     if( n == 0) {
  69.       // first pixel in the bar
  70.       bright = firstpixelbrightness;
  71.     } else if( n == width ) {
  72.       // last pixel in the bar
  73.       bright = lastpixelbrightness;
  74.     } else {
  75.       // middle pixels
  76.       bright = 255;
  77.     }
  78.    
  79.     leds[i] += CHSV( hue, 255, bright);
  80.     i++;
  81.     if( i == NUM_LEDS) i = 0; // wrap around
  82.   }
  83. }
  84.  
  85.  
  86.  
  87. void loop()
  88. {
  89.   // Update the "Fraction Bar" by 1/16th pixel every time
  90.   F16pos += F16delta;
  91.  
  92.   // wrap around at end
  93.   // remember that F16pos contains position in "16ths of a pixel"
  94.   // so the 'end of the strip' is (NUM_LEDS * 16)
  95.   if( F16pos >= (NUM_LEDS * 16)) {
  96.     F16pos -= (NUM_LEDS * 16);
  97.   }
  98.  
  99.    
  100.    // Draw everything:
  101.    // clear the pixel buffer
  102.    memset8( leds, 0, NUM_LEDS * sizeof(CRGB));
  103.  
  104.    // advance to the next hue
  105.    Fhue16 = Fhue16 + 19;
  106.    
  107.    // draw the Fractional Bar, length=4px
  108.    drawFractionalBar( F16pos, Width, Fhue16 / 256);
  109.    
  110.    FastLED.show();
  111. #if defined(FASTLED_VERSION) && (FASTLED_VERSION >= 2001000)
  112.   FastLED.delay(InterframeDelay);
  113. #else  
  114.   delay(InterframeDelay);
  115. #endif  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement