ESP32 Native version of Blinky, featureful controller code for WS2811/WS2812/NeoPixels
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

141 lignes
3.8KB

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