Skip to content

Commit

Permalink
Properly reset drawer in emulator #404
Browse files Browse the repository at this point in the history
Signed-off-by: simonmicro <simon@simonmicro.de>
  • Loading branch information
simonmicro committed Sep 30, 2024
1 parent ebfa6f9 commit d280f13
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 3 deletions.
2 changes: 2 additions & 0 deletions emulator/src/Emulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "../include/Display.h"
#include "../include/Emulator.hpp"

#include "globals.h"
#include "osw_ui.h"
#include "osw_config.h"
#include "osw_config_keys.h"
Expand Down Expand Up @@ -318,6 +319,7 @@ void OswEmulator::doCleanup() {
OswUI::resetInstance();
OswHal::resetInstance();
OswLogger::resetInstance();
OswGlobals::resetGlobals();
this->cpustate = CPUState::deep;
this->wantCleanup = false;
}
Expand Down
3 changes: 3 additions & 0 deletions include/apps/OswAppDrawer.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class OswAppDrawer: public OswAppV2 {
this->apps.at(category).emplace_back(nullptr);
this->apps.at(category).back().set<T>();
};
#ifdef OSW_EMULATOR
void reset();
#endif

// Control functions, those will schedule their action for the next loop() of the drawer (preventing e.g. undefined behavior of switching apps while drawing)
void showDrawer();
Expand Down
5 changes: 5 additions & 0 deletions include/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ namespace OswGlobals {
RTC_DATA_ATTR extern size_t main_AppIndex;
extern OswAppDrawer main_mainDrawer;
extern std::unique_ptr<OswAppTutorial> main_tutorialApp;
extern bool main_delayedAppInit;

#ifdef OSW_EMULATOR
void resetGlobals();
#endif
}
15 changes: 15 additions & 0 deletions src/apps/OswAppDrawer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,21 @@ void OswAppDrawer::onButton(Button id, bool up, ButtonStateNames state) {
}

#ifdef OSW_EMULATOR
/**
* @brief allow re-use of the drawer instance in the emulator
*
*/
void OswAppDrawer::reset() {
for (auto& category : this->apps) {
for (auto& app : category.second) {
app.cleanup();
}
}
this->apps.clear();
this->current = nullptr;
this->highlightApp = nullptr;
}

void OswAppDrawer::onLoopDebug() {
if(this->current)
this->current->get()->onLoopDebug(); // forward to the current app
Expand Down
8 changes: 8 additions & 0 deletions src/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,12 @@ namespace OswGlobals {
RTC_DATA_ATTR size_t main_AppIndex = OswAppDrawer::UNDEFINED_SLEEP_APP_INDEX;
OswAppDrawer main_mainDrawer{&main_AppIndex};
std::unique_ptr<OswAppTutorial> main_tutorialApp;
bool main_delayedAppInit = true;

#ifdef OSW_EMULATOR
void resetGlobals() {
main_delayedAppInit = true;
main_mainDrawer.reset();
}
#endif
}
5 changes: 2 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ void setup() {
void loop() {
static time_t lastPowerUpdate = time(nullptr) + 2; // We consider a run of at least 2 seconds as "success"
static time_t nextTimezoneUpdate = time(nullptr) + 60; // Already done after sleep -> revisit in a while
static bool delayedAppInit = true;

// check possible interaction with ULP program
#if USE_ULP == 1
Expand Down Expand Up @@ -191,8 +190,8 @@ void loop() {
ESP.restart();
}

if (delayedAppInit) {
delayedAppInit = false;
if (OswGlobals::main_delayedAppInit) {
OswGlobals::main_delayedAppInit = false;

// TODO port all v1 apps to v2, to allow for lazy loading (or let them in compat mode)

Expand Down

0 comments on commit d280f13

Please sign in to comment.