FastLED 3.9.15
Loading...
Searching...
No Matches
xymap.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdint.h>
4#include <string.h>
5
6#include "crgb.h"
7#include "fl/clamp.h"
8#include "fl/force_inline.h"
9#include "fl/lut.h"
10#include "fl/namespace.h"
11#include "fl/ptr.h"
12#include "fl/xmap.h" // Include xmap.h for LUT16
13
14namespace fl {
15class ScreenMap;
16
17FASTLED_FORCE_INLINE uint16_t xy_serpentine(uint16_t x, uint16_t y,
18 uint16_t width, uint16_t height) {
19 (void)height;
20 if (y & 1) // Even or odd row?
21 // reverse every second line for a serpentine lled layout
22 return (y + 1) * width - 1 - x;
23 else
24 return y * width + x;
25}
26
27FASTLED_FORCE_INLINE uint16_t xy_line_by_line(uint16_t x, uint16_t y,
28 uint16_t width, uint16_t height) {
29 (void)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// Maps x,y -> led index
38//
39// The common output led matrix you can buy on amazon is in a serpentine layout.
40//
41// XYMap allows you do to do graphic calculations on an LED layout as if it were
42// a grid.
43class XYMap {
44 public:
46
47 static XYMap constructWithUserFunction(uint16_t width, uint16_t height,
49 uint16_t offset = 0);
50
51 static XYMap constructRectangularGrid(uint16_t width, uint16_t height,
52 uint16_t offset = 0);
53
54 static XYMap constructWithLookUpTable(uint16_t width, uint16_t height,
55 const uint16_t *lookUpTable,
56 uint16_t offset = 0);
57
58 static XYMap constructSerpentine(uint16_t width, uint16_t height,
59 uint16_t offset = 0);
60
61 static XYMap identity(uint16_t width, uint16_t height) {
63 }
64
65 // is_serpentine is true by default. You probably want this unless you are
66 // using a different layout
67 XYMap(uint16_t width, uint16_t height, bool is_serpentine = true,
68 uint16_t offset = 0);
69
70 XYMap(const XYMap &other) = default;
71 XYMap &operator=(const XYMap &other) = default;
72
74
75 void mapPixels(const CRGB *input, CRGB *output) const;
76
78
79 void setRectangularGrid();
80
81 uint16_t operator()(uint16_t x, uint16_t y) const {
82 return mapToIndex(x, y);
83 }
84
85 uint16_t mapToIndex(const uint16_t &x, const uint16_t &y) const;
86
87 template <typename IntType,
89 uint16_t mapToIndex(IntType x, IntType y) const {
90 x = fl::clamp<int>(x, 0, width - 1);
91 y = fl::clamp<int>(y, 0, height - 1);
92 return mapToIndex((uint16_t)x, (uint16_t)y);
93 }
94
95 bool has(uint16_t x, uint16_t y) const {
96 return (x < width) && (y < height);
97 }
98
99 bool has(int x, int y) const {
100 return (x >= 0) && (y >= 0) && has((uint16_t)x, (uint16_t)y);
101 }
102
103 bool isSerpentine() const { return type == kSerpentine; }
104 bool isLineByLine() const { return type == kLineByLine; }
105 bool isFunction() const { return type == kFunction; }
106 bool isLUT() const { return type == kLookUpTable; }
107 bool isRectangularGrid() const { return type == kLineByLine; }
109 return type == kSerpentine || type == kLineByLine;
110 }
111
112 uint16_t getWidth() const;
113 uint16_t getHeight() const;
114 uint16_t getTotal() const;
115 XyMapType getType() const;
116
117 private:
118 XYMap(uint16_t width, uint16_t height, XyMapType type);
119
121 uint16_t width;
122 uint16_t height;
124 fl::LUT16Ptr mLookUpTable; // optional refptr to look up table.
125 uint16_t mOffset = 0; // offset to be added to the output
126};
127
128} // namespace fl
int y
Definition Audio.ino:72
int x
Definition Audio.ino:71
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
uint16_t mapToIndex(IntType x, IntType y) const
Definition xymap.h:89
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
bool isSerpentineOrLineByLine() const
Definition xymap.h:108
uint16_t operator()(uint16_t x, uint16_t y) const
Definition xymap.h:81
fl::ScreenMap toScreenMap() const
Definition xymap.cpp:15
uint16_t mapToIndex(const uint16_t &x, const uint16_t &y) const
Definition xymap.cpp:97
bool isFunction() const
Definition xymap.h:105
XYMap(const XYMap &other)=default
void mapPixels(const CRGB *input, CRGB *output) const
Definition xymap.cpp:66
bool has(int x, int y) const
Definition xymap.h:99
void convertToLookUpTable()
Definition xymap.cpp:76
bool isRectangularGrid() const
Definition xymap.h:107
XYMap(uint16_t width, uint16_t height, bool is_serpentine=true, uint16_t offset=0)
Definition xymap.cpp:61
bool isLUT() const
Definition xymap.h:106
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
static XYMap identity(uint16_t width, uint16_t height)
Definition xymap.h:61
uint16_t getHeight() const
Definition xymap.cpp:126
bool isLineByLine() const
Definition xymap.h:104
uint16_t height
Definition xymap.h:122
bool has(uint16_t x, uint16_t y) const
Definition xymap.h:95
bool isSerpentine() const
Definition xymap.h:103
XYMap & operator=(const XYMap &other)=default
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
Defines the red, green, and blue (RGB) pixel struct.
UISlider offset("Offset", 0.0f, 0.0f, 1.0f, 0.01f)
#define FASTLED_FORCE_INLINE
Definition force_inline.h:6
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
FASTLED_FORCE_INLINE T clamp(T value, T min, T max)
Definition clamp.h:10
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
typename enable_if< Condition, T >::type enable_if_t
Definition type_traits.h:29
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:55