FastLED 3.9.12
Loading...
Searching...
No Matches
xymap.h
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/ptr.h"
10#include "fl/xmap.h" // Include xmap.h for LUT16
11#include "fl/namespace.h"
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:
40 enum XyMapType { kSerpentine = 0, kLineByLine, kFunction, kLookUpTable };
41
42 static XYMap constructWithUserFunction(uint16_t width, uint16_t height,
43 XYFunction xyFunction,
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 // is_serpentine is true by default. You probably want this unless you are
54 // using a different layout
55 XYMap(uint16_t width, uint16_t height, bool is_serpentine = true,
56 uint16_t offset = 0);
57
58 XYMap(const XYMap &other) = default;
59 XYMap &operator=(const XYMap &other) = default;
60
61 fl::ScreenMap toScreenMap() const;
62
63 void mapPixels(const CRGB *input, CRGB *output) const;
64
65 void convertToLookUpTable();
66
67 void setRectangularGrid();
68
69 uint16_t operator()(uint16_t x, uint16_t y) const {
70 return mapToIndex(x, y);
71 }
72
73 uint16_t mapToIndex(uint16_t x, uint16_t y) const;
74 uint16_t mapToIndex(int x, int y) const {
75 if (x < 0) { x = 0; }
76 else if (uint16_t(x) >= width) { x = width - 1; }
77 if (y < 0) { y = 0; }
78 else if (uint16_t(y) >= height) { y = height - 1; }
79 return mapToIndex((uint16_t)x, (uint16_t)y);
80 }
81
82 uint16_t getWidth() const;
83 uint16_t getHeight() const;
84 uint16_t getTotal() const;
85 XyMapType getType() const;
86
87 private:
88 XYMap(uint16_t width, uint16_t height, XyMapType type);
89
90 XyMapType type;
91 uint16_t width;
92 uint16_t height;
93 XYFunction xyFunction = nullptr;
94 fl::LUT16Ptr mLookUpTable; // optional refptr to look up table.
95 uint16_t mOffset = 0; // offset to be added to the output
96};
97
98} // namespace fl
Defines the red, green, and blue (RGB) pixel struct.
Implements the FastLED namespace macros.
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