FastLED 3.9.15
Loading...
Searching...
No Matches
xmap.cpp
Go to the documentation of this file.
1
2
3
4#include "fl/xmap.h"
5
6namespace fl {
7
8
9
11 XMap out = XMap(length, kFunction);
12 out.xFunction = xFunction;
13 out.mOffset = offset;
14 return out;
15}
16
17
18
19XMap XMap::constructWithLookUpTable(uint16_t length, const uint16_t *lookUpTable, uint16_t offset) {
21 out.mData = lookUpTable;
22 out.mOffset = offset;
23 return out;
24}
25
26
27
28XMap::XMap(uint16_t length, bool is_reverse, uint16_t offset) {
29 type = is_reverse ? kReverse : kLinear;
30 this->length = length;
31 this->mOffset = offset;
32}
33
34
35
36XMap::XMap(const XMap &other) {
37 type = other.type;
38 length = other.length;
39 xFunction = other.xFunction;
40 mData = other.mData;
42 mOffset = other.mOffset;
43}
44
45
46
48 if (type == kLookUpTable) {
49 return;
50 }
51 mLookUpTable.reset();
52 mLookUpTable = LUT16Ptr::New(length);
53 uint16_t* dataMutable = mLookUpTable->getData();
54 mData = mLookUpTable->getData();
55 for (uint16_t x = 0; x < length; x++) {
56 dataMutable[x] = mapToIndex(x);
57 }
59 xFunction = nullptr;
60}
61
62
63
64uint16_t XMap::mapToIndex(uint16_t x) const {
65 uint16_t index;
66 switch (type) {
67 case kLinear:
68 index = x_linear(x, length);
69 break;
70 case kReverse:
71 index = x_reverse(x, length);
72 break;
73 case kFunction:
74 x = x % length;
75 index = xFunction(x, length);
76 break;
77 case kLookUpTable:
78 index = mData[x];
79 break;
80 default:
81 return 0;
82 }
83 return index + mOffset;
84}
85
86
87
88uint16_t XMap::getLength() const {
89 return length;
90}
91
92
93
95 return type;
96}
97
98
99
101 : length(length), type(type), mOffset(0) {
102}
103
104
105
106XMap &XMap::operator=(const XMap &other) {
107 if (this != &other) {
108 type = other.type;
109 length = other.length;
110 xFunction = other.xFunction;
111 mData = other.mData;
113 mOffset = other.mOffset;
114 }
115 return *this;
116}
117
118} // namespace fl
uint32_t x[NUM_LAYERS]
Definition Fire2023.ino:80
uint16_t mOffset
Definition xmap.h:66
void convertToLookUpTable()
Definition xmap.cpp:47
Type type
Definition xmap.h:62
uint16_t mapToIndex(uint16_t x) const
Definition xmap.cpp:64
const uint16_t * mData
Definition xmap.h:64
Type getType() const
Definition xmap.cpp:94
uint16_t length
Definition xmap.h:61
XMap(uint16_t length, bool is_reverse=false, uint16_t offset=0)
Definition xmap.cpp:28
fl::LUT16Ptr mLookUpTable
Definition xmap.h:65
static XMap constructWithUserFunction(uint16_t length, XFunction xFunction, uint16_t offset=0)
Definition xmap.cpp:10
uint16_t getLength() const
Definition xmap.cpp:88
static XMap constructWithLookUpTable(uint16_t length, const uint16_t *lookUpTable, uint16_t offset=0)
Definition xmap.cpp:19
@ kFunction
Definition xmap.h:30
@ kLookUpTable
Definition xmap.h:30
@ kLinear
Definition xmap.h:30
@ kReverse
Definition xmap.h:30
XFunction xFunction
Definition xmap.h:63
XMap & operator=(const XMap &other)
Definition xmap.cpp:106
uint16_t(* XFunction)(uint16_t x, uint16_t length)
Definition xmap.h:24
FASTLED_FORCE_INLINE uint16_t x_linear(uint16_t x, uint16_t length)
Definition xmap.h:14
FASTLED_FORCE_INLINE uint16_t x_reverse(uint16_t x, uint16_t length)
Definition xmap.h:19
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16