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