FastLED 3.9.7
Loading...
Searching...
No Matches
xymap.cpp
1
2#include <stdint.h>
3#include <string.h>
4
5#include "fl/force_inline.h"
6#include "fl/namespace.h"
7#include "fl/xymap.h"
8#include "fl/screenmap.h"
9
10using namespace fl;
11
12namespace fl {
13
14
15ScreenMap XYMap::toScreenMap() const {
16 const uint16_t length = width * height;
17 ScreenMap out(length);
18 for (uint16_t w = 0; w < width; w++) {
19 for (uint16_t h = 0; h < height; h++) {
20 uint16_t index = mapToIndex(w, h);
21 pair_xy_float p = {
22 static_cast<float>(w),
23 static_cast<float>(h)
24 };
25 out.set(index, p);
26 }
27 }
28 return out;
29}
30
31
32
33XYMap XYMap::constructWithUserFunction(uint16_t width, uint16_t height,
34 XYFunction xyFunction, uint16_t offset) {
35 XYMap out(width, height, kFunction);
36 out.xyFunction = xyFunction;
37 out.mOffset = offset;
38 return out;
39}
40
41
42
43XYMap XYMap::constructRectangularGrid(uint16_t width, uint16_t height, uint16_t offset) {
44 XYMap out(width, height, kLineByLine);
45 out.mOffset = offset;
46 return out;
47}
48
49
50
51XYMap XYMap::constructWithLookUpTable(uint16_t width, uint16_t height,
52 const uint16_t *lookUpTable, uint16_t offset) {
53 XYMap out(width, height, kLookUpTable);
54 out.mLookUpTable = LUT16Ptr::New(width * height);
55 memcpy(out.mLookUpTable->getData(), lookUpTable,
56 width * height * sizeof(uint16_t));
57 out.mOffset = offset;
58 return out;
59}
60
61
62
63XYMap::XYMap(uint16_t width, uint16_t height, bool is_serpentine, uint16_t offset)
64 : type(is_serpentine ? kSerpentine : kLineByLine),
65 width(width), height(height), mOffset(offset) {}
66
67
68
69void XYMap::mapPixels(const CRGB* input, CRGB* output) const {
70 uint16_t pos = 0;
71 for (uint16_t y = 0; y < height; y++) {
72 for (uint16_t x = 0; x < width; x++) {
73 uint16_t i = pos++;
74 output[i] = input[mapToIndex(x, y)];
75 }
76 }
77}
78
79
80
81void XYMap::convertToLookUpTable() {
82 if (type == kLookUpTable) {
83 return;
84 }
85 mLookUpTable = LUT16Ptr::New(width * height);
86 uint16_t *data = mLookUpTable->getData();
87 for (uint16_t y = 0; y < height; y++) {
88 for (uint16_t x = 0; x < width; x++) {
89 data[y * width + x] = mapToIndex(x, y);
90 }
91 }
92 type = kLookUpTable;
93 xyFunction = nullptr;
94}
95
96
97
98void XYMap::setRectangularGrid() {
99 type = kLineByLine;
100 xyFunction = nullptr;
101 mLookUpTable.reset();
102}
103
104
105
106uint16_t XYMap::mapToIndex(uint16_t x, uint16_t y) const {
107 uint16_t index;
108 switch (type) {
109 case kSerpentine:
110 x = x % width;
111 y = y % height;
112 index = xy_serpentine(x, y, width, height);
113 break;
114 case kLineByLine:
115 index = xy_line_by_line(x, y, width, height);
116 break;
117 case kFunction:
118 x = x % width;
119 y = y % height;
120 index = xyFunction(x, y, width, height);
121 break;
122 case kLookUpTable:
123 index = mLookUpTable->getData()[y * width + x];
124 break;
125 default:
126 return 0;
127 }
128 return index + mOffset;
129}
130
131
132
133uint16_t XYMap::getWidth() const { return width; }
134
135
136
137uint16_t XYMap::getHeight() const { return height; }
138
139
140
141uint16_t XYMap::getTotal() const { return width * height; }
142
143
144
145XYMap::XyMapType XYMap::getType() const { return type; }
146
147
148
149XYMap::XYMap(uint16_t width, uint16_t height, XyMapType type)
150 : type(type), width(width), height(height), mOffset(0) {}
151
152} // namespace fl
153
Implements the FastLED namespace macros.
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:54