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