diff --git a/src/application.cpp b/src/application.cpp index 0ea7833..a5ff2e6 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -34,6 +34,13 @@ Application::Application(Storage &config_storage, Storage & this->load(); }); } +void Application::begin() { + load(); + + for (int i = 0; i < preset_names.count; ++i) { + refresh_preset_name(i); + } +} void Application::load() { const auto &preset = this->preset(); @@ -53,8 +60,8 @@ void Application::load() { set_palette(current_palette, palette->value.data, palette->value.size); } - if (config.preset_id >= preset_names.count) { - config.preset_id = preset_names.count - 1; + if (config.preset_id >= preset_configs.count) { + config.preset_id = preset_configs.count - 1; } #if GAMMA_CORRECTION_RT == DISABLED @@ -86,6 +93,56 @@ void Application::update() { load(); } +void Application::refresh_preset_name(uint8 preset_id) { + if(preset_names.custom[preset_id]) return; + + auto &preset = preset_configs.presets[preset_id]; + + const char *effect_name; + switch (preset.color_effect) { + case ColorEffectEnum::PERLIN: + effect_name = "Noise"; + break; + case ColorEffectEnum::GRADIENT: + effect_name = "Gradient"; + break; + case ColorEffectEnum::FIRE: + effect_name = "Fire"; + break; + case ColorEffectEnum::AURORA: + effect_name = "Aurora"; + break; + case ColorEffectEnum::PLASMA: + effect_name = "Plasma"; + break; + case ColorEffectEnum::RIPPLE: + effect_name = "Ripple"; + break; + case ColorEffectEnum::VELUM: + effect_name = "Velum"; + break; + case ColorEffectEnum::PARTICLES: + effect_name = "Sparkles"; + break; + case ColorEffectEnum::CHANGE_COLOR: + effect_name = "Colors"; + break; + case ColorEffectEnum::SOLID: + effect_name = "Color"; + break; + default: + effect_name = "Unknown"; + } + + if (preset.color_effect != ColorEffectEnum::SOLID) { + const char *palette_name = Palettes.entries[(int) preset.palette].name; + snprintf(preset_names.names[preset_id], preset_names.length, + "%s %s", palette_name, effect_name); + } else { + snprintf(preset_names.names[preset_id], preset_names.length, "Single Color"); + } +} + void Application::change_state(AppState s) { state_change_time = millis(); state = s; @@ -112,7 +169,7 @@ void Application::set_power(bool on) { } void Application::change_preset(uint8_t preset_id) { - if (preset_id >= preset_names.count) return; + if (preset_id >= preset_configs.count) return; config.preset_id = preset_id; config_storage.save(); @@ -158,7 +215,8 @@ void Application::change_color(uint32_t color) { auto &preset = preset_configs.presets[preset_index]; auto preset_name = "API Color"; - memcpy(preset_names.names[preset_index], preset_name, std::min(preset_names.length, strlen(preset_name))); + memcpy(preset_names.names[preset_index], preset_name, std::min(preset_names.length, strlen(preset_name) + 1)); + preset_names.custom[preset_index] = true; preset.color_effect = ColorEffectEnum::SOLID; preset.brightness_effect = BrightnessEffectEnum::FIXED; diff --git a/src/application.h b/src/application.h index 526237e..525739f 100644 --- a/src/application.h +++ b/src/application.h @@ -38,10 +38,14 @@ class Application { NightModeManager &night_mode_manager, SignalProvider *wave_provider, SignalProvider *spectrum_provider, SignalProvider *parametric_provider); + void begin(); + void change_state(AppState s); void load(); void update(); + void refresh_preset_name(uint8 preset_id); + void set_power(bool on); void change_preset(uint8_t preset_id); @@ -53,7 +57,7 @@ class Application { void restart(); - inline PresetConfig &preset() { return preset_configs.presets[config.preset_id]; } + PresetConfig &preset() const { return preset_configs.presets[config.preset_id]; } [[nodiscard]] BrightnessSettings get_brightness_settings() const; SignalProvider *signal_provider() const; diff --git a/src/config.h b/src/config.h index 101930d..39aa83d 100644 --- a/src/config.h +++ b/src/config.h @@ -69,11 +69,11 @@ struct __attribute ((packed)) __PresetNames { uint8_t length = S; char names[N][S] = {}; + bool custom[N] = {}; __PresetNames() { - for (int i = 0; i < N; ++i) { - sprintf(names[i], "Preset %i", i + 1); - } + memset(names, 0, sizeof(names)); + memset(custom, 0, sizeof(custom)); } }; @@ -82,9 +82,15 @@ struct __attribute ((packed)) PresetConfig { uint8_t scale = 190; uint8_t light = 128; - PaletteEnum palette = PaletteEnum::MAGIC_PLUM; - ColorEffectEnum color_effect = ColorEffectEnum::FIRE; - BrightnessEffectEnum brightness_effect = BrightnessEffectEnum::FIXED; + PaletteEnum palette; + ColorEffectEnum color_effect; + BrightnessEffectEnum brightness_effect; + + PresetConfig() { + palette = (PaletteEnum) random((int) PaletteEnum::CUSTOM); // Exclude Custom + color_effect = (ColorEffectEnum) random((int) ColorEffectEnum::CHANGE_COLOR); // Exclude CHANGE_COLOR and SOLID + brightness_effect = BrightnessEffectEnum::FIXED; + } }; template diff --git a/src/main.cpp b/src/main.cpp index 1869308..313ae00 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -106,7 +106,7 @@ void setup() { preset_configs_storage.begin(&LittleFS); custom_palette_storage.begin(&LittleFS); - app.load(); + app.begin(); led.set_power_limit(MATRIX_VOLTAGE, CURRENT_LIMIT); led.set_correction(app.config.color_correction); @@ -209,9 +209,9 @@ void render() { //TODO: Refactor auto preset = app.preset(); - const auto &audio_cfg = app.config.audio_config; #if AUDIO == ENABLED + const auto &audio_cfg = app.config.audio_config; bool audio_applied = true; if (audio_cfg.is_parametric()) { @@ -300,8 +300,7 @@ void service_loop(void *) { case 1: wifi_manager.handle_connection(); - if (wifi_manager.state() == WifiManagerState::CONNECTED) - state++; + if (wifi_manager.state() == WifiManagerState::CONNECTED) state++; break; @@ -372,13 +371,13 @@ void button_on_click(uint8_t cnt) { break; case 2: - app.change_preset((app.config.preset_id + 1) % app.preset_names.count); + app.change_preset((app.config.preset_id + 1) % app.preset_configs.count); break; case 3: app.change_preset(app.config.preset_id > 0 - ? (app.config.preset_id - 1) % app.preset_names.count - : app.preset_names.count - 1); + ? (app.config.preset_id - 1) % app.preset_configs.count + : app.preset_configs.count - 1); break; case 5: diff --git a/src/network/protocol/server/base.cpp b/src/network/protocol/server/base.cpp index 858bcd9..2b46f56 100644 --- a/src/network/protocol/server/base.cpp +++ b/src/network/protocol/server/base.cpp @@ -32,10 +32,12 @@ Response ServerBase::handle_packet_data(const uint8_t *buffer, uint16_t length) if (response.is_ok()) { if (header->type >= PacketType::NIGHT_MODE_ENABLED && header->type <= PacketType::NIGHT_MODE_INTERVAL) { app().night_mode_manager.reset(); - } - - if (header->type == PacketType::UPDATE_CUSTOM_PALETTE) { + } else if (header->type == PacketType::UPDATE_CUSTOM_PALETTE) { app().custom_palette_storage.save(); + } else if (header->type == PacketType::PRESET_NAME) { + app().preset_names.custom[app().config.preset_id] = true; + } else if (header->type == PacketType::PALETTE || header->type == PacketType::COLOR_EFFECT) { + app().refresh_preset_name(app().config.preset_id); } app().update(); @@ -196,12 +198,12 @@ Response serialize_fx_config(const FxConfig> &config) { return Response::code(ResponseCode::INTERNAL_ERROR); } - return Response{ResponseType::BINARY, {.buffer = {.size = size, .data=buffer}}}; + return Response{ResponseType::BINARY, {.buffer = {.size = size, .data = buffer}}}; } template Response serialize(const T &obj) { - return Response{ResponseType::BINARY, {.buffer = {.size = sizeof(obj), .data=(uint8_t *) &obj}}}; + return Response{ResponseType::BINARY, {.buffer = {.size = sizeof(obj), .data = (uint8_t *) &obj}}}; } Response ServerBase::process_data_request(const PacketHeader &header) { diff --git a/src/network/web.cpp b/src/network/web.cpp index 7328314..78d7b1f 100644 --- a/src/network/web.cpp +++ b/src/network/web.cpp @@ -3,7 +3,7 @@ class WebLogger : public AsyncWebHandler { bool canHandle(AsyncWebServerRequest *request) override { D_PRINTF("WebServer: %s -> %s %s\n", request->client()->remoteIP().toString().c_str(), - request->methodToString(), request->url().c_str()); + ((String)request->methodToString()).c_str(), request->url().c_str()); return false; }