ESP32 Native version of Blinky, featureful controller code for WS2811/WS2812/NeoPixels
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

133 строки
3.7KB

  1. static const char *TAG = "random";
  2. #include <esp_log.h>
  3. #include <list>
  4. #include <cstring>
  5. #include <esp_random.h>
  6. #include "presets.hpp"
  7. #include "utils.hpp"
  8. static const int8_t ndist[64] = {-127, -104, -92, -84, -77, -72, -67, -62, -58, -55, -51, -48, -45, -42, -39, -37, -34, -32, -29, -27, -25, -22, -20, -18, -16, -14, -11, -9, -7, -5, -3, -1, 1, 3, 5, 7, 9, 11, 14, 16, 18, 20, 22, 25, 27, 29, 32, 34, 37, 39, 42, 45, 48, 51, 55, 58, 62, 67, 72, 77, 84, 92, 104, 127};
  9. int norm(int mean) {
  10. return (mean * 128) / ndist[esp_random() % 64];
  11. }
  12. struct Sparkle {
  13. Sparkle(int64_t time_us, int mean_dur_us, int len):
  14. start_us(time_us), end_us(time_us + (mean_dur_us)),
  15. pos(esp_random() % len) {}
  16. bool expired(int64_t time_us) { return time_us >= end_us; }
  17. uint8_t intensity_at(int64_t time_us) {
  18. if (time_us < start_us || time_us >= end_us) return 0;
  19. int64_t midpoint_us = (start_us + end_us) / 2;
  20. if (time_us >= midpoint_us) time_us = start_us + (end_us - time_us);
  21. return (((time_us - start_us) * 255) * 2) / (end_us - start_us);
  22. }
  23. int64_t start_us;
  24. int end_us;
  25. int pos;
  26. };
  27. Color fade(Color c, unsigned int i) {
  28. return {
  29. (uint8_t)((c.r * i) / 255),
  30. (uint8_t)((c.g * i) / 255),
  31. (uint8_t)((c.b * i) / 255)
  32. };
  33. }
  34. class SparklePattern : public Pattern {
  35. private:
  36. struct State : Pattern::State {
  37. std::list <Sparkle> sparkles;
  38. };
  39. public:
  40. SparklePattern(Color _color, int _duration_ms, int _delay_ms, int _density) :
  41. color(_color), duration_us(_duration_ms * 1000),
  42. delay_us(_delay_ms * 1000), density(_density)
  43. {}
  44. State* start(int len) const { return new State(); }
  45. void step(Color colors[], int len, Pattern::State *_state) const {
  46. State *state = (State*)_state;
  47. int64_t now_us = time_us();
  48. int64_t start_us = now_us;
  49. for ( int open = (len / density) + 1 - state->sparkles.size();
  50. open > 0;
  51. --open, start_us += delay_us
  52. ) {
  53. state->sparkles.push_front(Sparkle(start_us, duration_us, len));
  54. }
  55. memset(colors, 0, sizeof(*colors) * len);
  56. for (auto iter = state->sparkles.begin(); iter != state->sparkles.end(); ) {
  57. if (iter->expired(now_us)) {
  58. state->sparkles.erase(iter++);
  59. } else {
  60. colors[iter->pos] = fade(color, iter->intensity_at(now_us));
  61. ++iter;
  62. }
  63. }
  64. }
  65. private:
  66. Color color;
  67. int duration_us;
  68. int delay_us;
  69. int density;
  70. };
  71. Pattern* json_sparkle_pattern(cJSON *pattern, const Presets::ColorMap &colors) {
  72. int duration_ms = 250;
  73. int delay_ms = 3000;
  74. int density = 10;
  75. Color color = {255, 255, 255};
  76. if (pattern->type != cJSON_Object) {
  77. ESP_LOGE(TAG, "Not an object: %s", pattern->type);
  78. return NULL;
  79. }
  80. cJSON *jnum = cJSON_GetObjectItem(pattern, "delay");
  81. if (jnum) {
  82. if (jnum->type != cJSON_Number) {
  83. ESP_LOGE(TAG, "Not a number: %s", jnum->string);
  84. } else {
  85. delay_ms = jnum->valuedouble * 1000.;
  86. }
  87. }
  88. jnum = cJSON_GetObjectItem(pattern, "duration");
  89. if (jnum) {
  90. if (jnum->type != cJSON_Number) {
  91. ESP_LOGE(TAG, "Not a number: %s", jnum->string);
  92. } else {
  93. duration_ms = jnum->valuedouble * 1000.;
  94. }
  95. }
  96. jnum = cJSON_GetObjectItem(pattern, "density");
  97. if (jnum) {
  98. if (jnum->type != cJSON_Number) {
  99. ESP_LOGE(TAG, "Not a number: %s", jnum->string);
  100. } else {
  101. density = jnum->valuedouble;
  102. }
  103. }
  104. return new SparklePattern(color, duration_ms, delay_ms, density);
  105. }