FastLED 3.9.3
Loading...
Searching...
No Matches
screenmap.h
1#pragma once
2
3#include <stdint.h>
4
5#include "force_inline.h"
6#include "lut.h"
7#include "ref.h"
8
9#include "str.h"
10#include "fixed_map.h"
11#include "json.h"
12#include "namespace.h"
13
14/* Screenmap maps strip indexes to x,y coordinates. This is used for FastLED.js
15 * to map the 1D strip to a 2D grid. Note that the strip can have arbitrary
16 * size. this was first motivated during the (attempted? Oct. 19th 2024) port of
17 * the Chromancer project to FastLED.js.
18 */
19
20
21
22FASTLED_NAMESPACE_BEGIN
23
24class Str;
25
26// ScreenMap screen map maps strip indexes to x,y coordinates for a ui
27// canvas in float format.
28// This class is cheap to copy as it uses smart pointers for shared data.
29class ScreenMap {
30 public:
31
32 static ScreenMap Circle(int numLeds, float cm_between_leds = 1.5f, float cm_led_diameter = 0.5f);
33
34 ScreenMap() = default;
35
36 // is_reverse is false by default for linear layout
37 ScreenMap(uint32_t length, float mDiameter = -1.0f) : length(length), mDiameter(mDiameter) {
38 mLookUpTable = LUTXYFLOATRef::New(length);
39 LUTXYFLOAT &lut = *mLookUpTable.get();
40 pair_xy_float *data = lut.getData();
41 for (uint32_t x = 0; x < length; x++) {
42 data[x] = {0, 0};
43 }
44 }
45
46 ScreenMap(const pair_xy_float *lut, uint32_t length, float diameter = -1.0) : length(length), mDiameter(diameter) {
47 mLookUpTable = LUTXYFLOATRef::New(length);
48 LUTXYFLOAT &lut16xy = *mLookUpTable.get();
49 pair_xy_float *data = lut16xy.getData();
50 for (uint32_t x = 0; x < length; x++) {
51 data[x] = lut[x];
52 }
53 }
54
55 template <uint32_t N> ScreenMap(const pair_xy_float (&lut)[N], float diameter = -1.0) : length(N), mDiameter(diameter) {
56 mLookUpTable = LUTXYFLOATRef::New(length);
57 LUTXYFLOAT &lut16xy = *mLookUpTable.get();
58 pair_xy_float *data = lut16xy.getData();
59 for (uint32_t x = 0; x < length; x++) {
60 data[x] = lut[x];
61 }
62 }
63
64 ScreenMap(const ScreenMap &other) {
65 mDiameter = other.mDiameter;
66 length = other.length;
67 mLookUpTable = other.mLookUpTable;
68 }
69
70 const pair_xy_float &operator[](uint32_t x) const {
71 if (x >= length || !mLookUpTable) {
72 return empty(); // better than crashing.
73 }
74 LUTXYFLOAT &lut = *mLookUpTable.get();
75 return lut[x];
76 }
77
78 void set(uint16_t index, const pair_xy_float &p) {
79 if (mLookUpTable) {
80 LUTXYFLOAT &lut = *mLookUpTable.get();
81 auto *data = lut.getData();
82 data[index] = p;
83 }
84 }
85
86 pair_xy_float& operator[](uint32_t x) {
87 if (x >= length || !mLookUpTable) {
88 return const_cast<pair_xy_float &>(empty()); // better than crashing.
89 }
90 LUTXYFLOAT &lut = *mLookUpTable.get();
91 auto *data = lut.getData();
92 return data[x];
93 }
94
95 // TODO: change this name to setDiameterLed. Default should be .5f
96 // for 5 mm ws lense.
97 void setDiameter(float diameter) { mDiameter = diameter; }
98
99 // define the assignment operator
100 ScreenMap &operator=(const ScreenMap &other) {
101 if (this != &other) {
102 mDiameter = other.mDiameter;
103 length = other.length;
104 mLookUpTable = other.mLookUpTable;
105 }
106 return *this;
107 }
108
109 pair_xy_float mapToIndex(uint32_t x) const {
110 if (x >= length || !mLookUpTable) {
111 return {0, 0};
112 }
113 LUTXYFLOAT &lut = *mLookUpTable.get();
114 pair_xy_float screen_coords = lut[x];
115 return screen_coords;
116 }
117
118 uint32_t getLength() const { return length; }
119 // The diameter each point represents.
120 float getDiameter() const { return mDiameter; }
121
122 static void ParseJson(const char *jsonStrScreenMap,
123 FixedMap<Str, ScreenMap, 16> *segmentMaps);
124
125 static void toJsonStr(const FixedMap<Str, ScreenMap, 16>&, Str* jsonBuffer);
126 static void toJson(const FixedMap<Str, ScreenMap, 16>&, FLArduinoJson::JsonDocument* doc);
127
128 private:
129 static const pair_xy_float &empty() {
130 static const pair_xy_float s_empty = pair_xy_float(0, 0);
131 return s_empty;
132 }
133 uint32_t length = 0;
134 float mDiameter = -1.0f; // Only serialized if it's not > 0.0f.
135 LUTXYFLOATRef mLookUpTable;
136};
137
138
139FASTLED_NAMESPACE_END
Definition lut.h:41
Definition str.h:234
Definition lut.h:17