TranceGeniK

sinusTest

Oct 21st, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.23 KB | None | 0 0
  1. #include <FastLED.h>
  2.  
  3. #define NUM_LEDS_EXT 116
  4. #define NUM_LEDS_MIL 68
  5. #define NUM_LEDS_INT 48
  6.  
  7. #define DATA_PIN_EXT 6
  8. #define DATA_PIN_MIL 7
  9. #define DATA_PIN_INT 8
  10.  
  11. CRGB ledsExt[NUM_LEDS_EXT];
  12. CRGB ledsMil[NUM_LEDS_MIL];
  13. CRGB ledsInt[NUM_LEDS_INT];
  14. uint8_t ihue = 0;
  15.  
  16. void setup() {
  17.       FastLED.delay(2000);
  18.       FastLED.addLeds<WS2812, DATA_PIN_EXT, GRB>(ledsExt, NUM_LEDS_EXT).setDither(0);
  19.       FastLED.addLeds<WS2812, DATA_PIN_MIL, GRB>(ledsMil, NUM_LEDS_MIL).setDither(0);
  20.       FastLED.addLeds<WS2812, DATA_PIN_INT, GRB>(ledsInt, NUM_LEDS_INT).setDither(0);
  21. }
  22.  
  23. void loop() {
  24.   testAntiAliasedSinus() ;
  25. }
  26.  
  27. void fillAll(CRGB color)
  28. {
  29.   fill_solid(ledsExt, NUM_LEDS_EXT, color);
  30.   fill_solid(ledsMil, NUM_LEDS_MIL, color);
  31.   fill_solid(ledsInt, NUM_LEDS_INT, color);
  32. }
  33.  
  34. CRGB getBackgroundColor()
  35. {
  36.   return CHSV(ihue, 255, 255) ;
  37. }
  38.  
  39. uint16_t getSinValue()
  40. {
  41.   uint16_t w16 = (millis() * 5) % 65536 ;
  42.   return sin16(w16) + 32768 ;
  43. }
  44.  
  45. void testAntiAliasedSinus()
  46. {
  47.   fillAll(getBackgroundColor()) ;
  48.   antiAliasedSinusHelper(ledsExt, NUM_LEDS_EXT) ;
  49.   antiAliasedSinusHelper(ledsMil, NUM_LEDS_MIL) ;
  50.   antiAliasedSinusHelper(ledsInt, NUM_LEDS_INT) ;
  51.   FastLED.show() ;
  52. }
  53.  
  54. void antiAliasedSinusHelper(CRGB* heart, int nbPixels)
  55. {
  56.   int width = nbPixels / 14 ;
  57.   int pixelGauche = get16PixelPosition(nbPixels / 2, width) ;
  58.   int pixelDroit = nbPixels * 16 - pixelGauche - 16 * width;
  59.   drawFractionalBar(heart, pixelGauche, width) ;
  60.   drawFractionalBar(heart, pixelDroit, width) ;
  61. }
  62.  
  63. int get16PixelPosition(int nbPixels, int width)
  64. {
  65.   return getSinValue() * (nbPixels * 16 - 16 * width) / 65536;
  66. }
  67.  
  68. void drawFractionalBar(CRGB* heart, int pos16, int width )
  69. {
  70.   int i = pos16 / 16;
  71.   uint8_t frac = pos16 & 0x0F;
  72.   uint8_t firstpixelbrightness = 255 - (frac * 16);
  73.   uint8_t lastpixelbrightness  = 255 - firstpixelbrightness;
  74.   uint8_t bright;
  75.   for( int n = 0; n <= width; n++) {
  76.     if( n == 0 ) {
  77.       // first pixel in the bar
  78.       bright = firstpixelbrightness;
  79.     } else if( n == width ) {
  80.       // last pixel in the bar
  81.       bright = lastpixelbrightness;
  82.     } else {
  83.       // middle pixels
  84.       bright = 255;
  85.     }
  86.    
  87.     heart[i] += CHSV( 0, 0, bright);
  88.     i++;
  89.   }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment