diff options
-rw-r--r-- | CMakeLists.txt | 8 | ||||
-rw-r--r-- | api.cpp | 1 | ||||
-rw-r--r-- | api.hpp | 5 | ||||
-rw-r--r-- | app_manager.cpp | 109 | ||||
-rw-r--r-- | app_manager.hpp | 8 | ||||
-rw-r--r-- | apps/home_menu/main.cpp (renamed from apps/home_menu.cpp) | 9 | ||||
-rw-r--r-- | apps/home_menu/main.hpp (renamed from apps/home_menu.hpp) | 8 | ||||
-rw-r--r-- | apps/main_clock.cpp | 96 | ||||
-rw-r--r-- | apps/main_clock.hpp | 17 | ||||
-rw-r--r-- | apps/main_clock/main.cpp | 93 | ||||
-rw-r--r-- | apps/main_clock/main.hpp | 36 | ||||
-rw-r--r-- | apps/settings/main.cpp | 1 | ||||
-rw-r--r-- | base_app.hpp | 6 | ||||
-rw-r--r-- | buttons.cpp | 3 | ||||
-rw-r--r-- | buttons.hpp | 31 | ||||
-rw-r--r-- | globals.hpp | 28 | ||||
-rw-r--r-- | pico-watch.cpp | 5 |
17 files changed, 238 insertions, 226 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 1cbc55d..fe8dc50 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,10 +47,10 @@ add_executable(pico-watch buttons.hpp api.cpp api.hpp - apps/home_menu.cpp - apps/home_menu.hpp - apps/main_clock.cpp - apps/main_clock.hpp + apps/home_menu/main.cpp + apps/home_menu/main.hpp + apps/main_clock/main.cpp + apps/main_clock/main.hpp apps/settings/main.cpp apps/settings/main.hpp ) @@ -6,6 +6,7 @@ extern "C" { #include "api.hpp" #include "init.hpp" +#include "globals.hpp" void Api::init() { if (!m_init_done) { @@ -1,5 +1,4 @@ -#ifndef __API_H__ -#define __API_H__ +#pragma once #include <iostream> #include "pico/util/datetime.h" @@ -125,5 +124,3 @@ class Api { // Set last button pressed, should only be called by button gpio interrupt. void button_last_set(uint gpio); }; - -#endif
\ No newline at end of file diff --git a/app_manager.cpp b/app_manager.cpp index 2926921..bd71c6a 100644 --- a/app_manager.cpp +++ b/app_manager.cpp @@ -1,7 +1,10 @@ +#include <algorithm> + #include "app_manager.hpp" #include "api.hpp" -#include "apps/main_clock.hpp" -#include "apps/home_menu.hpp" +#include "globals.hpp" +#include "apps/main_clock/main.hpp" +#include "apps/home_menu/main.hpp" #include "apps/settings/main.hpp" #define NUMBER_OF_APPS 3 @@ -10,18 +13,32 @@ extern Api app_api; std::vector<BaseApp*> open_apps; int APPS_DESTROY_ON_EXIT[NUMBER_OF_APPS] = {0, 1, 1}; -int APPS_IS_INIT[NUMBER_OF_APPS] = {0}; // Only run in background if init + +// Check if the specified app (via app_id) is already running. +// \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) + return app; + } + return nullptr; +} // Called by app_init to create the app object. -int app_create(int app_id) { +BaseApp* app_create(int app_id) { switch (app_id) { case 0: open_apps.push_back(new app_home_menu(&app_api)); break; - default: __breakpoint(); + case 1: open_apps.push_back(new app_main_clock(&app_api)); break; + default: __breakpoint(); return open_apps.front(); // Should be home_menu } + // TODO: Check when new fails + + return open_apps.back(); } // Init a new app, that is not running. -int app_init(int app_id) { +BaseApp* app_init(int app_id) { + BaseApp* new_app; app_api.display_fill(0,1); // Clear OLED app_api.performance_render_interval_set(500); // Reset interval @@ -30,66 +47,34 @@ int app_init(int app_id) { return app_init(0); } - if (!APPS_IS_INIT[app_id]) { - app_create(app_id); - /* int status = (*APPS_FUNC_INIT[app_id])(&app_api); - - switch (status) { - case Api::app_init_return_status::MALLOC_FAILED: - printf("App init failed (alloc), "); - for (int i=0; i<10; i++) { - if ((*APPS_FUNC_INIT[app_id])(&app_api) != Api::app_init_return_status::MALLOC_FAILED) { - printf("worked after %d tries\n", i); - APPS_IS_INIT[app_id] = 1; - return app_id; - } - } - // Instead, the current app could continue running: return current_app - printf("gave up, launching app 0\n"); - return app_init(0); // Note: this has to work (and should) - - default: // OK and unhandled status codes - printf("App init, status: %d\n", status); - break; - } */ - APPS_IS_INIT[app_id] = 1; - } - return app_id; + auto app_ptr = app_check_if_init(app_id); + if (app_ptr) + new_app = app_ptr; + else + new_app = app_create(app_id); + + return new_app; } // Allow the running app, referenced by app_id, to invoke its render routine. -int app_render(int app_id) { - for (auto app : open_apps) { - if (app_id == app->app_id) - return app->render(&app_api); - } +int app_render(BaseApp* app) { + return app->render(&app_api); } // Delta is in ms, from time_since_button_press() -int app_btnpressed(int app_id, uint gpio, unsigned long delta) { - for (auto app : open_apps) { - if (app_id == app->app_id) - return app->btnpressed(&app_api, gpio, delta); - } +int app_btnpressed(BaseApp* app, uint gpio, unsigned long delta) { + return app->btnpressed(&app_api, gpio, delta); } // Quit the app referenced by the app_id. -int app_destroy(int app_id) { - BaseApp* to_destroy; - - // FIXME: Does not work if the app is not open. An invalid item tries to get freed. - int to_delete_id = 0; - for (auto app : open_apps) { - if (app_id == app->app_id) { - to_destroy = app; - break; - } - to_delete_id++; +int app_destroy(BaseApp* to_erase) { + auto erase_it = std::find(open_apps.begin(), open_apps.end(), to_erase); // "it" meaning iterator + if (erase_it != open_apps.end()) { + //assert(to_erase == erase_it); + delete to_erase; + open_apps.erase(erase_it); } - delete to_destroy; - open_apps.erase(open_apps.begin() + to_delete_id); - APPS_IS_INIT[app_id] = 0; return 0; } @@ -101,10 +86,16 @@ void app_switch_request(int to_appid) { app_api.performance_render_interval_set(0); // This will be reset on new app init } -void app_switch(int old_appid, int new_appid) { +void app_switch(BaseApp* app, int new_appid) { g_s.app_ready = false; - if (APPS_DESTROY_ON_EXIT[old_appid]) - app_destroy(old_appid); - g_s.current_app = app_init(new_appid); + if (app->app_destroy_on_exit) + app_destroy(app); + + auto app_ptr = app_check_if_init(new_appid); + if (app_ptr) + g_s.current_app = app_ptr; + else + g_s.current_app = app_init(new_appid); + g_s.app_ready = true; } diff --git a/app_manager.hpp b/app_manager.hpp index 6517ce9..b1d7a9c 100644 --- a/app_manager.hpp +++ b/app_manager.hpp @@ -4,8 +4,8 @@ extern std::vector<BaseApp*> open_apps; -int app_init(int app_id); -int app_render(int app_id); -int app_btnpressed(int app_id, uint gpio, unsigned long delta); -void app_switch(int old_appid, int new_appid); +BaseApp* app_init(int app_id); +int app_render(BaseApp* app); +int app_btnpressed(BaseApp* app, uint gpio, unsigned long delta); +void app_switch(BaseApp* app, int new_appid); void app_switch_request(int to_appid); diff --git a/apps/home_menu.cpp b/apps/home_menu/main.cpp index 4761c46..02965c3 100644 --- a/apps/home_menu.cpp +++ b/apps/home_menu/main.cpp @@ -1,11 +1,10 @@ #include <stdio.h> #include "pico/stdlib.h" -#include "home_menu.hpp" -#include "../api.hpp" -#include "../buttons.hpp" +#include "../../globals.hpp" +#include "../../app_manager.hpp" +#include "main.hpp" -extern void app_switch(int old_appid, int new_appid); extern bool rtc_get_datetime(datetime_t *t); void app_home_menu::title_str(char *buf, uint buf_size, const datetime_t *t) { @@ -40,7 +39,7 @@ int app_home_menu::render(Api *app_api) { int app_home_menu::btnpressed(Api *app_api, uint gpio, unsigned long delta) { switch (gpio) { case BUTTON_SELECT: - app_switch(0, selected_app); // FIXME: Should call app_switch_request instead + app_switch_request( selected_app); return 0; case BUTTON_DOWN: selected_app--; diff --git a/apps/home_menu.hpp b/apps/home_menu/main.hpp index dce83e4..421789b 100644 --- a/apps/home_menu.hpp +++ b/apps/home_menu/main.hpp @@ -1,10 +1,9 @@ #pragma once #include "pico/util/datetime.h" -#include "../oled/ss_oled.h" -#include "../api.hpp" -#include "../base_app.hpp" +// Includes also buttons, API and ss_oled +#include "../../base_app.hpp" #define NUMBER_OF_APPS 3 #define SIZE_APP_NAME 12 @@ -14,10 +13,13 @@ class app_home_menu : public BaseApp { const char *APPS_NAME[NUMBER_OF_APPS] = {"Home", "Clock", "Settings"}; int selected_app = 0; char display_app_name[SIZE_APP_NAME]; + void title_str(char *buf, uint buf_size, const datetime_t *t); void show_title(Api *app_api); public: uint app_id = 0; + bool app_destroy_on_exit = false; + app_home_menu(Api *app_api); int render(Api *app_api); int btnpressed(Api *app_api, uint gpio, unsigned long delta); diff --git a/apps/main_clock.cpp b/apps/main_clock.cpp deleted file mode 100644 index 24a4dfb..0000000 --- a/apps/main_clock.cpp +++ /dev/null @@ -1,96 +0,0 @@ -#include <stdio.h> -#include "pico/stdlib.h" - -#include "main_clock.hpp" -#include "../api.hpp" -#include "../buttons.hpp" - -namespace app_main_clock { - bool *ask_user_choice; - int *user_choice; - const char *choices[] = {"One", "Two", "Three!", "This is looong!", "make sure to choose me!:p"}; - // Time as string - // Adapted from pico-sdk/scr/common/pico_util/datetime.c - void time_as_str(char *buf, uint buf_size, const datetime_t *t) { - snprintf(buf, - buf_size, - "%d:%02d:%02d ", - t->hour, - t->min, - t->sec); - }; - void date_as_str(char *buf, uint buf_size, const datetime_t *t) { - static const char *DATETIME_DOWS[7] = { - "Sun", - "Mon", - "Tue", - "Wed", - "Thu", - "Fri", - "Sat", - }; - snprintf(buf, - buf_size, - "%02d-%02d-%02d %s", - t->year, - t->month, - t->day, - DATETIME_DOWS[t->dotw]); - }; - - void show_datetime(Api *app_api) { - char datetime_buf[256]; - char *datetime_str = &datetime_buf[0]; - datetime_t t; - app_api->datetime_get(&t); - - // time - time_as_str(datetime_str, sizeof(datetime_buf), &t); - app_api->display_write_string(0,10,3, datetime_str, FONT_12x16, 0, 1); - - // date - date_as_str(datetime_str, sizeof(datetime_buf), &t); - app_api->gui_footer_text((std::string)datetime_str); - } - - // Rendering of the app - int render(Api *app_api) { - app_api->gui_header_text("Test clock", 17); - show_datetime(app_api); - if (*ask_user_choice) { - *user_choice = app_api->gui_popup_strchoice("Ohh!", "Make a good choice:", choices, 5, 0, -1, *user_choice); - *ask_user_choice = false; - } - app_api->gui_footer_text(choices[*user_choice],0,1); - return 0; - } - - // Interpretation of button inputs - int btnpressed(Api *app_api, uint gpio, unsigned long delta) { - if (gpio == BUTTON_MODE) - *ask_user_choice = true; - return 0; - } - - // Initlisation of the app. - int init(Api *app_api) { - app_api->performance_set(Api::perf_modes::LOW_POWER); - ask_user_choice = new bool; *ask_user_choice = false; - user_choice = new int; *user_choice = 0; - if (!(user_choice or ask_user_choice)) - return Api::app_init_return_status::MALLOC_FAILED; - return Api::app_init_return_status::OK; - } - - // Processor intensive operations and functions related to drawing to the screen should only be done when the app is in_foreground(=1). This function is only called when the app is init. - int bgrefresh(Api *app_api, bool in_foreground) { - return 1; - } - - // Destruction of app, deinitlisation should be done here. This is only called if the app's APPS_DESTROY_ON_EXIT is set to 1. When it is not a "service" app. - int destroy(Api *app_api) { - delete ask_user_choice; ask_user_choice = 0; - delete user_choice; user_choice = 0; - return 0; - } -} diff --git a/apps/main_clock.hpp b/apps/main_clock.hpp deleted file mode 100644 index 72e02a4..0000000 --- a/apps/main_clock.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef __MAIN_CLOCK_H__ -#define __MAIN_CLOCK_H__ - -#include "pico/util/datetime.h" -#include "../oled/ss_oled.h" - -#include "../api.hpp" - -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); -} - -#endif
\ No newline at end of file diff --git a/apps/main_clock/main.cpp b/apps/main_clock/main.cpp new file mode 100644 index 0000000..78be6fd --- /dev/null +++ b/apps/main_clock/main.cpp @@ -0,0 +1,93 @@ +#include <stdio.h> +#include "pico/stdlib.h" + +#include "main.hpp" + +// TODO in this app +// - No need for ask_user_choice and user_choice to be a ptr as the app is now encapsulated in a class. + +// Time as string +// Adapted from pico-sdk/scr/common/pico_util/datetime.c +void app_main_clock::time_as_str(char *buf, uint buf_size, const datetime_t *t) { + snprintf(buf, + buf_size, + "%d:%02d:%02d ", + t->hour, + t->min, + t->sec); +}; + +void app_main_clock::date_as_str(char *buf, uint buf_size, const datetime_t *t) { + static const char *DATETIME_DOWS[7] = { + "Sun", + "Mon", + "Tue", + "Wed", + "Thu", + "Fri", + "Sat", + }; + snprintf(buf, + buf_size, + "%02d-%02d-%02d %s", + t->year, + t->month, + t->day, + DATETIME_DOWS[t->dotw]); +}; + +void app_main_clock::show_datetime(Api *app_api) { + char datetime_buf[256]; + char *datetime_str = &datetime_buf[0]; + datetime_t t; + app_api->datetime_get(&t); + + // time + time_as_str(datetime_str, sizeof(datetime_buf), &t); + app_api->display_write_string(0,10,3, datetime_str, FONT_12x16, 0, 1); + + // date + date_as_str(datetime_str, sizeof(datetime_buf), &t); + app_api->gui_footer_text((std::string)datetime_str); +} + +// Rendering of the app +int app_main_clock::render(Api *app_api) { + app_api->gui_header_text("Test clock", 17); + show_datetime(app_api); + if (*ask_user_choice) { + *user_choice = app_api->gui_popup_strchoice("Ohh!", "Make a good choice:", choices, 5, 0, -1, *user_choice); + *ask_user_choice = false; + } + app_api->gui_footer_text(choices[*user_choice],0,1); + return 0; +} + +// Interpretation of button inputs +int app_main_clock::btnpressed(Api *app_api, uint gpio, unsigned long delta) { + if (gpio == BUTTON_MODE) + *ask_user_choice = true; + return 0; +} + +// Initlisation of the app. +app_main_clock::app_main_clock(Api *app_api) { + app_api->performance_set(Api::perf_modes::LOW_POWER); + + ask_user_choice = new bool; *ask_user_choice = false; + user_choice = new int; *user_choice = 0; + // if (!(user_choice or ask_user_choice)) + // return Api::app_init_return_status::MALLOC_FAILED; + // return Api::app_init_return_status::OK; +} + +// Processor intensive operations and functions related to drawing to the screen should only be done when the app is in_foreground(=1). This function is only called when the app is init. +int app_main_clock::bgrefresh(Api *app_api, bool in_foreground) { + return 1; +} + +// Destruction of app, deinitlisation should be done here. This is only called if the app's APPS_DESTROY_ON_EXIT is set to 1. When it is not a "service" app. +app_main_clock::~app_main_clock() { + delete ask_user_choice; ask_user_choice = 0; + delete user_choice; user_choice = 0; +} diff --git a/apps/main_clock/main.hpp b/apps/main_clock/main.hpp new file mode 100644 index 0000000..37c9075 --- /dev/null +++ b/apps/main_clock/main.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include "pico/util/datetime.h" + +// Includes also buttons, API and ss_oled +#include "../../base_app.hpp" + +class app_main_clock : public BaseApp { + private: + bool *ask_user_choice; + int *user_choice; + const char *choices[26] = {"One", "Two", "Three!", "This is looong!", "make sure to choose me!:p"}; + + 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); + + public: + uint app_id = 1; + bool app_destroy_on_exit = true; + + app_main_clock(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); + ~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/apps/settings/main.cpp b/apps/settings/main.cpp index 5472adf..26c8e25 100644 --- a/apps/settings/main.cpp +++ b/apps/settings/main.cpp @@ -3,6 +3,7 @@ #include "main.hpp" #include "../../api.hpp" +#include "../../globals.hpp" extern void app_switch(int old_appid, int new_appid); extern bool rtc_get_datetime(datetime_t *t); diff --git a/base_app.hpp b/base_app.hpp index 58aeb2f..00122d8 100644 --- a/base_app.hpp +++ b/base_app.hpp @@ -4,7 +4,11 @@ // Base app class BaseApp { public: - uint app_id = 0; // CHECK: This has to be overwritten by derived classes + // 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 int render(Api *app_api) = 0; // Has to be implemented virtual int btnpressed(Api *app_api, uint gpio, unsigned long delta) {}; virtual int bgrefresh(Api *app_api, bool in_foreground) {}; diff --git a/buttons.cpp b/buttons.cpp index df2beef..23c5ab1 100644 --- a/buttons.cpp +++ b/buttons.cpp @@ -2,6 +2,7 @@ #include "pico/stdlib.h" #include "buttons.hpp" +#include "globals.hpp" #include "api.hpp" #include "app_manager.hpp" // From pico-watch.c: @@ -15,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 != 0)) // Home app + if (gpio == BUTTON_HOME && (g_s.current_app->app_id != 0)) // Home app app_switch_request(0); else app_btnpressed(g_s.current_app, gpio, delta_since_press); diff --git a/buttons.hpp b/buttons.hpp index c9a8b2d..f3ee020 100644 --- a/buttons.hpp +++ b/buttons.hpp @@ -1,5 +1,4 @@ -#ifndef __BUTTONS_H__ -#define __BUTTONS_H__ +#pragma once #include "init.hpp" // Init buttons used in conjuction with interrupts @@ -12,34 +11,6 @@ #define BUTTON_DOWN 20 #define BUTTON_UP 21 -struct global_status { - int current_app = 0; - bool is_sleeping = false; - bool app_ready = true; - bool app_switch_requested = false; - int app_switch_to_app = 0; - - // Debounce control - // See https://www.raspberrypi.org/forums/viewtopic.php?f=145&t=301522#p1812063 - // Time is currently shared between all buttons. - unsigned long button_last_pressed_time; - const int button_delay_time = 125; -}; - -struct user_settings { - unsigned char oled_contrast = OLED_DEFAULT_CONTRAST; - // In milliseconds - unsigned int sleep_delay = ENTER_SLEEP_DELAY; - // true: 24h, false: AM/PM - // TODO: Use an enum, but this would make programming the UI more complex. - bool time_format = true; -}; - -extern global_status g_s; -extern user_settings g_user; - void init_buttons(); void gpio_interrupt_cb(uint gpio, uint32_t events); unsigned long time_since_button_press(); - -#endif
\ No newline at end of file diff --git a/globals.hpp b/globals.hpp new file mode 100644 index 0000000..0f61f64 --- /dev/null +++ b/globals.hpp @@ -0,0 +1,28 @@ +#pragma once +#include "base_app.hpp" + +struct global_status { + BaseApp* current_app = 0; + bool is_sleeping = false; + bool app_ready = true; + bool app_switch_requested = false; + int app_switch_to_app = 0; + + // Debounce control + // See https://www.raspberrypi.org/forums/viewtopic.php?f=145&t=301522#p1812063 + // Time is currently shared between all buttons. + unsigned long button_last_pressed_time; + const int button_delay_time = 125; +}; + +struct user_settings { + unsigned char oled_contrast = OLED_DEFAULT_CONTRAST; + // In milliseconds + unsigned int sleep_delay = ENTER_SLEEP_DELAY; + // true: 24h, false: AM/PM + // TODO: Use an enum, but this would make programming the UI more complex. + bool time_format = true; +}; + +extern global_status g_s; +extern user_settings g_user;
\ No newline at end of file diff --git a/pico-watch.cpp b/pico-watch.cpp index 05ee208..1166716 100644 --- a/pico-watch.cpp +++ b/pico-watch.cpp @@ -8,6 +8,7 @@ #include "init.hpp" #include "api.hpp" #include "buttons.hpp" +#include "globals.hpp" #include "app_manager.hpp" global_status g_s; @@ -30,7 +31,7 @@ bool repeating_callback(struct repeating_timer *t) { // Refresh each app // should it be done when sleeping? for (auto app : open_apps) { - app->bgrefresh(&app_api, g_s.current_app == app->app_id); + app->bgrefresh(&app_api, true); // FIXME: second arg } return true; } @@ -43,7 +44,7 @@ int main() { struct repeating_timer timer; add_repeating_timer_ms(250, repeating_callback, NULL, &timer); // TODO: Execute on core1 - app_init(g_s.current_app); + g_s.current_app = app_init(0); while (1) { if (g_s.app_switch_requested) { |