FastLED 3.9.15
Loading...
Searching...
No Matches
FxWater.h
Go to the documentation of this file.
1
10
11// Author: sutaburosu
12
13// based on https://web.archive.org/web/20160418004149/http://freespace.virgin.net/hugo.elias/graphics/x_water.htm
14
15#include <FastLED.h>
16#include "Arduino.h"
17#include "fl/xymap.h"
18
19using namespace fl;
20
21#define WIDTH 32
22#define HEIGHT 32
23#define NUM_LEDS ((WIDTH) * (HEIGHT))
25
26// the water needs 2 arrays each slightly bigger than the screen
27#define WATERWIDTH (WIDTH + 2)
28#define WATERHEIGHT (HEIGHT + 2)
30
31void wu_water(uint8_t * const buf, uint16_t x, uint16_t y, uint8_t bright);
32void process_water(uint8_t * src, uint8_t * dst) ;
33
34void setup() {
35 Serial.begin(115200);
36 FastLED.addLeds<NEOPIXEL, 2>(leds, NUM_LEDS).setScreenMap(WIDTH, HEIGHT);
37}
38
39// from: https://github.com/FastLED/FastLED/pull/202
40CRGB MyColorFromPaletteExtended(const CRGBPalette16& pal, uint16_t index, uint8_t brightness, TBlendType blendType) {
41 // Extract the four most significant bits of the index as a palette index.
42 uint8_t index_4bit = (index >> 12);
43 // Calculate the 8-bit offset from the palette index.
44 uint8_t offset = (uint8_t)(index >> 4);
45 // Get the palette entry from the 4-bit index
46 const CRGB* entry = &(pal[0]) + index_4bit;
47 uint8_t red1 = entry->red;
48 uint8_t green1 = entry->green;
49 uint8_t blue1 = entry->blue;
50
51 uint8_t blend = offset && (blendType != NOBLEND);
52 if (blend) {
53 if (index_4bit == 15) {
54 entry = &(pal[0]);
55 } else {
56 entry++;
57 }
58
59 // Calculate the scaling factor and scaled values for the lower palette value.
60 uint8_t f1 = 255 - offset;
61 red1 = scale8_LEAVING_R1_DIRTY(red1, f1);
62 green1 = scale8_LEAVING_R1_DIRTY(green1, f1);
63 blue1 = scale8_LEAVING_R1_DIRTY(blue1, f1);
64
65 // Calculate the scaled values for the neighbouring palette value.
66 uint8_t red2 = entry->red;
67 uint8_t green2 = entry->green;
68 uint8_t blue2 = entry->blue;
69 red2 = scale8_LEAVING_R1_DIRTY(red2, offset);
70 green2 = scale8_LEAVING_R1_DIRTY(green2, offset);
71 blue2 = scale8_LEAVING_R1_DIRTY(blue2, offset);
72 cleanup_R1();
73
74 // These sums can't overflow, so no qadd8 needed.
75 red1 += red2;
76 green1 += green2;
77 blue1 += blue2;
78 }
79 if (brightness != 255) {
80 // nscale8x3_video(red1, green1, blue1, brightness);
81 nscale8x3(red1, green1, blue1, brightness);
82 }
83 return CRGB(red1, green1, blue1);
84}
85
86// Rectangular grid
88
89// map X & Y coordinates onto a horizontal serpentine matrix layout
90uint16_t XY(uint8_t x, uint8_t y) {
91 return xyMap.mapToIndex(x, y);
92}
93
94void loop() {
95 // swap the src/dest buffers on each frame
96 static uint8_t buffer = 0;
97 uint8_t * const bufA = &water[buffer][0];
98 buffer = (buffer + 1) % 2;
99 uint8_t * const bufB = &water[buffer][0];
100
101 // add a moving stimulus
102 wu_water(bufA, beatsin16(13, 256, HEIGHT * 256), beatsin16(7, 256, WIDTH * 256), beatsin8(160, 64, 255));
103
104 // animate the water
105 process_water(bufA, bufB);
106
107
108 // display the water effect on the LEDs
109 uint8_t * input = bufB + WATERWIDTH - 1;
110 static uint16_t pal_offset = 0;
111 pal_offset += 256;
112 for (uint8_t y = 0; y < HEIGHT; y++) {
113 input += 2;
114 for (uint8_t x = 0; x < WIDTH; x++) {
115 leds[XY(x, y)] = MyColorFromPaletteExtended(RainbowColors_p, pal_offset + (*input++ << 8), 255, LINEARBLEND);
116 }
117 }
118 FastLED.show();
119}
120
121void process_water(uint8_t * src, uint8_t * dst) {
122 src += WATERWIDTH - 1;
123 dst += WATERWIDTH - 1;
124 for (uint8_t y = 1; y < WATERHEIGHT - 1; y++) {
125 src += 2; dst += 2;
126 for (uint8_t x = 1; x < WATERWIDTH - 1; x++) {
127 uint16_t t = src[-1] + src[1] + src[-WATERWIDTH] + src[WATERWIDTH];
128 t >>= 1;
129 if (dst[0] < t)
130 dst[0] = t - dst[0];
131 else
132 dst[0] = 0;
133
134 dst[0] -= dst[0] >> 6;
135 src++; dst++;
136 }
137 }
138}
139
140// draw a blob of 4 pixels with their relative brightnesses conveying sub-pixel positioning
141void wu_water(uint8_t * const buf, uint16_t x, uint16_t y, uint8_t bright) {
142 // extract the fractional parts and derive their inverses
143 uint8_t xx = x & 0xff, yy = y & 0xff, ix = 255 - xx, iy = 255 - yy;
144 // calculate the intensities for each affected pixel
145 #define WU_WEIGHT(a, b) ((uint8_t)(((a) * (b) + (a) + (b)) >> 8))
146 uint8_t wu[4] = {WU_WEIGHT(ix, iy), WU_WEIGHT(xx, iy),
147 WU_WEIGHT(ix, yy), WU_WEIGHT(xx, yy)
148 };
149 #undef WU_WEIGHT
150 // multiply the intensities by the colour, and saturating-add them to the pixels
151 for (uint8_t i = 0; i < 4; i++) {
152 uint8_t local_x = (x >> 8) + (i & 1);
153 uint8_t local_y = (y >> 8) + ((i >> 1) & 1);
154 uint16_t xy = WATERWIDTH * local_y + local_x;
155 if (xy >= WATERWIDTH * WATERHEIGHT) continue;
156 uint16_t this_bright = bright * wu[i];
157 buf[xy] = qadd8(buf[xy], this_bright >> 8);
158 }
159}
CRGB leds[NUM_LEDS]
#define NUM_LEDS
int y
Definition simple.h:93
int x
Definition simple.h:92
fl::XYMap xyMap
Definition ColorBoost.h:61
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
Definition FastLED.cpp:74
central include file for FastLED, defines the CFastLED class/object
void process_water(uint8_t *src, uint8_t *dst)
Definition FxWater.h:121
#define WATERHEIGHT
Definition FxWater.h:28
void setup()
Definition FxWater.h:34
void wu_water(uint8_t *const buf, uint16_t x, uint16_t y, uint8_t bright)
Definition FxWater.h:141
CRGB MyColorFromPaletteExtended(const CRGBPalette16 &pal, uint16_t index, uint8_t brightness, TBlendType blendType)
Definition FxWater.h:40
#define WU_WEIGHT(a, b)
uint8_t water[2][WATERWIDTH *WATERHEIGHT]
Definition FxWater.h:29
#define WATERWIDTH
Definition FxWater.h:27
void loop()
Definition FxWater.h:94
unsigned int xy(unsigned int x, unsigned int y)
#define WIDTH
Definition advanced.h:36
UISlider brightness("Brightness", 128, 0, 255, 1)
#define HEIGHT
Definition advanced.h:37
LED controller for WS2812 LEDs with GRB color order.
Definition FastLED.h:158
UISlider offset("Offset", 0.0f, 0.0f, 1.0f, 0.01f)
static uint32_t t
Definition Luminova.h:54
LIB8STATIC uint16_t beatsin16(accum88 beats_per_minute, uint16_t lowest=0, uint16_t highest=65535, uint32_t timebase=0, uint16_t phase_offset=0)
Generates a 16-bit sine wave at a given BPM that oscillates within a given range.
Definition lib8tion.h:819
LIB8STATIC uint8_t beatsin8(accum88 beats_per_minute, uint8_t lowest=0, uint8_t highest=255, uint32_t timebase=0, uint8_t phase_offset=0)
Generates an 8-bit sine wave at a given BPM that oscillates within a given range.
Definition lib8tion.h:837
LIB8STATIC_ALWAYS_INLINE uint8_t qadd8(uint8_t i, uint8_t j)
Add one byte to another, saturating at 0xFF.
Definition math8.h:40
const TProgmemRGBPalette16 RainbowColors_p
HSV Rainbow.
LIB8STATIC_ALWAYS_INLINE void cleanup_R1()
Clean up the r1 register after a series of *LEAVING_R1_DIRTY calls.
Definition scale8.h:343
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.
Definition scale8.h:180
LIB8STATIC void nscale8x3(uint8_t &r, uint8_t &g, uint8_t &b, fract8 scale)
Scale three one-byte values by a fourth one, which is treated as the numerator of a fraction whose de...
Definition scale8.h:367
fl::u16 XY(fl::u8 x, fl::u8 y)
Definition blur.cpp:22
CRGB blend(const CRGB &p1, const CRGB &p2, fract8 amountOfP2)
IMPORTANT!
Definition crgb.h:20
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:86