diff --git a/Platformio/src/device_appleTV/gui_appleTV.cpp b/Platformio/src/device_appleTV/gui_appleTV.cpp index f2366e2..f24695d 100644 --- a/Platformio/src/device_appleTV/gui_appleTV.cpp +++ b/Platformio/src/device_appleTV/gui_appleTV.cpp @@ -1,6 +1,7 @@ #include #include "device_appleTV/device_appleTV.h" #include "gui_general_and_keys/gui_base.h" +#include "gui_general_and_keys/gui_registry.h" #include "hardware/tft.h" #include "commandHandler.h" @@ -16,7 +17,7 @@ static void appleKey_event_cb(lv_event_t* e) { Serial.println(50 + (int)e->user_data); } -void init_gui_appleTV(lv_obj_t* tabview) { +void init_gui_tab_appleTV(lv_obj_t* tabview) { lv_obj_t* tab = lv_tabview_add_tab(tabview, "Apple TV"); @@ -54,7 +55,7 @@ void init_gui_appleTV(lv_obj_t* tabview) { } -void init_gui_pageIndicator_appleTV() { +void init_gui_pageIndicator_appleTV(lv_obj_t* panel) { // Create actual (non-clickable) buttons for every tab lv_obj_t* btn = lv_btn_create(panel); lv_obj_clear_flag(btn, LV_OBJ_FLAG_CLICKABLE); @@ -66,3 +67,7 @@ void init_gui_pageIndicator_appleTV() { lv_obj_set_style_bg_color(btn, color_primary, LV_PART_MAIN); } + +void register_gui_appleTV(void){ + register_gui(& init_gui_tab_appleTV, & init_gui_pageIndicator_appleTV); +} diff --git a/Platformio/src/device_appleTV/gui_appleTV.h b/Platformio/src/device_appleTV/gui_appleTV.h index 15a4c47..b7dd127 100644 --- a/Platformio/src/device_appleTV/gui_appleTV.h +++ b/Platformio/src/device_appleTV/gui_appleTV.h @@ -3,7 +3,6 @@ #include -void init_gui_appleTV(lv_obj_t* tabview); -void init_gui_pageIndicator_appleTV(); +void register_gui_appleTV(void); #endif /*__GUI_APPLETV_H__*/ diff --git a/Platformio/src/device_smarthome/gui_smarthome.cpp b/Platformio/src/device_smarthome/gui_smarthome.cpp index 2c869c3..e94d704 100644 --- a/Platformio/src/device_smarthome/gui_smarthome.cpp +++ b/Platformio/src/device_smarthome/gui_smarthome.cpp @@ -1,6 +1,7 @@ #include #include #include "gui_general_and_keys/gui_base.h" +#include "gui_general_and_keys/gui_registry.h" #include "hardware/tft.h" #include "device_smarthome/device_smarthome.h" #include "commandHandler.h" @@ -33,7 +34,7 @@ static void smartHomeSlider_event_cb(lv_event_t * e){ #endif } -void init_gui_smarthome(lv_obj_t* tabview) { +void init_gui_tab_smarthome(lv_obj_t* tabview) { lv_obj_t* tab = lv_tabview_add_tab(tabview, "Smart Home"); @@ -127,7 +128,7 @@ void init_gui_smarthome(lv_obj_t* tabview) { } -void init_gui_pageIndicator_smarthome() { +void init_gui_pageIndicator_smarthome(lv_obj_t* panel) { // Create actual (non-clickable) buttons for every tab lv_obj_t* btn = lv_btn_create(panel); lv_obj_clear_flag(btn, LV_OBJ_FLAG_CLICKABLE); @@ -139,3 +140,7 @@ void init_gui_pageIndicator_smarthome() { lv_obj_set_style_bg_color(btn, color_primary, LV_PART_MAIN); } + +void register_gui_smarthome(void){ + register_gui(& init_gui_tab_smarthome, & init_gui_pageIndicator_smarthome); +} diff --git a/Platformio/src/device_smarthome/gui_smarthome.h b/Platformio/src/device_smarthome/gui_smarthome.h index 1e587a0..c283431 100644 --- a/Platformio/src/device_smarthome/gui_smarthome.h +++ b/Platformio/src/device_smarthome/gui_smarthome.h @@ -3,7 +3,6 @@ #include -void init_gui_smarthome(lv_obj_t* tabview); -void init_gui_pageIndicator_smarthome(); +void register_gui_smarthome(void); #endif /*__GUI_SMARTHOME_H__*/ diff --git a/Platformio/src/gui_general_and_keys/gui_base.cpp b/Platformio/src/gui_general_and_keys/gui_base.cpp index 1ebadf1..e1a5100 100644 --- a/Platformio/src/gui_general_and_keys/gui_base.cpp +++ b/Platformio/src/gui_general_and_keys/gui_base.cpp @@ -2,11 +2,7 @@ #include "hardware/tft.h" #include "hardware/sleep.h" #include "gui_general_and_keys/gui_base.h" -#include "gui_general_and_keys/gui_irReceiver.h" -#include "gui_general_and_keys/gui_numpad.h" -#include "gui_general_and_keys/gui_settings.h" -#include "device_appleTV/gui_appleTV.h" -#include "device_smarthome/gui_smarthome.h" +#include "gui_general_and_keys/gui_registry.h" lv_obj_t* panel; lv_color_t color_primary = lv_color_hex(0x303030); // gray @@ -123,11 +119,7 @@ void init_gui(void) { lv_obj_align(tabview, LV_ALIGN_TOP_MID, 0, 20); // Add all the tabs here - init_gui_irReceiver(tabview); - init_gui_settings(tabview); - init_gui_numpad(tabview); - init_gui_appleTV(tabview); - init_gui_smarthome(tabview); + create_gui_tabs_from_gui_registry(tabview); // Set current page according to the current screen lv_tabview_set_act(tabview, currentScreen, LV_ANIM_OFF); @@ -147,11 +139,7 @@ void init_gui(void) { lv_obj_set_style_opa(btn, LV_OPA_TRANSP, LV_PART_MAIN); // Create actual (non-clickable) buttons for every tab - init_gui_pageIndicator_irReceiver(); - init_gui_pageIndicator_settings(); - init_gui_pageIndicator_numpad(); - init_gui_pageIndicator_appleTV(); - init_gui_pageIndicator_smarthome(); + create_gui_pageIndicators_from_gui_registry(panel); // This small hidden button enables the page indicator to scroll further btn = lv_btn_create(panel); diff --git a/Platformio/src/gui_general_and_keys/gui_base.h b/Platformio/src/gui_general_and_keys/gui_base.h index 43aee4e..c276f95 100644 --- a/Platformio/src/gui_general_and_keys/gui_base.h +++ b/Platformio/src/gui_general_and_keys/gui_base.h @@ -1,5 +1,5 @@ -#ifndef __GUI_H__ -#define __GUI_H__ +#ifndef __GUI_BASE_H__ +#define __GUI_BASE_H__ #include #include "../hardware/tft.h" @@ -9,7 +9,6 @@ LV_IMG_DECLARE(high_brightness); LV_IMG_DECLARE(low_brightness); extern lv_obj_t* objBattPercentage; extern lv_obj_t* objBattIcon; -extern lv_obj_t* panel; extern lv_color_t color_primary; extern lv_obj_t* WifiLabel; @@ -20,4 +19,4 @@ extern byte currentScreen; // Current screen that is shown void init_gui(void); void gui_loop(void); -#endif /*__GUI_H__*/ +#endif /*__GUI_BASE_H__*/ diff --git a/Platformio/src/gui_general_and_keys/gui_irReceiver.cpp b/Platformio/src/gui_general_and_keys/gui_irReceiver.cpp index fe7dac4..d73d130 100644 --- a/Platformio/src/gui_general_and_keys/gui_irReceiver.cpp +++ b/Platformio/src/gui_general_and_keys/gui_irReceiver.cpp @@ -5,6 +5,7 @@ #include "hardware/sleep.h" #include "hardware/infrared_receiver.h" #include "gui_general_and_keys/gui_base.h" +#include "gui_general_and_keys/gui_registry.h" lv_obj_t* menuBoxToggle; lv_obj_t* menuBoxMessages; @@ -82,7 +83,7 @@ static void IRReceiverOnSetting_event_cb(lv_event_t * e){ } } -void init_gui_irReceiver(lv_obj_t* tabview) { +void init_gui_tab_irReceiver(lv_obj_t* tabview) { lv_obj_t* tab = lv_tabview_add_tab(tabview, "IR Receiver"); @@ -121,7 +122,7 @@ void init_gui_irReceiver(lv_obj_t* tabview) { printReceivedMessages(true); } -void init_gui_pageIndicator_irReceiver() { +void init_gui_pageIndicator_irReceiver(lv_obj_t* panel) { // Create actual (non-clickable) buttons for every tab lv_obj_t* btn = lv_btn_create(panel); lv_obj_clear_flag(btn, LV_OBJ_FLAG_CLICKABLE); @@ -132,3 +133,7 @@ void init_gui_pageIndicator_irReceiver() { lv_obj_set_style_shadow_width(btn, 0, LV_PART_MAIN); lv_obj_set_style_bg_color(btn, color_primary, LV_PART_MAIN); } + +void register_gui_irReceiver(void){ + register_gui(& init_gui_tab_irReceiver, & init_gui_pageIndicator_irReceiver); +} diff --git a/Platformio/src/gui_general_and_keys/gui_irReceiver.h b/Platformio/src/gui_general_and_keys/gui_irReceiver.h index 435ee74..7320235 100644 --- a/Platformio/src/gui_general_and_keys/gui_irReceiver.h +++ b/Platformio/src/gui_general_and_keys/gui_irReceiver.h @@ -4,8 +4,7 @@ #include #include -void init_gui_irReceiver(lv_obj_t* tabview); -void init_gui_pageIndicator_irReceiver(); +void register_gui_irReceiver(void); void showNewIRmessage(String); #endif /*__GUI_IRRECEIVER_H__*/ diff --git a/Platformio/src/gui_general_and_keys/gui_numpad.cpp b/Platformio/src/gui_general_and_keys/gui_numpad.cpp index 13a4ef8..c34d11a 100644 --- a/Platformio/src/gui_general_and_keys/gui_numpad.cpp +++ b/Platformio/src/gui_general_and_keys/gui_numpad.cpp @@ -1,6 +1,7 @@ #include #include "hardware/tft.h" #include "gui_general_and_keys/gui_base.h" +#include "gui_general_and_keys/gui_registry.h" #include "commandHandler.h" // Virtual Keypad Event handler @@ -16,7 +17,7 @@ static void virtualKeypad_event_cb(lv_event_t* e) { executeCommand(KEYBOARD_SENDSTRING, numberStr); } -void init_gui_numpad(lv_obj_t* tabview) { +void init_gui_tab_numpad(lv_obj_t* tabview) { lv_obj_t* tab = lv_tabview_add_tab(tabview, "Numpad"); @@ -71,7 +72,7 @@ void init_gui_numpad(lv_obj_t* tabview) { } -void init_gui_pageIndicator_numpad() { +void init_gui_pageIndicator_numpad(lv_obj_t* panel) { // Create actual (non-clickable) buttons for every tab lv_obj_t* btn = lv_btn_create(panel); lv_obj_clear_flag(btn, LV_OBJ_FLAG_CLICKABLE); @@ -83,3 +84,7 @@ void init_gui_pageIndicator_numpad() { lv_obj_set_style_bg_color(btn, color_primary, LV_PART_MAIN); } + +void register_gui_numpad(void){ + register_gui(& init_gui_tab_numpad, & init_gui_pageIndicator_numpad); +} diff --git a/Platformio/src/gui_general_and_keys/gui_numpad.h b/Platformio/src/gui_general_and_keys/gui_numpad.h index c84b300..7cdb1af 100644 --- a/Platformio/src/gui_general_and_keys/gui_numpad.h +++ b/Platformio/src/gui_general_and_keys/gui_numpad.h @@ -3,7 +3,6 @@ #include -void init_gui_numpad(lv_obj_t* tabview); -void init_gui_pageIndicator_numpad(); +void register_gui_numpad(void); #endif /*__GUI_NUMPAD_H__*/ diff --git a/Platformio/src/gui_general_and_keys/gui_registry.cpp b/Platformio/src/gui_general_and_keys/gui_registry.cpp new file mode 100644 index 0000000..4a8f2d9 --- /dev/null +++ b/Platformio/src/gui_general_and_keys/gui_registry.cpp @@ -0,0 +1,31 @@ +#include +#include +#include +#include "gui_registry.h" + +// https://stackoverflow.com/questions/840501/how-do-function-pointers-in-c-work +struct gui_functions { + init_gui_tab this_init_gui_tab; + init_gui_pageIndicator this_init_gui_pageIndicator; +}; + +std::list registered_guis; + +void register_gui(init_gui_tab a_init_gui_tab, init_gui_pageIndicator a_gui_pageIndicator) { + registered_guis.push_back(gui_functions{a_init_gui_tab, a_gui_pageIndicator}); + +} + +void create_gui_tabs_from_gui_registry(lv_obj_t* tabview) { + std::list::iterator it; + for (it = registered_guis.begin(); it != registered_guis.end(); ++it) { + it->this_init_gui_tab(tabview); + } +} + +void create_gui_pageIndicators_from_gui_registry(lv_obj_t* panel) { + std::list::iterator it; + for (it = registered_guis.begin(); it != registered_guis.end(); ++it) { + it->this_init_gui_pageIndicator(panel); + } +} diff --git a/Platformio/src/gui_general_and_keys/gui_registry.h b/Platformio/src/gui_general_and_keys/gui_registry.h new file mode 100644 index 0000000..6c2f0ee --- /dev/null +++ b/Platformio/src/gui_general_and_keys/gui_registry.h @@ -0,0 +1,24 @@ +/* + If you want to create a new GUI for the touch screen, then + - copy one of the existing GUIs, e.g. gui_numpad.cpp and gui_numpad.h + - place the files in the right folder: + - in folder "gui_general_and_keys" if they are of general purpose, not only for a specific device + - in the device folder, if the GUI is specific for a certain device + - in folder "scenes" if the GUI is intended for a specific scene + It does not really matter where the files are placed, but doing so helps to keep a clean structure + - rename the functions, they must not have the same name as the ones in the file you copied from + - call "register_gui_();" in main.cpp + + Technically spoken, to register a GUI, you need to provide two function pointers: + - one for creating the tab + - one for creating the page indicator at the bottom of the tab +*/ +#include +#include + +typedef void (*init_gui_tab)(lv_obj_t* tabview); +typedef void (*init_gui_pageIndicator)(lv_obj_t* tabview); + +void register_gui(init_gui_tab a_init_gui_tab, init_gui_pageIndicator a_gui_pageIndicator); +void create_gui_tabs_from_gui_registry(lv_obj_t* tabview); +void create_gui_pageIndicators_from_gui_registry(lv_obj_t* panel); \ No newline at end of file diff --git a/Platformio/src/gui_general_and_keys/gui_settings.cpp b/Platformio/src/gui_general_and_keys/gui_settings.cpp index 4a1eab5..347a4f9 100644 --- a/Platformio/src/gui_general_and_keys/gui_settings.cpp +++ b/Platformio/src/gui_general_and_keys/gui_settings.cpp @@ -2,6 +2,7 @@ #include "hardware/tft.h" #include "hardware/sleep.h" #include "gui_general_and_keys/gui_base.h" +#include "gui_general_and_keys/gui_registry.h" lv_obj_t* objBattSettingsVoltage; lv_obj_t* objBattSettingsPercentage; @@ -35,7 +36,7 @@ static void timout_event_cb(lv_event_t * e){ resetStandbyTimer(); } -void init_gui_settings(lv_obj_t* tabview) { +void init_gui_tab_settings(lv_obj_t* tabview) { lv_obj_t* tab = lv_tabview_add_tab(tabview, "Settings"); @@ -156,7 +157,7 @@ void init_gui_settings(lv_obj_t* tabview) { // lv_obj_align(objBattSettingsIscharging, LV_ALIGN_TOP_LEFT, 0, 64); } -void init_gui_pageIndicator_settings() { +void init_gui_pageIndicator_settings(lv_obj_t* panel) { // Create actual (non-clickable) buttons for every tab lv_obj_t* btn = lv_btn_create(panel); lv_obj_clear_flag(btn, LV_OBJ_FLAG_CLICKABLE); @@ -168,3 +169,7 @@ void init_gui_pageIndicator_settings() { lv_obj_set_style_bg_color(btn, color_primary, LV_PART_MAIN); } + +void register_gui_settings(void){ + register_gui(& init_gui_tab_settings, & init_gui_pageIndicator_settings); +} diff --git a/Platformio/src/gui_general_and_keys/gui_settings.h b/Platformio/src/gui_general_and_keys/gui_settings.h index c512c77..94918bf 100644 --- a/Platformio/src/gui_general_and_keys/gui_settings.h +++ b/Platformio/src/gui_general_and_keys/gui_settings.h @@ -7,7 +7,6 @@ extern lv_obj_t* objBattSettingsVoltage; extern lv_obj_t* objBattSettingsPercentage; //extern lv_obj_t* objBattSettingsIscharging; -void init_gui_settings(lv_obj_t* tabview); -void init_gui_pageIndicator_settings(); +void register_gui_settings(void); #endif /*__GUI_SETTINGS_H__*/ diff --git a/Platformio/src/main.cpp b/Platformio/src/main.cpp index ff97405..2b8a075 100644 --- a/Platformio/src/main.cpp +++ b/Platformio/src/main.cpp @@ -18,6 +18,11 @@ #include "device_keyboard_ble/device_keyboard_ble.h" #include "gui_general_and_keys/keys.h" #include "gui_general_and_keys/gui_base.h" +#include "gui_general_and_keys/gui_irReceiver.h" +#include "gui_general_and_keys/gui_settings.h" +#include "gui_general_and_keys/gui_numpad.h" +#include "device_appleTV/gui_appleTV.h" +#include "device_smarthome/gui_smarthome.h" #include "preferences_storage.h" #include "commandHandler.h" @@ -43,6 +48,12 @@ void setup() { // init TFT init_tft(); + // Here you can register the GUIs you want to display. Will be displayed in the order they are registered. + register_gui_irReceiver(); + register_gui_settings(); + register_gui_numpad(); + register_gui_appleTV(); + register_gui_smarthome(); // init GUI init_gui();