FastLED 3.9.15
Loading...
Searching...
No Matches
xmap.cpp
Go to the documentation of this file.
1
2
3#include "fl/xmap.h"
4
5namespace fl {
6
8 uint16_t offset) {
10 out.xFunction = xFunction;
11 out.mOffset = offset;
12 return out;
13}
14
16 const uint16_t *lookUpTable,
17 uint16_t offset) {
19 out.mData = lookUpTable;
20 out.mOffset = offset;
21 return out;
22}
23
24XMap::XMap(uint16_t length, bool is_reverse, uint16_t offset) {
25 type = is_reverse ? kReverse : kLinear;
26 this->length = length;
27 this->mOffset = offset;
28}
29
30XMap::XMap(const XMap &other) {
31 type = other.type;
32 length = other.length;
33 xFunction = other.xFunction;
34 mData = other.mData;
36 mOffset = other.mOffset;
37}
38
40 if (type == kLookUpTable) {
41 return;
42 }
43 mLookUpTable.reset();
44 mLookUpTable = LUT16Ptr::New(length);
45 uint16_t *dataMutable = mLookUpTable->getDataMutable();
46 mData = mLookUpTable->getData();
47 for (uint16_t x = 0; x < length; x++) {
48 dataMutable[x] = mapToIndex(x);
49 }
51 xFunction = nullptr;
52}
53
54uint16_t XMap::mapToIndex(uint16_t x) const {
55 uint16_t index;
56 switch (type) {
57 case kLinear:
58 index = x_linear(x, length);
59 break;
60 case kReverse:
61 index = x_reverse(x, length);
62 break;
63 case kFunction:
64 x = x % length;
65 index = xFunction(x, length);
66 break;
67 case kLookUpTable:
68 index = mData[x];
69 break;
70 default:
71 return 0;
72 }
73 return index + mOffset;
74}
75
76uint16_t XMap::getLength() const { return length; }
77
78XMap::Type XMap::getType() const { return type; }
79
82
83XMap &XMap::operator=(const XMap &other) {
84 if (this != &other) {
85 type = other.type;
86 length = other.length;
87 xFunction = other.xFunction;
88 mData = other.mData;
90 mOffset = other.mOffset;
91 }
92 return *this;
93}
94
95} // namespace fl
int x
Definition Audio.ino:71
uint16_t mOffset
Definition xmap.h:66
void convertToLookUpTable()
Definition xmap.cpp:39
Type type
Definition xmap.h:62
uint16_t mapToIndex(uint16_t x) const
Definition xmap.cpp:54
const uint16_t * mData
Definition xmap.h:64
Type getType() const
Definition xmap.cpp:78
uint16_t length
Definition xmap.h:61
XMap(uint16_t length, bool is_reverse=false, uint16_t offset=0)
Definition xmap.cpp:24
fl::LUT16Ptr mLookUpTable
Definition xmap.h:65
static XMap constructWithUserFunction(uint16_t length, XFunction xFunction, uint16_t offset=0)
Definition xmap.cpp:7
uint16_t getLength() const
Definition xmap.cpp:76
static XMap constructWithLookUpTable(uint16_t length, const uint16_t *lookUpTable, uint16_t offset=0)
Definition xmap.cpp:15
@ 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:83
UISlider offset("Offset", 0.0f, 0.0f, 1.0f, 0.01f)
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