FastLED 3.9.7
Loading...
Searching...
No Matches
xmap.cpp
1
2
3
4#include "fl/xmap.h"
5
6namespace fl {
7
8
9
10XMap XMap::constructWithUserFunction(uint16_t length, XFunction xFunction, uint16_t offset) {
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) {
20 XMap out = XMap(length, kLookUpTable);
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;
41 mLookUpTable = other.mLookUpTable;
42 mOffset = other.mOffset;
43}
44
45
46
47void XMap::convertToLookUpTable() {
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 }
58 type = kLookUpTable;
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
94XMap::Type XMap::getType() const {
95 return type;
96}
97
98
99
100XMap::XMap(uint16_t length, Type type)
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;
112 mLookUpTable = other.mLookUpTable;
113 mOffset = other.mOffset;
114 }
115 return *this;
116}
117
118} // namespace fl
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16