diff --git a/Platformio/HAL/HardwareAbstract.hpp b/Platformio/HAL/HardwareAbstract.hpp index e9278734..784a3f14 100644 --- a/Platformio/HAL/HardwareAbstract.hpp +++ b/Platformio/HAL/HardwareAbstract.hpp @@ -17,6 +17,9 @@ class HardwareAbstract { /// @brief Override in order to do setup of hardware devices post construction virtual void init() = 0; + /// @brief Override to processing in main thread + virtual void loopHandler() = 0; + /// @brief Override to allow printing of a message for debugging /// @param message - Debug message virtual void debugPrint(const char *fmt, ...) = 0; @@ -34,7 +37,4 @@ class HardwareAbstract { virtual uint16_t getSleepTimeout() = 0; virtual void setSleepTimeout(uint16_t sleepTimeout) = 0; - -protected: - }; \ No newline at end of file diff --git a/Platformio/HAL/HardwareFactory.cpp b/Platformio/HAL/HardwareFactory.cpp new file mode 100644 index 00000000..e289393a --- /dev/null +++ b/Platformio/HAL/HardwareFactory.cpp @@ -0,0 +1,20 @@ +#include "HardwareFactory.hpp" + +#if OMOTE_SIM +#include "HardwareSimulator.hpp" +#endif + +#if OMOTE_ESP32 +#include "HardwareRevX.hpp" +#endif + +#if OMOTE_SIM +std::unique_ptr HardwareFactory::mHardware = + std::make_unique(); +#endif +#if OMOTE_ESP32 +std::unique_ptr HardwareFactory::mHardware = + std::make_unique(); +#endif + +HardwareAbstract &HardwareFactory::getAbstract() { return *mHardware; } \ No newline at end of file diff --git a/Platformio/HAL/HardwareFactory.hpp b/Platformio/HAL/HardwareFactory.hpp new file mode 100644 index 00000000..b840f39f --- /dev/null +++ b/Platformio/HAL/HardwareFactory.hpp @@ -0,0 +1,11 @@ +#include "HardwareAbstract.hpp" +#include +/** + * @brief The HardwareFactory is responsible for making the + */ +class HardwareFactory { +public: + static HardwareAbstract &getAbstract(); + + static std::unique_ptr mHardware; +}; diff --git a/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp b/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp index fd467efb..fe3ae2d1 100644 --- a/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp +++ b/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp @@ -2,8 +2,6 @@ #include "display.hpp" #include "wifihandler.hpp" -std::shared_ptr HardwareRevX::mInstance = nullptr; - void HardwareRevX::initIO() { // Button Pin Definition pinMode(SW_1, OUTPUT); @@ -99,13 +97,6 @@ void HardwareRevX::debugPrint(const char *fmt, ...) { Serial.print(result); } -std::shared_ptr HardwareRevX::getInstance() { - if (!mInstance) { - mInstance = std::shared_ptr(new HardwareRevX()); - } - return mInstance; -} - std::shared_ptr HardwareRevX::wifi() { return mWifiHandler; } diff --git a/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp b/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp index 43d424ea..4043db27 100644 --- a/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp +++ b/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp @@ -26,8 +26,7 @@ class HardwareRevX : public HardwareAbstract { public: enum class WakeReason { RESET, IMU, KEYPAD }; - static std::shared_ptr getInstance(); - static std::weak_ptr getRefrence() { return getInstance(); } + HardwareRevX(); // HardwareAbstract virtual void init() override; @@ -49,7 +48,7 @@ class HardwareRevX : public HardwareAbstract { /// @brief To be ran in loop out in main // TODO move to a freertos task - void loopHandler(); + void loopHandler() override; protected: // Init Functions to setup hardware @@ -67,8 +66,6 @@ class HardwareRevX : public HardwareAbstract { void startTasks(); private: - HardwareRevX(); - std::shared_ptr mBattery; std::shared_ptr mDisplay; std::shared_ptr mWifiHandler; diff --git a/Platformio/HAL/Targets/Simulator/HardwareSimulator.hpp b/Platformio/HAL/Targets/Simulator/HardwareSimulator.hpp index 1a688185..cd6d9669 100644 --- a/Platformio/HAL/Targets/Simulator/HardwareSimulator.hpp +++ b/Platformio/HAL/Targets/Simulator/HardwareSimulator.hpp @@ -12,28 +12,29 @@ class HardwareSimulator : public HardwareAbstract { public: HardwareSimulator(); - virtual void init() override{}; + void init() override{}; + void loopHandler() override{}; - virtual void debugPrint(const char *fmt, ...) override { + void debugPrint(const char *fmt, ...) override { va_list arguments; va_start(arguments, fmt); vprintf(fmt, arguments); va_end(arguments); } - virtual std::shared_ptr battery() override; - virtual std::shared_ptr display() override; - virtual std::shared_ptr wifi() override; - virtual std::shared_ptr keys() override; + std::shared_ptr battery() override; + std::shared_ptr display() override; + std::shared_ptr wifi() override; + std::shared_ptr keys() override; - virtual char getCurrentDevice() override; - virtual void setCurrentDevice(char currentDevice) override; + char getCurrentDevice() override; + void setCurrentDevice(char currentDevice) override; - virtual bool getWakeupByIMUEnabled() override; - virtual void setWakeupByIMUEnabled(bool wakeupByIMUEnabled) override; + bool getWakeupByIMUEnabled() override; + void setWakeupByIMUEnabled(bool wakeupByIMUEnabled) override; - virtual uint16_t getSleepTimeout() override; - virtual void setSleepTimeout(uint16_t sleepTimeout) override; + uint16_t getSleepTimeout() override; + void setSleepTimeout(uint16_t sleepTimeout) override; private: std::thread mTickThread; diff --git a/Platformio/OmoteUI/UIs/BasicRefactored/BasicUI.cpp b/Platformio/OmoteUI/UIs/BasicRefactored/BasicUI.cpp index 755aefd6..a66c5f53 100644 --- a/Platformio/OmoteUI/UIs/BasicRefactored/BasicUI.cpp +++ b/Platformio/OmoteUI/UIs/BasicRefactored/BasicUI.cpp @@ -1,20 +1,21 @@ #include "BasicUI.hpp" +#include "HardwareFactory.hpp" #include "HomeScreen.hpp" #include "ScreenManager.hpp" using namespace UI; -BasicUI::BasicUI(std::shared_ptr aHardware) - : UIBase(aHardware) { +BasicUI::BasicUI() : UIBase() { - aHardware->keys()->RegisterKeyPressHandler([](auto aKeyEvent) { - return Screen::Manager::getInstance().distributeKeyEvent(aKeyEvent); - // Could potentially add a check here and display that a key event was - // unused. - }); + HardwareFactory::getAbstract().keys()->RegisterKeyPressHandler( + [](auto aKeyEvent) { + return Screen::Manager::getInstance().distributeKeyEvent(aKeyEvent); + // Could potentially add a check here and display that a key event was + // unused. + }); Screen::Manager::getInstance().pushScreen( - std::make_unique(aHardware)); + std::make_unique()); - mHardware->wifi()->begin(); + HardwareFactory::getAbstract().wifi()->begin(); } \ No newline at end of file diff --git a/Platformio/OmoteUI/UIs/BasicRefactored/BasicUI.hpp b/Platformio/OmoteUI/UIs/BasicRefactored/BasicUI.hpp index 5bd6cb67..62a05d9a 100644 --- a/Platformio/OmoteUI/UIs/BasicRefactored/BasicUI.hpp +++ b/Platformio/OmoteUI/UIs/BasicRefactored/BasicUI.hpp @@ -5,7 +5,7 @@ namespace UI { class BasicUI : public UIBase { public: - BasicUI(std::shared_ptr aHardware); + BasicUI(); }; } // namespace UI \ No newline at end of file diff --git a/Platformio/OmoteUI/UIs/BasicRefactored/page/Demo.cpp b/Platformio/OmoteUI/UIs/BasicRefactored/page/Demo.cpp index c9213b92..a70c4ec3 100644 --- a/Platformio/OmoteUI/UIs/BasicRefactored/page/Demo.cpp +++ b/Platformio/OmoteUI/UIs/BasicRefactored/page/Demo.cpp @@ -3,17 +3,14 @@ using namespace UI::Page; -Demo::Demo(std::shared_ptr aHardware): Base(ID::Pages::Demo), mHardware(aHardware){ - - -} +Demo::Demo() : Base(ID::Pages::Demo) {} void Demo::AddSlider() { - auto fakeSlider = std::make_unique([](auto data){}); + auto fakeSlider = std::make_unique([](auto data) {}); fakeSlider->SetHeight(lv_pct(10)); fakeSlider->SetWidth(GetContentWidth()); if (sliders.empty()) { - fakeSlider->AlignTo(this,LV_ALIGN_TOP_MID); + fakeSlider->AlignTo(this, LV_ALIGN_TOP_MID); } else { auto nextY = sliders.back()->GetY() + sliders.back()->GetHeight(); fakeSlider->SetY(nextY + 10); diff --git a/Platformio/OmoteUI/UIs/BasicRefactored/page/Demo.hpp b/Platformio/OmoteUI/UIs/BasicRefactored/page/Demo.hpp index 63a839e0..5e010092 100644 --- a/Platformio/OmoteUI/UIs/BasicRefactored/page/Demo.hpp +++ b/Platformio/OmoteUI/UIs/BasicRefactored/page/Demo.hpp @@ -1,22 +1,20 @@ #pragma once #include "PageBase.hpp" -#include "HardwareAbstract.hpp" -namespace UI::Page{ +namespace UI::Page { -class Demo : public Base{ +class Demo : public Base { public: - Demo(std::shared_ptr aHardware); + Demo(); - void AddSlider(); + void AddSlider(); - void OnShow()override{}; - void OnHide()override{}; - bool OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent); + void OnShow() override{}; + void OnHide() override{}; + bool OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent); private: - std::shared_ptr mHardware; - std::vector sliders; + std::vector sliders; }; -} \ No newline at end of file +} // namespace UI::Page \ No newline at end of file diff --git a/Platformio/OmoteUI/UIs/BasicRefactored/page/SettingsPage.cpp b/Platformio/OmoteUI/UIs/BasicRefactored/page/SettingsPage.cpp index f101ba93..bcac6e99 100644 --- a/Platformio/OmoteUI/UIs/BasicRefactored/page/SettingsPage.cpp +++ b/Platformio/OmoteUI/UIs/BasicRefactored/page/SettingsPage.cpp @@ -3,6 +3,7 @@ #include "Button.hpp" #include "Colors.hpp" #include "DisplaySettings.hpp" +#include "HardwareFactory.hpp" #include "List.hpp" #include "PopUpScreen.hpp" #include "ScreenManager.hpp" @@ -13,10 +14,9 @@ using namespace UI::Page; using namespace UI::Color; -SettingsPage::SettingsPage(std::shared_ptr aHardware) - : Base(ID::Pages::Settings), - mSettingsList(AddElement(std::make_unique())), - mHardware(aHardware) { +SettingsPage::SettingsPage() + : Base(ID::Pages::Settings), mSettingsList(AddElement( + std::make_unique())) { mSettingsList->AddItem("Display", LV_SYMBOL_EYE_OPEN, [this] { PushDisplaySettings(); }); @@ -28,15 +28,16 @@ SettingsPage::SettingsPage(std::shared_ptr aHardware) void SettingsPage::PushDisplaySettings() { UI::Screen::Manager::getInstance().pushPopUp( - std::make_unique(mHardware->display())); + std::make_unique( + HardwareFactory::getAbstract().display())); } void SettingsPage::PushSystemSettings() { UI::Screen::Manager::getInstance().pushPopUp( - std::make_unique(mHardware)); + std::make_unique()); } void SettingsPage::PushWifiSettings() { UI::Screen::Manager::getInstance().pushPopUp( - std::make_unique(mHardware->wifi())); + std::make_unique(HardwareFactory::getAbstract().wifi())); } diff --git a/Platformio/OmoteUI/UIs/BasicRefactored/page/SettingsPage.hpp b/Platformio/OmoteUI/UIs/BasicRefactored/page/SettingsPage.hpp index 1a017e56..18675b21 100644 --- a/Platformio/OmoteUI/UIs/BasicRefactored/page/SettingsPage.hpp +++ b/Platformio/OmoteUI/UIs/BasicRefactored/page/SettingsPage.hpp @@ -8,7 +8,7 @@ class List; namespace UI::Page { class SettingsPage : public Base { public: - SettingsPage(std::shared_ptr aHardware = nullptr); + SettingsPage(); bool OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) override { return false; @@ -24,6 +24,5 @@ class SettingsPage : public Base { Widget::Button *mButton; Widget::List *mSettingsList; - std::shared_ptr mHardware; }; } // namespace UI::Page diff --git a/Platformio/OmoteUI/UIs/BasicRefactored/page/SystemSettings.cpp b/Platformio/OmoteUI/UIs/BasicRefactored/page/SystemSettings.cpp index c36521a5..983d4e8b 100644 --- a/Platformio/OmoteUI/UIs/BasicRefactored/page/SystemSettings.cpp +++ b/Platformio/OmoteUI/UIs/BasicRefactored/page/SystemSettings.cpp @@ -1,15 +1,16 @@ #include "SystemSettings.hpp" +#include "HardwareFactory.hpp" #include "Label.hpp" using namespace UI::Page; -SystemSettings::SystemSettings(std::shared_ptr aHardware) - : Base(ID::Pages::SystemSettings), mHardware(aHardware), +SystemSettings::SystemSettings() + : Base(ID::Pages::SystemSettings), mTimeoutLabel(AddElement( std::make_unique("TimeOut"))), mScreenTimeOutDropDown(AddElement>( std::make_unique>([this](int aTimeout) { - mHardware->setSleepTimeout(aTimeout); + HardwareFactory::getAbstract().setSleepTimeout(aTimeout); }))) { mTimeoutLabel->AlignTo(this, LV_ALIGN_TOP_MID); @@ -21,5 +22,6 @@ SystemSettings::SystemSettings(std::shared_ptr aHardware) mScreenTimeOutDropDown->AddItem("15 Seconds", 15000); mScreenTimeOutDropDown->AddItem("20 Seconds", 20000); mScreenTimeOutDropDown->AlignTo(mTimeoutLabel, LV_ALIGN_OUT_BOTTOM_MID); - mScreenTimeOutDropDown->SetSelected(mHardware->getSleepTimeout()); + mScreenTimeOutDropDown->SetSelected( + HardwareFactory::getAbstract().getSleepTimeout()); } \ No newline at end of file diff --git a/Platformio/OmoteUI/UIs/BasicRefactored/page/SystemSettings.hpp b/Platformio/OmoteUI/UIs/BasicRefactored/page/SystemSettings.hpp index ec760a10..ef17278f 100644 --- a/Platformio/OmoteUI/UIs/BasicRefactored/page/SystemSettings.hpp +++ b/Platformio/OmoteUI/UIs/BasicRefactored/page/SystemSettings.hpp @@ -1,6 +1,5 @@ #pragma once #include "DropDown.hpp" -#include "HardwareAbstract.hpp" #include "PageBase.hpp" namespace UI::Widget { @@ -11,13 +10,12 @@ namespace UI::Page { class SystemSettings : public Base { public: - SystemSettings(std::shared_ptr aHardware); + SystemSettings(); protected: std::string GetTitle() override { return "System Settings"; } private: - std::shared_ptr mHardware; Widget::Label *mTimeoutLabel; Widget::DropDown *mScreenTimeOutDropDown; }; diff --git a/Platformio/OmoteUI/UIs/BasicRefactored/screen/HomeScreen.cpp b/Platformio/OmoteUI/UIs/BasicRefactored/screen/HomeScreen.cpp index 8817cbaf..4b3f5214 100644 --- a/Platformio/OmoteUI/UIs/BasicRefactored/screen/HomeScreen.cpp +++ b/Platformio/OmoteUI/UIs/BasicRefactored/screen/HomeScreen.cpp @@ -1,22 +1,21 @@ #include "HomeScreen.hpp" #include "Colors.hpp" -#include "SettingsPage.hpp" #include "Demo.hpp" +#include "SettingsPage.hpp" using namespace UI::Screen; -HomeScreen::HomeScreen(std::shared_ptr aHardware) +HomeScreen::HomeScreen() : Base(UI::ID::Screens::Home), - mTabView( AddElement(std::make_unique( - ID(ID::Pages::INVALID_PAGE_ID)))), - mHardware(aHardware) { + mTabView(AddElement( + std::make_unique(ID(ID::Pages::INVALID_PAGE_ID)))) { SetBgColor(UI::Color::BLACK); SetPushAnimation(LV_SCR_LOAD_ANIM_FADE_IN); // Adds pages to the Tab view - mTabView->AddTab(std::make_unique(aHardware)); - mTabView->AddTab(std::make_unique(aHardware)); + mTabView->AddTab(std::make_unique()); + mTabView->AddTab(std::make_unique()); } void HomeScreen::SetBgColor(lv_color_t value, lv_style_selector_t selector) { diff --git a/Platformio/OmoteUI/UIs/BasicRefactored/screen/HomeScreen.hpp b/Platformio/OmoteUI/UIs/BasicRefactored/screen/HomeScreen.hpp index 9104f7cb..a10ddb60 100644 --- a/Platformio/OmoteUI/UIs/BasicRefactored/screen/HomeScreen.hpp +++ b/Platformio/OmoteUI/UIs/BasicRefactored/screen/HomeScreen.hpp @@ -8,7 +8,7 @@ namespace UI::Screen { class HomeScreen : public Base { public: - HomeScreen(std::shared_ptr aHardware = nullptr); + HomeScreen(); void SetBgColor(lv_color_t value, lv_style_selector_t selector = LV_PART_MAIN) override; @@ -18,7 +18,6 @@ class HomeScreen : public Base { private: Page::TabView *mTabView; - std::shared_ptr mHardware = nullptr; }; } // namespace UI::Screen \ No newline at end of file diff --git a/Platformio/OmoteUI/UIs/UIBase.cpp b/Platformio/OmoteUI/UIs/UIBase.cpp index fef3f760..bba04d9e 100644 --- a/Platformio/OmoteUI/UIs/UIBase.cpp +++ b/Platformio/OmoteUI/UIs/UIBase.cpp @@ -3,8 +3,7 @@ using namespace UI; -UIBase::UIBase(std::shared_ptr aHardware) - : mHardware(aHardware) {} +UIBase::UIBase() {} void UIBase::loopHandler() { { diff --git a/Platformio/OmoteUI/UIs/UIBase.hpp b/Platformio/OmoteUI/UIs/UIBase.hpp index 36573933..ab940be7 100644 --- a/Platformio/OmoteUI/UIs/UIBase.hpp +++ b/Platformio/OmoteUI/UIs/UIBase.hpp @@ -9,12 +9,11 @@ namespace UI { class UIBase { public: - UIBase(std::shared_ptr aHardware); + UIBase(); - void loopHandler(); + virtual void loopHandler(); protected: - std::shared_ptr mHardware; }; } // namespace UI \ No newline at end of file diff --git a/Platformio/platformio.ini b/Platformio/platformio.ini index eab10496..3dc87e7b 100644 --- a/Platformio/platformio.ini +++ b/Platformio/platformio.ini @@ -60,7 +60,7 @@ lib_archive = false build_src_filter = +<../OmoteUI/*> -<../OmoteUI/UIs/Basic/*> - +<../HAL/HardwareAbstract.cpp> + +<../HAL/*.cpp> +<../HAL/HardwareModules/*.cpp> @@ -88,6 +88,7 @@ lib_deps = Preferences build_flags = ${env.build_flags} + -D OMOTE_ESP32 -D CORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG @@ -130,7 +131,7 @@ build_src_filter = platform = native@^1.1.3 build_flags = ${env.build_flags} - + -D OMOTE_SIM ;-D LV_LOG_LEVEL=LV_LOG_LEVEL_INFO ;-D LV_LOG_PRINTF=1 -lSDL2 ; SDL2 must be installed on system! Windows:msys2 ubuntu:apt-get diff --git a/Platformio/src/OmoteSetup.hpp b/Platformio/src/OmoteSetup.hpp new file mode 100644 index 00000000..78c93378 --- /dev/null +++ b/Platformio/src/OmoteSetup.hpp @@ -0,0 +1,18 @@ +#include "BasicUI.hpp" +#include "HardwareFactory.hpp" + +namespace OMOTE { +std::shared_ptr ui = nullptr; + +void setup() { + HardwareFactory::getAbstract().init(); + ui = std::make_unique(); + lv_timer_handler(); // Run the LVGL UI once before the loop takes over +} + +void loop() { + HardwareFactory::getAbstract().loopHandler(); + ui->loopHandler(); +} + +} // namespace OMOTE diff --git a/Platformio/src/main.cpp b/Platformio/src/main.cpp index 708aebfe..3f5ba833 100644 --- a/Platformio/src/main.cpp +++ b/Platformio/src/main.cpp @@ -1,25 +1,6 @@ // OMOTE firmware for ESP32 // 2023 Maximilian Kern -#include "BasicUI.hpp" -#include "HardwareRevX.hpp" -#include "omoteconfig.h" -#include - -std::shared_ptr hal = nullptr; -std::shared_ptr ui = nullptr; - -void setup() { - hal = HardwareRevX::getInstance(); - hal->init(); - - auto ui = UI::BasicUI(hal); - // ui->layout_UI(); - - lv_timer_handler(); // Run the LVGL UI once before the loop takes over -} - -void loop() { - hal->loopHandler(); - ui->loopHandler(); -} \ No newline at end of file +#include "OmoteSetup.hpp" +void setup() { OMOTE::setup(); } +void loop() { OMOTE::loop(); } \ No newline at end of file diff --git a/Platformio/src/simMain.cpp b/Platformio/src/simMain.cpp index f996b734..678b4941 100644 --- a/Platformio/src/simMain.cpp +++ b/Platformio/src/simMain.cpp @@ -1,16 +1,8 @@ -#include "BasicUI.hpp" -#include "HardwareSimulator.hpp" -#include "omoteconfig.h" -#include +#include "OmoteSetup.hpp" int main() { - auto hwSim = std::make_shared(); - hwSim->init(); - - auto ui = UI::BasicUI(hwSim); - // ui->layout_UI(); - + OMOTE::setup(); while (true) { - ui.loopHandler(); + OMOTE::loop(); } } \ No newline at end of file