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