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/clamp.h"
6#include "fl/force_inline.h"
7#include "fl/namespace.h"
8#include "fl/screenmap.h"
9#include "fl/xymap.h"
10
11using namespace fl;
12
13namespace fl {
14
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 vec2f p = {static_cast<float>(w), static_cast<float>(h)};
22 out.set(index, p);
23 }
24 }
25 return out;
26}
27
29 XYFunction xyFunction, uint16_t offset) {
32 out.mOffset = offset;
33 return out;
34}
35
37 uint16_t offset) {
39 out.mOffset = offset;
40 return out;
41}
42
44 const uint16_t *lookUpTable,
45 uint16_t offset) {
47 out.mLookUpTable = LUT16Ptr::New(width * height);
48 memcpy(out.mLookUpTable->getDataMutable(), lookUpTable,
49 width * height * sizeof(uint16_t));
50 out.mOffset = offset;
51 return out;
52}
53
55 uint16_t offset) {
56 XYMap out(width, height, true);
57 out.mOffset = offset;
58 return out;
59}
60
61XYMap::XYMap(uint16_t width, uint16_t height, bool is_serpentine,
62 uint16_t offset)
63 : type(is_serpentine ? kSerpentine : kLineByLine), width(width),
65
66void XYMap::mapPixels(const CRGB *input, CRGB *output) const {
67 uint16_t pos = 0;
68 for (uint16_t y = 0; y < height; y++) {
69 for (uint16_t x = 0; x < width; x++) {
70 uint16_t i = pos++;
71 output[i] = input[mapToIndex(x, y)];
72 }
73 }
74}
75
77 if (type == kLookUpTable) {
78 return;
79 }
80 mLookUpTable = LUT16Ptr::New(width * height);
81 uint16_t *data = mLookUpTable->getDataMutable();
82 for (uint16_t y = 0; y < height; y++) {
83 for (uint16_t x = 0; x < width; x++) {
84 data[y * width + x] = mapToIndex(x, y);
85 }
86 }
88 xyFunction = nullptr;
89}
90
93 xyFunction = nullptr;
94 mLookUpTable.reset();
95}
96
97uint16_t XYMap::mapToIndex(const uint16_t &x, const uint16_t &y) const {
98 uint16_t index;
99 switch (type) {
100 case kSerpentine: {
101 uint16_t xx = x % width;
102 uint16_t yy = y % height;
103 index = xy_serpentine(xx, yy, width, height);
104 break;
105 }
106 case kLineByLine: {
107 uint16_t xx = x % width;
108 uint16_t yy = y % height;
109 index = xy_line_by_line(xx, yy, width, height);
110 break;
111 }
112 case kFunction:
113 index = xyFunction(x, y, width, height);
114 break;
115 case kLookUpTable:
116 index = mLookUpTable->getData()[y * width + x];
117 break;
118 default:
119 return 0;
120 }
121 return index + mOffset;
122}
123
124uint16_t XYMap::getWidth() const { return width; }
125
126uint16_t XYMap::getHeight() const { return height; }
127
128uint16_t XYMap::getTotal() const { return width * height; }
129
131
134
135} // namespace fl
int y
Definition Audio.ino:72
int x
Definition Audio.ino:71
uint8_t pos
Definition Blur.ino:11
void set(uint16_t index, const vec2f &p)
XyMapType getType() const
Definition xymap.cpp:130
XYFunction xyFunction
Definition xymap.h:123
uint16_t mOffset
Definition xymap.h:125
uint16_t width
Definition xymap.h:121
static XYMap constructWithLookUpTable(uint16_t width, uint16_t height, const uint16_t *lookUpTable, uint16_t offset=0)
Definition xymap.cpp:43
static XYMap constructSerpentine(uint16_t width, uint16_t height, uint16_t offset=0)
Definition xymap.cpp:54
uint16_t getWidth() const
Definition xymap.cpp:124
fl::ScreenMap toScreenMap() const
Definition xymap.cpp:15
uint16_t mapToIndex(const uint16_t &x, const uint16_t &y) const
Definition xymap.cpp:97
void mapPixels(const CRGB *input, CRGB *output) const
Definition xymap.cpp:66
void convertToLookUpTable()
Definition xymap.cpp:76
XYMap(uint16_t width, uint16_t height, bool is_serpentine=true, uint16_t offset=0)
Definition xymap.cpp:61
XyMapType
Definition xymap.h:45
@ kSerpentine
Definition xymap.h:45
@ kFunction
Definition xymap.h:45
@ kLineByLine
Definition xymap.h:45
@ kLookUpTable
Definition xymap.h:45
static XYMap constructRectangularGrid(uint16_t width, uint16_t height, uint16_t offset=0)
Definition xymap.cpp:36
uint16_t getHeight() const
Definition xymap.cpp:126
uint16_t height
Definition xymap.h:122
fl::LUT16Ptr mLookUpTable
Definition xymap.h:124
void setRectangularGrid()
Definition xymap.cpp:91
static XYMap constructWithUserFunction(uint16_t width, uint16_t height, XYFunction xyFunction, uint16_t offset=0)
Definition xymap.cpp:28
uint16_t getTotal() const
Definition xymap.cpp:128
XyMapType type
Definition xymap.h:120
UISlider offset("Offset", 0.0f, 0.0f, 1.0f, 0.01f)
UISlider length("Length", 1.0f, 0.0f, 1.0f, 0.01f)
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:17
vec2< float > vec2f
Definition geometry.h:301
FASTLED_FORCE_INLINE uint16_t xy_line_by_line(uint16_t x, uint16_t y, uint16_t width, uint16_t height)
Definition xymap.h:27
uint16_t(* XYFunction)(uint16_t x, uint16_t y, uint16_t width, uint16_t height)
Definition xymap.h:34
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:30
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:55