7#include "fl/screenmap.h"
11#include "fl/math_macros.h"
12#include "fl/screenmap.h"
21ScreenMap ScreenMap::Circle(
int numLeds,
float cm_between_leds,
22 float cm_led_diameter) {
23 ScreenMap screenMap = ScreenMap(numLeds);
24 float circumference = numLeds * cm_between_leds;
25 float radius = circumference / (2 * PI);
27 for (
int i = 0; i < numLeds; i++) {
28 float angle = i * 2 * PI / numLeds;
29 float x = radius * cos(angle) * 2;
30 float y = radius * sin(angle) * 2;
31 screenMap[i] = {x, y};
33 screenMap.setDiameter(cm_led_diameter);
37bool ScreenMap::ParseJson(
const char *jsonStrScreenMap,
38 FixedMap<Str, ScreenMap, 16> *segmentMaps, Str *err) {
39#if !FASTLED_ENABLE_JSON
41 *err =
"JSON not enabled";
51 bool ok = parseJson(jsonStrScreenMap, &doc, err);
53 FASTLED_WARN(
"Failed to parse json: " << err->c_str());
56 auto map = doc[
"map"];
57 for (
auto kv : map.as<FLArduinoJson::JsonObject>()) {
58 auto segment = kv.value();
59 auto x = segment[
"x"];
60 auto y = segment[
"y"];
61 auto obj = segment[
"diameter"];
62 float diameter = -1.0f;
63 if (obj.is<
float>()) {
64 float d = obj.as<
float>();
70 ScreenMap segment_map(n, diameter);
71 for (uint16_t j = 0; j < n; j++) {
72 segment_map.set(j, pair_xy_float{x[j], y[j]});
74 segmentMaps->insert(kv.key().c_str(), segment_map);
80bool ScreenMap::ParseJson(
const char *jsonStrScreenMap,
81 const char *screenMapName, ScreenMap *screenmap,
83#if !FASTLED_ENABLE_JSON
85 *err =
"JSON not enabled";
89 FixedMap<Str, ScreenMap, 16> segmentMaps;
90 bool ok = ParseJson(jsonStrScreenMap, &segmentMaps, err);
94 if (segmentMaps.size() == 0) {
97 if (segmentMaps.has(screenMapName)) {
98 *screenmap = segmentMaps[screenMapName];
101 Str _err =
"ScreenMap not found: ";
102 _err.append(screenMapName);
106 FASTLED_WARN(_err.c_str());
111void ScreenMap::toJson(
const FixedMap<Str, ScreenMap, 16> &segmentMaps,
112 JsonDocument *_doc) {
114#if !FASTLED_ENABLE_JSON
118 auto map = doc[
"map"].to<FLArduinoJson::JsonObject>();
119 for (
auto kv : segmentMaps) {
120 auto segment = map[kv.first].to<FLArduinoJson::JsonObject>();
121 auto x_array = segment[
"x"].to<FLArduinoJson::JsonArray>();
122 auto y_array = segment[
"y"].to<FLArduinoJson::JsonArray>();
123 for (uint16_t i = 0; i < kv.second.getLength(); i++) {
124 const pair_xy_float &xy = kv.second[i];
128 float diameter = kv.second.getDiameter();
129 if (diameter < 0.0f) {
132 if (diameter > 0.0f) {
133 segment[
"diameter"] = diameter;
139void ScreenMap::toJsonStr(
const FixedMap<Str, ScreenMap, 16> &segmentMaps,
141#if !FASTLED_ENABLE_JSON
145 toJson(segmentMaps, &doc);
146 fl::toJson(doc, jsonBuffer);
150ScreenMap::ScreenMap(uint32_t length,
float mDiameter)
151 : length(length), mDiameter(mDiameter) {
152 mLookUpTable = LUTXYFLOATPtr::New(length);
153 LUTXYFLOAT &lut = *mLookUpTable.get();
154 pair_xy_float *data = lut.getData();
155 for (uint32_t x = 0; x < length; x++) {
160ScreenMap::ScreenMap(
const pair_xy_float *lut, uint32_t length,
float diameter)
161 : length(length), mDiameter(diameter) {
162 mLookUpTable = LUTXYFLOATPtr::New(length);
163 LUTXYFLOAT &lut16xy = *mLookUpTable.get();
164 pair_xy_float *data = lut16xy.getData();
165 for (uint32_t x = 0; x < length; x++) {
170ScreenMap::ScreenMap(
const ScreenMap &other) {
171 mDiameter = other.mDiameter;
172 length = other.length;
173 mLookUpTable = other.mLookUpTable;
176void ScreenMap::set(uint16_t index,
const pair_xy_float &p) {
178 LUTXYFLOAT &lut = *mLookUpTable.get();
179 auto *data = lut.getData();
184void ScreenMap::setDiameter(
float diameter) { mDiameter = diameter; }
186pair_xy_float ScreenMap::mapToIndex(uint32_t x)
const {
187 if (x >= length || !mLookUpTable) {
190 LUTXYFLOAT &lut = *mLookUpTable.get();
191 pair_xy_float screen_coords = lut[x];
192 return screen_coords;
195uint32_t ScreenMap::getLength()
const {
return length; }
197float ScreenMap::getDiameter()
const {
return mDiameter; }
199const pair_xy_float &ScreenMap::empty() {
200 static const pair_xy_float s_empty = pair_xy_float(0, 0);
204const pair_xy_float &ScreenMap::operator[](uint32_t x)
const {
205 if (x >= length || !mLookUpTable) {
208 LUTXYFLOAT &lut = *mLookUpTable.get();
212pair_xy_float &ScreenMap::operator[](uint32_t x) {
213 if (x >= length || !mLookUpTable) {
214 return const_cast<pair_xy_float &
>(empty());
216 LUTXYFLOAT &lut = *mLookUpTable.get();
217 auto *data = lut.getData();
221ScreenMap &ScreenMap::operator=(
const ScreenMap &other) {
222 if (
this != &other) {
223 mDiameter = other.mDiameter;
224 length = other.length;
225 mLookUpTable = other.mLookUpTable;
Implements the FastLED namespace macros.
Implements a simple red square effect for 2D LED grids.