FastLED 3.10.6
Loading...
Searching...
No Matches
xymap.h
Go to the documentation of this file.
1#pragma once
2
4#include "fl/math/math.h"
5#include "fl/math/lut.h"
7#include "fl/stl/int.h"
8#include "fl/stl/noexcept.h"
9
10namespace fl {
11struct CRGB; // Forward declaration
12class ScreenMap;
13class XMap; // Forward declaration
14
16 u16 width, u16 height) FL_NO_EXCEPT {
17 (void)height;
18 if (y & 1) // Even or odd row?
19 // reverse every second line for a serpentine lled layout
20 return (y + 1) * width - 1 - x;
21 else
22 return y * width + x;
23}
24
26 u16 width, u16 height) FL_NO_EXCEPT {
27 (void)height;
28 return y * width + x;
29}
30
31// typedef for xyMap function type
32typedef u16 (*XYFunction)(u16 x, u16 y, u16 width,
33 u16 height);
34
35// Maps x,y -> led index
36//
37// The common output led matrix you can buy on amazon is in a serpentine layout.
38//
39// XYMap allows you do to do graphic calculations on an LED layout as if it were
40// a grid.
41class XYMap {
42 public:
44
47 u16 offset = 0) FL_NO_EXCEPT;
48
50 u16 offset = 0) FL_NO_EXCEPT;
51
52 // This isn't working right, but the user function works just fine. The discussion
53 // is here:
54 // https://www.reddit.com/r/FastLED/comments/1kwwvcd/using_screenmap_with_nonstandard_led_layouts/
55 // Remember that this is open source software so if you want to fix it, go for it.
57 const u16 *lookUpTable,
58 u16 offset = 0) FL_NO_EXCEPT;
59
60 static XYMap constructSerpentine(u16 width, u16 height,
61 u16 offset = 0) FL_NO_EXCEPT;
62
66 static XYMap fromXMap(const XMap& xmap) FL_NO_EXCEPT;
67
71
72 // is_serpentine is true by default. You probably want this unless you are
73 // using a different layout
74 XYMap(u16 width, u16 height, bool is_serpentine = true,
75 u16 offset = 0) FL_NO_EXCEPT;
76
77 XYMap(const XYMap &other) FL_NO_EXCEPT = default;
78 XYMap &operator=(const XYMap &other) FL_NO_EXCEPT = default;
79
81
82 void mapPixels(const CRGB *input, CRGB *output) const FL_NO_EXCEPT;
83
85
87
88 u16 operator()(u16 x, u16 y) const FL_NO_EXCEPT {
89 return mapToIndex(x, y);
90 }
91
92 u16 mapToIndex(const u16 &x, const u16 &y) const FL_NO_EXCEPT;
93
94 template <typename IntType,
96 u16 mapToIndex(IntType x, IntType y) const FL_NO_EXCEPT {
97 x = fl::clamp<int>(x, 0, width - 1);
98 y = fl::clamp<int>(y, 0, height - 1);
99 return mapToIndex((u16)x, (u16)y);
100 }
101
102 bool has(u16 x, u16 y) const FL_NO_EXCEPT {
103 return (x < width) && (y < height);
104 }
105
106 bool has(int x, int y) const FL_NO_EXCEPT {
107 return (x >= 0) && (y >= 0) && has((u16)x, (u16)y);
108 }
109
110 bool isSerpentine() const FL_NO_EXCEPT { return type == kSerpentine; }
111 bool isLineByLine() const FL_NO_EXCEPT { return type == kLineByLine; }
112 bool isFunction() const FL_NO_EXCEPT { return type == kFunction; }
113 bool isLUT() const FL_NO_EXCEPT { return type == kLookUpTable; }
114 bool isRectangularGrid() const FL_NO_EXCEPT { return type == kLineByLine; }
116 return type == kSerpentine || type == kLineByLine;
117 }
118
119 u16 getWidth() const FL_NO_EXCEPT;
120 u16 getHeight() const FL_NO_EXCEPT;
121 u16 getTotal() const FL_NO_EXCEPT;
123
124 private:
126
128 u16 width;
131 fl::LUT16Ptr mLookUpTable; // optional refptr to look up table.
132 u16 mOffset = 0; // offset to be added to the output
133};
134
135} // namespace fl
bool has(u16 x, u16 y) const FL_NO_EXCEPT
Definition xymap.h:102
XYFunction xyFunction
Definition xymap.h:130
bool isFunction() const FL_NO_EXCEPT
Definition xymap.h:112
fl::ScreenMap toScreenMap() const FL_NO_EXCEPT
Definition xymap.cpp.hpp:12
u16 getHeight() const FL_NO_EXCEPT
void mapPixels(const CRGB *input, CRGB *output) const FL_NO_EXCEPT
Definition xymap.cpp.hpp:93
bool isSerpentineOrLineByLine() const FL_NO_EXCEPT
Definition xymap.h:115
void setRectangularGrid() FL_NO_EXCEPT
static XYMap constructSerpentine(u16 width, u16 height, u16 offset=0) FL_NO_EXCEPT
Definition xymap.cpp.hpp:60
static XYMap constructWithUserFunction(u16 width, u16 height, XYFunction xyFunction, u16 offset=0) FL_NO_EXCEPT
Definition xymap.cpp.hpp:27
u16 mOffset
Definition xymap.h:132
u16 mapToIndex(IntType x, IntType y) const FL_NO_EXCEPT
Definition xymap.h:96
bool isLUT() const FL_NO_EXCEPT
Definition xymap.h:113
bool has(int x, int y) const FL_NO_EXCEPT
Definition xymap.h:106
u16 mapToIndex(const u16 &x, const u16 &y) const FL_NO_EXCEPT
bool isLineByLine() const FL_NO_EXCEPT
Definition xymap.h:111
bool isSerpentine() const FL_NO_EXCEPT
Definition xymap.h:110
u16 getTotal() const FL_NO_EXCEPT
u16 width
Definition xymap.h:128
XyMapType
Definition xymap.h:43
@ kSerpentine
Definition xymap.h:43
@ kFunction
Definition xymap.h:43
@ kLineByLine
Definition xymap.h:43
@ kLookUpTable
Definition xymap.h:43
static XYMap identity(u16 width, u16 height) FL_NO_EXCEPT
Definition xymap.h:68
u16 getWidth() const FL_NO_EXCEPT
XyMapType getType() const FL_NO_EXCEPT
static XYMap constructRectangularGrid(u16 width, u16 height, u16 offset=0) FL_NO_EXCEPT
Definition xymap.cpp.hpp:35
void convertToLookUpTable() FL_NO_EXCEPT
static XYMap constructWithLookUpTable(u16 width, u16 height, const u16 *lookUpTable, u16 offset=0) FL_NO_EXCEPT
Definition xymap.cpp.hpp:42
u16 height
Definition xymap.h:129
bool isRectangularGrid() const FL_NO_EXCEPT
Definition xymap.h:114
fl::LUT16Ptr mLookUpTable
Definition xymap.h:131
static XYMap fromXMap(const XMap &xmap) FL_NO_EXCEPT
Create an XYMap from an XMap (treats 1D as 2D with height=1)
Definition xymap.cpp.hpp:67
XYMap(u16 width, u16 height, bool is_serpentine=true, u16 offset=0) FL_NO_EXCEPT
Definition xymap.cpp.hpp:88
XyMapType type
Definition xymap.h:127
#define FASTLED_FORCE_INLINE
fl::UISlider offset("Offset", 0.0f, 0.0f, 1.0f, 0.01f)
u8 u8 height
Definition blur.h:186
constexpr enable_if< is_fixed_point< T >::value, T >::type clamp(T x, T lo, T hi) FL_NO_EXCEPT
u16(* XYFunction)(u16 x, u16 y, u16 width, u16 height)
Definition xymap.h:32
u8 width
Definition blur.h:186
InputGamut g FL_NO_EXCEPT
Definition rgbw.h:121
FASTLED_FORCE_INLINE u16 xy_line_by_line(u16 x, u16 y, u16 width, u16 height) FL_NO_EXCEPT
Definition xymap.h:25
FASTLED_FORCE_INLINE u16 xy_serpentine(u16 x, u16 y, u16 width, u16 height) FL_NO_EXCEPT
Definition xymap.h:15
typename enable_if< Condition, T >::type enable_if_t
Definition type_traits.h:66
Base definition for an LED controller.
Definition crgb.hpp:179
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38