Skip to content

Commit 6116050

Browse files
committed
flame test
1 parent 3ab4780 commit 6116050

File tree

2 files changed

+73
-6
lines changed

2 files changed

+73
-6
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ one indexed led access
2424

2525
mosquitto_pub -t 'esp/curvy/pixels/one' -m '{"index":0,"red":0,"green":20,"blue":30}'
2626
## list
27-
list with a color for every led
27+
list with a color for every led in raw : [r0,g0,b0,r1,g1,b1,...]
2828

2929
570 bytes buffer required instead of 1080 for multiple r,g,b json objects
3030

@@ -58,3 +58,8 @@ mosquitto_pub -t 'esp/curvy/panel' -m '{"action":"wave", "duration_ms":10000,"fr
5858
# brightness
5959

6060
mosquitto_pub -t 'esp/curvy/brightness' -m '2'
61+
62+
# flame
63+
64+
mosquitto_pub -t 'esp/curvy/flame' -m 'burn'
65+
mosquitto_pub -t 'esp/curvy/flame' -m '{"r":226, "g":121, "b":35, "random":55, "period":20000, "duration_ms":5000}'

rgb_led/main/app_main.cpp

+67-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <stddef.h>
44
#include <string.h>
55
#include <list>
6+
#include <stdlib.h>
67
#include "esp_wifi.h"
78
#include "esp_system.h"
89
#include "nvs_flash.h"
@@ -38,9 +39,11 @@ static const char* TOPIC_LINES_GRAD = "esp/curvy/lines/grad";
3839
static const char* TOPIC_PANEL = "esp/curvy/panel";
3940
static const char* TOPIC_BRIGHTNESS = "esp/curvy/brightness";
4041
static const char* TOPIC_STATUS = "esp/curvy/status";
42+
static const char* TOPIC_FLAME = "esp/curvy/flame";
4143
static const char* TOPIC_SUB = "esp/curvy/#";
4244

4345

46+
esp_timer_handle_t periodic_timer;
4447

4548
static EventGroupHandle_t wifi_event_group;
4649
const static int CONNECTED_BIT = BIT0;
@@ -52,6 +55,9 @@ float g_brightness = 1.0;
5255
const gpio_num_t BLUE_LED=(gpio_num_t)2;
5356
const gpio_num_t RGB_GPIO=(gpio_num_t)13;
5457

58+
//static const uint8_t g_nb_led = 24;
59+
static const uint16_t g_nb_led = 256;
60+
5561
struct grad_t{
5662
uint8_t start_red ;
5763
uint8_t start_green ;
@@ -72,7 +78,14 @@ struct action_wave_t{
7278
bool is_wavelet;
7379
};
7480

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 };
7689

7790
class action_t{
7891
public:
@@ -85,6 +98,7 @@ class action_t{
8598
union{
8699
action_flash_t flash;
87100
action_wave_t wave;
101+
action_flame_t flame;
88102
};
89103
};
90104

@@ -96,6 +110,7 @@ class animation_t{
96110
void add_flash(action_flash_t &v_flash,int v_duration_ms);
97111
void add_wave(action_wave_t &v_wave,int v_duration_ms);
98112
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);
99114
public:
100115
bool enabled;
101116
WS2812* leds;
@@ -143,6 +158,22 @@ bool action_t::run(WS2812* leds,int delay_ms)
143158
ESP_LOGD(TAG, "ANIMATION> wave time %0.2f",t);
144159
}
145160
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;
146177
default:
147178
break;
148179
}
@@ -180,6 +211,17 @@ void animation_t::add_wave(action_wave_t &v_wave,int v_duration_ms)
180211
enabled = true;
181212
}
182213

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+
183225
void animation_t::kill()
184226
{
185227
actions.clear();
@@ -215,9 +257,6 @@ void animation_t::run()
215257

216258
static void animation_timer_callback(void* arg);
217259

218-
//static const uint8_t g_nb_led = 24;
219-
static const uint16_t g_nb_led = 256;
220-
221260
WS2812 my_rgb(RGB_GPIO,g_nb_led,8);
222261

223262
animation_t animation(&my_rgb);
@@ -234,7 +273,6 @@ void timers_init()
234273

235274
esp_timer_create_args_t periodic_timer_args;
236275
periodic_timer_args.callback = &animation_timer_callback;
237-
esp_timer_handle_t periodic_timer;
238276
ESP_ERROR_CHECK(esp_timer_create(&periodic_timer_args, &periodic_timer));
239277
ESP_ERROR_CHECK(esp_timer_start_periodic(periodic_timer, 20000));
240278

@@ -386,6 +424,29 @@ void led_set_brightness(const char * payload,int len)
386424
}
387425
}
388426

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+
389450
void json_led_set_panel(const char * payload,int len)
390451
{
391452
ArduinoJson::StaticJsonBuffer<600> jsonBuffer;
@@ -500,6 +561,7 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event)
500561
else if (match_and_call(event,TOPIC_GRAD, &json_led_set_grad)) {}
501562
else if (match_and_call(event,TOPIC_PANEL,&json_led_set_panel)) {}
502563
else if (match_and_call(event,TOPIC_BRIGHTNESS,&led_set_brightness)) {}
564+
else if (match_and_call(event,TOPIC_FLAME,&led_test_flame)) {}
503565
else
504566
{
505567
ESP_LOGI(TAG, "MQTT> unhandled topic len=%d", event->topic_len);

0 commit comments

Comments
 (0)