1 #define FASTLED_INTERNAL
4 FASTLED_NAMESPACE_BEGIN
6 #define P(x) FL_PGM_READ_BYTE_NEAR(p + x)
8 FL_PROGMEM static uint8_t
const p[] = { 151,160,137,91,90,15,
9 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
10 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
11 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
12 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
13 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
14 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
15 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
16 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
17 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
18 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
19 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
20 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180,151
24 #if FASTLED_NOISE_ALLOW_AVERAGE_TO_OVERFLOW == 1
25 #define AVG15(U,V) (((U)+(V)) >> 1)
28 #if defined(__AVR__) && (LIB8_ATTINY == 0)
29 #define AVG15(U,V) (avg15_inline_avr_mul((U),(V)))
33 static int16_t
inline __attribute__((always_inline)) avg15_inline_avr_mul( int16_t i, int16_t j)
43 "adc %A[i], %A[j] \n\t"
44 "adc %B[i], %B[j] \n\t"
50 #define AVG15(U,V) (avg15((U),(V)))
59 #define FADE logfade12
60 #define LERP(a,b,u) lerp15by12(a,b,u)
62 #define FADE(x) scale16(x,x)
63 #define LERP(a,b,u) lerp15by16(a,b,u)
65 static int16_t
inline __attribute__((always_inline)) grad16(uint8_t hash, int16_t x, int16_t y, int16_t z) {
68 case 0:
return (( x) + ( y))>>1;
69 case 1:
return ((-x) + ( y))>>1;
70 case 2:
return (( x) + (-y))>>1;
71 case 3:
return ((-x) + (-y))>>1;
72 case 4:
return (( x) + ( z))>>1;
73 case 5:
return ((-x) + ( z))>>1;
74 case 6:
return (( x) + (-z))>>1;
75 case 7:
return ((-x) + (-z))>>1;
76 case 8:
return (( y) + ( z))>>1;
77 case 9:
return ((-y) + ( z))>>1;
78 case 10:
return (( y) + (-z))>>1;
79 case 11:
return ((-y) + (-z))>>1;
80 case 12:
return (( y) + ( x))>>1;
81 case 13:
return ((-y) + ( z))>>1;
82 case 14:
return (( y) + (-x))>>1;
83 case 15:
return ((-y) + (-z))>>1;
87 int16_t u = hash<8?x:y;
88 int16_t v = hash<4?y:hash==12||hash==14?x:z;
89 if(hash&1) { u = -u; }
90 if(hash&2) { v = -v; }
96 static int16_t
inline __attribute__((always_inline)) grad16(uint8_t hash, int16_t x, int16_t y) {
99 if(hash < 4) { u = x; v = y; }
else { u = y; v = x; }
100 if(hash&1) { u = -u; }
101 if(hash&2) { v = -v; }
106 static int16_t
inline __attribute__((always_inline)) grad16(uint8_t hash, int16_t x) {
109 if(hash > 8) { u=x;v=x; }
110 else if(hash < 4) { u=x;v=1; }
112 if(hash&1) { u = -u; }
113 if(hash&2) { v = -v; }
122 static int8_t
inline __attribute__((always_inline)) selectBasedOnHashBit(uint8_t hash, uint8_t bitnumber, int8_t a, int8_t b) {
124 #if !defined(__AVR__)
125 result = (hash & (1<<bitnumber)) ? a : b;
128 "mov %[result],%[a] \n\t"
129 "sbrs %[hash],%[bitnumber] \n\t"
130 "mov %[result],%[b] \n\t"
131 : [result]
"=r" (result)
133 [bitnumber]
"M" (bitnumber),
141 static int8_t
inline __attribute__((always_inline)) grad8(uint8_t hash, int8_t x, int8_t y, int8_t z) {
144 case 0:
return (( x) + ( y))>>1;
145 case 1:
return ((-x) + ( y))>>1;
146 case 2:
return (( x) + (-y))>>1;
147 case 3:
return ((-x) + (-y))>>1;
148 case 4:
return (( x) + ( z))>>1;
149 case 5:
return ((-x) + ( z))>>1;
150 case 6:
return (( x) + (-z))>>1;
151 case 7:
return ((-x) + (-z))>>1;
152 case 8:
return (( y) + ( z))>>1;
153 case 9:
return ((-y) + ( z))>>1;
154 case 10:
return (( y) + (-z))>>1;
155 case 11:
return ((-y) + (-z))>>1;
156 case 12:
return (( y) + ( x))>>1;
157 case 13:
return ((-y) + ( z))>>1;
158 case 14:
return (( y) + (-x))>>1;
159 case 15:
return ((-y) + (-z))>>1;
167 u = selectBasedOnHashBit( hash, 3, y, x);
170 v = hash<4?y:hash==12||hash==14?x:z;
176 if( hash==12 || hash==14) {
184 if(hash&1) { u = -u; }
185 if(hash&2) { v = -v; }
191 static int8_t
inline __attribute__((always_inline)) grad8(uint8_t hash, int8_t x, int8_t y)
204 if(hash&1) { u = -u; }
205 if(hash&2) { v = -v; }
210 static int8_t
inline __attribute__((always_inline)) grad8(uint8_t hash, int8_t x)
227 if(hash&1) { u = -u; }
228 if(hash&2) { v = -v; }
235 uint16_t logfade12(uint16_t val) {
239 static int16_t
inline __attribute__((always_inline)) lerp15by12( int16_t a, int16_t b,
fract16 frac)
244 uint16_t delta = b - a;
245 uint16_t scaled =
scale16(delta,frac<<4);
248 uint16_t delta = a - b;
249 uint16_t scaled =
scale16(delta,frac<<4);
256 static int8_t
inline __attribute__((always_inline)) lerp7by8( int8_t a, int8_t b,
fract8 frac)
265 uint8_t delta = b - a;
266 uint8_t scaled =
scale8( delta, frac);
269 uint8_t delta = a - b;
270 uint8_t scaled =
scale8( delta, frac);
279 uint8_t X = (x>>16)&0xFF;
280 uint8_t Y = (y>>16)&0xFF;
281 uint8_t Z = (z>>16)&0xFF;
286 uint8_t AB = P(A+1)+Z;
287 uint8_t B = P(X+1)+Y;
288 uint8_t BA = P(B) + Z;
289 uint8_t BB = P(B+1)+Z;
292 uint16_t u = x & 0xFFFF;
293 uint16_t v = y & 0xFFFF;
294 uint16_t w = z & 0xFFFF;
297 int16_t xx = (u >> 1) & 0x7FFF;
298 int16_t yy = (v >> 1) & 0x7FFF;
299 int16_t zz = (w >> 1) & 0x7FFF;
300 uint16_t N = 0x8000L;
302 u = FADE(u); v = FADE(v); w = FADE(w);
307 int16_t X1 = LERP(grad16(P(AA), xx, yy, zz), grad16(P(BA), xx - N, yy, zz), u);
308 int16_t X2 = LERP(grad16(P(AB), xx, yy-N, zz), grad16(P(BB), xx - N, yy - N, zz), u);
309 int16_t X3 = LERP(grad16(P(AA+1), xx, yy, zz-N), grad16(P(BA+1), xx - N, yy, zz-N), u);
310 int16_t X4 = LERP(grad16(P(AB+1), xx, yy-N, zz-N), grad16(P(BB+1), xx - N, yy - N, zz - N), u);
312 int16_t Y1 = LERP(X1,X2,v);
313 int16_t Y2 = LERP(X3,X4,v);
315 int16_t ans = LERP(Y1,Y2,w);
320 uint16_t inoise16(uint32_t x, uint32_t y, uint32_t z) {
346 uint8_t B = P(X+1)+Y;
351 uint16_t u = x & 0xFFFF;
352 uint16_t v = y & 0xFFFF;
355 int16_t xx = (u >> 1) & 0x7FFF;
356 int16_t yy = (v >> 1) & 0x7FFF;
357 uint16_t N = 0x8000L;
359 u = FADE(u); v = FADE(v);
361 int16_t X1 = LERP(grad16(P(AA), xx, yy), grad16(P(BA), xx - N, yy), u);
362 int16_t X2 = LERP(grad16(P(AB), xx, yy-N), grad16(P(BB), xx - N, yy - N), u);
364 int16_t ans = LERP(X1,X2,v);
369 uint16_t inoise16(uint32_t x, uint32_t y) {
396 uint16_t u = x & 0xFFFF;
399 int16_t xx = (u >> 1) & 0x7FFF;
400 uint16_t N = 0x8000L;
404 int16_t ans = LERP(grad16(P(AA), xx), grad16(P(BA), xx - N), u);
409 uint16_t inoise16(uint32_t x) {
410 return ((uint32_t)((int32_t)
inoise16_raw(x) + 17308L)) << 1;
413 int8_t inoise8_raw(uint16_t x, uint16_t y, uint16_t z)
423 uint8_t AB = P(A+1)+Z;
424 uint8_t B = P(X+1)+Y;
425 uint8_t BA = P(B) + Z;
426 uint8_t BB = P(B+1)+Z;
434 int8_t xx = ((uint8_t)(x)>>1) & 0x7F;
435 int8_t yy = ((uint8_t)(y)>>1) & 0x7F;
436 int8_t zz = ((uint8_t)(z)>>1) & 0x7F;
442 int8_t X1 = lerp7by8(grad8(P(AA), xx, yy, zz), grad8(P(BA), xx - N, yy, zz), u);
443 int8_t X2 = lerp7by8(grad8(P(AB), xx, yy-N, zz), grad8(P(BB), xx - N, yy - N, zz), u);
444 int8_t X3 = lerp7by8(grad8(P(AA+1), xx, yy, zz-N), grad8(P(BA+1), xx - N, yy, zz-N), u);
445 int8_t X4 = lerp7by8(grad8(P(AB+1), xx, yy-N, zz-N), grad8(P(BB+1), xx - N, yy - N, zz - N), u);
447 int8_t Y1 = lerp7by8(X1,X2,v);
448 int8_t Y2 = lerp7by8(X3,X4,v);
450 int8_t ans = lerp7by8(Y1,Y2,w);
455 uint8_t inoise8(uint16_t x, uint16_t y, uint16_t z) {
456 return scale8(76+(inoise8_raw(x,y,z)),215)<<1;
459 int8_t inoise8_raw(uint16_t x, uint16_t y)
469 uint8_t B = P(X+1)+Y;
478 int8_t xx = ((uint8_t)(x)>>1) & 0x7F;
479 int8_t yy = ((uint8_t)(y)>>1) & 0x7F;
485 int8_t X1 = lerp7by8(grad8(P(AA), xx, yy), grad8(P(BA), xx - N, yy), u);
486 int8_t X2 = lerp7by8(grad8(P(AB), xx, yy-N), grad8(P(BB), xx - N, yy - N), u);
488 int8_t ans = lerp7by8(X1,X2,v);
494 uint8_t inoise8(uint16_t x, uint16_t y) {
495 return scale8(69+inoise8_raw(x,y),237)<<1;
498 int8_t inoise8_raw(uint16_t x)
513 int8_t xx = ((uint8_t)(x)>>1) & 0x7F;
518 int8_t ans = lerp7by8(grad8(P(AA), xx), grad8(P(BA), xx - N), u);
524 uint8_t inoise8(uint16_t x) {
525 return scale8(69+inoise8_raw(x), 255)<<1;
542 void fill_raw_noise8(uint8_t *pData, uint8_t num_points, uint8_t octaves, uint16_t x,
int scale, uint16_t time) {
544 uint32_t scx = scale;
545 for(
int o = 0; o < octaves; o++) {
546 for(
int i = 0,xx=_xx; i < num_points; i++, xx+=scx) {
547 pData[i] =
qadd8(pData[i],inoise8(xx,time)>>o);
555 void fill_raw_noise16into8(uint8_t *pData, uint8_t num_points, uint8_t octaves, uint32_t x,
int scale, uint32_t time) {
557 uint32_t scx = scale;
558 for(
int o = 0; o < octaves; o++) {
559 for(
int i = 0,xx=_xx; i < num_points; i++, xx+=scx) {
560 uint32_t accum = (inoise16(xx,time))>>o;
561 accum += (pData[i]<<8);
562 if(accum > 65535) { accum = 65535; }
571 void fill_raw_2dnoise8(uint8_t *pData,
int width,
int height, uint8_t octaves,
q44 freq44,
fract8 amplitude,
int skip, uint16_t x,
int scalex, uint16_t y,
int scaley, uint16_t time) {
573 fill_raw_2dnoise8(pData, width, height, octaves-1, freq44, amplitude, skip+1, x*freq44, freq44 * scalex, y*freq44, freq44 * scaley, time);
582 fract8 invamp = 255-amplitude;
584 for(
int i = 0; i < height; i++, y+=scaley) {
585 uint8_t *pRow = pData + (i*width);
587 for(
int j = 0; j < width; j++, xx+=scalex) {
588 uint8_t noise_base = inoise8(xx,y,time);
589 noise_base = (0x80 & noise_base) ? (noise_base - 127) : (127 - noise_base);
590 noise_base =
scale8(noise_base<<1,amplitude);
592 pRow[j] =
scale8(pRow[j],invamp) + noise_base;
594 for(
int ii = i; ii<(i+skip) && ii<height; ii++) {
595 uint8_t *pRow = pData + (ii*width);
596 for(
int jj=j; jj<(j+skip) && jj<width; jj++) {
597 pRow[jj] =
scale8(pRow[jj],invamp) + noise_base;
605 void fill_raw_2dnoise8(uint8_t *pData,
int width,
int height, uint8_t octaves, uint16_t x,
int scalex, uint16_t y,
int scaley, uint16_t time) {
606 fill_raw_2dnoise8(pData, width, height, octaves,
q44(2,0), 128, 1, x, scalex, y, scaley, time);
609 void fill_raw_2dnoise16(uint16_t *pData,
int width,
int height, uint8_t octaves,
q88 freq88,
fract16 amplitude,
int skip, uint32_t x,
int scalex, uint32_t y,
int scaley, uint32_t time) {
611 fill_raw_2dnoise16(pData, width, height, octaves-1, freq88, amplitude, skip, x *freq88 , scalex *freq88, y * freq88, scaley * freq88, time);
619 fract16 invamp = 65535-amplitude;
620 for(
int i = 0; i < height; i+=skip, y+=scaley) {
621 uint16_t *pRow = pData + (i*width);
622 for(
int j = 0,xx=x; j < width; j+=skip, xx+=scalex) {
623 uint16_t noise_base = inoise16(xx,y,time);
624 noise_base = (0x8000 & noise_base) ? noise_base - (32767) : 32767 - noise_base;
625 noise_base =
scale16(noise_base<<1, amplitude);
627 pRow[j] =
scale16(pRow[j],invamp) + noise_base;
629 for(
int ii = i; ii<(i+skip) && ii<height; ii++) {
630 uint16_t *pRow = pData + (ii*width);
631 for(
int jj=j; jj<(j+skip) && jj<width; jj++) {
632 pRow[jj] =
scale16(pRow[jj],invamp) + noise_base;
640 int32_t nmin=11111110;
643 void fill_raw_2dnoise16into8(uint8_t *pData,
int width,
int height, uint8_t octaves,
q44 freq44,
fract8 amplitude,
int skip, uint32_t x,
int scalex, uint32_t y,
int scaley, uint32_t time) {
645 fill_raw_2dnoise16into8(pData, width, height, octaves-1, freq44, amplitude, skip+1, x*freq44, scalex *freq44, y*freq44, scaley * freq44, time);
654 fract8 invamp = 255-amplitude;
655 for(
int i = 0; i < height; i+=skip, y+=scaley) {
656 uint8_t *pRow = pData + (i*width);
658 for(
int j = 0; j < width; j+=skip, xx+=scalex) {
659 uint16_t noise_base = inoise16(xx,y,time);
660 noise_base = (0x8000 & noise_base) ? noise_base - (32767) : 32767 - noise_base;
661 noise_base =
scale8(noise_base>>7,amplitude);
663 pRow[j] =
qadd8(
scale8(pRow[j],invamp),noise_base);
665 for(
int ii = i; ii<(i+skip) && ii<height; ii++) {
666 uint8_t *pRow = pData + (ii*width);
667 for(
int jj=j; jj<(j+skip) && jj<width; jj++) {
668 pRow[jj] =
scale8(pRow[jj],invamp) + noise_base;
676 void fill_raw_2dnoise16into8(uint8_t *pData,
int width,
int height, uint8_t octaves, uint32_t x,
int scalex, uint32_t y,
int scaley, uint32_t time) {
677 fill_raw_2dnoise16into8(pData, width, height, octaves,
q44(2,0), 171, 1, x, scalex, y, scaley, time);
680 void fill_noise8(
CRGB *leds,
int num_leds,
681 uint8_t octaves, uint16_t x,
int scale,
682 uint8_t hue_octaves, uint16_t hue_x,
int hue_scale,
687 memset(V,0,num_leds);
688 memset(H,0,num_leds);
690 fill_raw_noise8(V,num_leds,octaves,x,scale,time);
691 fill_raw_noise8(H,num_leds,hue_octaves,hue_x,hue_scale,time);
693 for(
int i = 0; i < num_leds; i++) {
694 leds[i] =
CHSV(H[i],255,V[i]);
698 void fill_noise16(
CRGB *leds,
int num_leds,
699 uint8_t octaves, uint16_t x,
int scale,
700 uint8_t hue_octaves, uint16_t hue_x,
int hue_scale,
701 uint16_t time, uint8_t hue_shift) {
705 memset(V,0,num_leds);
706 memset(H,0,num_leds);
708 fill_raw_noise16into8(V,num_leds,octaves,x,scale,time);
709 fill_raw_noise8(H,num_leds,hue_octaves,hue_x,hue_scale,time);
711 for(
int i = 0; i < num_leds; i++) {
712 leds[i] =
CHSV(H[i] + hue_shift,255,V[i]);
716 void fill_2dnoise8(
CRGB *leds,
int width,
int height,
bool serpentine,
717 uint8_t octaves, uint16_t x,
int xscale, uint16_t y,
int yscale, uint16_t time,
718 uint8_t hue_octaves, uint16_t hue_x,
int hue_xscale, uint16_t hue_y, uint16_t hue_yscale,uint16_t hue_time,
bool blend) {
719 uint8_t V[height][width];
720 uint8_t H[height][width];
722 memset(V,0,height*width);
723 memset(H,0,height*width);
725 fill_raw_2dnoise8((uint8_t*)V,width,height,octaves,x,xscale,y,yscale,time);
726 fill_raw_2dnoise8((uint8_t*)H,width,height,hue_octaves,hue_x,hue_xscale,hue_y,hue_yscale,hue_time);
730 for(
int i = 0; i < height; i++) {
732 for(
int j = 0; j < width; j++) {
733 CRGB led(
CHSV(H[h1-i][w1-j],255,V[i][j]));
736 if(serpentine && (i & 0x1)) {
741 leds[wb+pos] >>= 1; leds[wb+pos] += (led>>=1);
749 void fill_2dnoise16(
CRGB *leds,
int width,
int height,
bool serpentine,
750 uint8_t octaves, uint32_t x,
int xscale, uint32_t y,
int yscale, uint32_t time,
751 uint8_t hue_octaves, uint16_t hue_x,
int hue_xscale, uint16_t hue_y, uint16_t hue_yscale,uint16_t hue_time,
bool blend, uint16_t hue_shift) {
752 uint8_t V[height][width];
753 uint8_t H[height][width];
755 memset(V,0,height*width);
756 memset(H,0,height*width);
758 fill_raw_2dnoise16into8((uint8_t*)V,width,height,octaves,
q44(2,0),171,1,x,xscale,y,yscale,time);
761 fill_raw_2dnoise8((uint8_t*)H,width,height,hue_octaves,hue_x,hue_xscale,hue_y,hue_yscale,hue_time);
768 for(
int i = 0; i < height; i++) {
770 for(
int j = 0; j < width; j++) {
771 CRGB led(
CHSV(hue_shift + (H[h1-i][w1-j]),196,V[i][j]));
774 if(serpentine && (i & 0x1)) {
779 leds[wb+pos] >>= 1; leds[wb+pos] += (led>>=1);
787 FASTLED_NAMESPACE_END
Representation of an RGB pixel (Red, Green, Blue)
uint16_t fract16
ANSI: unsigned _Fract.
LIB8STATIC uint16_t scale16(uint16_t i, fract16 scale)
scale a 16-bit unsigned value by a 16-bit value, considered as numerator of a fraction whose denomina...
LIB8STATIC_ALWAYS_INLINE int8_t avg7(int8_t i, int8_t j)
Calculate an integer average of two signed 7-bit integers (int8_t) If the first argument is even...
uint8_t fract8
ANSI unsigned short _Fract.
q< uint8_t, 4, 4 > q44
A 4.4 integer (4 bits integer, 4 bits fraction)
central include file for FastLED, defines the CFastLED class/object
LIB8STATIC_ALWAYS_INLINE uint8_t qadd8(uint8_t i, uint8_t j)
add one byte to another, saturating at 0xFF
__attribute__((always_inline)) inline void swapbits8(bitswap_type in
Do an 8byte by 8bit rotation.
LIB8STATIC_ALWAYS_INLINE uint8_t scale8(uint8_t i, fract8 scale)
scale one byte by a second one, which is treated as the numerator of a fraction whose denominator is ...
int16_t inoise16_raw(uint32_t x, uint32_t y, uint32_t z)
16 bit raw versions of the noise functions.
FASTLED_USING_NAMESPACE const TProgmemRGBPalette16 CloudColors_p FL_PROGMEM
HSV Rainbow.
Representation of an HSV pixel (hue, saturation, value (aka brightness)).
LIB8STATIC_ALWAYS_INLINE uint8_t scale8_LEAVING_R1_DIRTY(uint8_t i, fract8 scale)
This version of scale8 does not clean up the R1 register on AVR If you are doing several 'scale8's in...
Template class for represneting fractional ints.