2
2
#include " board.h"
3
3
#include " display.h"
4
4
#include " application.h"
5
+ #include " lvgl_theme.h"
5
6
6
7
#include < esp_log.h>
7
8
#include < spi_flash_mmap.h>
@@ -32,12 +33,6 @@ Assets::Assets(std::string default_assets_url) {
32
33
}
33
34
34
35
Assets::~Assets () {
35
- if (custom_emoji_collection_ != nullptr ) {
36
- delete custom_emoji_collection_;
37
- }
38
- if (text_font_) {
39
- cbin_font_delete (text_font_);
40
- }
41
36
if (mmap_handle_ != 0 ) {
42
37
esp_partition_munmap (mmap_handle_);
43
38
}
@@ -111,6 +106,17 @@ bool Assets::InitializePartition() {
111
106
return checksum_valid_;
112
107
}
113
108
109
+ lv_color_t Assets::ParseColor (const std::string& color) {
110
+ if (color.find (" #" ) == 0 ) {
111
+ // Convert #112233 to lv_color_t
112
+ uint8_t r = strtol (color.substr (1 , 2 ).c_str (), nullptr , 16 );
113
+ uint8_t g = strtol (color.substr (3 , 2 ).c_str (), nullptr , 16 );
114
+ uint8_t b = strtol (color.substr (5 , 2 ).c_str (), nullptr , 16 );
115
+ return lv_color_make (r, g, b);
116
+ }
117
+ return lv_color_black ();
118
+ }
119
+
114
120
bool Assets::Apply () {
115
121
void * ptr = nullptr ;
116
122
size_t size = 0 ;
@@ -123,6 +129,14 @@ bool Assets::Apply() {
123
129
ESP_LOGE (TAG, " The index.json file is not valid" );
124
130
return false ;
125
131
}
132
+
133
+ cJSON* version = cJSON_GetObjectItem (root, " version" );
134
+ if (cJSON_IsNumber (version)) {
135
+ if (version->valuedouble > 1 ) {
136
+ ESP_LOGE (TAG, " The assets version %d is not supported, please upgrade the firmware" , version->valueint );
137
+ return false ;
138
+ }
139
+ }
126
140
127
141
cJSON* srmodels = cJSON_GetObjectItem (root, " srmodels" );
128
142
if (cJSON_IsString (srmodels)) {
@@ -144,28 +158,29 @@ bool Assets::Apply() {
144
158
}
145
159
}
146
160
161
+ auto & theme_manager = LvglThemeManager::GetInstance ();
162
+ auto light_theme = theme_manager.GetTheme (" light" );
163
+ auto dark_theme = theme_manager.GetTheme (" dark" );
164
+
147
165
cJSON* font = cJSON_GetObjectItem (root, " text_font" );
148
166
if (cJSON_IsString (font)) {
149
167
std::string fonts_text_file = font->valuestring ;
150
168
if (GetAssetData (fonts_text_file, ptr, size)) {
151
- if (text_font_ != nullptr ) {
152
- cbin_font_delete (text_font_);
153
- }
154
- text_font_ = cbin_font_create (static_cast <uint8_t *>(ptr));
155
- if (text_font_ == nullptr ) {
169
+ auto text_font = std::make_shared<LvglCBinFont>(ptr);
170
+ if (text_font->font () == nullptr ) {
156
171
ESP_LOGE (TAG, " Failed to load fonts.bin" );
172
+ return false ;
157
173
}
174
+ light_theme->set_text_font (text_font);
175
+ dark_theme->set_text_font (text_font);
158
176
} else {
159
177
ESP_LOGE (TAG, " The font file %s is not found" , fonts_text_file.c_str ());
160
178
}
161
179
}
162
180
163
181
cJSON* emoji_collection = cJSON_GetObjectItem (root, " emoji_collection" );
164
182
if (cJSON_IsArray (emoji_collection)) {
165
- if (custom_emoji_collection_ != nullptr ) {
166
- delete custom_emoji_collection_;
167
- }
168
- custom_emoji_collection_ = new CustomEmojiCollection ();
183
+ auto custom_emoji_collection = std::make_shared<CustomEmojiCollection>();
169
184
int emoji_count = cJSON_GetArraySize (emoji_collection);
170
185
for (int i = 0 ; i < emoji_count; i++) {
171
186
cJSON* emoji = cJSON_GetArrayItem (emoji_collection, i);
@@ -177,28 +192,63 @@ bool Assets::Apply() {
177
192
ESP_LOGE (TAG, " Emoji %s image file %s is not found" , name->valuestring , file->valuestring );
178
193
continue ;
179
194
}
180
- auto img = new lv_img_dsc_t {
181
- .header = {
182
- .magic = LV_IMAGE_HEADER_MAGIC,
183
- .cf = LV_COLOR_FORMAT_RAW_ALPHA,
184
- },
185
- .data_size = size,
186
- .data = static_cast <uint8_t *>(ptr),
187
- };
188
- custom_emoji_collection_->AddEmoji (name->valuestring , img);
195
+ custom_emoji_collection->AddEmoji (name->valuestring , new LvglRawImage (ptr, size));
189
196
}
190
197
}
191
198
}
199
+ light_theme->set_emoji_collection (custom_emoji_collection);
200
+ dark_theme->set_emoji_collection (custom_emoji_collection);
201
+ }
202
+
203
+ cJSON* skin = cJSON_GetObjectItem (root, " skin" );
204
+ if (cJSON_IsObject (skin)) {
205
+ cJSON* light_skin = cJSON_GetObjectItem (skin, " light" );
206
+ if (cJSON_IsObject (light_skin)) {
207
+ cJSON* text_color = cJSON_GetObjectItem (light_skin, " text_color" );
208
+ cJSON* background_color = cJSON_GetObjectItem (light_skin, " background_color" );
209
+ cJSON* background_image = cJSON_GetObjectItem (light_skin, " background_image" );
210
+ if (cJSON_IsString (text_color)) {
211
+ light_theme->set_text_color (ParseColor (text_color->valuestring ));
212
+ }
213
+ if (cJSON_IsString (background_color)) {
214
+ light_theme->set_background_color (ParseColor (background_color->valuestring ));
215
+ light_theme->set_chat_background_color (ParseColor (background_color->valuestring ));
216
+ }
217
+ if (cJSON_IsString (background_image)) {
218
+ if (!GetAssetData (background_image->valuestring , ptr, size)) {
219
+ ESP_LOGE (TAG, " The background image file %s is not found" , background_image->valuestring );
220
+ return false ;
221
+ }
222
+ auto background_image = std::make_shared<LvglCBinImage>(ptr);
223
+ light_theme->set_background_image (background_image);
224
+ }
225
+ }
226
+ cJSON* dark_skin = cJSON_GetObjectItem (skin, " dark" );
227
+ if (cJSON_IsObject (dark_skin)) {
228
+ cJSON* text_color = cJSON_GetObjectItem (dark_skin, " text_color" );
229
+ cJSON* background_color = cJSON_GetObjectItem (dark_skin, " background_color" );
230
+ cJSON* background_image = cJSON_GetObjectItem (dark_skin, " background_image" );
231
+ if (cJSON_IsString (text_color)) {
232
+ dark_theme->set_text_color (ParseColor (text_color->valuestring ));
233
+ }
234
+ if (cJSON_IsString (background_color)) {
235
+ dark_theme->set_background_color (ParseColor (background_color->valuestring ));
236
+ dark_theme->set_chat_background_color (ParseColor (background_color->valuestring ));
237
+ }
238
+ if (cJSON_IsString (background_image)) {
239
+ if (!GetAssetData (background_image->valuestring , ptr, size)) {
240
+ ESP_LOGE (TAG, " The background image file %s is not found" , background_image->valuestring );
241
+ return false ;
242
+ }
243
+ auto background_image = std::make_shared<LvglCBinImage>(ptr);
244
+ dark_theme->set_background_image (background_image);
245
+ }
246
+ }
192
247
}
193
248
194
249
auto display = Board::GetInstance ().GetDisplay ();
195
- ESP_LOGI (TAG, " Applying new assets to display" );
196
- display->UpdateStyle ({
197
- .text_font = text_font_,
198
- .icon_font = nullptr ,
199
- .emoji_collection = custom_emoji_collection_,
200
- });
201
-
250
+ ESP_LOGI (TAG, " Refreshing display theme..." );
251
+ display->SetTheme (display->GetTheme ());
202
252
cJSON_Delete (root);
203
253
return true ;
204
254
}
0 commit comments