FastLED 3.10.10
Loading...
Searching...
No Matches
Polygon.ino
Go to the documentation of this file.
1
4//
5// Draws filled, antialiased polygons straight into the LED array through a
6// fl::CanvasRGB view (no copy). Wire a WIDTH x HEIGHT WS2812 matrix in
7// row-major order to PIN_DATA. Four 5 s scenes, each showing one part of
8// the drawPolygon API:
9//
10// 1. Pinwheel - s16x16 (Q16.16) vertices, ABuffer fill, additive blend:
11// six overlapping blades sum toward white in the middle
12// 2. Star - float vertices, Exact fill (the default) and the even-odd
13// rule: a self-intersecting pentagram leaves its center
14// pentagon empty
15// 3. Layers - DRAW_MODE_OVERWRITE: three rotating squares drawn back
16// to front, each hiding the ones below
17// 4. Knot - two 14-vertex self-intersecting Lissajous curves, more
18// vertices than the small-stack default MAXN of 8
19//
20// The API in brief:
21//
22// canvas.drawPolygon(color, pts); // Exact fill, additive blend
23// canvas.drawPolygon(color, pts, fl::DrawMode::DRAW_MODE_OVERWRITE);
24// canvas.drawPolygon<Coord, MAXN, CHUNK, fl::gfx::PolyFill::ABuffer>(
25// color, pts, mode);
26//
27// pts is an array (or fl::span) of fl::PolyPoint<Coord>; Coord may be
28// float, int or a fixed-point type such as fl::s16x16. Both fill backends
29// use the even-odd rule and the same edge conventions:
30//
31// Exact - exact per-pixel area coverage (256 levels). The default.
32// ABuffer - 8x4 subpixel sampling (32 levels): faster on dense fills and
33// a shallower stack. It ignores CHUNK.
34//
35// MAXN is a compile-time vertex cap that sizes the kernel's stack frame;
36// vertices past MAXN are ignored. The default (FL_GFX_POLYGON_DEFAULT_MAXN)
37// is 8 on small-stack targets (AVR, ESP8266, ...) and 32 elsewhere, so a
38// polygon with more vertices must ask for a bigger MAXN, and a known small
39// polygon can ask for a smaller one to save stack. CHUNK (Exact only) is a
40// power-of-two column-chunk width; the default is fine.
41
42// @filter
43// require:
44// - memory: low
45// exclude:
46// - board: uno
47// - board: nano
48// - board: nano_every
49// - board: leonardo
50// - board: attiny1616
51// @end-filter
52// The four scenes instantiate four polygon kernels (Exact for float and
53// s16x16, ABuffer at two MAXN tiers) plus soft-float math: ~50 KB of AVR
54// flash, more than the 16-48 KB parts above have. Mega 2560 fits (50.9 KB
55// flash, 940 B static RAM plus a ~1.2 KB worst-case polygon stack frame).
56
57#include <Arduino.h>
58#include <FastLED.h>
59#include <fl/gfx/gfx.h>
60#include <fl/math/math.h> // fl::sinf / fl::cosf for the float scene
61
62#ifndef WIDTH
63#define WIDTH 16
64#endif
65#ifndef HEIGHT
66#define HEIGHT 16
67#endif
68#define NUM_LEDS (WIDTH * HEIGHT)
69
70#ifndef PIN_DATA
71#define PIN_DATA FL_PIN_CLOCKLESS_1
72#endif
73
75
76// The canvas is a view over the LED array itself - no copy anywhere.
78
79typedef fl::s16x16 Fix; // Q16.16 fixed point: raw value = real * 65536
81
82// ---- Q16.16 helpers ----------------------------------------------------------
83
84static const fl::i32 kOne = 65536L;
85static const fl::i32 kTwoPi = 411775L; // 2*pi radians
86static const fl::i32 kPi = 205887L; // pi
87static const fl::i32 kCX = WIDTH * kOne / 2; // canvas center
88static const fl::i32 kCY = HEIGHT * kOne / 2;
89static const fl::i32 kR = (WIDTH < HEIGHT ? WIDTH : HEIGHT) * kOne / 2;
90
91static fl::i32 mulQ16(fl::i32 a, fl::i32 b) {
92 return (fl::i32)(((fl::i64)a * b) >> 16);
93}
94
95// Angle in [0, 2*pi) for time t (ms) and a period in ms.
96static fl::i32 angleAt(unsigned long t, unsigned long periodMs) {
97 return (fl::i32)(((fl::i64)(t % periodMs) * kTwoPi) / (fl::i64)periodMs);
98}
99
100// Wrap an angle into [0, 2*pi): the s16x16 trig wants a non-negative input.
101static Fix wrap(fl::i32 a) {
102 a %= kTwoPi;
103 if (a < 0) a += kTwoPi;
104 return Fix::from_raw(a);
105}
106
107// Point at (cx, cy) + r * (cos a, sin a); every value is Q16.16.
108static FixPt polar(fl::i32 cx, fl::i32 cy, fl::i32 r, fl::i32 a) {
109 Fix s, c;
110 Fix::sincos(wrap(a), s, c);
111 FixPt p = {Fix::from_raw(cx + mulQ16(c.raw(), r)),
112 Fix::from_raw(cy + mulQ16(s.raw(), r))};
113 return p;
114}
115
116// ---- 1. Pinwheel: s16x16, ABuffer, additive ----------------------------------
117// DRAW_MODE_BLEND (the default) adds the antialiased color to what is
118// already there, saturating at 255, so overlapping blades brighten toward
119// white. A blade is a triangle, so MAXN = 3 is the smallest stack frame.
120static void scenePinwheel(unsigned long t) {
121 const fl::i32 spin = angleAt(t, 7000);
122 const fl::i32 len = mulQ16(kR, 62259L); // blade tip at 0.95 R
123 const fl::i32 back = mulQ16(len, 36045L); // trailing corner at 0.55 len
124 const fl::u8 hue = (fl::u8)(t / 40);
125 for (int k = 0; k < 6; ++k) {
126 const fl::i32 a = spin + k * (kTwoPi / 6);
127 const FixPt blade[3] = {
129 polar(kCX, kCY, len, a),
130 polar(kCX, kCY, back, a + 58982L), // swept 0.9 rad back
131 };
134 CHSV((fl::u8)(hue + 42 * k), 200, 240), blade,
136 }
137}
138
139// ---- 2. Star: float, Exact (default), even-odd -------------------------------
140// Joining every second corner of a pentagon gives a pentagram whose edges
141// cross. Under the even-odd rule a point is inside when a ray from it
142// crosses an odd number of edges; the center pentagon crosses an even
143// number, so it stays empty. A small counter-rotating star sits in it.
144static void drawStar(float cx, float cy, float r, float spin, const CRGB& color) {
145 fl::PolyPoint<float> star[5];
146 for (int i = 0; i < 5; ++i) {
147 const float a = spin + (float)i * (2.0f * 6.2831853f / 5.0f);
148 star[i].x = cx + r * fl::cosf(a);
149 star[i].y = cy + r * fl::sinf(a);
150 }
151 canvas.drawPolygon(color, star); // Exact fill, default MAXN, additive
152}
153
154static void sceneStar(unsigned long t) {
155 const float cx = WIDTH * 0.5f, cy = HEIGHT * 0.5f;
156 const float r = (WIDTH < HEIGHT ? WIDTH : HEIGHT) * 0.5f - 0.25f;
157 const float spin = (float)(t % 9000) * (6.2831853f / 9000.0f);
158 drawStar(cx, cy, r, spin, CRGB(255, 140, 0));
159 drawStar(cx, cy, r * 0.28f, -2.0f * spin, CRGB(0, 120, 255));
160}
161
162// ---- 3. Layers: DRAW_MODE_OVERWRITE -------------------------------------------
163// OVERWRITE replaces each touched pixel with color x coverage instead of
164// adding to it, so shapes drawn later hide the ones below (draw back to
165// front). Edge pixels keep only the top shape's partial coverage, which is
166// the thin dark seam around each square; BLEND would mix the colors instead.
167static void sceneLayers(unsigned long t) {
168 static const CRGB kColors[3] = {CRGB(255, 0, 80), CRGB(255, 200, 0), CRGB(0, 160, 255)};
169 for (int layer = 0; layer < 3; ++layer) {
170 const fl::i32 r = kR - layer * (kR / 4); // corner radius R, 3/4 R, 1/2 R
171 fl::i32 a = angleAt(t, 9000UL - 2000UL * layer);
172 if (layer & 1) a = -a; // the middle square turns the other way
173 FixPt square[4];
174 for (int j = 0; j < 4; ++j) square[j] = polar(kCX, kCY, r, a + j * (kTwoPi / 4));
175 canvas.drawPolygon(kColors[layer], square, fl::DrawMode::DRAW_MODE_OVERWRITE);
176 }
177}
178
179// ---- 4. Knot: more vertices than the default MAXN ----------------------------
180// A closed 2:3 Lissajous curve sampled at 14 points crosses itself several
181// times; even-odd fills alternate lobes. 14 vertices exceed the 8-vertex
182// default on small-stack targets, so MAXN is given explicitly (sized exactly, to
183// keep the stack frame small). The ABuffer backend has the shallower stack.
184static const int kKnotN = 14;
185
186static void sceneKnot(unsigned long t) {
187 const fl::i32 ph1 = angleAt(t, 11000);
188 const fl::i32 ph2 = angleAt(t, 8700);
189 const fl::i32 rx = mulQ16(kCX, 60293L); // 0.92 of the half-width
190 const fl::i32 ry = mulQ16(kCY, 60293L);
191 for (int pass = 0; pass < 2; ++pass) { // second knot: x phase + pi
192 FixPt knot[kKnotN];
193 for (int i = 0; i < kKnotN; ++i) {
194 const fl::i32 th = (fl::i32)((fl::i64)i * kTwoPi / kKnotN);
195 const fl::i32 sx = Fix::sin(wrap(2 * th + ph1 + pass * kPi)).raw();
196 const fl::i32 sy = Fix::sin(wrap(3 * th + ph2)).raw();
197 knot[i].x = Fix::from_raw(kCX + mulQ16(rx, sx));
198 knot[i].y = Fix::from_raw(kCY + mulQ16(ry, sy));
199 }
202 CHSV((fl::u8)(160 + 90 * pass), 190, 255), knot,
204 }
205}
206
207// ---- frame driver --------------------------------------------------------------
208
209typedef void (*SceneFn)(unsigned long t);
211static const int kNumScenes = (int)(sizeof(kScenes) / sizeof(kScenes[0]));
212static const unsigned long kSceneMs = 5000;
213
214void setup() {
215 FastLED.addLeds<NEOPIXEL, PIN_DATA>(leds, NUM_LEDS);
216 FastLED.setBrightness(64);
217}
218
219void loop() {
220 const unsigned long t = millis();
222 kScenes[(t / kSceneMs) % kNumScenes](t);
223 FastLED.show();
224}
CRGB leds[1]
#define NUM_LEDS
#define PIN_DATA
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
static void sceneKnot(unsigned long t)
Definition Polygon.ino:186
static const int kNumScenes
Definition Polygon.ino:211
static const fl::i32 kOne
Definition Polygon.ino:84
static fl::i32 angleAt(unsigned long t, unsigned long periodMs)
Definition Polygon.ino:96
static const fl::i32 kCY
Definition Polygon.ino:88
void setup()
Definition Polygon.ino:214
fl::PolyPoint< Fix > FixPt
Definition Polygon.ino:80
static const fl::i32 kTwoPi
Definition Polygon.ino:85
static FixPt polar(fl::i32 cx, fl::i32 cy, fl::i32 r, fl::i32 a)
Definition Polygon.ino:108
static const int kKnotN
Definition Polygon.ino:184
static fl::i32 mulQ16(fl::i32 a, fl::i32 b)
Definition Polygon.ino:91
static const fl::i32 kCX
Definition Polygon.ino:87
fl::CanvasRGB canvas(fl::span< CRGB >(leds, NUM_LEDS), WIDTH, HEIGHT)
fl::s16x16 Fix
Definition Polygon.ino:79
static const fl::i32 kPi
Definition Polygon.ino:86
void(* SceneFn)(unsigned long t)
Definition Polygon.ino:209
static void scenePinwheel(unsigned long t)
Definition Polygon.ino:120
static const fl::i32 kR
Definition Polygon.ino:89
static void sceneLayers(unsigned long t)
Definition Polygon.ino:167
static const SceneFn kScenes[]
Definition Polygon.ino:210
static void drawStar(float cx, float cy, float r, float spin, const CRGB &color)
Definition Polygon.ino:144
static void sceneStar(unsigned long t)
Definition Polygon.ino:154
static const unsigned long kSceneMs
Definition Polygon.ino:212
static Fix wrap(fl::i32 a)
Definition Polygon.ino:101
void loop()
Definition Polygon.ino:219
#define WIDTH
uint8_t hue
Definition advanced.h:94
#define HEIGHT
#define FL_GFX_POLYGON_DEFAULT_CHUNK
Definition canvas.h:70
Convenience alias for CRGB canvas — use fl::CanvasRGB for no-template syntax.
Definition gfx.h:132
constexpr i32 raw() const FL_NO_EXCEPT
Definition s16x16.h:62
FASTLED_FORCE_INLINE s16x16 sin() const FL_NO_EXCEPT
Definition s16x16.h:315
static constexpr FASTLED_FORCE_INLINE s16x16 from_raw(i32 raw) FL_NO_EXCEPT
Definition s16x16.h:56
static FASTLED_FORCE_INLINE void sincos(s16x16 angle, s16x16 &out_sin, s16x16 &out_cos) FL_NO_EXCEPT
Definition s16x16.h:355
void fill_solid(CRGB *targetArray, int numToFill, const CRGB &color) FL_NO_EXCEPT
Fill a range of LEDs with a solid color.
Definition fill.cpp.hpp:10
static uint32_t t
Definition Luminova.h:55
2D antialiased graphics for LED matrices
fl::hsv8 CHSV
Definition chsv.h:11
fl::CRGB CRGB
Definition crgb.h:25
unsigned char u8
Definition stdint.h:132
float sinf(float value) FL_NO_EXCEPT
Definition math.h:360
fl::i64 i64
Definition s16x16x4.h:222
gfx::PolyPoint< Coord > PolyPoint
Polygon vertex (x, y coordinate pair) for drawPolygon.
Definition gfx.h:57
@ DRAW_MODE_OVERWRITE
Definition draw_mode.h:5
@ DRAW_MODE_BLEND
Definition draw_mode.h:5
float cosf(float value) FL_NO_EXCEPT
Definition math.h:366
@ Black
<div style='background:#000000;width:4em;height:4em;'></div>
Definition crgb.h:517