MCPcopy Create free account
hub / github.com/FastLED/FastLED / toJson

Method toJson

src/fl/math/screenmap.cpp.hpp:369–445  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

367}
368
369void ScreenMap::toJson(const fl::flat_map<string, ScreenMap> &segmentMaps,
370 fl::json *doc) {
371
372#if FASTLED_NO_JSON
373 FL_WARN("ScreenMap::toJson called with FASTLED_NO_JSON");
374 return;
375#else
376 if (!doc) {
377 FL_WARN("ScreenMap::toJson called with nullptr doc");
378 return;
379 }
380
381 // Emits the v2 screenmap shape (issue ledmapper#143):
382 // { "version": 2,
383 // "groups": { "<name>": { "color": "#hex" } },
384 // "segments": [ { "id": "<name>", "pin": "pin1", "group": "<name>",
385 // "x": [...], "y": [...], "diameter": ... } ] }
386 // Bilingual readers (`ScreenMap::ParseJson`, ledmapper) accept both v1
387 // and v2, so any existing on-disk v1 JSON keeps loading. v1 emission
388 // is no longer supported.
389 *doc = fl::json::object();
390
391 fl::json groupsObj = fl::json::object();
392 fl::json segmentsArr = fl::json::array();
393
394 // Distinct palette so each strip lights up differently in the editor
395 // preview without the user having to pick a colour. Cycle on overflow.
396 static const char *const kPalette[] = {
397 "#3b82f6", "#10b981", "#f59e0b", "#ef4444",
398 "#a855f7", "#06b6d4", "#ec4899", "#84cc16",
399 };
400 constexpr size_t kPaletteSize = sizeof(kPalette) / sizeof(kPalette[0]);
401
402 size_t idx = 0;
403 for (const auto& kv : segmentMaps) {
404 if (kv.second.getLength() == 0) {
405 FL_WARN("ScreenMap::toJson called with empty segment: " << fl::string(kv.first));
406 continue;
407 }
408
409 const auto& name = kv.first;
410 const auto& segment = kv.second;
411 const float diameter = segment.getDiameter();
412
413 fl::json xArray = fl::json::array();
414 for (u16 i = 0; i < segment.getLength(); i++) {
415 xArray.push_back(fl::json(static_cast<double>(segment[i].x)));
416 }
417 fl::json yArray = fl::json::array();
418 for (u16 i = 0; i < segment.getLength(); i++) {
419 yArray.push_back(fl::json(static_cast<double>(segment[i].y)));
420 }
421
422 fl::json groupObj = fl::json::object();
423 groupObj.set("color", fl::json(fl::string(kPalette[idx % kPaletteSize])));
424 groupsObj.set(name, groupObj);
425
426 fl::json segmentObj = fl::json::object();

Callers

nothing calls this directly

Calls 8

getDiameterMethod · 0.80
stringClass · 0.70
jsonClass · 0.70
arrayClass · 0.50
getLengthMethod · 0.45
push_backMethod · 0.45
setMethod · 0.45
to_stringMethod · 0.45

Tested by

no test coverage detected