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/force_inline.h"
8#include "fl/lut.h"
9#include "fl/namespace.h"
10#include "fl/ptr.h"
11#include "fl/xmap.h" // Include xmap.h for LUT16
12
13namespace fl {
14class ScreenMap;
15
16FASTLED_FORCE_INLINE uint16_t xy_serpentine(uint16_t x, uint16_t y,
17 uint16_t width, uint16_t height) {
18 (void)height;
19 if (y & 1) // Even or odd row?
20 // reverse every second line for a serpentine lled layout
21 return (y + 1) * width - 1 - x;
22 else
23 return y * width + x;
24}
25
26FASTLED_FORCE_INLINE uint16_t xy_line_by_line(uint16_t x, uint16_t y,
27 uint16_t width, uint16_t height) {
28 (void)height;
29 return y * width + x;
30}
31
32// typedef for xyMap function type
33typedef uint16_t (*XYFunction)(uint16_t x, uint16_t y, uint16_t width,
34 uint16_t height);
35
36// XYMap holds either a function or a look up table to map x, y coordinates to a
37// 1D index.
38class XYMap {
39 public:
41
42 static XYMap constructWithUserFunction(uint16_t width, uint16_t height,
44 uint16_t offset = 0);
45
46 static XYMap constructRectangularGrid(uint16_t width, uint16_t height,
47 uint16_t offset = 0);
48
49 static XYMap constructWithLookUpTable(uint16_t width, uint16_t height,
50 const uint16_t *lookUpTable,
51 uint16_t offset = 0);
52
53 static XYMap constructSerpentine(uint16_t width, uint16_t height,
54 uint16_t offset = 0);
55
56 // is_serpentine is true by default. You probably want this unless you are
57 // using a different layout
58 XYMap(uint16_t width, uint16_t height, bool is_serpentine = true,
59 uint16_t offset = 0);
60
61 XYMap(const XYMap &other) = default;
62 XYMap &operator=(const XYMap &other) = default;
63
65
66 void mapPixels(const CRGB *input, CRGB *output) const;
67
69
70 void setRectangularGrid();
71
72 uint16_t operator()(uint16_t x, uint16_t y) const {
73 return mapToIndex(x, y);
74 }
75
76 uint16_t mapToIndex(uint16_t x, uint16_t y) const;
77 uint16_t mapToIndex(int x, int y) const {
78 if (x < 0) {
79 x = 0;
80 } else if (uint16_t(x) >= width) {
81 x = width - 1;
82 }
83 if (y < 0) {
84 y = 0;
85 } else if (uint16_t(y) >= height) {
86 y = height - 1;
87 }
88 return mapToIndex((uint16_t)x, (uint16_t)y);
89 }
90
91 uint16_t getWidth() const;
92 uint16_t getHeight() const;
93 uint16_t getTotal() const;
94 XyMapType getType() const;
95
96 private:
97 XYMap(uint16_t width, uint16_t height, XyMapType type);
98
100 uint16_t width;
101 uint16_t height;
103 fl::LUT16Ptr mLookUpTable; // optional refptr to look up table.
104 uint16_t mOffset = 0; // offset to be added to the output
105};
106
107} // namespace fl
uint32_t x[NUM_LAYERS]
Definition Fire2023.ino:80
uint32_t y[NUM_LAYERS]
Definition Fire2023.ino:81
XyMapType getType() const
Definition xymap.cpp:127
XYFunction xyFunction
Definition xymap.h:102
uint16_t mOffset
Definition xymap.h:104
uint16_t width
Definition xymap.h:100
static XYMap constructWithLookUpTable(uint16_t width, uint16_t height, const uint16_t *lookUpTable, uint16_t offset=0)
Definition xymap.cpp:42
static XYMap constructSerpentine(uint16_t width, uint16_t height, uint16_t offset=0)
Definition xymap.cpp:53
uint16_t getWidth() const
Definition xymap.cpp:121
uint16_t operator()(uint16_t x, uint16_t y) const
Definition xymap.h:72
fl::ScreenMap toScreenMap() const
Definition xymap.cpp:14
XYMap(const XYMap &other)=default
void mapPixels(const CRGB *input, CRGB *output) const
Definition xymap.cpp:65
uint16_t mapToIndex(int x, int y) const
Definition xymap.h:77
void convertToLookUpTable()
Definition xymap.cpp:75
XYMap(uint16_t width, uint16_t height, bool is_serpentine=true, uint16_t offset=0)
Definition xymap.cpp:60
uint16_t mapToIndex(uint16_t x, uint16_t y) const
Definition xymap.cpp:96
XyMapType
Definition xymap.h:40
@ kSerpentine
Definition xymap.h:40
@ kFunction
Definition xymap.h:40
@ kLineByLine
Definition xymap.h:40
@ kLookUpTable
Definition xymap.h:40
static XYMap constructRectangularGrid(uint16_t width, uint16_t height, uint16_t offset=0)
Definition xymap.cpp:35
uint16_t getHeight() const
Definition xymap.cpp:123
uint16_t height
Definition xymap.h:101
XYMap & operator=(const XYMap &other)=default
fl::LUT16Ptr mLookUpTable
Definition xymap.h:103
void setRectangularGrid()
Definition xymap.cpp:90
static XYMap constructWithUserFunction(uint16_t width, uint16_t height, XYFunction xyFunction, uint16_t offset=0)
Definition xymap.cpp:27
uint16_t getTotal() const
Definition xymap.cpp:125
XyMapType type
Definition xymap.h:99
Defines the red, green, and blue (RGB) pixel struct.
#define FASTLED_FORCE_INLINE
Definition force_inline.h:7
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:16
FASTLED_FORCE_INLINE uint16_t xy_line_by_line(uint16_t x, uint16_t y, uint16_t width, uint16_t height)
Definition xymap.h:26
uint16_t(* XYFunction)(uint16_t x, uint16_t y, uint16_t width, uint16_t height)
Definition xymap.h:33
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:54