From ead076f9a3b70ef6cd3c02c422cec7cbe779abe6 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Wed, 19 Jun 2019 22:28:38 +0200 Subject: [PATCH] You can now set options for effects via MQTT and HTTP. Basic syntax is: ,=,=,... --- include/Effect.h | 1 + src/effects.cpp | 35 ++++++++++++++++++++++++++++++++--- src/pitrix.cpp | 5 ++--- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/include/Effect.h b/include/Effect.h index de02f05..81c60f7 100644 --- a/include/Effect.h +++ b/include/Effect.h @@ -17,6 +17,7 @@ public: void setWindow(Window* win) { window = win; }; + virtual void apply_option(String key, String value) {}; }; #endif diff --git a/src/effects.cpp b/src/effects.cpp index fb6f641..fb3ba34 100644 --- a/src/effects.cpp +++ b/src/effects.cpp @@ -17,6 +17,7 @@ #include "effect_firework.h" #include "effect_gol.h" #include "effect_pixelclock.h" +#include "effect_dvd.h" Effect* current_effect; @@ -54,11 +55,39 @@ Effect* select_effect(uint32_t code) { }; } -bool change_current_effect(String name) { - Effect* new_effect = string_to_effect(name); - if (new_effect == NULL) return false; +bool change_current_effect(String payload) { + int pos = payload.indexOf(","); + String options = ""; + if (pos != -1) { + LOGln("Effects * Effect comes with options."); + options = payload.substring(pos+1); + payload.remove(pos); + LOGln("Effects * Cleaned effect name: %s", payload.c_str()); + } + + Effect* new_effect = select_effect( crc32String(payload.c_str()) ); + if (new_effect == NULL) { + LOGln("Effects * Could not find effect with name %s", payload.c_str()); + return false; + } delete current_effect; current_effect = new_effect; + + if (options.length() > 0) { + LOGln("Effects * Parsing options: %s", options.c_str()); + options += ","; + int p_colon; + while ((p_colon = options.indexOf(",")) >= 0) { + int p_equal = options.indexOf("="); + if (p_equal >= 0 && p_equal < p_colon) { + String key = options.substring(0, p_equal); + String value = options.substring(p_equal + 1, p_colon); + LOGln("Effects * Applying option: %s = %s", key.c_str(), value.c_str()); + current_effect->apply_option(key, value); + } + options.remove(0, p_colon + 1); + } + } return true; } diff --git a/src/pitrix.cpp b/src/pitrix.cpp index fd70cf8..ae4a0cb 100644 --- a/src/pitrix.cpp +++ b/src/pitrix.cpp @@ -1,5 +1,4 @@ #include -#include #include "ntp.h" #include "config.h" @@ -20,7 +19,7 @@ char hostname[30]; // defined as extern in prototypes.h void setup() { Serial.begin(74880); LOGln("Core * Starting"); - + int chipid; #if defined( ESP8266 ) chipid = ESP.getChipId(); @@ -28,7 +27,7 @@ void setup() { chipid = ESP.getEfuseMac() & 0xFFFFFF; #endif snprintf(hostname, 30, HOSTNAME, chipid); - + setup_effects(); wifi_setup(); ntp_setup();