FastLED 3.9.15
Loading...
Searching...
No Matches
fill.cpp
Go to the documentation of this file.
1
2
3#include <stdint.h>
4
5#include "fill.h"
6
7namespace fl {
8
9void fill_solid(struct CRGB *targetArray, int numToFill,
10 const struct CRGB &color) {
11 for (int i = 0; i < numToFill; ++i) {
12 targetArray[i] = color;
13 }
14}
15
16void fill_solid(struct CHSV *targetArray, int numToFill,
17 const struct CHSV &color) {
18 for (int i = 0; i < numToFill; ++i) {
19 targetArray[i] = color;
20 }
21}
22
23// void fill_solid( struct CRGB* targetArray, int numToFill,
24// const struct CHSV& hsvColor)
25// {
26// fill_solid<CRGB>( targetArray, numToFill, (CRGB) hsvColor);
27// }
28
29void fill_rainbow(struct CRGB *targetArray, int numToFill, uint8_t initialhue,
30 uint8_t deltahue) {
31 CHSV hsv;
32 hsv.hue = initialhue;
33 hsv.val = 255;
34 hsv.sat = 240;
35 for (int i = 0; i < numToFill; ++i) {
36 targetArray[i] = hsv;
37 hsv.hue += deltahue;
38 }
39}
40
41void fill_rainbow(struct CHSV *targetArray, int numToFill, uint8_t initialhue,
42 uint8_t deltahue) {
43 CHSV hsv;
44 hsv.hue = initialhue;
45 hsv.val = 255;
46 hsv.sat = 240;
47 for (int i = 0; i < numToFill; ++i) {
48 targetArray[i] = hsv;
49 hsv.hue += deltahue;
50 }
51}
52
53void fill_rainbow_circular(struct CRGB *targetArray, int numToFill,
54 uint8_t initialhue, bool reversed) {
55 if (numToFill == 0)
56 return; // avoiding div/0
57
58 CHSV hsv;
59 hsv.hue = initialhue;
60 hsv.val = 255;
61 hsv.sat = 240;
62
63 const uint16_t hueChange =
64 65535 / (uint16_t)numToFill; // hue change for each LED, * 256 for
65 // precision (256 * 256 - 1)
66 uint16_t hueOffset = 0; // offset for hue value, with precision (*256)
67
68 for (int i = 0; i < numToFill; ++i) {
69 targetArray[i] = hsv;
70 if (reversed)
71 hueOffset -= hueChange;
72 else
73 hueOffset += hueChange;
74 hsv.hue = initialhue +
75 (uint8_t)(hueOffset >>
76 8); // assign new hue with precise offset (as 8-bit)
77 }
78}
79
80void fill_rainbow_circular(struct CHSV *targetArray, int numToFill,
81 uint8_t initialhue, bool reversed) {
82 if (numToFill == 0)
83 return; // avoiding div/0
84
85 CHSV hsv;
86 hsv.hue = initialhue;
87 hsv.val = 255;
88 hsv.sat = 240;
89
90 const uint16_t hueChange =
91 65535 / (uint16_t)numToFill; // hue change for each LED, * 256 for
92 // precision (256 * 256 - 1)
93 uint16_t hueOffset = 0; // offset for hue value, with precision (*256)
94
95 for (int i = 0; i < numToFill; ++i) {
96 targetArray[i] = hsv;
97 if (reversed)
98 hueOffset -= hueChange;
99 else
100 hueOffset += hueChange;
101 hsv.hue = initialhue +
102 (uint8_t)(hueOffset >>
103 8); // assign new hue with precise offset (as 8-bit)
104 }
105}
106
107void fill_gradient_RGB(CRGB *leds, uint16_t startpos, CRGB startcolor,
108 uint16_t endpos, CRGB endcolor) {
109 // if the points are in the wrong order, straighten them
110 if (endpos < startpos) {
111 uint16_t t = endpos;
112 CRGB tc = endcolor;
113 endcolor = startcolor;
114 endpos = startpos;
115 startpos = t;
116 startcolor = tc;
117 }
118
119 saccum87 rdistance87;
120 saccum87 gdistance87;
121 saccum87 bdistance87;
122
123 rdistance87 = (endcolor.r - startcolor.r) << 7;
124 gdistance87 = (endcolor.g - startcolor.g) << 7;
125 bdistance87 = (endcolor.b - startcolor.b) << 7;
126
127 uint16_t pixeldistance = endpos - startpos;
128 int16_t divisor = pixeldistance ? pixeldistance : 1;
129
130 saccum87 rdelta87 = rdistance87 / divisor;
131 saccum87 gdelta87 = gdistance87 / divisor;
132 saccum87 bdelta87 = bdistance87 / divisor;
133
134 rdelta87 *= 2;
135 gdelta87 *= 2;
136 bdelta87 *= 2;
137
138 accum88 r88 = startcolor.r << 8;
139 accum88 g88 = startcolor.g << 8;
140 accum88 b88 = startcolor.b << 8;
141 for (uint16_t i = startpos; i <= endpos; ++i) {
142 leds[i] = CRGB(r88 >> 8, g88 >> 8, b88 >> 8);
143 r88 += rdelta87;
144 g88 += gdelta87;
145 b88 += bdelta87;
146 }
147}
148
149void fill_gradient_RGB(CRGB *leds, uint16_t numLeds, const CRGB &c1,
150 const CRGB &c2) {
151 uint16_t last = numLeds - 1;
152 fill_gradient_RGB(leds, 0, c1, last, c2);
153}
154
155void fill_gradient_RGB(CRGB *leds, uint16_t numLeds, const CRGB &c1,
156 const CRGB &c2, const CRGB &c3) {
157 uint16_t half = (numLeds / 2);
158 uint16_t last = numLeds - 1;
159 fill_gradient_RGB(leds, 0, c1, half, c2);
160 fill_gradient_RGB(leds, half, c2, last, c3);
161}
162
163void fill_gradient_RGB(CRGB *leds, uint16_t numLeds, const CRGB &c1,
164 const CRGB &c2, const CRGB &c3, const CRGB &c4) {
165 uint16_t onethird = (numLeds / 3);
166 uint16_t twothirds = ((numLeds * 2) / 3);
167 uint16_t last = numLeds - 1;
168 fill_gradient_RGB(leds, 0, c1, onethird, c2);
169 fill_gradient_RGB(leds, onethird, c2, twothirds, c3);
170 fill_gradient_RGB(leds, twothirds, c3, last, c4);
171}
172
173} // namespace fl
CRGB leds[NUM_LEDS]
Definition Apa102.ino:11
#define saccum87
ANSI: signed short _Accum.
Definition fill.h:9
void fill_rainbow_circular(struct CRGB *targetArray, int numToFill, uint8_t initialhue, bool reversed)
Fill a range of LEDs with a rainbow of colors, so that the hues are continuous between the end of the...
Definition fill.cpp:53
void fill_rainbow(struct CRGB *targetArray, int numToFill, uint8_t initialhue, uint8_t deltahue)
Fill a range of LEDs with a rainbow of colors.
Definition fill.cpp:29
void fill_gradient_RGB(CRGB *leds, uint16_t startpos, CRGB startcolor, uint16_t endpos, CRGB endcolor)
Fill a range of LEDs with a smooth RGB gradient between two RGB colors.
Definition fill.cpp:107
void fill_solid(struct CRGB *targetArray, int numToFill, const struct CRGB &color)
Fill a range of LEDs with a solid color.
Definition fill.cpp:9
uint16_t accum88
ANSI: unsigned short _Accum. 8 bits int, 8 bits fraction.
Definition types.h:58
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
Representation of an HSV pixel (hue, saturation, value (aka brightness)).
Definition chsv.h:16
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:55