Skip to content

Commit

Permalink
Rework Mains so they are basically as identical as possible.
Browse files Browse the repository at this point in the history
Create HardwareFactory which is responsible for providing the HardwareAbstract to any part of the program based on compiler defines
  • Loading branch information
MatthewColvin committed Oct 15, 2023
1 parent 99787a6 commit efa2d4a
Show file tree
Hide file tree
Showing 22 changed files with 125 additions and 121 deletions.
6 changes: 3 additions & 3 deletions Platformio/HAL/HardwareAbstract.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,7 +37,4 @@ class HardwareAbstract {

virtual uint16_t getSleepTimeout() = 0;
virtual void setSleepTimeout(uint16_t sleepTimeout) = 0;

protected:

};
20 changes: 20 additions & 0 deletions Platformio/HAL/HardwareFactory.cpp
Original file line number Diff line number Diff line change
@@ -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; }
11 changes: 11 additions & 0 deletions Platformio/HAL/HardwareFactory.hpp
Original file line number Diff line number Diff line change
@@ -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;
};
9 changes: 0 additions & 9 deletions Platformio/HAL/Targets/ESP32/HardwareRevX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
7 changes: 2 additions & 5 deletions Platformio/HAL/Targets/ESP32/HardwareRevX.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -67,8 +66,6 @@ class HardwareRevX : public HardwareAbstract {
void startTasks();

private:
HardwareRevX();

std::shared_ptr<Battery> mBattery;
std::shared_ptr<Display> mDisplay;
std::shared_ptr<wifiHandler> mWifiHandler;
Expand Down
25 changes: 13 additions & 12 deletions Platformio/HAL/Targets/Simulator/HardwareSimulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 10 additions & 9 deletions Platformio/OmoteUI/UIs/BasicRefactored/BasicUI.cpp
Original file line number Diff line number Diff line change
@@ -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();
}
2 changes: 1 addition & 1 deletion Platformio/OmoteUI/UIs/BasicRefactored/BasicUI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace UI {

class BasicUI : public UIBase {
public:
BasicUI(std::shared_ptr<HardwareAbstract> aHardware);
BasicUI();
};

} // namespace UI
9 changes: 3 additions & 6 deletions Platformio/OmoteUI/UIs/BasicRefactored/page/Demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 9 additions & 11 deletions Platformio/OmoteUI/UIs/BasicRefactored/page/Demo.hpp
Original file line number Diff line number Diff line change
@@ -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
15 changes: 8 additions & 7 deletions Platformio/OmoteUI/UIs/BasicRefactored/page/SettingsPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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(); });
Expand All @@ -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()));
}
3 changes: 1 addition & 2 deletions Platformio/OmoteUI/UIs/BasicRefactored/page/SettingsPage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,6 +24,5 @@ class SettingsPage : public Base {

Widget::Button *mButton;
Widget::List *mSettingsList;
std::shared_ptr<HardwareAbstract> mHardware;
};
} // namespace UI::Page
10 changes: 6 additions & 4 deletions Platformio/OmoteUI/UIs/BasicRefactored/page/SystemSettings.cpp
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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());
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once
#include "DropDown.hpp"
#include "HardwareAbstract.hpp"
#include "PageBase.hpp"

namespace UI::Widget {
Expand All @@ -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;
};
Expand Down
13 changes: 6 additions & 7 deletions Platformio/OmoteUI/UIs/BasicRefactored/screen/HomeScreen.cpp
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions Platformio/OmoteUI/UIs/BasicRefactored/screen/HomeScreen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,7 +18,6 @@ class HomeScreen : public Base {

private:
Page::TabView *mTabView;
std::shared_ptr<HardwareAbstract> mHardware = nullptr;
};

} // namespace UI::Screen
Loading

0 comments on commit efa2d4a

Please sign in to comment.