FastLED 3.10.8
Loading...
Searching...
No Matches
xymap.cpp.hpp
Go to the documentation of this file.
1
2#include "fl/stl/stdint.h"
3#include "fl/stl/cstring.h"
4#include "fl/math/math.h"
6#include "fl/math/screenmap.h"
7#include "fl/math/xymap.h"
8#include "fl/math/xmap.h"
9#include "fl/stl/noexcept.h"
10
11namespace fl {
12
14 const u16 length = width * height;
15 ScreenMap out(length);
16 for (u16 w = 0; w < width; w++) {
17 for (u16 h = 0; h < height; h++) {
18 u16 index = mapToIndex(w, h);
19 vec2f p = {static_cast<float>(w), static_cast<float>(h)};
20 out.set(index, p);
21 }
22 }
23 // Store a shared_ptr to this XYMap so it can be used for encoding
25 return out;
26}
27
35
37 u16 offset) FL_NO_EXCEPT {
39 out.mOffset = offset;
40 return out;
41}
42
44 const u16 *lookUpTable,
45 u16 offset) FL_NO_EXCEPT {
48 // A null table means "allocate it, I will fill it" -- which is what
49 // `fromXMap` below asks for, and it used to `memcpy` from nullptr and
50 // segfault every caller of `Channel::setScreenMap(const XMap&)`
51 // (#4201). `LUT16` zeroes its storage, so the unfilled case is an
52 // identity-free map rather than garbage.
53 if (lookUpTable != nullptr) {
54 fl::memcpy(out.mLookUpTable->getDataMutable(), lookUpTable,
55 width * height * sizeof(u16));
56 }
57 out.mOffset = offset;
58 return out;
59}
60
62 u16 offset) FL_NO_EXCEPT {
63 XYMap out(width, height, true);
64 out.mOffset = offset;
65 return out;
66}
67
69 // Create an XYMap with width=xmap.length and height=1
70 // This treats the 1D strip as a 2D grid with height 1
71 u16 length = xmap.getLength();
72
73 // Null rather than a table: the LUT is allocated here and filled just
74 // below, which is what the null case in `constructWithLookUpTable`
75 // exists for.
76 auto out = XYMap::constructWithLookUpTable(length, 1, nullptr);
78 u16* data = lut->getDataMutable();
79
80 // Fill the LUT with xmap's mappings
81 for (u16 i = 0; i < length; i++) {
82 data[i] = xmap.mapToIndex(i);
83 }
84
85 out.mLookUpTable = lut;
86 return out;
87}
88
89XYMap::XYMap(u16 width, u16 height, bool is_serpentine,
90 u16 offset)
91 FL_NO_EXCEPT : type(is_serpentine ? kSerpentine : kLineByLine), width(width),
93
94void XYMap::mapPixels(const CRGB *input, CRGB *output) const FL_NO_EXCEPT {
95 u16 pos = 0;
96 for (u16 y = 0; y < height; y++) {
97 for (u16 x = 0; x < width; x++) {
98 u16 i = pos++;
99 output[i] = input[mapToIndex(x, y)];
100 }
101 }
102}
103
105 if (type == kLookUpTable) {
106 return;
107 }
109 u16 *data = mLookUpTable->getDataMutable();
110 for (u16 y = 0; y < height; y++) {
111 for (u16 x = 0; x < width; x++) {
112 data[y * width + x] = mapToIndex(x, y);
113 }
114 }
116 xyFunction = nullptr;
117}
118
121 xyFunction = nullptr;
122 mLookUpTable.reset();
123}
124
125u16 XYMap::mapToIndex(const u16 &x, const u16 &y) const FL_NO_EXCEPT {
126 u16 index;
127 switch (type) {
128 case kSerpentine: {
129 u16 xx = x % width;
130 u16 yy = y % height;
131 index = xy_serpentine(xx, yy, width, height);
132 break;
133 }
134 case kLineByLine: {
135 u16 xx = x % width;
136 u16 yy = y % height;
137 index = xy_line_by_line(xx, yy, width, height);
138 break;
139 }
140 case kFunction:
141 if (xyFunction) {
142 index = xyFunction(x, y, width, height);
143 } else {
144 // Null function pointer — fall back to line-by-line to avoid crash.
145 // This can happen due to cross-DLL static initialization order issues.
146 index = xy_line_by_line(x, y, width, height);
147 }
148 break;
149 case kLookUpTable:
150 index = mLookUpTable->getData()[y * width + x];
151 break;
152 default:
153 return 0;
154 }
155 return index + mOffset;
156}
157
158u16 XYMap::getWidth() const FL_NO_EXCEPT { return width; }
159
160u16 XYMap::getHeight() const FL_NO_EXCEPT { return height; }
161
162u16 XYMap::getTotal() const FL_NO_EXCEPT { return width * height; }
163
165
168
169} // namespace fl
uint8_t pos
Definition Blur.ino:11
void setSourceXYMap(const fl::shared_ptr< XYMap > &xymap) FL_NO_EXCEPT
Set the source XYMap (used for pixel transformation during encoding)
void set(u16 index, const vec2f &p) FL_NO_EXCEPT
XYFunction xyFunction
Definition xymap.h:130
fl::ScreenMap toScreenMap() const FL_NO_EXCEPT
Definition xymap.cpp.hpp:13
u16 getHeight() const FL_NO_EXCEPT
void mapPixels(const CRGB *input, CRGB *output) const FL_NO_EXCEPT
Definition xymap.cpp.hpp:94
void setRectangularGrid() FL_NO_EXCEPT
static XYMap constructSerpentine(u16 width, u16 height, u16 offset=0) FL_NO_EXCEPT
Definition xymap.cpp.hpp:61
static XYMap constructWithUserFunction(u16 width, u16 height, XYFunction xyFunction, u16 offset=0) FL_NO_EXCEPT
Definition xymap.cpp.hpp:28
u16 mOffset
Definition xymap.h:132
u16 mapToIndex(const u16 &x, const u16 &y) const FL_NO_EXCEPT
u16 getTotal() const FL_NO_EXCEPT
u16 width
Definition xymap.h:128
XyMapType
Definition xymap.h:43
@ kSerpentine
Definition xymap.h:43
@ kFunction
Definition xymap.h:43
@ kLineByLine
Definition xymap.h:43
@ kLookUpTable
Definition xymap.h:43
u16 getWidth() const FL_NO_EXCEPT
XyMapType getType() const FL_NO_EXCEPT
static XYMap constructRectangularGrid(u16 width, u16 height, u16 offset=0) FL_NO_EXCEPT
Definition xymap.cpp.hpp:36
void convertToLookUpTable() FL_NO_EXCEPT
static XYMap constructWithLookUpTable(u16 width, u16 height, const u16 *lookUpTable, u16 offset=0) FL_NO_EXCEPT
Definition xymap.cpp.hpp:43
u16 height
Definition xymap.h:129
fl::LUT16Ptr mLookUpTable
Definition xymap.h:131
static XYMap fromXMap(const XMap &xmap) FL_NO_EXCEPT
Create an XYMap from an XMap (treats 1D as 2D with height=1)
Definition xymap.cpp.hpp:68
XYMap(u16 width, u16 height, bool is_serpentine=true, u16 offset=0) FL_NO_EXCEPT
Definition xymap.cpp.hpp:89
XyMapType type
Definition xymap.h:127
fl::UISlider length("Length", 1.0f, 0.0f, 1.0f, 0.01f)
fl::UISlider offset("Offset", 0.0f, 0.0f, 1.0f, 0.01f)
shared_ptr< T > make_shared(Args &&... args) FL_NO_EXCEPT
Definition shared_ptr.h:423
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
u8 u8 height
Definition blur.h:186
vec2< float > vec2f
Definition geometry.h:333
u16(* XYFunction)(u16 x, u16 y, u16 width, u16 height)
Definition xymap.h:32
FASTLED_FORCE_INLINE void * memcpy(void *dest, const void *src, size_t n) FL_NO_EXCEPT
Definition cstring.h:136
u8 width
Definition blur.h:186
FASTLED_FORCE_INLINE u16 xy_line_by_line(u16 x, u16 y, u16 width, u16 height) FL_NO_EXCEPT
Definition xymap.h:25
FASTLED_FORCE_INLINE u16 xy_serpentine(u16 x, u16 y, u16 width, u16 height) FL_NO_EXCEPT
Definition xymap.h:15
Base definition for an LED controller.
Definition crgb.hpp:179
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38