FastLED 3.9.3
Loading...
Searching...
No Matches
xmap.h
1#pragma once
2
3#include <stdint.h>
4#include <string.h>
5
6#include "ref.h"
7#include "force_inline.h"
8#include "lut.h"
9
10#include "namespace.h"
11
12FASTLED_NAMESPACE_BEGIN
13
14FASTLED_FORCE_INLINE uint16_t x_linear(uint16_t x, uint16_t length) {
15 (void)length;
16 return x;
17}
18
19FASTLED_FORCE_INLINE uint16_t x_reverse(uint16_t x, uint16_t length) {
20 return length - 1 - x;
21}
22
23// typedef for xMap function type
24typedef uint16_t (*XFunction)(uint16_t x, uint16_t length);
25
26
27// XMap holds either a function or a look up table to map x coordinates to a 1D index.
28class XMap {
29public:
30
31 enum Type {
32 kLinear = 0,
33 kReverse,
34 kFunction,
35 kLookUpTable
36 };
37
38 static XMap constructWithUserFunction(uint16_t length, XFunction xFunction, uint16_t offset = 0) {
39 XMap out = XMap(length, kFunction);
40 out.xFunction = xFunction;
41 out.mOffset = offset;
42 return out;
43 }
44
45 // When a pointer to a lookup table is passed in then we assume it's
46 // owned by someone else and will not be deleted.
47 static XMap constructWithLookUpTable(uint16_t length, const uint16_t *lookUpTable, uint16_t offset = 0) {
48 XMap out = XMap(length, kLookUpTable);
49 out.mData = lookUpTable;
50 out.mOffset = offset;
51 return out;
52 }
53
54 // is_reverse is false by default for linear layout
55 XMap(uint16_t length, bool is_reverse = false, uint16_t offset = 0) {
56 type = is_reverse ? kReverse : kLinear;
57 this->length = length;
58 this->mOffset = offset;
59 }
60
61 XMap(const XMap &other) {
62 type = other.type;
63 length = other.length;
64 xFunction = other.xFunction;
65 mData = other.mData;
66 mLookUpTable = other.mLookUpTable;
67 mOffset = other.mOffset;
68 }
69
70 // define the assignment operator
71 XMap& operator=(const XMap &other) {
72 if (this != &other) {
73 type = other.type;
74 length = other.length;
75 xFunction = other.xFunction;
76 mData = other.mData;
77 mLookUpTable = other.mLookUpTable;
78 mOffset = other.mOffset;
79 }
80 return *this;
81 }
82
83 void convertToLookUpTable() {
84 if (type == kLookUpTable) {
85 return;
86 }
87 mLookUpTable.reset();
88 mLookUpTable = LUT16Ref::New(length);
89 uint16_t* dataMutable = mLookUpTable->getData();
90 mData = mLookUpTable->getData();
91 for (uint16_t x = 0; x < length; x++) {
92 dataMutable[x] = mapToIndex(x);
93 }
94 type = kLookUpTable;
95 xFunction = nullptr;
96 }
97
98 uint16_t mapToIndex(uint16_t x) const {
99 uint16_t index;
100 switch (type) {
101 case kLinear:
102 index = x_linear(x, length);
103 break;
104 case kReverse:
105 index = x_reverse(x, length);
106 break;
107 case kFunction:
108 x = x % length;
109 index = xFunction(x, length);
110 break;
111 case kLookUpTable:
112 index = mData[x];
113 break;
114 default:
115 return 0;
116 }
117 return index + mOffset;
118 }
119
120 uint16_t getLength() const {
121 return length;
122 }
123
124 Type getType() const {
125 return type;
126 }
127
128
129private:
130 XMap(uint16_t length, Type type)
131 : length(length), type(type), mOffset(0) {
132 }
133 uint16_t length = 0;
134 Type type = kLinear;
135 XFunction xFunction = nullptr;
136 const uint16_t *mData = nullptr;
137 LUT16Ref mLookUpTable;
138 uint16_t mOffset = 0; // offset to be added to the output
139};
140
141FASTLED_NAMESPACE_END
Definition xmap.h:28