3
3
#include < stddef.h>
4
4
#include < string.h>
5
5
#include < list>
6
+ #include < stdlib.h>
6
7
#include " esp_wifi.h"
7
8
#include " esp_system.h"
8
9
#include " nvs_flash.h"
@@ -38,9 +39,11 @@ static const char* TOPIC_LINES_GRAD = "esp/curvy/lines/grad";
38
39
static const char * TOPIC_PANEL = " esp/curvy/panel" ;
39
40
static const char * TOPIC_BRIGHTNESS = " esp/curvy/brightness" ;
40
41
static const char * TOPIC_STATUS = " esp/curvy/status" ;
42
+ static const char * TOPIC_FLAME = " esp/curvy/flame" ;
41
43
static const char * TOPIC_SUB = " esp/curvy/#" ;
42
44
43
45
46
+ esp_timer_handle_t periodic_timer;
44
47
45
48
static EventGroupHandle_t wifi_event_group;
46
49
const static int CONNECTED_BIT = BIT0;
@@ -52,6 +55,9 @@ float g_brightness = 1.0;
52
55
const gpio_num_t BLUE_LED=(gpio_num_t )2 ;
53
56
const gpio_num_t RGB_GPIO=(gpio_num_t )13 ;
54
57
58
+ // static const uint8_t g_nb_led = 24;
59
+ static const uint16_t g_nb_led = 256 ;
60
+
55
61
struct grad_t {
56
62
uint8_t start_red ;
57
63
uint8_t start_green ;
@@ -72,7 +78,14 @@ struct action_wave_t{
72
78
bool is_wavelet;
73
79
};
74
80
75
- enum class action_type_t { flash, wave, wavelet };
81
+ struct action_flame_t {
82
+ pixel_t color;
83
+ int length;
84
+ float freq;
85
+ int random;
86
+ };
87
+
88
+ enum class action_type_t { flash, wave, wavelet, flame };
76
89
77
90
class action_t {
78
91
public:
@@ -85,6 +98,7 @@ class action_t{
85
98
union {
86
99
action_flash_t flash;
87
100
action_wave_t wave;
101
+ action_flame_t flame;
88
102
};
89
103
};
90
104
@@ -96,6 +110,7 @@ class animation_t{
96
110
void add_flash (action_flash_t &v_flash,int v_duration_ms);
97
111
void add_wave (action_wave_t &v_wave,int v_duration_ms);
98
112
void add_wavelet (action_wave_t &v_wavelet,int v_duration_ms);
113
+ void add_flame (action_flame_t &v_flame,int v_duration_ms);
99
114
public:
100
115
bool enabled;
101
116
WS2812* leds;
@@ -143,6 +158,22 @@ bool action_t::run(WS2812* leds,int delay_ms)
143
158
ESP_LOGD (TAG, " ANIMATION> wave time %0.2f" ,t);
144
159
}
145
160
break ;
161
+ case action_type_t ::flame :
162
+ {
163
+ // Flicker, based on our initial RGB values
164
+ for (int i=0 ; i<g_nb_led; i++)
165
+ {
166
+ int flicker = rand () % flame.random ;
167
+ int r1 = flame.color .red -flicker;
168
+ int g1 = flame.color .green -flicker;
169
+ int b1 = flame.color .blue -flicker;
170
+ if (g1<0 ) g1=0 ;
171
+ if (r1<0 ) r1=0 ;
172
+ if (b1<0 ) b1=0 ;
173
+ leds->setPixel (i,r1,g1, b1);
174
+ }
175
+ }
176
+ break ;
146
177
default :
147
178
break ;
148
179
}
@@ -180,6 +211,17 @@ void animation_t::add_wave(action_wave_t &v_wave,int v_duration_ms)
180
211
enabled = true ;
181
212
}
182
213
214
+ void animation_t::add_flame (action_flame_t &v_flame,int v_duration_ms)
215
+ {
216
+ action_t flame_action;
217
+ flame_action.a_type = action_type_t ::flame;
218
+ flame_action.progress_ms = 0 ;
219
+ flame_action.duration_ms = v_duration_ms;
220
+ flame_action.flame = v_flame;
221
+ actions.push_back (flame_action);
222
+ enabled = true ;
223
+ }
224
+
183
225
void animation_t::kill ()
184
226
{
185
227
actions.clear ();
@@ -215,9 +257,6 @@ void animation_t::run()
215
257
216
258
static void animation_timer_callback (void * arg);
217
259
218
- // static const uint8_t g_nb_led = 24;
219
- static const uint16_t g_nb_led = 256 ;
220
-
221
260
WS2812 my_rgb (RGB_GPIO,g_nb_led,8 );
222
261
223
262
animation_t animation (&my_rgb);
@@ -234,7 +273,6 @@ void timers_init()
234
273
235
274
esp_timer_create_args_t periodic_timer_args;
236
275
periodic_timer_args.callback = &animation_timer_callback;
237
- esp_timer_handle_t periodic_timer;
238
276
ESP_ERROR_CHECK (esp_timer_create (&periodic_timer_args, &periodic_timer));
239
277
ESP_ERROR_CHECK (esp_timer_start_periodic (periodic_timer, 20000 ));
240
278
@@ -386,6 +424,29 @@ void led_set_brightness(const char * payload,int len)
386
424
}
387
425
}
388
426
427
+ void led_test_flame (const char * payload,int len)
428
+ {
429
+ ArduinoJson::StaticJsonBuffer<600 > jsonBuffer;
430
+ ArduinoJson::JsonObject& root = jsonBuffer.parseObject (payload);
431
+ if (!root.success ())
432
+ {
433
+ ESP_LOGE (TAG, " MQTT-JSON> Parsing error" );
434
+ return ;
435
+ }
436
+ animation.kill ();
437
+
438
+ action_flame_t flame;
439
+ int duration_ms = root[" duration_ms" ];
440
+ int period = root[" period" ];
441
+ flame.color .red = root[" r" ];
442
+ flame.color .green = root[" g" ];
443
+ flame.color .blue = root[" b" ];
444
+ flame.random = root[" random" ];
445
+ animation.add_flame (flame,duration_ms);
446
+ ESP_ERROR_CHECK (esp_timer_stop (periodic_timer));
447
+ ESP_ERROR_CHECK (esp_timer_start_periodic (periodic_timer, period));
448
+ }
449
+
389
450
void json_led_set_panel (const char * payload,int len)
390
451
{
391
452
ArduinoJson::StaticJsonBuffer<600 > jsonBuffer;
@@ -500,6 +561,7 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event)
500
561
else if (match_and_call (event,TOPIC_GRAD, &json_led_set_grad)) {}
501
562
else if (match_and_call (event,TOPIC_PANEL,&json_led_set_panel)) {}
502
563
else if (match_and_call (event,TOPIC_BRIGHTNESS,&led_set_brightness)) {}
564
+ else if (match_and_call (event,TOPIC_FLAME,&led_test_flame)) {}
503
565
else
504
566
{
505
567
ESP_LOGI (TAG, " MQTT> unhandled topic len=%d" , event->topic_len );
0 commit comments