FastLED 3.9.15
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
10namespace fl {
11
13 const u16 length = width * height;
14 ScreenMap out(length);
15 for (u16 w = 0; w < width; w++) {
16 for (u16 h = 0; h < height; h++) {
17 u16 index = mapToIndex(w, h);
18 vec2f p = {static_cast<float>(w), static_cast<float>(h)};
19 out.set(index, p);
20 }
21 }
22 // Store a shared_ptr to this XYMap so it can be used for encoding
24 return out;
25}
26
34
36 u16 offset) {
38 out.mOffset = offset;
39 return out;
40}
41
43 const u16 *lookUpTable,
44 u16 offset) {
47 fl::memcpy(out.mLookUpTable->getDataMutable(), lookUpTable,
48 width * height * sizeof(u16));
49 out.mOffset = offset;
50 return out;
51}
52
54 u16 offset) {
55 XYMap out(width, height, true);
56 out.mOffset = offset;
57 return out;
58}
59
61 // Create an XYMap with width=xmap.length and height=1
62 // This treats the 1D strip as a 2D grid with height 1
63 u16 length = xmap.getLength();
64
65 // Create a user function that dispatches to the XMap
66 // Since we can't capture xmap directly, we create a LUT and use that
67 auto out = XYMap::constructWithLookUpTable(length, 1, nullptr);
69 u16* data = lut->getDataMutable();
70
71 // Fill the LUT with xmap's mappings
72 for (u16 i = 0; i < length; i++) {
73 data[i] = xmap.mapToIndex(i);
74 }
75
76 out.mLookUpTable = lut;
77 return out;
78}
79
80XYMap::XYMap(u16 width, u16 height, bool is_serpentine,
81 u16 offset)
82 : type(is_serpentine ? kSerpentine : kLineByLine), width(width),
84
85void XYMap::mapPixels(const CRGB *input, CRGB *output) const {
86 u16 pos = 0;
87 for (u16 y = 0; y < height; y++) {
88 for (u16 x = 0; x < width; x++) {
89 u16 i = pos++;
90 output[i] = input[mapToIndex(x, y)];
91 }
92 }
93}
94
96 if (type == kLookUpTable) {
97 return;
98 }
100 u16 *data = mLookUpTable->getDataMutable();
101 for (u16 y = 0; y < height; y++) {
102 for (u16 x = 0; x < width; x++) {
103 data[y * width + x] = mapToIndex(x, y);
104 }
105 }
107 xyFunction = nullptr;
108}
109
112 xyFunction = nullptr;
113 mLookUpTable.reset();
114}
115
116u16 XYMap::mapToIndex(const u16 &x, const u16 &y) const {
117 u16 index;
118 switch (type) {
119 case kSerpentine: {
120 u16 xx = x % width;
121 u16 yy = y % height;
122 index = xy_serpentine(xx, yy, width, height);
123 break;
124 }
125 case kLineByLine: {
126 u16 xx = x % width;
127 u16 yy = y % height;
128 index = xy_line_by_line(xx, yy, width, height);
129 break;
130 }
131 case kFunction:
132 if (xyFunction) {
133 index = xyFunction(x, y, width, height);
134 } else {
135 // Null function pointer — fall back to line-by-line to avoid crash.
136 // This can happen due to cross-DLL static initialization order issues.
137 index = xy_line_by_line(x, y, width, height);
138 }
139 break;
140 case kLookUpTable:
141 index = mLookUpTable->getData()[y * width + x];
142 break;
143 default:
144 return 0;
145 }
146 return index + mOffset;
147}
148
149u16 XYMap::getWidth() const { return width; }
150
151u16 XYMap::getHeight() const { return height; }
152
153u16 XYMap::getTotal() const { return width * height; }
154
156
159
160} // namespace fl
uint8_t pos
Definition Blur.ino:11
void setSourceXYMap(const fl::shared_ptr< XYMap > &xymap) FL_NOEXCEPT
Set the source XYMap (used for pixel transformation during encoding)
void set(u16 index, const vec2f &p) FL_NOEXCEPT
u16 getLength() const
Definition xmap.cpp.hpp:77
u16 mapToIndex(u16 x) const
Definition xmap.cpp.hpp:55
u16 mapToIndex(const u16 &x, const u16 &y) const FL_NOEXCEPT
fl::ScreenMap toScreenMap() const FL_NOEXCEPT
Definition xymap.cpp.hpp:12
XYFunction xyFunction
Definition xymap.h:130
static XYMap constructWithUserFunction(u16 width, u16 height, XYFunction xyFunction, u16 offset=0) FL_NOEXCEPT
Definition xymap.cpp.hpp:27
u16 getWidth() const FL_NOEXCEPT
u16 getHeight() const FL_NOEXCEPT
void setRectangularGrid() FL_NOEXCEPT
u16 mOffset
Definition xymap.h:132
void convertToLookUpTable() FL_NOEXCEPT
Definition xymap.cpp.hpp:95
static XYMap fromXMap(const XMap &xmap) FL_NOEXCEPT
Create an XYMap from an XMap (treats 1D as 2D with height=1)
Definition xymap.cpp.hpp:60
static XYMap constructSerpentine(u16 width, u16 height, u16 offset=0) FL_NOEXCEPT
Definition xymap.cpp.hpp:53
void mapPixels(const CRGB *input, CRGB *output) const FL_NOEXCEPT
Definition xymap.cpp.hpp:85
u16 width
Definition xymap.h:128
u16 getTotal() const FL_NOEXCEPT
XyMapType getType() const FL_NOEXCEPT
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 height
Definition xymap.h:129
fl::LUT16Ptr mLookUpTable
Definition xymap.h:131
static XYMap constructWithLookUpTable(u16 width, u16 height, const u16 *lookUpTable, u16 offset=0) FL_NOEXCEPT
Definition xymap.cpp.hpp:42
XyMapType type
Definition xymap.h:127
static XYMap constructRectangularGrid(u16 width, u16 height, u16 offset=0) FL_NOEXCEPT
Definition xymap.cpp.hpp:35
XYMap(u16 width, u16 height, bool is_serpentine=true, u16 offset=0) FL_NOEXCEPT
Definition xymap.cpp.hpp:80
fl::UISlider length("Length", 1.0f, 0.0f, 1.0f, 0.01f)
fl::UISlider offset("Offset", 0.0f, 0.0f, 1.0f, 0.01f)
void * memcpy(void *dest, const void *src, size_t n) FL_NOEXCEPT
vec2< float > vec2f
Definition geometry.h:333
FASTLED_FORCE_INLINE u16 xy_line_by_line(u16 x, u16 y, u16 width, u16 height) FL_NOEXCEPT
Definition xymap.h:25
FASTLED_FORCE_INLINE u16 xy_serpentine(u16 x, u16 y, u16 width, u16 height) FL_NOEXCEPT
Definition xymap.h:15
u16(* XYFunction)(u16 x, u16 y, u16 width, u16 height)
Definition xymap.h:32
shared_ptr< T > make_shared(Args &&... args) FL_NOEXCEPT
Definition shared_ptr.h:414
Base definition for an LED controller.
Definition crgb.hpp:179
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38