FastLED 3.9.3
Loading...
Searching...
No Matches
xymap.h
1#pragma once
2
3#include <stdint.h>
4#include <string.h>
5
6#include "force_inline.h"
7#include "ref.h"
8#include "xmap.h" // Include xmap.h for LUT16
9#include "lut.h"
10#include "crgb.h"
11#include "namespace.h"
12
13FASTLED_NAMESPACE_BEGIN
14
15class ScreenMap;
16
17
18FASTLED_FORCE_INLINE uint16_t xy_serpentine(uint16_t x, uint16_t y,
19 uint16_t width, uint16_t height) {
20 (void)height;
21 if (y & 1) // Even or odd row?
22 // reverse every second line for a serpentine lled layout
23 return (y + 1) * width - 1 - x;
24 else
25 return y * width + x;
26}
27
28FASTLED_FORCE_INLINE uint16_t xy_line_by_line(uint16_t x, uint16_t y,
29 uint16_t width, uint16_t height) {
30 return y * width + x;
31}
32
33// typedef for xyMap function type
34typedef uint16_t (*XYFunction)(uint16_t x, uint16_t y, uint16_t width,
35 uint16_t height);
36
37// XYMap holds either a function or a look up table to map x, y coordinates to a
38// 1D index.
39class XYMap {
40 public:
41 enum XyMapType { kSeperentine = 0, kLineByLine, kFunction, kLookUpTable };
42
43 static XYMap constructWithUserFunction(uint16_t width, uint16_t height,
44 XYFunction xyFunction, uint16_t offset = 0) {
45 XYMap out(width, height, kFunction);
46 out.xyFunction = xyFunction;
47 out.mOffset = offset;
48 return out;
49 }
50
51 static XYMap constructRectangularGrid(uint16_t width, uint16_t height, uint16_t offset = 0) {
52 XYMap out(width, height, kLineByLine);
53 out.mOffset = offset;
54 return out;
55 }
56
57 static XYMap constructWithLookUpTable(uint16_t width, uint16_t height,
58 const uint16_t *lookUpTable, uint16_t offset = 0) {
59 XYMap out(width, height, kLookUpTable);
60 out.mLookUpTable = LUT16Ref::New(width * height);
61 memcpy(out.mLookUpTable->getData(), lookUpTable,
62 width * height * sizeof(uint16_t));
63 out.mOffset = offset;
64 return out;
65 }
66
67 // is_serpentine is true by default. You probably want this unless you are
68 // using a different layout
69 XYMap(uint16_t width, uint16_t height, bool is_serpentine = true, uint16_t offset = 0)
70 : type(is_serpentine ? kSeperentine : kLineByLine),
71 width(width), height(height), mOffset(offset) {}
72
73 XYMap(const XYMap &other) = default;
74
75 ScreenMap toScreenMap() const;
76
77 void mapPixels(const CRGB* input, CRGB* output) const {
78 uint16_t pos = 0;
79 for (uint16_t y = 0; y < height; y++) {
80 for (uint16_t x = 0; x < width; x++) {
81 uint16_t i = pos++;
82 output[i] = input[mapToIndex(x, y)];
83 }
84 }
85 }
86
87 void convertToLookUpTable() {
88 if (type == kLookUpTable) {
89 return;
90 }
91 mLookUpTable = LUT16Ref::New(width * height);
92 uint16_t *data = mLookUpTable->getData();
93 for (uint16_t y = 0; y < height; y++) {
94 for (uint16_t x = 0; x < width; x++) {
95 data[y * width + x] = mapToIndex(x, y);
96 }
97 }
98 type = kLookUpTable;
99 xyFunction = nullptr;
100 }
101
102 void setRectangularGrid() {
103 type = kLineByLine;
104 xyFunction = nullptr;
105 mLookUpTable.reset();
106 }
107
108 uint16_t mapToIndex(uint16_t x, uint16_t y) const {
109 uint16_t index;
110 switch (type) {
111 case kSeperentine:
112 x = x % width;
113 y = y % height;
114 index = xy_serpentine(x, y, width, height);
115 break;
116 case kLineByLine:
117 index = xy_line_by_line(x, y, width, height);
118 break;
119 case kFunction:
120 x = x % width;
121 y = y % height;
122 index = xyFunction(x, y, width, height);
123 break;
124 case kLookUpTable:
125 index = mLookUpTable->getData()[y * width + x];
126 break;
127 default:
128 return 0;
129 }
130 return index + mOffset;
131 }
132
133 uint16_t getWidth() const { return width; }
134 uint16_t getHeight() const { return height; }
135 uint16_t getTotal() const { return width * height; }
136 XyMapType getType() const { return type; }
137
138 private:
139 XYMap(uint16_t width, uint16_t height, XyMapType type)
140 : type(type), width(width), height(height), mOffset(0) {}
141
142 XyMapType type;
143 uint16_t width;
144 uint16_t height;
145 XYFunction xyFunction = nullptr;
146 LUT16Ref mLookUpTable; // optional refptr to look up table.
147 uint16_t mOffset = 0; // offset to be added to the output
148};
149
150FASTLED_NAMESPACE_END
Definition xymap.h:39
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:39