FastLED 3.9.15
Loading...
Searching...
No Matches
xymap.cpp
Go to the documentation of this file.
1
2#include "fl/stdint.h"
3#include <string.h>
4
5#include "fl/clamp.h"
6#include "fl/force_inline.h"
7#include "fl/namespace.h"
8#include "fl/screenmap.h"
9#include "fl/xymap.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 return out;
24}
25
33
35 u16 offset) {
37 out.mOffset = offset;
38 return out;
39}
40
42 const u16 *lookUpTable,
43 u16 offset) {
46 memcpy(out.mLookUpTable->getDataMutable(), lookUpTable,
47 width * height * sizeof(u16));
48 out.mOffset = offset;
49 return out;
50}
51
53 u16 offset) {
54 XYMap out(width, height, true);
55 out.mOffset = offset;
56 return out;
57}
58
59XYMap::XYMap(u16 width, u16 height, bool is_serpentine,
60 u16 offset)
61 : type(is_serpentine ? kSerpentine : kLineByLine), width(width),
63
64void XYMap::mapPixels(const CRGB *input, CRGB *output) const {
65 u16 pos = 0;
66 for (u16 y = 0; y < height; y++) {
67 for (u16 x = 0; x < width; x++) {
68 u16 i = pos++;
69 output[i] = input[mapToIndex(x, y)];
70 }
71 }
72}
73
75 if (type == kLookUpTable) {
76 return;
77 }
79 u16 *data = mLookUpTable->getDataMutable();
80 for (u16 y = 0; y < height; y++) {
81 for (u16 x = 0; x < width; x++) {
82 data[y * width + x] = mapToIndex(x, y);
83 }
84 }
86 xyFunction = nullptr;
87}
88
91 xyFunction = nullptr;
92 mLookUpTable.reset();
93}
94
95u16 XYMap::mapToIndex(const u16 &x, const u16 &y) const {
96 u16 index;
97 switch (type) {
98 case kSerpentine: {
99 u16 xx = x % width;
100 u16 yy = y % height;
101 index = xy_serpentine(xx, yy, width, height);
102 break;
103 }
104 case kLineByLine: {
105 u16 xx = x % width;
106 u16 yy = y % height;
107 index = xy_line_by_line(xx, yy, width, height);
108 break;
109 }
110 case kFunction:
111 index = xyFunction(x, y, width, height);
112 break;
113 case kLookUpTable:
114 index = mLookUpTable->getData()[y * width + x];
115 break;
116 default:
117 return 0;
118 }
119 return index + mOffset;
120}
121
122u16 XYMap::getWidth() const { return width; }
123
124u16 XYMap::getHeight() const { return height; }
125
126u16 XYMap::getTotal() const { return width * height; }
127
129
132
133} // namespace fl
int y
Definition simple.h:93
int x
Definition simple.h:92
uint8_t pos
Definition Blur.ino:11
void set(u16 index, const vec2f &p)
XyMapType getType() const
Definition xymap.cpp:128
XYFunction xyFunction
Definition xymap.h:129
static XYMap constructWithUserFunction(u16 width, u16 height, XYFunction xyFunction, u16 offset=0)
Definition xymap.cpp:26
fl::ScreenMap toScreenMap() const
Definition xymap.cpp:13
u16 mOffset
Definition xymap.h:131
static XYMap constructSerpentine(u16 width, u16 height, u16 offset=0)
Definition xymap.cpp:52
void mapPixels(const CRGB *input, CRGB *output) const
Definition xymap.cpp:64
static XYMap constructRectangularGrid(u16 width, u16 height, u16 offset=0)
Definition xymap.cpp:34
u16 getWidth() const
Definition xymap.cpp:122
void convertToLookUpTable()
Definition xymap.cpp:74
u16 width
Definition xymap.h:127
static XYMap constructWithLookUpTable(u16 width, u16 height, const u16 *lookUpTable, u16 offset=0)
Definition xymap.cpp:41
u16 mapToIndex(const u16 &x, const u16 &y) const
Definition xymap.cpp:95
XyMapType
Definition xymap.h:47
@ kSerpentine
Definition xymap.h:47
@ kFunction
Definition xymap.h:47
@ kLineByLine
Definition xymap.h:47
@ kLookUpTable
Definition xymap.h:47
u16 getTotal() const
Definition xymap.cpp:126
XYMap(u16 width, u16 height, bool is_serpentine=true, u16 offset=0)
Definition xymap.cpp:59
u16 getHeight() const
Definition xymap.cpp:124
u16 height
Definition xymap.h:128
fl::LUT16Ptr mLookUpTable
Definition xymap.h:130
void setRectangularGrid()
Definition xymap.cpp:89
XyMapType type
Definition xymap.h:126
UISlider offset("Offset", 0.0f, 0.0f, 1.0f, 0.01f)
UISlider length("Length", 1.0f, 0.0f, 1.0f, 0.01f)
Implements the FastLED namespace macros.
FASTLED_FORCE_INLINE u16 xy_line_by_line(u16 x, u16 y, u16 width, u16 height)
Definition xymap.h:29
u16(* XYFunction)(u16 x, u16 y, u16 width, u16 height)
Definition xymap.h:36
vec2< float > vec2f
Definition geometry.h:333
shared_ptr< T > make_shared(Args &&... args)
Definition shared_ptr.h:348
FASTLED_FORCE_INLINE u16 xy_serpentine(u16 x, u16 y, u16 width, u16 height)
Definition xymap.h:19
IMPORTANT!
Definition crgb.h:20
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:86