FastLED 3.9.15
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_NOEXCEPT {
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_NOEXCEPT {
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_NOEXCEPT;
48
50 u16 offset = 0) FL_NOEXCEPT;
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_NOEXCEPT;
59
60 static XYMap constructSerpentine(u16 width, u16 height,
61 u16 offset = 0) FL_NOEXCEPT;
62
66 static XYMap fromXMap(const XMap& xmap) FL_NOEXCEPT;
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_NOEXCEPT;
76
77 XYMap(const XYMap &other) FL_NOEXCEPT = default;
78 XYMap &operator=(const XYMap &other) FL_NOEXCEPT = default;
79
81
82 void mapPixels(const CRGB *input, CRGB *output) const FL_NOEXCEPT;
83
85
87
88 u16 operator()(u16 x, u16 y) const FL_NOEXCEPT {
89 return mapToIndex(x, y);
90 }
91
92 u16 mapToIndex(const u16 &x, const u16 &y) const FL_NOEXCEPT;
93
94 template <typename IntType,
96 u16 mapToIndex(IntType x, IntType y) const FL_NOEXCEPT {
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_NOEXCEPT {
103 return (x < width) && (y < height);
104 }
105
106 bool has(int x, int y) const FL_NOEXCEPT {
107 return (x >= 0) && (y >= 0) && has((u16)x, (u16)y);
108 }
109
110 bool isSerpentine() const FL_NOEXCEPT { return type == kSerpentine; }
111 bool isLineByLine() const FL_NOEXCEPT { return type == kLineByLine; }
112 bool isFunction() const FL_NOEXCEPT { return type == kFunction; }
113 bool isLUT() const FL_NOEXCEPT { return type == kLookUpTable; }
114 bool isRectangularGrid() const FL_NOEXCEPT { return type == kLineByLine; }
116 return type == kSerpentine || type == kLineByLine;
117 }
118
119 u16 getWidth() const FL_NOEXCEPT;
120 u16 getHeight() const FL_NOEXCEPT;
121 u16 getTotal() const FL_NOEXCEPT;
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
u16 mapToIndex(const u16 &x, const u16 &y) const FL_NOEXCEPT
fl::ScreenMap toScreenMap() const FL_NOEXCEPT
Definition xymap.cpp.hpp:12
XYFunction xyFunction
Definition xymap.h:130
static XYMap constructWithUserFunction(u16 width, u16 height, XYFunction xyFunction, u16 offset=0) FL_NOEXCEPT
Definition xymap.cpp.hpp:27
u16 getWidth() const FL_NOEXCEPT
u16 getHeight() const FL_NOEXCEPT
bool has(u16 x, u16 y) const FL_NOEXCEPT
Definition xymap.h:102
void setRectangularGrid() FL_NOEXCEPT
u16 mOffset
Definition xymap.h:132
u16 mapToIndex(IntType x, IntType y) const FL_NOEXCEPT
Definition xymap.h:96
void convertToLookUpTable() FL_NOEXCEPT
Definition xymap.cpp.hpp:95
bool isFunction() const FL_NOEXCEPT
Definition xymap.h:112
bool isLineByLine() const FL_NOEXCEPT
Definition xymap.h:111
bool isSerpentine() const FL_NOEXCEPT
Definition xymap.h:110
bool has(int x, int y) const FL_NOEXCEPT
Definition xymap.h:106
static XYMap fromXMap(const XMap &xmap) FL_NOEXCEPT
Create an XYMap from an XMap (treats 1D as 2D with height=1)
Definition xymap.cpp.hpp:60
bool isRectangularGrid() const FL_NOEXCEPT
Definition xymap.h:114
static XYMap identity(u16 width, u16 height) FL_NOEXCEPT
Definition xymap.h:68
static XYMap constructSerpentine(u16 width, u16 height, u16 offset=0) FL_NOEXCEPT
Definition xymap.cpp.hpp:53
void mapPixels(const CRGB *input, CRGB *output) const FL_NOEXCEPT
Definition xymap.cpp.hpp:85
u16 width
Definition xymap.h:128
u16 getTotal() const FL_NOEXCEPT
XyMapType getType() const FL_NOEXCEPT
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
bool isSerpentineOrLineByLine() const FL_NOEXCEPT
Definition xymap.h:115
bool isLUT() const FL_NOEXCEPT
Definition xymap.h:113
u16 height
Definition xymap.h:129
fl::LUT16Ptr mLookUpTable
Definition xymap.h:131
static XYMap constructWithLookUpTable(u16 width, u16 height, const u16 *lookUpTable, u16 offset=0) FL_NOEXCEPT
Definition xymap.cpp.hpp:42
XyMapType type
Definition xymap.h:127
static XYMap constructRectangularGrid(u16 width, u16 height, u16 offset=0) FL_NOEXCEPT
Definition xymap.cpp.hpp:35
XYMap(u16 width, u16 height, bool is_serpentine=true, u16 offset=0) FL_NOEXCEPT
Definition xymap.cpp.hpp:80
fl::UISlider offset("Offset", 0.0f, 0.0f, 1.0f, 0.01f)
typename enable_if< Condition, T >::type enable_if_t
Definition s16x16x4.h:66
u8 u8 height
Definition blur.h:186
FASTLED_FORCE_INLINE u16 xy_line_by_line(u16 x, u16 y, u16 width, u16 height) FL_NOEXCEPT
Definition xymap.h:25
FASTLED_FORCE_INLINE u16 xy_serpentine(u16 x, u16 y, u16 width, u16 height) FL_NOEXCEPT
Definition xymap.h:15
u16(* XYFunction)(u16 x, u16 y, u16 width, u16 height)
Definition xymap.h:32
u8 width
Definition blur.h:186
constexpr enable_if< is_fixed_point< T >::value, T >::type clamp(T x, T lo, T hi) FL_NOEXCEPT
Base definition for an LED controller.
Definition crgb.hpp:179
#define FASTLED_FORCE_INLINE
#define FL_NOEXCEPT
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38