Rework Mains so they are basically as identical as possible.

Create HardwareFactory which is responsible for providing the HardwareAbstract to any part of the program based on compiler defines
This commit is contained in:
MatthewColvin 2023-10-15 08:24:29 -05:00
parent 99787a69f2
commit efa2d4a137
22 changed files with 125 additions and 121 deletions

View file

@ -17,6 +17,9 @@ public:
/// @brief Override in order to do setup of hardware devices post construction /// @brief Override in order to do setup of hardware devices post construction
virtual void init() = 0; 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 /// @brief Override to allow printing of a message for debugging
/// @param message - Debug message /// @param message - Debug message
virtual void debugPrint(const char *fmt, ...) = 0; virtual void debugPrint(const char *fmt, ...) = 0;
@ -34,7 +37,4 @@ public:
virtual uint16_t getSleepTimeout() = 0; virtual uint16_t getSleepTimeout() = 0;
virtual void setSleepTimeout(uint16_t sleepTimeout) = 0; virtual void setSleepTimeout(uint16_t sleepTimeout) = 0;
protected:
}; };

View file

@ -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<HardwareAbstract> HardwareFactory::mHardware =
std::make_unique<HardwareSimulator>();
#endif
#if OMOTE_ESP32
std::unique_ptr<HardwareAbstract> HardwareFactory::mHardware =
std::make_unique<HardwareRevX>();
#endif
HardwareAbstract &HardwareFactory::getAbstract() { return *mHardware; }

View file

@ -0,0 +1,11 @@
#include "HardwareAbstract.hpp"
#include <memory>
/**
* @brief The HardwareFactory is responsible for making the
*/
class HardwareFactory {
public:
static HardwareAbstract &getAbstract();
static std::unique_ptr<HardwareAbstract> mHardware;
};

View file

