Skip to content

Commit

Permalink
Implemented Effect name generation
Browse files Browse the repository at this point in the history
  • Loading branch information
DrA1ex committed Nov 27, 2024
1 parent c011a1d commit 22cc8be
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 24 deletions.
66 changes: 62 additions & 4 deletions src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ Application::Application(Storage<Config> &config_storage, Storage<PresetNames> &
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();
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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<uint8_t>(preset_names.length, strlen(preset_name)));
memcpy(preset_names.names[preset_index], preset_name, std::min<uint8_t>(preset_names.length, strlen(preset_name) + 1));
preset_names.custom[preset_index] = true;

preset.color_effect = ColorEffectEnum::SOLID;
preset.brightness_effect = BrightnessEffectEnum::FIXED;
Expand Down
6 changes: 5 additions & 1 deletion src/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
Expand Down
18 changes: 12 additions & 6 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
};

Expand All @@ -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<uint8_t N>
Expand Down
13 changes: 6 additions & 7 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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:
Expand Down
12 changes: 7 additions & 5 deletions src/network/protocol/server/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -196,12 +198,12 @@ Response serialize_fx_config(const FxConfig<FxConfigEntry<C, V>> &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<typename T>
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) {
Expand Down
2 changes: 1 addition & 1 deletion src/network/web.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 22cc8be

Please sign in to comment.