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/compiler_control.h"
12#include "fl/deprecated.h"
13#include "fl/unused.h"
14#include "fl/xymap.h"
15#include "lib8tion/scale8.h"
16#include "fl/int.h"
17
18namespace fl {
19
20// Legacy XY function. This is a weak symbol that can be overridden by the user.
21fl::u16 XY(fl::u8 x, fl::u8 y) FL_LINK_WEAK;
22
26 FASTLED_ASSERT(false, "the user didn't provide an XY function");
27 return 0;
28}
29
30// fl::u16 XY(fl::u8 x, fl::u8 y) {
31// return 0;
32// }
33// make this a weak symbol
34namespace {
35fl::u16 xy_legacy_wrapper(fl::u16 x, fl::u16 y, fl::u16 width,
36 fl::u16 height) {
37 FASTLED_UNUSED(width);
38 FASTLED_UNUSED(height);
39 return XY(x, y);
40}
41} // namespace
42
43// blur1d: one-dimensional blur filter. Spreads light to 2 line neighbors.
44// blur2d: two-dimensional blur filter. Spreads light to 8 XY neighbors.
45//
46// 0 = no spread at all
47// 64 = moderate spreading
48// 172 = maximum smooth, even spreading
49//
50// 173..255 = wider spreading, but increasing flicker
51//
52// Total light is NOT entirely conserved, so many repeated
53// calls to 'blur' will also result in the light fading,
54// eventually all the way to black; this is by design so that
55// it can be used to (slowly) clear the LEDs to black.
56void blur1d(CRGB *leds, fl::u16 numLeds, fract8 blur_amount) {
57 fl::u8 keep = 255 - blur_amount;
58 fl::u8 seep = blur_amount >> 1;
59 CRGB carryover = CRGB::Black;
60 for (fl::u16 i = 0; i < numLeds; ++i) {
61 CRGB cur = leds[i];
62 CRGB part = cur;
63 part.nscale8(seep);
64 cur.nscale8(keep);
65 cur += carryover;
66 if (i)
67 leds[i - 1] += part;
68 leds[i] = cur;
69 carryover = part;
70 }
71}
72
73void blur2d(CRGB *leds, fl::u8 width, fl::u8 height, fract8 blur_amount,
74 const XYMap &xymap) {
75 blurRows(leds, width, height, blur_amount, xymap);
76 blurColumns(leds, width, height, blur_amount, xymap);
77}
78
79void blur2d(CRGB *leds, fl::u8 width, fl::u8 height, fract8 blur_amount) {
80 XYMap xy =
81 XYMap::constructWithUserFunction(width, height, xy_legacy_wrapper);
82 blur2d(leds, width, height, blur_amount, xy);
83}
84
85void blurRows(CRGB *leds, fl::u8 width, fl::u8 height, fract8 blur_amount,
86 const XYMap &xyMap) {
87
88 /* for( fl::u8 row = 0; row < height; row++) {
89 CRGB* rowbase = leds + (row * width);
90 blur1d( rowbase, width, blur_amount);
91 }
92 */
93 // blur rows same as columns, for irregular matrix
94 fl::u8 keep = 255 - blur_amount;
95 fl::u8 seep = blur_amount >> 1;
96 for (fl::u8 row = 0; row < height; row++) {
97 CRGB carryover = CRGB::Black;
98 for (fl::u8 i = 0; i < width; i++) {
99 CRGB cur = leds[xyMap.mapToIndex(i, row)];
100 CRGB part = cur;
101 part.nscale8(seep);
102 cur.nscale8(keep);
103 cur += carryover;
104 if (i)
105 leds[xyMap.mapToIndex(i - 1, row)] += part;
106 leds[xyMap.mapToIndex(i, row)] = cur;
107 carryover = part;
108 }
109 }
110}
111
112// blurColumns: perform a blur1d on each column of a rectangular matrix
113void blurColumns(CRGB *leds, fl::u8 width, fl::u8 height, fract8 blur_amount,
114 const XYMap &xyMap) {
115 // blur columns
116 fl::u8 keep = 255 - blur_amount;
117 fl::u8 seep = blur_amount >> 1;
118 for (fl::u8 col = 0; col < width; ++col) {
119 CRGB carryover = CRGB::Black;
120 for (fl::u8 i = 0; i < height; ++i) {
121 CRGB cur = leds[xyMap.mapToIndex(col, i)];
122 CRGB part = cur;
123 part.nscale8(seep);
124 cur.nscale8(keep);
125 cur += carryover;
126 if (i)
127 leds[xyMap.mapToIndex(col, i - 1)] += part;
128 leds[xyMap.mapToIndex(col, i)] = cur;
129 carryover = part;
130 }
131 }
132}
133
134} // 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:57
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
#define FL_LINK_WEAK
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:113
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:85
void blur2d(CRGB *leds, fl::u8 width, fl::u8 height, fract8 blur_amount, const XYMap &xymap)
Two-dimensional blur filter.
Definition blur.cpp:73
fl::u16 xy_legacy_wrapper(fl::u16 x, fl::u16 y, fl::u16 width, fl::u16 height)
Definition blur.cpp:35
unsigned char u8
Definition int.h:17
u8 fract8
Fixed-Point Fractional Types.
Definition int.h:49
void blur1d(CRGB *leds, fl::u16 numLeds, fract8 blur_amount)
Definition blur.cpp:56
fl::u16 XY(fl::u8 x, fl::u8 y) FL_LINK_WEAK
Definition blur.cpp:23
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