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
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 @@ public:
virtual uint16_t getSleepTimeout() = 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 "wifihandler.hpp"
std::shared_ptr<HardwareRevX> 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> HardwareRevX::getInstance() {
if (!mInstance) {
mInstance = std::shared_ptr<HardwareRevX>(new HardwareRevX());
}
return mInstance;
}
std::shared_ptr<wifiHandlerInterface> HardwareRevX::wifi() {
return mWifiHandler;
}

View file

@ -26,8 +26,7 @@ class HardwareRevX : public HardwareAbstract {
public:
enum class WakeReason { RESET, IMU, KEYPAD };
static std::shared_ptr<HardwareRevX> getInstance();
static std::weak_ptr<HardwareRevX> getRefrence() { return getInstance(); }
HardwareRevX();
// HardwareAbstract
virtual void init() override;
@ -49,7 +48,7 @@ public:
/// @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 @@ protected:
void startTasks();
private:
HardwareRevX();
std::shared_ptr<Battery> mBattery;
std::shared_ptr<Display> mDisplay;
std::shared_ptr<wifiHandler> mWifiHandler;

View file

@ -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<BatteryInterface> battery() override;
virtual std::shared_ptr<DisplayAbstract> display() override;
virtual std::shared_ptr<wifiHandlerInterface> wifi() override;
virtual std::shared_ptr<KeyPressAbstract> keys() override;
std::shared_ptr<BatteryInterface> battery() override;
std::shared_ptr<DisplayAbstract> display() override;
std::shared_ptr<wifiHandlerInterface> wifi() override;
std::shared_ptr<KeyPressAbstract> 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;

View file

@ -1,20 +1,21 @@
#include "BasicUI.hpp"
#include "HardwareFactory.hpp"
#include "HomeScreen.hpp"
#include "ScreenManager.hpp"
using namespace UI;
BasicUI::BasicUI(std::shared_ptr<HardwareAbstract> 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<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 {
public:
BasicUI(std::shared_ptr<HardwareAbstract> aHardware);
BasicUI();
};
} // namespace UI

View file

@ -3,17 +3,14 @@
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() {
auto fakeSlider = std::make_unique<Widget::Slider>([](auto data){});
auto fakeSlider = std::make_unique<Widget::Slider>([](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);

View file

@ -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<HardwareAbstract> 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<HardwareAbstract> mHardware;
std::vector<UIElement *> sliders;
std::vector<UIElement *> sliders;
};
}
} // namespace UI::Page

View file

@ -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<HardwareAbstract> aHardware)
: Base(ID::Pages::Settings),
mSettingsList(AddElement<Widget::List>(std::make_unique<Widget::List>())),
mHardware(aHardware) {
SettingsPage::SettingsPage()
: Base(ID::Pages::Settings), mSettingsList(AddElement<Widget::List>(
std::make_unique<Widget::List>())) {
mSettingsList->AddItem("Display", LV_SYMBOL_EYE_OPEN,
[this] { PushDisplaySettings(); });
@ -28,15 +28,16 @@ SettingsPage::SettingsPage(std::shared_ptr<HardwareAbstract> aHardware)
void SettingsPage::PushDisplaySettings() {
UI::Screen::Manager::getInstance().pushPopUp(
std::make_unique<DisplaySettings>(mHardware->display()));
std::make_unique<DisplaySettings>(
HardwareFactory::getAbstract().display()));
}
void SettingsPage::PushSystemSettings() {
UI::Screen::Manager::getInstance().pushPopUp(
std::make_unique<SystemSettings>(mHardware));
std::make_unique<SystemSettings>());
}
void SettingsPage::PushWifiSettings() {
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 {
class SettingsPage : public Base {
public:
SettingsPage(std::shared_ptr<HardwareAbstract> aHardware = nullptr);
SettingsPage();
bool OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) override {
return false;
@ -24,6 +24,5 @@ protected:
Widget::Button *mButton;
Widget::List *mSettingsList;
std::shared_ptr<HardwareAbstract> mHardware;
};
} // namespace UI::Page

View file

@ -1,15 +1,16 @@
#include "SystemSettings.hpp"
#include "HardwareFactory.hpp"
#include "Label.hpp"
using namespace UI::Page;
SystemSettings::SystemSettings(std::shared_ptr<HardwareAbstract> aHardware)
: Base(ID::Pages::SystemSettings), mHardware(aHardware),
SystemSettings::SystemSettings()
: Base(ID::Pages::SystemSettings),
mTimeoutLabel(AddElement<Widget::Label>(
std::make_unique<Widget::Label>("TimeOut"))),
mScreenTimeOutDropDown(AddElement<Widget::DropDown<int>>(
std::make_unique<Widget::DropDown<int>>([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<HardwareAbstract> 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());
}

View file

@ -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<HardwareAbstract> aHardware);
SystemSettings();
protected:
std::string GetTitle() override { return "System Settings"; }
private:
std::shared_ptr<HardwareAbstract> mHardware;
Widget::Label *mTimeoutLabel;
Widget::DropDown<int> *mScreenTimeOutDropDown;
};

View file

@ -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<HardwareAbstract> aHardware)
HomeScreen::HomeScreen()
: Base(UI::ID::Screens::Home),
mTabView( AddElement<Page::TabView>(std::make_unique<Page::TabView>(
ID(ID::Pages::INVALID_PAGE_ID)))),
mHardware(aHardware) {
mTabView(AddElement<Page::TabView>(
std::make_unique<Page::TabView>(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<Page::SettingsPage>(aHardware));
mTabView->AddTab(std::make_unique<Page::Demo>(aHardware));
mTabView->AddTab(std::make_unique<Page::SettingsPage>());
mTabView->AddTab(std::make_unique<Page::Demo>());
}
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 {
public:
HomeScreen(std::shared_ptr<HardwareAbstract> aHardware = nullptr);
HomeScreen();
void SetBgColor(lv_color_t value,
lv_style_selector_t selector = LV_PART_MAIN) override;
@ -18,7 +18,6 @@ protected:
private:
Page::TabView *mTabView;
std::shared_ptr<HardwareAbstract> mHardware = nullptr;
};
} // namespace UI::Screen

View file

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

View file

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

View file

@ -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

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
// 2023 Maximilian Kern
#include "BasicUI.hpp"
#include "HardwareRevX.hpp"
#include "omoteconfig.h"
#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();
}
#include "OmoteSetup.hpp"
void setup() { OMOTE::setup(); }
void loop() { OMOTE::loop(); }

View file

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