FastLED 3.9.15
Loading...
Searching...
No Matches
blur.cpp
Go to the documentation of this file.
1
2
3#include "fl/stdint.h"
4
5#define FASTLED_INTERNAL
6#include "FastLED.h"
7
8#include "crgb.h"
9#include "fl/blur.h"
10#include "fl/colorutils_misc.h"
11#include "fl/deprecated.h"
12#include "fl/unused.h"
13#include "fl/xymap.h"
14#include "lib8tion/scale8.h"
15#include "fl/int.h"
16
17namespace fl {
18
19// Legacy XY function. This is a weak symbol that can be overridden by the user.
20fl::u16 XY(fl::u8 x, fl::u8 y) __attribute__((weak));
21
22__attribute__((weak)) fl::u16 XY(fl::u8 x, fl::u8 y) {
25 FASTLED_ASSERT(false, "the user didn't provide an XY function");
26 return 0;
27}
28
29// fl::u16 XY(fl::u8 x, fl::u8 y) {
30// return 0;
31// }
32// make this a weak symbol
33namespace {
34fl::u16 xy_legacy_wrapper(fl::u16 x, fl::u16 y, fl::u16 width,
35 fl::u16 height) {
36 FASTLED_UNUSED(width);
37 FASTLED_UNUSED(height);
38 return XY(x, y);
39}
40} // namespace
41
42// blur1d: one-dimensional blur filter. Spreads light to 2 line neighbors.
43// blur2d: two-dimensional blur filter. Spreads light to 8 XY neighbors.
44//
45// 0 = no spread at all
46// 64 = moderate spreading
47// 172 = maximum smooth, even spreading
48//
49// 173..255 = wider spreading, but increasing flicker
50//
51// Total light is NOT entirely conserved, so many repeated
52// calls to 'blur' will also result in the light fading,
53// eventually all the way to black; this is by design so that
54// it can be used to (slowly) clear the LEDs to black.
55void blur1d(CRGB *leds, fl::u16 numLeds, fract8 blur_amount) {
56 fl::u8 keep = 255 - blur_amount;
57 fl::u8 seep = blur_amount >> 1;
58 CRGB carryover = CRGB::Black;
59 for (fl::u16 i = 0; i < numLeds; ++i) {
60 CRGB cur = leds[i];
61 CRGB part = cur;
62 part.nscale8(seep);
63 cur.nscale8(keep);
64 cur += carryover;
65 if (i)
66 leds[i - 1] += part;
67 leds[i] = cur;
68 carryover = part;
69 }
70}
71
72void blur2d(CRGB *leds, fl::u8 width, fl::u8 height, fract8 blur_amount,
73 const XYMap &xymap) {
74 blurRows(leds, width, height, blur_amount, xymap);
75 blurColumns(leds, width, height, blur_amount, xymap);
76}
77
78void blur2d(CRGB *leds, fl::u8 width, fl::u8 height, fract8 blur_amount) {
79 XYMap xy =
80 XYMap::constructWithUserFunction(width, height, xy_legacy_wrapper);
81 blur2d(leds, width, height, blur_amount, xy);
82}
83
84void blurRows(CRGB *leds, fl::u8 width, fl::u8 height, fract8 blur_amount,
85 const XYMap &xyMap) {
86
87 /* for( fl::u8 row = 0; row < height; row++) {
88 CRGB* rowbase = leds + (row * width);
89 blur1d( rowbase, width, blur_amount);
90 }
91 */
92 // blur rows same as columns, for irregular matrix
93 fl::u8 keep = 255 - blur_amount;
94 fl::u8 seep = blur_amount >> 1;
95 for (fl::u8 row = 0; row < height; row++) {
96 CRGB carryover = CRGB::Black;
97 for (fl::u8 i = 0; i < width; i++) {
98 CRGB cur = leds[xyMap.mapToIndex(i, row)];
99 CRGB part = cur;
100 part.nscale8(seep);
101 cur.nscale8(keep);
102 cur += carryover;
103 if (i)
104 leds[xyMap.mapToIndex(i - 1, row)] += part;
105 leds[xyMap.mapToIndex(i, row)] = cur;
106 carryover = part;
107 }
108 }
109}
110
111// blurColumns: perform a blur1d on each column of a rectangular matrix
112void blurColumns(CRGB *leds, fl::u8 width, fl::u8 height, fract8 blur_amount,
113 const XYMap &xyMap) {
114 // blur columns
115 fl::u8 keep = 255 - blur_amount;
116 fl::u8 seep = blur_amount >> 1;
117 for (fl::u8 col = 0; col < width; ++col) {
118 CRGB carryover = CRGB::Black;
119 for (fl::u8 i = 0; i < height; ++i) {
120 CRGB cur = leds[xyMap.mapToIndex(col, i)];
121 CRGB part = cur;
122 part.nscale8(seep);
123 cur.nscale8(keep);
124 cur += carryover;
125 if (i)
126 leds[xyMap.mapToIndex(col, i - 1)] += part;
127 leds[xyMap.mapToIndex(col, i)] = cur;
128 carryover = part;
129 }
130 }
131}
132
133} // namespace fl
CRGB leds[NUM_LEDS]
int y
Definition simple.h:93
int x
Definition simple.h:92
XYMap xymap(WIDTH, HEIGHT, SERPENTINE)
fl::XYMap xyMap
Definition ColorBoost.h:61
central include file for FastLED, defines the CFastLED class/object
unsigned int xy(unsigned int x, unsigned int y)
static XYMap constructWithUserFunction(u16 width, u16 height, XYFunction xyFunction, u16 offset=0)
Definition xymap.cpp:26
Defines the red, green, and blue (RGB) pixel struct.
void blurColumns(CRGB *leds, fl::u8 width, fl::u8 height, fract8 blur_amount, const XYMap &xyMap)
Perform a blur1d() on every column of a rectangular matrix.
Definition blur.cpp:112
void blurRows(CRGB *leds, fl::u8 width, fl::u8 height, fract8 blur_amount, const XYMap &xyMap)
Perform a blur1d() on every row of a rectangular matrix.
Definition blur.cpp:84
void blur2d(CRGB *leds, fl::u8 width, fl::u8 height, fract8 blur_amount, const XYMap &xymap)
Two-dimensional blur filter.
Definition blur.cpp:72
fl::u16 xy_legacy_wrapper(fl::u16 x, fl::u16 y, fl::u16 width, fl::u16 height)
Definition blur.cpp:34
unsigned char u8
Definition int.h:17
fl::u16 XY(fl::u8 x, fl::u8 y)
Definition blur.cpp:22
u8 fract8
Fixed-Point Fractional Types.
Definition int.h:49
void blur1d(CRGB *leds, fl::u16 numLeds, fract8 blur_amount)
Definition blur.cpp:55
IMPORTANT!
Definition crgb.h:20
Fast, efficient 8-bit scaling functions specifically designed for high-performance LED programming.
CRGB & nscale8(fl::u8 scaledown)
Scale down a RGB to N/256ths of its current brightness, using "plain math" dimming rules.
@ Black
<div style='background:#000000;width:4em;height:4em;'></div>
Definition crgb.h:567
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:86
#define FASTLED_UNUSED(x)
Definition unused.h:4