@ -2,8 +2,6 @@
#include "display.hpp" #include "display.hpp"
#include "wifihandler.hpp" #include "wifihandler.hpp"
std::shared_ptr<HardwareRevX> HardwareRevX::mInstance = nullptr;
void HardwareRevX::initIO() { void HardwareRevX::initIO() {
// Button Pin Definition // Button Pin Definition
pinMode(SW_1, OUTPUT); pinMode(SW_1, OUTPUT);
@ -99,13 +97,6 @@ void HardwareRevX::debugPrint(const char *fmt, ...) {
Serial.print(result); Serial.print(result);
} }
std::shared_ptr<HardwareRevX> HardwareRevX::getInstance() {
if (!mInstance) {
mInstance = std::shared_ptr<HardwareRevX>(new HardwareRevX());
}
return mInstance;
}
std::shared_ptr<wifiHandlerInterface> HardwareRevX::wifi() { std::shared_ptr<wifiHandlerInterface> HardwareRevX::wifi() {
return mWifiHandler; return mWifiHandler;
} }

View file

@ -26,8 +26,7 @@ class HardwareRevX : public HardwareAbstract {
public: public:
enum class WakeReason { RESET, IMU, KEYPAD }; enum class WakeReason { RESET, IMU, KEYPAD };
static std::shared_ptr<HardwareRevX> getInstance(); HardwareRevX();
static std::weak_ptr<HardwareRevX> getRefrence() { return getInstance(); }
// HardwareAbstract // HardwareAbstract
virtual void init() override; virtual void init() override;
@ -49,7 +48,7 @@ public:
/// @brief To be ran in loop out in main /// @brief To be ran in loop out in main
// TODO move to a freertos task // TODO move to a freertos task
void loopHandler(); void loopHandler() override;
protected: protected:
// Init Functions to setup hardware // Init Functions to setup hardware
@ -67,8 +66,6 @@ protected:
void startTasks(); void startTasks();
private: private:
HardwareRevX();
std::shared_ptr<Battery> mBattery; std::shared_ptr<Battery> mBattery;
std::shared_ptr<Display> mDisplay; std::shared_ptr<Display> mDisplay;
std::shared_ptr<wifiHandler> mWifiHandler; std::shared_ptr<wifiHandler> mWifiHandler;

View file

@ -12,28 +12,29 @@ class HardwareSimulator : public HardwareAbstract {
public: public:
HardwareSimulator(); 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_list arguments;
va_start(arguments, fmt); va_start(arguments, fmt);
vprintf(fmt, arguments); vprintf(fmt, arguments);
va_end(arguments); va_end(arguments);
} }
virtual std::shared_ptr<BatteryInterface> battery() override; std::shared_ptr<BatteryInterface> battery() override;
virtual std::shared_ptr<DisplayAbstract> display() override; std::shared_ptr<DisplayAbstract> display() override;
virtual std::shared_ptr<wifiHandlerInterface> wifi() override; std::shared_ptr<wifiHandlerInterface> wifi() override;
virtual std::shared_ptr<KeyPressAbstract> keys() override; std::shared_ptr<KeyPressAbstract> keys() override;
virtual char getCurrentDevice() override; char getCurrentDevice() override;
virtual void setCurrentDevice(char currentDevice) override; void setCurrentDevice(char currentDevice) override;
virtual bool getWakeupByIMUEnabled() override; bool getWakeupByIMUEnabled() override;
virtual void setWakeupByIMUEnabled(bool wakeupByIMUEnabled) override; void setWakeupByIMUEnabled(bool wakeupByIMUEnabled) override;
virtual uint16_t getSleepTimeout() override; uint16_t getSleepTimeout() override;
virtual void setSleepTimeout(uint16_t sleepTimeout) override; void setSleepTimeout(uint16_t sleepTimeout) override;
private: private:
std::thread mTickThread; std::thread mTickThread;

View file

@ -1,20 +1,21 @@
#include "BasicUI.hpp" #include "BasicUI.hpp"
#include "HardwareFactory.hpp"
#include "HomeScreen.hpp" #include "HomeScreen.hpp"
#include "ScreenManager.hpp" #include "ScreenManager.hpp"
using namespace UI; using namespace UI;
BasicUI::BasicUI(std::shared_ptr<HardwareAbstract> aHardware) BasicUI::BasicUI() : UIBase() {
: UIBase(aHardware) {
aHardware->keys()->RegisterKeyPressHandler([](auto aKeyEvent) { HardwareFactory::getAbstract().keys()->RegisterKeyPressHandler(
return Screen::Manager::getInstance().distributeKeyEvent(aKeyEvent); [](auto aKeyEvent) {
// Could potentially add a check here and display that a key event was return Screen::Manager::getInstance().distributeKeyEvent(aKeyEvent);
// unused. // Could potentially add a check here and display that a key event was
}); // unused.
});
Screen::Manager::getInstance().pushScreen( Screen::Manager::getInstance().pushScreen(
std::make_unique<Screen::HomeScreen>(aHardware)); std::make_unique<Screen::HomeScreen>());
mHardware->wifi()->begin(); HardwareFactory::getAbstract().wifi()->begin();
} }

View file

@ -5,7 +5,7 @@ namespace UI {
class BasicUI : public UIBase { class BasicUI : public UIBase {
public: public:
BasicUI(std::shared_ptr<HardwareAbstract> aHardware); BasicUI();
}; };
} // namespace UI } // namespace UI

View file

@ -3,17 +3,14 @@
using namespace UI::Page; using namespace UI::Page;
Demo::Demo(std::shared_ptr<HardwareAbstract> aHardware): Base(ID::Pages::Demo), mHardware(aHardware){ Demo::Demo() : Base(ID::Pages::Demo) {}
}
void Demo::AddSlider() { void Demo::AddSlider() {
auto fakeSlider = std::make_unique<Widget::Slider>([](auto data){}); auto fakeSlider = std::make_unique<Widget::Slider>([](auto data) {});
fakeSlider->SetHeight(lv_pct(10)); fakeSlider->SetHeight(lv_pct(10));
fakeSlider->SetWidth(GetContentWidth()); fakeSlider->SetWidth(GetContentWidth());
if (sliders.empty()) { if (sliders.empty()) {
fakeSlider->AlignTo(this,LV_ALIGN_TOP_MID); fakeSlider->AlignTo(this, LV_ALIGN_TOP_MID);
} else { } else {
auto nextY = sliders.back()->GetY() + sliders.back()->GetHeight(); auto nextY = sliders.back()->GetY() + sliders.back()->GetHeight();
fakeSlider->SetY(nextY + 10); fakeSlider->SetY(nextY + 10);

View file

@ -1,22 +1,20 @@
#pragma once #pragma once
#include "PageBase.hpp" #include "PageBase.hpp"
#include "HardwareAbstract.hpp"
namespace UI::Page{ namespace UI::Page {
class Demo : public Base{ class Demo : public Base {
public: public:
Demo(std::shared_ptr<HardwareAbstract> aHardware); Demo();
void AddSlider(); void AddSlider();
void OnShow()override{}; void OnShow() override{};
void OnHide()override{}; void OnHide() override{};
bool OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent); bool OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent);
private: private:
std::shared_ptr<HardwareAbstract> mHardware; std::vector<UIElement *> sliders;
std::vector<UIElement *> sliders;
}; };
} } // namespace UI::Page

View file

@ -3,6 +3,7 @@
#include "Button.hpp" #include "Button.hpp"
#include "Colors.hpp" #include "Colors.hpp"
#include "DisplaySettings.hpp" #include "DisplaySettings.hpp"
#include "HardwareFactory.hpp"
#include "List.hpp" #include "List.hpp"
#include "PopUpScreen.hpp" #include "PopUpScreen.hpp"
#include "ScreenManager.hpp" #include "ScreenManager.hpp"
@ -13,10 +14,9 @@
using namespace UI::Page; using namespace UI::Page;
using namespace UI::Color; using namespace UI::Color;
SettingsPage::SettingsPage(std::shared_ptr<HardwareAbstract> aHardware) SettingsPage::SettingsPage()
: Base(ID::Pages::Settings), : Base(ID::Pages::Settings), mSettingsList(AddElement<Widget::List>(
mSettingsList(AddElement<Widget::List>(std::make_unique<Widget::List>())), std::make_unique<Widget::List>())) {
mHardware(aHardware) {
mSettingsList->AddItem("Display", LV_SYMBOL_EYE_OPEN, mSettingsList->AddItem("Display", LV_SYMBOL_EYE_OPEN,
[this] { PushDisplaySettings(); }); [this] { PushDisplaySettings(); });
@ -28,15 +28,16 @@ SettingsPage::SettingsPage(std::shared_ptr<HardwareAbstract> aHardware)
void SettingsPage::PushDisplaySettings() { void SettingsPage::PushDisplaySettings() {
UI::Screen::Manager::getInstance().pushPopUp( UI::Screen::Manager::getInstance().pushPopUp(
std::make_unique<DisplaySettings>(mHardware->display())); std::make_unique<DisplaySettings>(
HardwareFactory::getAbstract().display()));
} }
void SettingsPage::PushSystemSettings() { void SettingsPage::PushSystemSettings() {
UI::Screen::Manager::getInstance().pushPopUp( UI::Screen::Manager::getInstance().pushPopUp(
std::make_unique<SystemSettings>(mHardware)); std::make_unique<SystemSettings>());
} }
void SettingsPage::PushWifiSettings() { void SettingsPage::PushWifiSettings() {
UI::Screen::Manager::getInstance().pushPopUp( UI::Screen::Manager::getInstance().pushPopUp(
std::make_unique<WifiSettings>(mHardware->wifi())); std::make_unique<WifiSettings>(HardwareFactory::getAbstract().wifi()));
} }

View file

@ -8,7 +8,7 @@ class List;
namespace UI::Page { namespace UI::Page {
class SettingsPage : public Base { class SettingsPage : public Base {
public: public:
SettingsPage(std::shared_ptr<HardwareAbstract> aHardware = nullptr); SettingsPage();
bool OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) override { bool OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) override {
return false; return false;
@ -24,6 +24,5 @@ protected:
Widget::Button *mButton; Widget::Button *mButton;
Widget::List *mSettingsList; Widget::List *mSettingsList;
std::shared_ptr<HardwareAbstract> mHardware;
}; };
} // namespace UI::Page } // namespace UI::Page

View file

@ -1,15 +1,16 @@
#include "SystemSettings.hpp" #include "SystemSettings.hpp"
#include "HardwareFactory.hpp"
#include "Label.hpp" #include "Label.hpp"
using namespace UI::Page; using namespace UI::Page;
SystemSettings::SystemSettings(std::shared_ptr<HardwareAbstract> aHardware) SystemSettings::SystemSettings()
: Base(ID::Pages::SystemSettings), mHardware(aHardware), : Base(ID::Pages::SystemSettings),
mTimeoutLabel(AddElement<Widget::Label>( mTimeoutLabel(AddElement<Widget::Label>(
std::make_unique<Widget::Label>("TimeOut"))), std::make_unique<Widget::Label>("TimeOut"))),
mScreenTimeOutDropDown(AddElement<Widget::DropDown<int>>( mScreenTimeOutDropDown(AddElement<Widget::DropDown<int>>(
std::make_unique<Widget::DropDown<int>>([this](int aTimeout) { std::make_unique<Widget::DropDown<int>>([this](int aTimeout) {
mHardware->setSleepTimeout(aTimeout); HardwareFactory::getAbstract().setSleepTimeout(aTimeout);
}))) { }))) {
mTimeoutLabel->AlignTo(this, LV_ALIGN_TOP_MID); mTimeoutLabel->AlignTo(this, LV_ALIGN_TOP_MID);
@ -21,5 +22,6 @@ SystemSettings::SystemSettings(std::shared_ptr<HardwareAbstract> aHardware)
mScreenTimeOutDropDown->AddItem("15 Seconds", 15000); mScreenTimeOutDropDown->AddItem("15 Seconds", 15000);
mScreenTimeOutDropDown->AddItem("20 Seconds", 20000); mScreenTimeOutDropDown->AddItem("20 Seconds", 20000);
mScreenTimeOutDropDown->AlignTo(mTimeoutLabel, LV_ALIGN_OUT_BOTTOM_MID); mScreenTimeOutDropDown->AlignTo(mTimeoutLabel, LV_ALIGN_OUT_BOTTOM_MID);
mScreenTimeOutDropDown->SetSelected(mHardware->getSleepTimeout()); mScreenTimeOutDropDown->SetSelected(
HardwareFactory::getAbstract().getSleepTimeout());
} }

View file

@ -1,6 +1,5 @@
#pragma once #pragma once
#include "DropDown.hpp" #include "DropDown.hpp"
#include "HardwareAbstract.hpp"
#include "PageBase.hpp" #include "PageBase.hpp"
namespace UI::Widget { namespace UI::Widget {
@ -11,13 +10,12 @@ namespace UI::Page {
class SystemSettings : public Base { class SystemSettings : public Base {
public: public:
SystemSettings(std::shared_ptr<HardwareAbstract> aHardware); SystemSettings();
protected: protected:
std::string GetTitle() override { return "System Settings"; } std::string GetTitle() override { return "System Settings"; }
private: private:
std::shared_ptr<HardwareAbstract> mHardware;
Widget::Label *mTimeoutLabel; Widget::Label *mTimeoutLabel;
Widget::DropDown<int> *mScreenTimeOutDropDown; Widget::DropDown<int> *mScreenTimeOutDropDown;
}; };

View file

@ -1,22 +1,21 @@
#include "HomeScreen.hpp" #include "HomeScreen.hpp"
#include "Colors.hpp" #include "Colors.hpp"
#include "SettingsPage.hpp"
#include "Demo.hpp" #include "Demo.hpp"
#include "SettingsPage.hpp"
using namespace UI::Screen; using namespace UI::Screen;
HomeScreen::HomeScreen(std::shared_ptr<HardwareAbstract> aHardware) HomeScreen::HomeScreen()
: Base(UI::ID::Screens::Home), : Base(UI::ID::Screens::Home),
mTabView( AddElement<Page::TabView>(std::make_unique<Page::TabView>( mTabView(AddElement<Page::TabView>(
ID(ID::Pages::INVALID_PAGE_ID)))), std::make_unique<Page::TabView>(ID(ID::Pages::INVALID_PAGE_ID)))) {
mHardware(aHardware) {
SetBgColor(UI::Color::BLACK); SetBgColor(UI::Color::BLACK);
SetPushAnimation(LV_SCR_LOAD_ANIM_FADE_IN); SetPushAnimation(LV_SCR_LOAD_ANIM_FADE_IN);
// Adds pages to the Tab view // Adds pages to the Tab view
mTabView->AddTab(std::make_unique<Page::SettingsPage>(aHardware)); mTabView->AddTab(std::make_unique<Page::SettingsPage>());
mTabView->AddTab(std::make_unique<Page::Demo>(aHardware)); mTabView->AddTab(std::make_unique<Page::Demo>());
} }
void HomeScreen::SetBgColor(lv_color_t value, lv_style_selector_t selector) { void HomeScreen::SetBgColor(lv_color_t value, lv_style_selector_t selector) {

View file

@ -8,7 +8,7 @@ namespace UI::Screen {
class HomeScreen : public Base { class HomeScreen : public Base {
public: public:
HomeScreen(std::shared_ptr<HardwareAbstract> aHardware = nullptr); HomeScreen();
void SetBgColor(lv_color_t value, void SetBgColor(lv_color_t value,
lv_style_selector_t selector = LV_PART_MAIN) override; lv_style_selector_t selector = LV_PART_MAIN) override;
@ -18,7 +18,6 @@ protected:
private: private:
Page::TabView *mTabView; Page::TabView *mTabView;
std::shared_ptr<HardwareAbstract> mHardware = nullptr;
}; };
} // namespace UI::Screen } // namespace UI::Screen

View file

@ -3,8 +3,7 @@
using namespace UI; using namespace UI;
UIBase::UIBase(std::shared_ptr<HardwareAbstract> aHardware) UIBase::UIBase() {}
: mHardware(aHardware) {}
void UIBase::loopHandler() { void UIBase::loopHandler() {
{ {

View file

@ -9,12 +9,11 @@ namespace UI {
class UIBase { class UIBase {
public: public:
UIBase(std::shared_ptr<HardwareAbstract> aHardware); UIBase();
void loopHandler(); virtual void loopHandler();
protected: protected:
std::shared_ptr<HardwareAbstract> mHardware;
}; };
} // namespace UI } // namespace UI

View file

@ -60,7 +60,7 @@ lib_archive = false
build_src_filter = build_src_filter =
+<../OmoteUI/*> +<../OmoteUI/*>
-<../OmoteUI/UIs/Basic/*> -<../OmoteUI/UIs/Basic/*>
+<../HAL/HardwareAbstract.cpp> +<../HAL/*.cpp>
+<../HAL/HardwareModules/*.cpp> +<../HAL/HardwareModules/*.cpp>
@ -88,6 +88,7 @@ lib_deps =
Preferences Preferences
build_flags = build_flags =
${env.build_flags} ${env.build_flags}
-D OMOTE_ESP32
-D CORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG -D CORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG
@ -130,7 +131,7 @@ build_src_filter =
platform = native@^1.1.3 platform = native@^1.1.3
build_flags = build_flags =
${env.build_flags} ${env.build_flags}
-D OMOTE_SIM
;-D LV_LOG_LEVEL=LV_LOG_LEVEL_INFO ;-D LV_LOG_LEVEL=LV_LOG_LEVEL_INFO
;-D LV_LOG_PRINTF=1 ;-D LV_LOG_PRINTF=1
-lSDL2 ; SDL2 must be installed on system! Windows:msys2 ubuntu:apt-get -lSDL2 ; SDL2 must be installed on system! Windows:msys2 ubuntu:apt-get

View file

@ -0,0 +1,18 @@
#include "BasicUI.hpp"
#include "HardwareFactory.hpp"
namespace OMOTE {
std::shared_ptr<UI::UIBase> ui = nullptr;
void setup() {
HardwareFactory::getAbstract().init();
ui = std::make_unique<UI::BasicUI>();
lv_timer_handler(); // Run the LVGL UI once before the loop takes over
}
void loop() {
HardwareFactory::getAbstract().loopHandler();
ui->loopHandler();
}
} // namespace OMOTE

View file

@ -1,25 +1,6 @@
// OMOTE firmware for ESP32 // OMOTE firmware for ESP32
// 2023 Maximilian Kern // 2023 Maximilian Kern
#include "BasicUI.hpp" #include "OmoteSetup.hpp"
#include "HardwareRevX.hpp" void setup() { OMOTE::setup(); }
#include "omoteconfig.h" void loop() { OMOTE::loop(); }
#include <lvgl.h>
std::shared_ptr<HardwareRevX> hal = nullptr;
std::shared_ptr<UI::UIBase> 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();
}

View file

@ -1,16 +1,8 @@
#include "BasicUI.hpp" #include "OmoteSetup.hpp"
#include "HardwareSimulator.hpp"
#include "omoteconfig.h"
#include <memory>
int main() { int main() {
auto hwSim = std::make_shared<HardwareSimulator>(); OMOTE::setup();
hwSim->init();
auto ui = UI::BasicUI(hwSim);
// ui->layout_UI();
while (true) { while (true) {
ui.loopHandler(); OMOTE::loop();
} }
} }