FastLED 3.9.3
Loading...
Searching...
No Matches
screenmap.cpp
1/* Screenmap maps strip indexes to x,y coordinates. This is used for FastLED.js
2 * to map the 1D strip to a 2D grid. Note that the strip can have arbitrary
3 * size. this was first motivated during the (attempted? Oct. 19th 2024) port of
4 * the Chromancer project to FastLED.js.
5 */
6
7#include "screenmap.h"
8
9#include "str.h"
10#include "fixed_map.h"
11#include "json.h"
12#include "namespace.h"
13#include "fixed_vector.h"
14#include "math_macros.h"
15#include "math.h"
16
17FASTLED_NAMESPACE_BEGIN
18
19ScreenMap ScreenMap::Circle(int numLeds, float cm_between_leds, float cm_led_diameter) {
20 ScreenMap screenMap = ScreenMap(numLeds);
21 float circumference = numLeds * cm_between_leds;
22 float radius = circumference / (2 * PI);
23
24 for (int i = 0; i < numLeds; i++) {
25 float angle = i * 2 * PI / numLeds;
26 float x = radius * cos(angle) * 2;
27 float y = radius * sin(angle) * 2;
28 screenMap[i] = {x, y};
29 }
30 screenMap.setDiameter(cm_led_diameter);
31 return screenMap;
32}
33
34
35void ScreenMap::ParseJson(const char *jsonStrScreenMap,
36 FixedMap<Str, ScreenMap, 16> *segmentMaps) {
37 FLArduinoJson::JsonDocument doc;
38 FLArduinoJson::deserializeJson(doc, jsonStrScreenMap);
39 auto map = doc["map"];
40 for (auto kv : map.as<FLArduinoJson::JsonObject>()) {
41 auto segment = kv.value();
42 auto x = segment["x"];
43 auto y = segment["y"];
44 auto obj = segment["diameter"];
45 float diameter = -1.0f;
46 if (obj.is<float>()) {
47 float d = obj.as<float>();
48 if (d > 0.0f) {
49 diameter = d;
50 }
51 }
52 auto n = x.size();
53 ScreenMap segment_map(n, diameter);
54 for (uint16_t j = 0; j < n; j++) {
55 segment_map.set(j, pair_xy_float{x[j], y[j]});
56 }
57 segmentMaps->insert(kv.key().c_str(), segment_map);
58 }
59}
60
61void ScreenMap::toJson(const FixedMap<Str, ScreenMap, 16>& segmentMaps, FLArduinoJson::JsonDocument* _doc) {
62 auto& doc = *_doc;
63 auto map = doc["map"].to<FLArduinoJson::JsonObject>();
64 for (auto kv : segmentMaps) {
65 auto segment = map[kv.first].to<FLArduinoJson::JsonObject>();
66 auto x_array = segment["x"].to<FLArduinoJson::JsonArray>();
67 auto y_array = segment["y"].to<FLArduinoJson::JsonArray>();
68 for (uint16_t i = 0; i < kv.second.getLength(); i++) {
69 const pair_xy_float& xy = kv.second[i];
70 x_array.add(xy.x);
71 y_array.add(xy.y);
72 }
73 float diameter = kv.second.getDiameter();
74 if (diameter < 0.0f) {
75 diameter = .5f; // 5mm.
76 }
77 if (diameter > 0.0f) {
78 segment["diameter"] = diameter;
79 }
80 }
81}
82
83void ScreenMap::toJsonStr(const FixedMap<Str, ScreenMap, 16>& segmentMaps, Str* jsonBuffer) {
84 FLArduinoJson::JsonDocument doc;
85 toJson(segmentMaps, &doc);
86 FLArduinoJson::serializeJson(doc, *jsonBuffer);
87}
88
89FASTLED_NAMESPACE_END
Definition str.h:234
Definition lut.h:17