Skip to content

Commit 9d0f529

Browse files
committed
Use screenHistory in a different way. Create menu objects as shared_ptr with awareness of the current screen object.
1 parent 4201cab commit 9d0f529

3 files changed

Lines changed: 19 additions & 19 deletions

File tree

lib/3rd_party_adapters/LVGL/GuiEngine.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "GuiEngine.hpp"
66
#include "Screen.hpp"
77
#include <Adafruit_SSD1306.h>
8+
#include <memory>
89
#include <user_interaction/MenuItem.hpp>
910

1011
/* lvgl log functionality via serial interface */
@@ -212,6 +213,6 @@ void GuiEngine::refresh()
212213
*/
213214
void GuiEngine::drawMenu(const MenuItemList *menuList)
214215
{
215-
auto ptrMenuScreen = new ScreenMenu{*menuList};
216-
ptrMenuScreen->draw();
216+
CurrentScreen = std::make_shared<ScreenMenu>(*menuList);
217+
CurrentScreen->draw();
217218
}

lib/3rd_party_adapters/LVGL/Screen.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,25 @@
22
#include <math.h>
33
#include <stack>
44

5+
std::shared_ptr<IScreen> CurrentScreen;
56
static std::stack<std::shared_ptr<IScreen>> screenHistory;
67

78
static inline void IScreen_leave()
89
{
910
//only leave if we have something to go back to
10-
if (screenHistory.size() > 1)
11+
if (screenHistory.size() > 0)
1112
{
1213
//clear the current screen of lvgl
1314
lv_obj_clean(lv_scr_act());
1415

15-
//remove current screen
16-
screenHistory.pop();
1716
//draw previous screen
1817
screenHistory.top()->draw();
19-
//remove it once again as the draw function has added it again
18+
//remove current screen
2019
screenHistory.pop();
2120
}
2221
else
2322
{
24-
LV_LOG_INFO("I won't leave the first screen!");
23+
LV_LOG_INFO("No previous screen!");
2524
}
2625
}
2726

@@ -40,10 +39,12 @@ static void ScreenMenu_submenu_cb(lv_event_t *e)
4039

4140
if ((code == LV_EVENT_SHORT_CLICKED) && (item != nullptr))
4241
{
42+
//save current screen in history
43+
screenHistory.push(CurrentScreen);
4344
//if we were clicked shortly, draw the next menu screen
4445
lv_obj_clean(lv_scr_act());
45-
auto ptrSubMenuScreen = new ScreenMenu{*item->getSubMenuList()};
46-
ptrSubMenuScreen->draw();
46+
CurrentScreen = std::make_shared<ScreenMenu>(*item->getSubMenuList());
47+
CurrentScreen->draw();
4748
}
4849
else if (code == LV_EVENT_KEY)
4950
{
@@ -104,9 +105,11 @@ static void ScreenMenu_value_cb(lv_event_t *e)
104105

105106
if ((code == LV_EVENT_SHORT_CLICKED) && (item != nullptr))
106107
{
108+
//save current screen in history
109+
screenHistory.push(CurrentScreen);
107110
//if we were clicked shortly, draw the value modification screen
108-
auto ValModifyScreen = new ScreenValueModifier{item};
109-
ValModifyScreen->draw();
111+
CurrentScreen = std::make_shared<ScreenValueModifier>(item);
112+
CurrentScreen->draw();
110113
}
111114
else if (code == LV_EVENT_KEY)
112115
{
@@ -253,9 +256,6 @@ void ScreenMenu::draw()
253256

254257
/* actually draw the screen with lvgl */
255258
lv_scr_load(screen);
256-
257-
/* add the screen to the screens history list */
258-
screenHistory.push(shared_from_this());
259259
}
260260

261261
/**
@@ -464,7 +464,4 @@ void ScreenValueModifier::draw()
464464

465465
/* actually draw the screen with lvgl */
466466
lv_scr_load(screen);
467-
468-
/* add the screen to the screens history list */
469-
screenHistory.push(shared_from_this());
470467
}

lib/3rd_party_adapters/LVGL/Screen.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class IScreen
2626
* it receives a list of menu items for it's topology
2727
*
2828
*/
29-
class ScreenMenu final : public IScreen, std::enable_shared_from_this<ScreenMenu>
29+
class ScreenMenu final : public IScreen
3030
{
3131
public:
3232
ScreenMenu(MenuItemList itemList);
@@ -42,7 +42,7 @@ class ScreenMenu final : public IScreen, std::enable_shared_from_this<ScreenMenu
4242
* @brief Screen for value modification
4343
*
4444
*/
45-
class ScreenValueModifier final : public IScreen, std::enable_shared_from_this<ScreenValueModifier>
45+
class ScreenValueModifier final : public IScreen
4646
{
4747
public:
4848
ScreenValueModifier(const MenuItemValue *const menuItem);
@@ -53,3 +53,5 @@ class ScreenValueModifier final : public IScreen, std::enable_shared_from_this<S
5353
const MenuItemValue *const _menuItem;
5454
lv_obj_t *_spinbox;
5555
};
56+
57+
extern std::shared_ptr<IScreen> CurrentScreen;

0 commit comments

Comments
 (0)