diff options
author | ConfuSomu | 2021-08-01 13:48:15 -0400 |
---|---|---|
committer | ConfuSomu | 2021-08-01 13:48:15 -0400 |
commit | ec6c7be7ba57d4ac9f87b66c252fd23fa3f6eb13 (patch) | |
tree | 76a94d5a453b9bddc0fc36d9897c8604bca4e3fc | |
parent | 5d896b77910431cdaac2ee4bfbfa8bbc91ab39ef (diff) | |
download | pico-watch-ec6c7be7ba57d4ac9f87b66c252fd23fa3f6eb13.tar pico-watch-ec6c7be7ba57d4ac9f87b66c252fd23fa3f6eb13.tar.gz pico-watch-ec6c7be7ba57d4ac9f87b66c252fd23fa3f6eb13.zip |
Implement virtual getter for app attributes
This is done to allow each app to be able to set their own app
attributes without having them be overshadowed by BaseApp's defaults.
-rw-r--r-- | app_manager.cpp | 4 | ||||
-rw-r--r-- | apps/home_menu/main.hpp | 7 | ||||
-rw-r--r-- | apps/main_clock/main.hpp | 17 | ||||
-rw-r--r-- | base_app.hpp | 11 | ||||
-rw-r--r-- | buttons.cpp | 2 |
5 files changed, 21 insertions, 20 deletions
diff --git a/app_manager.cpp b/app_manager.cpp index bd71c6a..5bf10a6 100644 --- a/app_manager.cpp +++ b/app_manager.cpp @@ -18,7 +18,7 @@ int APPS_DESTROY_ON_EXIT[NUMBER_OF_APPS] = {0, 1, 1}; // \return If app is init, pointer to app, else nullptr (more or less 0). BaseApp* app_check_if_init(int app_id) { for (auto app : open_apps) { - if (app_id == app->app_id) + if (app_id == app->app_get_attributes().id) return app; } return nullptr; @@ -88,7 +88,7 @@ void app_switch_request(int to_appid) { void app_switch(BaseApp* app, int new_appid) { g_s.app_ready = false; - if (app->app_destroy_on_exit) + if (app->app_get_attributes().destroy_on_exit) app_destroy(app); auto app_ptr = app_check_if_init(new_appid); diff --git a/apps/home_menu/main.hpp b/apps/home_menu/main.hpp index 421789b..155009b 100644 --- a/apps/home_menu/main.hpp +++ b/apps/home_menu/main.hpp @@ -16,9 +16,12 @@ class app_home_menu : public BaseApp { void title_str(char *buf, uint buf_size, const datetime_t *t); void show_title(Api *app_api); + + AppAttributes app_attributes = {0, false}; public: - uint app_id = 0; - bool app_destroy_on_exit = false; + const AppAttributes& app_get_attributes() { + return app_attributes; + } app_home_menu(Api *app_api); int render(Api *app_api); diff --git a/apps/main_clock/main.hpp b/apps/main_clock/main.hpp index 37c9075..f00958e 100644 --- a/apps/main_clock/main.hpp +++ b/apps/main_clock/main.hpp @@ -14,10 +14,12 @@ class app_main_clock : public BaseApp { void time_as_str(char *buf, uint buf_size, const datetime_t *t); void date_as_str(char *buf, uint buf_size, const datetime_t *t); void show_datetime(Api *app_api); - + + AppAttributes app_attributes = {1, true}; public: - uint app_id = 1; - bool app_destroy_on_exit = true; + const AppAttributes& app_get_attributes() { + return app_attributes; + } app_main_clock(Api *app_api); int render(Api *app_api); @@ -25,12 +27,3 @@ class app_main_clock : public BaseApp { int bgrefresh(Api *app_api, bool in_foreground); ~app_main_clock(); }; - - -/* namespace app_main_clock { - int init(Api *app_api); - int render(Api *app_api); - int btnpressed(Api *app_api, uint gpio, unsigned long delta); - int bgrefresh(Api *app_api, bool in_foreground); - int destroy(Api *app_api); -} */ diff --git a/base_app.hpp b/base_app.hpp index 00122d8..c9d8e85 100644 --- a/base_app.hpp +++ b/base_app.hpp @@ -4,10 +4,15 @@ // Base app class BaseApp { public: + struct AppAttributes { + uint id = 0; + bool destroy_on_exit = true; + }; // CHECK: Following have to be overwritten by derived classes - // TODO: Replace this with a call to a function returning a struct with all app attributes. - uint app_id = 0; - bool app_destroy_on_exit = true; + virtual const AppAttributes& app_get_attributes() = 0; + // Could be implemented as: + // {return app_attributes;} + // where app_attribues is an instance of AppAttributes virtual int render(Api *app_api) = 0; // Has to be implemented virtual int btnpressed(Api *app_api, uint gpio, unsigned long delta) {}; diff --git a/buttons.cpp b/buttons.cpp index 23c5ab1..df947db 100644 --- a/buttons.cpp +++ b/buttons.cpp @@ -16,7 +16,7 @@ void gpio_interrupt_cb(uint gpio, uint32_t events) { if (delta_since_press > g_s.button_delay_time) { if (app_api.m_interpret_button_press) { - if (gpio == BUTTON_HOME && (g_s.current_app->app_id != 0)) // Home app + if (gpio == BUTTON_HOME && (g_s.current_app->app_get_attributes().id != 0)) // Home app app_switch_request(0); else app_btnpressed(g_s.current_app, gpio, delta_since_press); |