Advertisement
Guest User

testSinus

a guest
Oct 21st, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.37 KB | None | 0 0
  1. uint16_t getSinValue()
  2. {
  3.   uint16_t w16 = (millis() * 5) % 65536 ;
  4.   return sin16(w16) + 32768 ;
  5. }
  6.  
  7. void testAntiAliasedSinus()
  8. {
  9.   fillAll(getBackgroundColor(4)) ;
  10.   antiAliasedSinusHelper(ledsExt, NUM_LEDS_EXT) ;
  11.   antiAliasedSinusHelper(ledsMil, NUM_LEDS_MIL) ;
  12.   antiAliasedSinusHelper(ledsInt, NUM_LEDS_INT) ;
  13.   FastLED.show() ;
  14. }
  15.  
  16. void antiAliasedSinusHelper(CRGB* heart, int nbPixels)
  17. {
  18.   int width = nbPixels / 14 ;
  19.   int pixelGauche = get16PixelPosition(nbPixels / 2, width) ;
  20.   int pixelDroit = nbPixels * 16 - pixelGauche - 16 * width;
  21.   drawFractionalBar(heart, pixelGauche, width) ;
  22.   drawFractionalBar(heart, pixelDroit, width) ;
  23. }
  24.  
  25. int get16PixelPosition(int nbPixels, int width)
  26. {
  27.   return getSinValue() * (nbPixels * 16 - 16 * width) / 65536;
  28. }
  29.  
  30. void drawFractionalBar(CRGB* heart, int pos16, int width )
  31. {
  32.   int i = pos16 / 16;
  33.   uint8_t frac = pos16 & 0x0F;
  34.   uint8_t firstpixelbrightness = 255 - (frac * 16);
  35.   uint8_t lastpixelbrightness  = 255 - firstpixelbrightness;
  36.   uint8_t bright;
  37.   for( int n = 0; n <= width; n++) {
  38.     if( n == 0 ) {
  39.       // first pixel in the bar
  40.       bright = firstpixelbrightness;
  41.     } else if( n == width ) {
  42.       // last pixel in the bar
  43.       bright = lastpixelbrightness;
  44.     } else {
  45.       // middle pixels
  46.       bright = 255;
  47.     }
  48.    
  49.     heart[i] += CHSV( 0, 0, bright);
  50.     i++;
  51.   }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement