diff --git a/src/application.cpp b/src/application.cpp index ab3db6b..90e716d 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -25,8 +25,10 @@ Application::Application(Storage &config_storage, Storage & event_property_changed.subscribe(this, [this](auto sender, auto type, auto arg) { if (sender == this) return; - if (type == NotificationProperty::COLOR) { + if (type == NotificationProperty::COLOR && arg) { this->change_color(*(uint32_t *) arg); + } else if (type == NotificationProperty::PALETTE && arg) { + this->preset().palette = *(PaletteEnum *) arg; } else if (type == NotificationProperty::NIGHT_MODE_ENABLED) { this->night_mode_manager.reset(); } @@ -53,6 +55,10 @@ 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 GAMMA_CORRECTION_RT == DISABLED if (config.gamma != 0) { napplyGamma_video(current_palette.entries, 16, gamma_value(config.gamma)); diff --git a/src/constants.h b/src/constants.h index e431a6c..2b8464c 100644 --- a/src/constants.h +++ b/src/constants.h @@ -6,16 +6,12 @@ #include "sys_constants.h" #define WIFI_MODE (WIFI_AP_MODE) -#define WIFI_SSID CREDENTIAL_WIFI_SSID -#define WIFI_PASSWORD CREDENTIAL_WIFI_PASSWORD #define WIFI_CONNECTION_CHECK_INTERVAL (5000u) // Interval (ms) between Wi-Fi connection check #define WIFI_MAX_CONNECTION_ATTEMPT_INTERVAL (0u) // Max time (ms) to wait for Wi-Fi connection before switch to AP mode // 0 - Newer switch to AP mode #define WEB_AUTH -#define AUTH_USER CREDENTIAL_AUTH_USER -#define AUTH_PASSWORD CREDENTIAL_AUTH_PASSWORD #define TIME_ZONE (5.f) // GMT +5:00 @@ -49,7 +45,7 @@ #define AUDIO_WINDOW_DURATION (5000u) -#define MQTT (0u) // MQTT protocol Enabled +#define MQTT (1u) // MQTT protocol Enabled #define MQTT_CONNECTION_TIMEOUT (15000u) // Connection attempt timeout to MQTT server #define MQTT_RECONNECT_TIMEOUT (5000u) // Time before new reconnection attempt to MQTT server @@ -58,10 +54,14 @@ #define MQTT_TOPIC_BRIGHTNESS MQTT_PREFIX "/brightness" #define MQTT_TOPIC_POWER MQTT_PREFIX "/power" #define MQTT_TOPIC_COLOR MQTT_PREFIX "/color" +#define MQTT_TOPIC_PRESET MQTT_PREFIX "/preset" +#define MQTT_TOPIC_PALETTE MQTT_PREFIX "/palette" #define MQTT_TOPIC_NIGHT_MODE MQTT_PREFIX "/night_mode" #define MQTT_OUT_PREFIX MQTT_PREFIX "/out" #define MQTT_OUT_TOPIC_BRIGHTNESS MQTT_OUT_PREFIX "/brightness" #define MQTT_OUT_TOPIC_POWER MQTT_OUT_PREFIX "/power" #define MQTT_OUT_TOPIC_COLOR MQTT_OUT_PREFIX "/color" +#define MQTT_OUT_TOPIC_PRESET MQTT_OUT_PREFIX "/preset" +#define MQTT_OUT_TOPIC_PALETTE MQTT_OUT_PREFIX "/palette" #define MQTT_OUT_TOPIC_NIGHT_MODE MQTT_OUT_PREFIX "/night_mode" \ No newline at end of file diff --git a/src/metadata.cpp b/src/metadata.cpp index 446017d..ca04735 100644 --- a/src/metadata.cpp +++ b/src/metadata.cpp @@ -29,6 +29,14 @@ std::map PacketTypeMetadataMap = { MQTT_TOPIC_BRIGHTNESS, MQTT_OUT_TOPIC_BRIGHTNESS, } }, + { + PacketType::PRESET_ID, + { + NotificationProperty::PRESET, PacketType::PRESET_ID, + offsetof(Config, preset_id), sizeof(Config::preset_id), + MQTT_TOPIC_PRESET, MQTT_OUT_TOPIC_PRESET, + } + }, { PacketType::NIGHT_MODE_ENABLED, { diff --git a/src/metadata.h b/src/metadata.h index a318d3e..4dd9777 100644 --- a/src/metadata.h +++ b/src/metadata.h @@ -17,12 +17,14 @@ MAKE_ENUM(NotificationProperty, uint8_t, POWER, 0, BRIGHTNESS, 1, COLOR, 2, - NIGHT_MODE_ENABLED, 3, + PRESET, 3, + PALETTE, 4, + NIGHT_MODE_ENABLED, 5, ) struct PropertyMetadata { - NotificationProperty property; - PacketType packet_type; + NotificationProperty property{}; + PacketType packet_type{}; uint8_t value_offset{}; uint8_t value_size{}; diff --git a/src/network/protocol/server/base.cpp b/src/network/protocol/server/base.cpp index 858bcd9..605cc00 100644 --- a/src/network/protocol/server/base.cpp +++ b/src/network/protocol/server/base.cpp @@ -46,6 +46,8 @@ Response ServerBase::handle_packet_data(const uint8_t *buffer, uint16_t length) auto iter = PacketTypeMetadataMap.find(header->type); if (iter != PacketTypeMetadataMap.end()) { _app.event_property_changed.publish(this, iter->second.property); + } else if (header->type == PacketType::PALETTE) { + _app.event_property_changed.publish(this, NotificationProperty::PALETTE); } } diff --git a/src/network/protocol/server/mqtt.h b/src/network/protocol/server/mqtt.h index 9fb75b2..a8320ef 100644 --- a/src/network/protocol/server/mqtt.h +++ b/src/network/protocol/server/mqtt.h @@ -118,6 +118,7 @@ void MqttServer::_on_connect(bool) { } _subscribe(MQTT_TOPIC_COLOR, 1); + _subscribe(MQTT_TOPIC_PALETTE, 1); _last_connection_attempt_time = millis(); _change_state(MqttServerState::CONNECTED); @@ -144,6 +145,9 @@ void MqttServer::_on_message(char *topic, char *payload, AsyncMqttClientMessageP if (topic_str == MQTT_TOPIC_COLOR) { uint32_t color = payload_str.toInt(); _app.event_property_changed.publish(this, NotificationProperty::COLOR, &color); + } else if (topic_str == MQTT_TOPIC_PALETTE) { + auto palette_id = (PaletteEnum) payload_str.toInt(); + _app.event_property_changed.publish(this, NotificationProperty::PALETTE, &palette_id); } else { _process_message(topic_str, payload_str); } @@ -212,6 +216,10 @@ void MqttServer::_process_notification(NotificationProperty prop) { String str(_app.current_color()); _publish(MQTT_OUT_TOPIC_COLOR, 1, str.c_str(), str.length()); return; + } else if (prop == NotificationProperty::PALETTE) { + String str((int) _app.palette->code); + _publish(MQTT_OUT_TOPIC_PALETTE, 1, str.c_str(), str.length()); + return; } auto iter_meta = PropertyMetadataMap.find(prop);