diff --git a/Platformio/HAL/HardwareAbstract.cpp b/Platformio/HAL/HardwareAbstract.cpp index 31aa920..ee40bcc 100644 --- a/Platformio/HAL/HardwareAbstract.cpp +++ b/Platformio/HAL/HardwareAbstract.cpp @@ -1,23 +1,5 @@ #include "HardwareAbstract.hpp" -HardwareAbstract::HardwareAbstract() -{} - - -std::optional HardwareAbstract::getBatteryStatus(){ -#if 0 - if(mBattery){ - HardwareAbstract::batteryStatus currentStatus; - currentStatus.percentage = mBattery->getPercentage(); - currentStatus.isCharging = mBattery->isCharging(); - return currentStatus; - } -#endif - return std::nullopt; +HardwareAbstract::HardwareAbstract(){ + } - -#if 0 -void HardwareAbstract::onBatteryChange(std::function onBatteryStatusChangeHandler){ - mBatteryNotification.onNotify(std::move(onBatteryStatusChangeHandler)); -} -#endif diff --git a/Platformio/HAL/HardwareAbstract.hpp b/Platformio/HAL/HardwareAbstract.hpp index 694ae4d..b8f8e84 100644 --- a/Platformio/HAL/HardwareAbstract.hpp +++ b/Platformio/HAL/HardwareAbstract.hpp @@ -1,45 +1,30 @@ // OMOTE Hardware Abstraction // 2023 Matthew Colvin #pragma once +#include "BatteryInterface.h" +#include "DisplayAbstract.h" #include "wifiHandlerInterface.h" -#include -#include -#include -#include -#include -#include + #include "Notification.hpp" +#include + class HardwareAbstract { public: - HardwareAbstract( - ); + HardwareAbstract(); - struct batteryStatus { - /// @brief Percent of battery remaining (0-100] - int percentage; - /// @brief True - Battery is Charging - /// False - Battery discharging - bool isCharging; - }; - virtual std::optional getBatteryStatus(); - - /// @brief Register function to be ran when hardware notifies battery - /// status has changed. - /// @param onBatteryStatusChangeHandler - Callable to be ran when batter status changes - void onBatteryChange(std::function onBatteryStatusChangeHandler); - - virtual std::shared_ptr wifi() = 0; - /// @brief Override in order to do setup of hardware devices + /// @brief Override in order to do setup of hardware devices post construction virtual void init() = 0; + /// @brief Override to allow printing of a message for debugging /// @param message - Debug message virtual void debugPrint(const char* fmt, ...) = 0; + virtual std::shared_ptr battery() = 0; + virtual std::shared_ptr display() = 0; + virtual std::shared_ptr wifi() = 0; + protected: - Notification mBatteryNotification; - - private: }; \ No newline at end of file diff --git a/Platformio/HAL/HardwareModules/BatteryInterface.cpp b/Platformio/HAL/HardwareModules/BatteryInterface.cpp new file mode 100644 index 0000000..ed93972 --- /dev/null +++ b/Platformio/HAL/HardwareModules/BatteryInterface.cpp @@ -0,0 +1,9 @@ +#include "BatteryInterface.h" + +void BatteryInterface::NotifyCurrentStatus(){ + mBatteryNotification.notify(getPercentage(),isCharging()); +} + +void BatteryInterface::onBatteryStatusChange(std::function batteryChangeHandler){ + mBatteryNotification.onNotify(std::move(batteryChangeHandler)); +} \ No newline at end of file diff --git a/Platformio/HAL/HardwareModules/BatteryInterface.h b/Platformio/HAL/HardwareModules/BatteryInterface.h index 1be5337..6ebae91 100644 --- a/Platformio/HAL/HardwareModules/BatteryInterface.h +++ b/Platformio/HAL/HardwareModules/BatteryInterface.h @@ -1,9 +1,17 @@ #pragma once -#include "DisplayAbstract.h" +#include "Notification.hpp" class BatteryInterface { public: BatteryInterface() = default; virtual int getPercentage() = 0; virtual bool isCharging() = 0; + + /// @brief Notify on Current battery status. + void NotifyCurrentStatus(); + /// @brief Register Handler to be ran on battery status change + /// @param Callable to be ran on status change (percentage, IsCharging) + void onBatteryStatusChange(std::function); + private: + Notification mBatteryNotification; }; \ No newline at end of file diff --git a/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp b/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp index 94bbb6d..d822e5e 100644 --- a/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp +++ b/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp @@ -71,9 +71,9 @@ void HardwareRevX::init() { // Make sure ESP32 is running at full speed setCpuFrequencyMhz(240); - this->mDisplay = Display::getInstance(std::shared_ptr(this)); - this->mBattery = std::make_shared(ADC_BAT,CRG_STAT); - this->mWifiHandler = wifiHandler::getInstance(std::shared_ptr(this)); + mDisplay = Display::getInstance(std::shared_ptr()); + mBattery = std::make_shared(ADC_BAT,CRG_STAT); + mWifiHandler = wifiHandler::getInstance(); wakeup_reason = getWakeReason(); initIO(); setupBacklight(); @@ -86,11 +86,6 @@ void HardwareRevX::init() { debugPrint("Finished Hardware Setup in %d", millis()); } -#if 0 -void HardwareRevX::debugPrint(std::string aDebugMessage) { - Serial.print(aDebugMessage.c_str()); -} -#else void HardwareRevX::debugPrint(const char* fmt, ...) { char result[100]; @@ -102,7 +97,6 @@ void HardwareRevX::debugPrint(const char* fmt, ...) Serial.print(result); } -#endif std::shared_ptr HardwareRevX::getInstance(){ if (!mInstance) { @@ -113,7 +107,15 @@ std::shared_ptr HardwareRevX::getInstance(){ std::shared_ptr HardwareRevX::wifi() { - return this->mWifiHandler; + return mWifiHandler; +} + +std::shared_ptr HardwareRevX::battery(){ + return mBattery; +} + +std::shared_ptr HardwareRevX::display(){ + return mDisplay; } void HardwareRevX::activityDetection() { @@ -326,9 +328,7 @@ void HardwareRevX::startTasks() { void HardwareRevX::updateBatteryTask(void*){ while(true){ - if(auto status = mInstance->getBatteryStatus(); status.has_value()){ - mInstance->mBatteryNotification.notify(status.value()); - } + mInstance->battery()->NotifyCurrentStatus(); vTaskDelay(5000 / portTICK_PERIOD_MS); } } diff --git a/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp b/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp index 79d0329..34668d3 100644 --- a/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp +++ b/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp @@ -19,8 +19,7 @@ #include "omoteconfig.h" #include "BatteryInterface.h" #include "wifiHandlerInterface.h" -#include "DisplayAbstract.h" - +#include "display.hpp" class HardwareRevX : public HardwareAbstract { public: @@ -31,12 +30,15 @@ public: // HardwareAbstract virtual void init() override; - void debugPrint(const char* fmt, ...); + virtual void debugPrint(const char* fmt, ...) override; + virtual std::shared_ptr battery() override; + virtual std::shared_ptr display() override; + virtual std::shared_ptr wifi() override; + + /// @brief To be ran in loop out in main + // TODO move to a freertos task void loopHandler(); - - std::shared_ptr wifi(); - protected: // Init Functions to setup hardware void initIO(); @@ -60,9 +62,9 @@ protected: private: HardwareRevX(); - std::shared_ptr mBattery; - std::shared_ptr mWifiHandler; - std::shared_ptr mDisplay; + std::shared_ptr mBattery; + std::shared_ptr mDisplay; + std::shared_ptr mWifiHandler; // IMU Motion Detection LIS3DH IMU = LIS3DH(I2C_MODE, 0x19); // Default constructor is I2C, addr 0x19. int standbyTimer = SLEEP_TIMEOUT; diff --git a/Platformio/HAL/Targets/Simulator/HardwareSimulator.cpp b/Platformio/HAL/Targets/Simulator/HardwareSimulator.cpp index ada3f47..03d8074 100644 --- a/Platformio/HAL/Targets/Simulator/HardwareSimulator.cpp +++ b/Platformio/HAL/Targets/Simulator/HardwareSimulator.cpp @@ -31,12 +31,18 @@ HardwareSimulator::HardwareSimulator(): HardwareAbstract(){ /* Tick init. * You have to call 'lv_tick_inc()' in periodically to inform LittelvGL about how much time were elapsed * Create an SDL thread to do this*/ - this->mDisplay = SDLDisplay::getInstance(); - this->mWifiHandler = std::make_shared(wifiHandlerSim()); + mBattery = std::make_shared(); + mDisplay = SDLDisplay::getInstance(); + mWifiHandler = std::make_shared(); SDL_CreateThread(tick_thread, "tick", NULL); } -std::shared_ptr HardwareSimulator::wifi() -{ - return this->mWifiHandler; +std::shared_ptr HardwareSimulator::battery(){ + return mBattery; +} +std::shared_ptr HardwareSimulator::display(){ + return mDisplay; +} +std::shared_ptr HardwareSimulator::wifi(){ + return mWifiHandler; } \ No newline at end of file diff --git a/Platformio/HAL/Targets/Simulator/HardwareSimulator.hpp b/Platformio/HAL/Targets/Simulator/HardwareSimulator.hpp index 8d8c970..c2c14ee 100644 --- a/Platformio/HAL/Targets/Simulator/HardwareSimulator.hpp +++ b/Platformio/HAL/Targets/Simulator/HardwareSimulator.hpp @@ -1,13 +1,16 @@ #pragma once #include "HardwareAbstract.hpp" -#include "wifiHandlerSim.hpp" -#include -#include + +#include "batterySimulator.hpp" #include "SDLDisplay.hpp" +#include "wifiHandlerSim.hpp" class HardwareSimulator : public HardwareAbstract { public: HardwareSimulator(); + + virtual void init() override {}; + virtual void debugPrint(const char* fmt, ...) override { va_list arguments; va_start(arguments, fmt); @@ -15,17 +18,12 @@ public: va_end(arguments); } - std::shared_ptr wifi(); + virtual std::shared_ptr battery() override; + virtual std::shared_ptr display() override; + virtual std::shared_ptr wifi() override; - virtual void init() override {}; - - virtual std::optional getBatteryStatus() override { - HardwareAbstract::batteryStatus fakeStatus; - fakeStatus.isCharging = false; - fakeStatus.percentage = 100; - return fakeStatus; - } private: - std::shared_ptr mWifiHandler; + std::shared_ptr mBattery; std::shared_ptr mDisplay; + std::shared_ptr mWifiHandler; }; diff --git a/Platformio/HAL/Targets/Simulator/batterySimulator.hpp b/Platformio/HAL/Targets/Simulator/batterySimulator.hpp new file mode 100644 index 0000000..69f2172 --- /dev/null +++ b/Platformio/HAL/Targets/Simulator/batterySimulator.hpp @@ -0,0 +1,9 @@ +#include "BatteryInterface.h" + +class BatterySimulator: public BatteryInterface{ + public: + BatterySimulator() {}; + virtual int getPercentage() override { return 75; } + virtual bool isCharging() override { return true; } + +}; \ No newline at end of file diff --git a/Platformio/platformio.ini b/Platformio/platformio.ini index 430b7f1..8894a46 100644 --- a/Platformio/platformio.ini +++ b/Platformio/platformio.ini @@ -43,7 +43,9 @@ build_flags = -I HAL/HardwareModules lib_deps = - lvgl/lvgl@^8.3.9 + ;lvgl/lvgl@^8.3.9 + lvgl=https://github.com/lvgl/lvgl/archive/refs/tags/v8.3.9.zip + lib_archive = false build_src_filter = +<../OmoteUI/*> @@ -64,6 +66,7 @@ lib_deps = ${env.lib_deps} sparkfun/SparkFun LIS3DH Arduino Library@^1.0.3 crankyoldgit/IRremoteESP8266@^2.8.4 + adafruit/Adafruit BusIO @ 1.9.6 adafruit/Adafruit FT6206 Library@^1.0.6 bodmer/TFT_eSPI@^2.5.23 knolleary/PubSubClient@^2.8