Skip to content

Commit

Permalink
Merge branch 'main' into lvgl_9.1
Browse files Browse the repository at this point in the history
  • Loading branch information
KlausMu committed May 9, 2024
2 parents 312072b + 8f0ceeb commit d69913c
Show file tree
Hide file tree
Showing 29 changed files with 961 additions and 434 deletions.
48 changes: 33 additions & 15 deletions Platformio/hardware/ESP32/preferencesStorage_hal_esp32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

Preferences preferences;

std::string currentScene;
std::string currentGUIname;
std::string activeScene;
std::string activeGUIname;
int activeGUIlist;
int lastActiveGUIlistIndex;

void init_preferences_HAL(void) {
// Restore settings from internal flash memory
Expand All @@ -17,10 +19,12 @@ void init_preferences_HAL(void) {
// from tft.h
set_backlightBrightness_HAL(preferences.getUChar("blBrightness"));
// from here
currentScene = std::string(preferences.getString("currentScene").c_str());
currentGUIname = std::string(preferences.getString("currentGUIname").c_str());
activeScene = std::string(preferences.getString("currentScene").c_str());
activeGUIname = std::string(preferences.getString("currentGUIname").c_str());
activeGUIlist =(preferences.getInt("currentGUIlist"));
lastActiveGUIlistIndex = (preferences.getInt("lastActiveIndex"));

// Serial.printf("Preferences restored: brightness %d, GUI %s, scene %s\r\n", get_backlightBrightness_HAL(), get_currentGUIname().c_str(), get_currentScene().c_str());
// Serial.printf("Preferences restored: brightness %d, GUI %s, scene %s\r\n", get_backlightBrightness_HAL(), get_activeGUIname().c_str(), get_activeScene().c_str());
} else {
// Serial.printf("No preferences to restore\r\n");
}
Expand All @@ -35,23 +39,37 @@ void save_preferences_HAL(void) {
preferences.putUInt("slpTimeout", get_sleepTimeout_HAL());
preferences.putUChar("blBrightness", get_backlightBrightness_HAL());
// from here
preferences.putString("currentScene", currentScene.c_str());
preferences.putString("currentGUIname", currentGUIname.c_str());
preferences.putString("currentScene", activeScene.c_str());
preferences.putString("currentGUIname", activeGUIname.c_str());
preferences.putInt("currentGUIlist", activeGUIlist);
preferences.putInt("lastActiveIndex", lastActiveGUIlistIndex);
if (!preferences.getBool("alreadySetUp")) {
preferences.putBool("alreadySetUp", true);
}
preferences.end();
}

std::string get_currentScene_HAL() {
return currentScene;
std::string get_activeScene_HAL() {
return activeScene;
}
void set_currentScene_HAL(std::string aCurrentScene) {
currentScene = aCurrentScene;
void set_activeScene_HAL(std::string anActiveScene) {
activeScene = anActiveScene;
}
std::string get_currentGUIname_HAL(){
return currentGUIname;
std::string get_activeGUIname_HAL(){
return activeGUIname;
}
void set_currentGUIname_HAL(std::string aCurrentGUIname) {
currentGUIname = aCurrentGUIname;
void set_activeGUIname_HAL(std::string anActiveGUIname) {
activeGUIname = anActiveGUIname;
}
int get_activeGUIlist_HAL() {
return activeGUIlist;
}
void set_activeGUIlist_HAL(int anActiveGUIlist) {
activeGUIlist = anActiveGUIlist;
}
int get_lastActiveGUIlistIndex_HAL() {
return lastActiveGUIlistIndex;
}
void set_lastActiveGUIlistIndex_HAL(int aGUIlistIndex) {
lastActiveGUIlistIndex = aGUIlistIndex;
}
12 changes: 8 additions & 4 deletions Platformio/hardware/ESP32/preferencesStorage_hal_esp32.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
void init_preferences_HAL(void);
void save_preferences_HAL(void);

std::string get_currentScene_HAL();
void set_currentScene_HAL(std::string aCurrentScene);
std::string get_currentGUIname_HAL();
void set_currentGUIname_HAL(std::string aCurrentGUIname);
std::string get_activeScene_HAL();
void set_activeScene_HAL(std::string anActiveScene);
std::string get_activeGUIname_HAL();
void set_activeGUIname_HAL(std::string anActiveGUIname);
int get_activeGUIlist_HAL();
void set_activeGUIlist_HAL(int anActiveGUIlist);
int get_lastActiveGUIlistIndex_HAL();
void set_lastActiveGUIlistIndex_HAL(int aGUIlistIndex);
Original file line number Diff line number Diff line change
@@ -1,31 +1,58 @@
#include <string>

std::string currentScene;
std::string currentGUIname;
enum GUIlists {
// MAIN_GUI_LIST: we are in the main_gui_list (with the scene selector as first gui), either if a scene is active or not
// SCENE_GUI_LIST: a scene is active and we are not in the main_gui_list. In that case, we try to use the scene specific gui list, if the scene defined one.
MAIN_GUI_LIST,
SCENE_GUI_LIST
};

/*
Possible example:
main_gui_list : "Scene selection", "Smart Home", "Settings", "IR Receiver"};
"Off" :
"TV" : "Numpad"
"Fire TV" : "Numpad", "Settings"
"Chromecast" :
"Apple TV" : "Apple TV", "Settings", "IR Receiver"
*/
std::string activeScene;
std::string activeGUIname;
int activeGUIlist;
int lastActiveGUIlistIndex;

void init_preferences_HAL(void) {
// set some values for tests
activeScene = ""; // "Off", "TV", "Fire TV", "Chromecast", "Apple TV";
activeGUIname = ""; // "Scene selection", "Smart Home", "Settings", "IR Receiver" // "Numpad", "Apple TV"
activeGUIlist = MAIN_GUI_LIST; // MAIN_GUI_LIST, SCENE_GUI_LIST;
lastActiveGUIlistIndex = 0;
}
void save_preferences_HAL(void) {
}

std::string get_currentScene_HAL() {
// if (currentScene == "") {
// // set here something if you need it for a test at startup
// return "Apple TV";
// } else
{return currentScene;}

std::string get_activeScene_HAL() {
return activeScene;
}
void set_activeScene_HAL(std::string anActiveScene) {
activeScene = anActiveScene;
}
std::string get_activeGUIname_HAL(){
return activeGUIname;
}
void set_activeGUIname_HAL(std::string anActiveGUIname) {
activeGUIname = anActiveGUIname;
}
int get_activeGUIlist_HAL() {
return activeGUIlist;
}
void set_currentScene_HAL(std::string aCurrentScene) {
currentScene = aCurrentScene;
void set_activeGUIlist_HAL(int anActiveGUIlist) {
activeGUIlist = anActiveGUIlist;
}
std::string get_currentGUIname_HAL(){
// if (currentGUIname == "") {
// // set here something if you need it for a test at startup
// return "IR Receiver"; // "Numpad"; // "Apple TV";
// } else
{return currentGUIname;}
int get_lastActiveGUIlistIndex_HAL() {
return lastActiveGUIlistIndex;
}
void set_currentGUIname_HAL(std::string aCurrentGUIname) {
currentGUIname = aCurrentGUIname;
void set_lastActiveGUIlistIndex_HAL(int aGUIlistIndex) {
lastActiveGUIlistIndex = aGUIlistIndex;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
void init_preferences_HAL(void);
void save_preferences_HAL(void);

std::string get_currentScene_HAL();
void set_currentScene_HAL(std::string aCurrentScene);
std::string get_currentGUIname_HAL();
void set_currentGUIname_HAL(std::string aCurrentGUIname);
std::string get_activeScene_HAL();
void set_activeScene_HAL(std::string anActiveScene);
std::string get_activeGUIname_HAL();
void set_activeGUIname_HAL(std::string anActiveGUIname);
int get_activeGUIlist_HAL();
void set_activeGUIlist_HAL(int anActiveGUIlist);
int get_lastActiveGUIlistIndex_HAL();
void set_lastActiveGUIlistIndex_HAL(int aGUIlistIndex);
7 changes: 7 additions & 0 deletions Platformio/src/applicationInternal/commandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,13 @@ void executeCommandWithData(uint16_t command, commandData commandData, std::stri
break;
}

case GUI: {
// let the sceneHandler find and show the gui
Serial.printf("execute: will send gui command to the sceneHandler\r\n");
handleGUI(command, commandData, additionalPayload);
break;
}

case SPECIAL: {
if (command == MY_SPECIAL_COMMAND) {
// do your special command here
Expand Down
1 change: 1 addition & 0 deletions Platformio/src/applicationInternal/commandHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ extern uint16_t KEYBOARD_VOLUME_DECREMENT;
enum commandHandlers {
SPECIAL,
SCENE,
GUI,
IR,
#if (ENABLE_WIFI_AND_MQTT == 1)
MQTT,
Expand Down
64 changes: 40 additions & 24 deletions Platformio/src/applicationInternal/gui/guiBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ lv_obj_t* BluetoothLabel = NULL;
lv_obj_t* BattPercentageLabel = NULL;
lv_obj_t* BattIconLabel = NULL;
lv_obj_t* SceneLabel = NULL;
uint32_t currentTabID = -1; // id of the current tab
uint32_t oldTabID = -1;

lv_obj_t* tabview = NULL;
// page indicator
Expand All @@ -30,8 +28,8 @@ lv_style_t panel_style;
lv_style_t style_red_border;
#endif

void guis_doTabCreationAtStartup();
void guis_doAfterSliding(int oldTabID, int newTabID, bool newGuiList);
void guis_doTabCreationOnStartup();
void guis_doTabCreationAfterSliding(int newTabID);

// Helper Functions -----------------------------------------------------------------------------------------------------------------------

Expand Down Expand Up @@ -89,40 +87,41 @@ void tabview_content_is_scrolling_event_cb(lv_event_t* e){
// -----------------------
static bool waitBeforeActionAfterSlidingAnimationEnded = false;
static unsigned long waitBeforeActionAfterSlidingAnimationEnded_timerStart;
static int newTabID_forLateTabCreation;
// This is the callback when the animation of the tab sliding ended
static void tabview_animation_ready_cb(lv_anim_t* a) {
// Unfortunately, the animation has not completely ended here. We cannot do the recreation of the tabs here.
// We have to wait some more milliseconds or at least one cycle of lv_timer_handler();
// calling lv_timer_handler(); here does not help
// lv_timer_handler();
// guis_doAfterSliding(oldTabID, currentTabID, false);
// guis_doTabCreationAfterSliding(newTabID_forLateTabCreation);

waitBeforeActionAfterSlidingAnimationEnded = true;
waitBeforeActionAfterSlidingAnimationEnded_timerStart = millis();
}

// Update currentTabID when a new tab is selected
// Update gui when a new tab is selected
// this is a callback if the tabview is changed (LV_EVENT_VALUE_CHANGED)
// Sent when a new tab is selected by sliding or clicking the tab button. lv_tabview_get_tab_act(tabview) returns the zero based index of the current tab.
void tabview_tab_changed_event_cb(lv_event_t* e) {
if (lv_event_get_code(e) == LV_EVENT_VALUE_CHANGED) {

oldTabID = currentTabID;
currentTabID = lv_tabview_get_tab_active((lv_obj_t*)lv_event_get_target(e));
int newTabID = lv_tabview_get_tab_active((lv_obj_t*)lv_event_get_target(e));

// Wait until the animation ended, then call "guis_doAfterSliding(oldTabID, currentTabID, false);"
// Wait until the animation ended, then call "guis_doTabCreationAfterSliding(newTabID);"
// https://forum.lvgl.io/t/delete-a-tab-after-the-tabview-scroll-animation-is-complete/3155/4
lv_obj_t* myTabview = (lv_obj_t*)lv_event_get_target(e);
lv_obj_t* tabContainer = lv_tabview_get_content(myTabview);
lv_anim_t* anim = lv_anim_get(tabContainer, NULL);
if(anim) {
// Swipe is not yet complete. User released the touch screen, an animation will bring it to the end.
// That's the normal (and only?) case for the tft touchscreen
newTabID_forLateTabCreation = newTabID;
lv_anim_set_completed_cb(anim, tabview_animation_ready_cb);
} else {
// Swipe is complete, no additional animation is needed. Most likely only possible in simulator
Serial.println("Change of tab detected, without animation at the end. Will directly do my job after sliding.");
guis_doAfterSliding(oldTabID, currentTabID, false);
guis_doTabCreationAfterSliding(newTabID);
}
}
}
Expand Down Expand Up @@ -156,8 +155,8 @@ void init_gui(void) {
lv_obj_set_style_bg_color(lv_screen_active(), lv_color_black(), LV_PART_MAIN);
// set default height and position of main widgets
setMainWidgetsHeightAndPosition();
// At startup, set current GUI according to get_currentGUIname(), and create the content of that tab (and the previous and the next) for the first time
guis_doTabCreationAtStartup();
// On startup, set current GUIname and GUIlist according to last state before going to sleep
guis_doTabCreationOnStartup();
// memoryUsage bar
init_gui_memoryUsage_bar();
// status bar
Expand Down Expand Up @@ -272,6 +271,7 @@ void init_gui_status_bar() {
lv_obj_align(SceneLabel, LV_ALIGN_TOP_MID, 0, labelsPositionTopStatusbar);
lv_obj_set_style_text_font(SceneLabel, &lv_font_montserrat_12, LV_PART_MAIN);
lv_obj_add_flag(SceneLabel, LV_OBJ_FLAG_CLICKABLE);
lv_obj_set_user_data(SceneLabel,(void *)(intptr_t)0);
lv_obj_add_event_cb(SceneLabel, sceneLabel_or_pageIndicator_event_cb, LV_EVENT_CLICKED, NULL);

// Battery ----------------------------------------------------------------------
Expand All @@ -294,32 +294,48 @@ void gui_loop(void) {
waitBeforeActionAfterSlidingAnimationEnded = false;
} else if (waitOneLoop) {
waitOneLoop = false;
guis_doAfterSliding(oldTabID, currentTabID, false);
guis_doTabCreationAfterSliding(newTabID_forLateTabCreation);
};
// // as alternative, we could wait some milliseconds. But one cycle of gui_loop() works well.
// if (waitBeforeActionAfterSlidingAnimationEnded) {
// if (millis() - waitBeforeActionAfterSlidingAnimationEnded_timerStart >= 5) {
// guis_doAfterSliding(oldTabID, currentTabID, false);
// guis_doTabCreationAfterSliding(newTabID_forLateTabCreation);
// waitBeforeActionAfterSlidingAnimationEnded = false;
// }
// }

lv_timer_handler();
}

void guis_doTabCreationAtStartup() {
gui_memoryOptimizer_prepare_startup();

guis_doAfterSliding(-1, -1, false);
// ------------------------------------------------------------------------------------------------------------
// There are several reasons why the tabs could get recreated. All are going through these functions in "guiBase.cpp", which are calling functions in "guiMemoryOptimizer.cpp"
// 1. tab creation on startup (called by init_gui())
void guis_doTabCreationOnStartup() {
gui_memoryOptimizer_onStartup(&tabview, &panel, &img1, &img2);
doLogMemoryUsage();
}

void guis_doAfterSliding(int oldTabID, int newTabID, bool newGuiList) {
// With parameter newGuiList it is signaled that we are changing from a scene specific list to the main list or vice versa
// In that case, we have to do special treatment because we are not simply sliding to the left or to the right, but we start newly with a different gui list.
gui_memoryOptimizer_doAfterSliding_deletionAndCreation(&tabview, oldTabID, newTabID, newGuiList, &panel, &img1, &img2);

// 2. tab creation after sliding (called by tabview_tab_changed_event_cb())
void guis_doTabCreationAfterSliding(int newTabID) {
gui_memoryOptimizer_afterSliding(&tabview, &panel, &img1, &img2, newTabID);
doLogMemoryUsage();
}
// 3. after gui list has changed (called by handleScene()), when switching between main_gui_list and scene specific list. Will show first GUi in list
void guis_doTabCreationAfterGUIlistChanged(GUIlists newGUIlist) {
gui_memoryOptimizer_afterGUIlistChanged(&tabview, &panel, &img1, &img2, newGUIlist);
doLogMemoryUsage();
}
// 4. navigate to a specific GUI in gui_list
void guis_doTabCreationForSpecificGUI(GUIlists GUIlist, int gui_list_index) {
gui_memoryOptimizer_navigateToGUI(&tabview, &panel, &img1, &img2, GUIlist, gui_list_index);
doLogMemoryUsage();
}
// 5. navigate back to last active gui of previous gui_list
void guis_doTabCreationForNavigateToLastActiveGUIofPreviousGUIlist() {
gui_memoryOptimizer_navigateToLastActiveGUIofPreviousGUIlist(&tabview, &panel, &img1, &img2);
doLogMemoryUsage();
}

// ------------------------------------------------------------------------------------------------------------

void setActiveTab(uint32_t index, lv_anim_enable_t anim_en, bool send_tab_changed_event) {
// unsigned long startTime = millis();
Expand Down
12 changes: 8 additions & 4 deletions Platformio/src/applicationInternal/gui/guiBase.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <lvgl.h>
#include "applicationInternal/gui/guiMemoryOptimizer.h"

// used by memoryUsage.cpp
extern lv_span_t* MemoryUsageSpanHeap;
Expand All @@ -17,7 +18,6 @@ extern lv_style_t panel_style;
extern int tabviewTop;
extern int tabviewHeight;
extern int panelHeight;
extern uint32_t currentTabID;
// used by almost all gui_*.cpp
extern lv_color_t color_primary;

Expand All @@ -26,18 +26,22 @@ extern lv_color_t color_primary;
extern lv_style_t style_red_border;
#endif

// used by main.cpp and sceneHandler.cpp
// used by main.cpp
void init_gui(void);
// used by main.cpp and sceneHandler.cpp
void gui_loop(void);
// used by guiMemoryOptimizer.cpp
void tabview_content_is_scrolling_event_cb(lv_event_t* e);
void tabview_tab_changed_event_cb(lv_event_t* e);
void sceneLabel_or_pageIndicator_event_cb(lv_event_t* e);
void pageIndicator_navigate_event_cb(lv_event_t* e);
// used by sceneHandler.cpp
void guis_doTabCreationAfterGUIlistChanged(GUIlists newGUIlist);
void guis_doTabCreationForSpecificGUI(GUIlists GUIlist, int gui_list_index);
void guis_doTabCreationForNavigateToLastActiveGUIofPreviousGUIlist();
// used by guiMemoryOptimizer.cpp and sceneHandler.cpp
void setActiveTab(uint32_t index, lv_anim_enable_t anim_en, bool send_tab_changed_event = false);
// used by memoryUsage.cpp
void showMemoryUsageBar(bool showBar);
// used by commandHandler to show WiFi status
void showWiFiConnected(bool connected);

void guis_doAfterSliding(int oldTabID, int newTabID, bool newGuiList);
Loading

0 comments on commit d69913c

Please sign in to comment.