ESP32 Native version of Blinky, featureful controller code for WS2811/WS2812/NeoPixels
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

143 líneas
3.7KB

  1. static const char *TAG = "presets";
  2. #include <esp_log.h>
  3. #include <cJSON.h>
  4. #include "presets.hpp"
  5. #include "utils.hpp"
  6. /* Sadly, need to define these here */
  7. extern Pattern* json_gradient_pattern(cJSON *pattern, const Presets::ColorMap &colors);
  8. extern Pattern* json_random_pattern(cJSON *pattern, const Presets::ColorMap &colors);
  9. extern Pattern* json_sparkle_pattern(cJSON *pattern, const Presets::ColorMap &colors);
  10. namespace Presets {
  11. /* Factory */
  12. typedef Pattern* (*PatternGenerator)(cJSON*, const Presets::ColorMap&);
  13. static const std::map <std::string, PatternGenerator> generators {
  14. {"gradient", json_gradient_pattern},
  15. {"random", json_random_pattern},
  16. {"sparkle", json_sparkle_pattern},
  17. };
  18. /* Preset Map */
  19. std::map <std::string, const Pattern*> map;
  20. const Pattern* find(const std::string &name) {
  21. auto e = map.find(name);
  22. return e == map.end() ? NULL : e->second;
  23. }
  24. std::map<std::string, const Pattern*>::const_iterator map_begin() {
  25. return map.begin();
  26. }
  27. std::map<std::string, const Pattern*>::const_iterator map_end() {
  28. return map.end();
  29. }
  30. static inline cJSON* load_json(const char *path) {
  31. return cJSON_Parse(read_file(path).c_str());
  32. }
  33. /* Preset Loader */
  34. static Color json_raw_color(cJSON *json) {
  35. if (json->type != cJSON_Array) {
  36. ESP_LOGE(TAG, "Not an array: %s", json->string);
  37. return {0, 0, 0};
  38. }
  39. int count = cJSON_GetArraySize(json);
  40. if (count != 3) {
  41. ESP_LOGE(TAG, "Wrong size for %s: %d", json->string, count);
  42. return {0, 0, 0};
  43. }
  44. // TODO: Number checks for array elements?
  45. return Color{
  46. (uint8_t)cJSON_GetArrayItem(json, 0)->valueint,
  47. (uint8_t)cJSON_GetArrayItem(json, 1)->valueint,
  48. (uint8_t)cJSON_GetArrayItem(json, 2)->valueint
  49. };
  50. }
  51. Color json_color(cJSON *json, const ColorMap &colors) {
  52. if (json->type == cJSON_Array) return json_raw_color(json);
  53. if (json->type != cJSON_String) {
  54. ESP_LOGE(TAG, "Not a string: %s", json->string);
  55. return {0, 0, 0};
  56. }
  57. auto iter = colors.find(json->valuestring);
  58. if (iter == colors.end()) {
  59. ESP_LOGE(TAG, "Not a color: %s", json->valuestring);
  60. return {0, 0, 0};
  61. }
  62. return iter->second;
  63. }
  64. static Pattern* json_pattern(cJSON *json, const ColorMap &colors) {
  65. if (json->type != cJSON_Object) {
  66. ESP_LOGE(TAG, "Not an object: %s", json->string);
  67. return NULL;
  68. }
  69. cJSON *pattern = json->child;
  70. std::string type(pattern->string);
  71. auto iter = generators.find(type);
  72. if (iter == generators.end()) {
  73. ESP_LOGE(TAG, "Unknown pattern type: %s", type.c_str());
  74. return NULL;
  75. }
  76. return iter->second(pattern, colors);
  77. }
  78. void reload() {
  79. if (generators.empty()) {
  80. ESP_LOGE(TAG, "No pattern generators defined!");
  81. return;
  82. }
  83. ColorMap colors;
  84. cJSON *jcolors = load_json("/spiffs/colors.json");
  85. if (!jcolors) {
  86. ESP_LOGW(TAG, "No preset colors!");
  87. } else {
  88. cJSON *jcolor;
  89. cJSON_ArrayForEach(jcolor, jcolors) {
  90. Color color = json_color(jcolor, colors);
  91. colors[jcolor->string] = color;
  92. }
  93. cJSON_Delete(jcolors);
  94. }
  95. for (const auto &k_v : map) delete k_v.second;
  96. map.clear();
  97. cJSON *jpresets = load_json("/spiffs/presets.json");
  98. if (!jpresets) {
  99. ESP_LOGE(TAG, "No preset patterns!");
  100. } else {
  101. cJSON *jpreset;
  102. cJSON_ArrayForEach(jpreset, jpresets) {
  103. Pattern *pattern = json_pattern(jpreset, colors);
  104. if (pattern) {
  105. ESP_LOGI(TAG, "Creating preset: %s", jpreset->string);
  106. map[jpreset->string] = pattern;
  107. }
  108. }
  109. cJSON_Delete(jpresets);
  110. }
  111. }
  112. } // Presets