diff --git a/Platformio/HAL/Interface/BatteryInterface.h b/Platformio/HAL/Interface/BatteryInterface.h index 3cc7381..43c264b 100644 --- a/Platformio/HAL/Interface/BatteryInterface.h +++ b/Platformio/HAL/Interface/BatteryInterface.h @@ -1,3 +1,4 @@ +#pragma once #include "DisplayInterface.h" class BatteryInterface { @@ -11,10 +12,11 @@ class BatteryInterface { /// False - Battery discharging bool isCharging; }; - - virtual void setup(DisplayInterface& display, int adc_pin, int charging_pin) = 0; - virtual int getPercentage() = 0; - virtual bool isCharging() = 0; - virtual bool isConnected() = 0; - virtual void update() = 0; + + virtual BatteryInterface::batteryStatus getBatteryPercentage() = 0; + //virtual void setup(DisplayInterface& display, int adc_pin, int charging_pin) = 0; + //virtual int getPercentage() = 0; + //virtual bool isCharging() = 0; + //virtual bool isConnected() = 0; + //virtual void update() = 0; }; \ No newline at end of file diff --git a/Platformio/HAL/Interface/HardwareInterface.cpp b/Platformio/HAL/Interface/HardwareInterface.cpp new file mode 100644 index 0000000..ff8861a --- /dev/null +++ b/Platformio/HAL/Interface/HardwareInterface.cpp @@ -0,0 +1,6 @@ +#include "HardwareInterface.h" + +HardwareInterface::HardwareInterface(std::shared_ptr aBattery) +: mBattery(aBattery){ + +} diff --git a/Platformio/HAL/Interface/HardwareInterface.h b/Platformio/HAL/Interface/HardwareInterface.h index 6e2df38..e2e0d60 100644 --- a/Platformio/HAL/Interface/HardwareInterface.h +++ b/Platformio/HAL/Interface/HardwareInterface.h @@ -3,17 +3,28 @@ #pragma once #include +#include +#include #include #include "BatteryInterface.h" class HardwareInterface { public: - HardwareInterface() = default; + HardwareInterface(std::shared_ptr aBattery); virtual void init() = 0; virtual void sendIR() = 0; virtual void MQTTPublish(const char *topic, const char *payload) = 0; - virtual BatteryInterface::batteryStatus getBatteryPercentage() = 0; virtual void debugPrint(std::string message) = 0; + + virtual std::optional getBatteryStatus() { + if(mBattery){ + return mBattery->getBatteryPercentage(); + } + return std::nullopt; + } + + private: + std::shared_ptr mBattery; }; diff --git a/Platformio/HAL/Interface/wifiHandlerInterface.h b/Platformio/HAL/Interface/wifiHandlerInterface.h index b69db91..08a48aa 100644 --- a/Platformio/HAL/Interface/wifiHandlerInterface.h +++ b/Platformio/HAL/Interface/wifiHandlerInterface.h @@ -4,7 +4,7 @@ class wifiHandlerInterface{ public: - virtual void begin(DisplayInterface& display) = 0; + virtual void begin() = 0; virtual void connect(const char* SSID, const char* password) = 0; virtual void disconnect() = 0; virtual bool isConnected() = 0; diff --git a/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp b/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp index 151974c..b505659 100644 --- a/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp +++ b/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp @@ -99,10 +99,6 @@ void HardwareRevX::MQTTPublish(const char *topic, const char *payload) { #endif } -BatteryInterface::batteryStatus HardwareRevX::getBatteryPercentage() { - return battery; -} - void HardwareRevX::initLVGL() { lv_init(); @@ -420,23 +416,10 @@ void HardwareRevX::setupWifi() { } void HardwareRevX::startTasks() { - if (xTaskCreate(&HardwareRevX::updateBatteryTask, "Battery Percent Update", - 1024, nullptr, 5, &batteryUpdateTskHndl) != pdPASS) { - debugPrint("ERROR Could not Create Battery Update Task!"); - } -} - -void HardwareRevX::updateBatteryTask([[maybe_unused]] void *aData) { - while (true) { - mInstance->battery.voltage = - analogRead(ADC_BAT) * 2 * 3300 / 4095 + 350; // 350mV ADC offset - mInstance->battery.percentage = - constrain(map(mInstance->battery.voltage, 3700, 4200, 0, 100), 0, 100); - mInstance->battery.isCharging = !digitalRead(CRG_STAT); - // Check if battery is charging, fully charged or disconnected - vTaskDelay(1000 / portTICK_PERIOD_MS); - // Update battery at 1Hz - } + // if (xTaskCreate(&HardwareRevX::updateBatteryTask, "Battery Percent Update", + // 1024, nullptr, 5, &batteryUpdateTskHndl) != pdPASS) { + // debugPrint("ERROR Could not Create Battery Update Task!"); + // } } void HardwareRevX::loopHandler() { diff --git a/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp b/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp index 0ff2c68..eeb4757 100644 --- a/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp +++ b/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp @@ -5,6 +5,7 @@ #include #include "Wire.h" #include "lvgl.h" +#include "battery.hpp" #include #include #include @@ -32,12 +33,11 @@ public: } static std::weak_ptr getRefrence() { return getInstance(); } - HardwareRevX() : HardwareInterface(){}; + HardwareRevX() : HardwareInterface(std::make_shared()){}; // HardwareInterface virtual void init() override; virtual void sendIR() override; virtual void MQTTPublish(const char *topic, const char *payload) override; - virtual batteryStatus getBatteryPercentage() override; virtual void debugPrint(std::string aDebugMessage) override; void loopHandler(); @@ -69,7 +69,6 @@ protected: // Tasks void startTasks(); - static void updateBatteryTask([[maybe_unused]] void *aData); TaskHandle_t batteryUpdateTskHndl = nullptr; private: @@ -113,7 +112,7 @@ private: IRsend IrSender = IRsend(IR_LED, true); IRrecv IrReceiver = IRrecv(IR_RX); - HardwareInterface::batteryStatus battery; + Battery battery; // LVGL Screen Buffers lv_disp_draw_buf_t mdraw_buf; diff --git a/Platformio/HAL/Targets/ESP32/battery/battery.cpp b/Platformio/HAL/Targets/ESP32/battery/battery.cpp index 9901741..31877c2 100644 --- a/Platformio/HAL/Targets/ESP32/battery/battery.cpp +++ b/Platformio/HAL/Targets/ESP32/battery/battery.cpp @@ -1,9 +1,9 @@ #include "battery.hpp" #include -void Battery::setup(DisplayInterface& display, int adc_pin, int charging_pin) +void Battery::setup( int adc_pin, int charging_pin) { - this->display = display; + //this->display = display; this->adc_pin = adc_pin; this->charging_pin = charging_pin; // Power Pin Definition @@ -33,6 +33,14 @@ int Battery::getVoltage() void Battery::update() { - display.update_battery(this->getPercentage(), this->isCharging(), this->isConnected()); + // TODO make Callback into UI + //display.update_battery(this->getPercentage(), this->isCharging(), this->isConnected()); +} -} \ No newline at end of file +BatteryInterface::batteryStatus Battery::getBatteryPercentage(){ + BatteryInterface::batteryStatus currentStatus; + currentStatus.isCharging = isCharging(); + currentStatus.percentage = getPercentage(); + currentStatus.voltage = getVoltage(); + return currentStatus; +} diff --git a/Platformio/HAL/Targets/ESP32/battery/battery.hpp b/Platformio/HAL/Targets/ESP32/battery/battery.hpp index df56e63..8bdf659 100644 --- a/Platformio/HAL/Targets/ESP32/battery/battery.hpp +++ b/Platformio/HAL/Targets/ESP32/battery/battery.hpp @@ -5,9 +5,10 @@ class Battery: public BatteryInterface { public: + virtual BatteryInterface::batteryStatus getBatteryPercentage(); - void setup(DisplayInterface& display, int adc_pin, int charging_pin); + void setup(int adc_pin, int charging_pin); /** * @brief Get the Percentage of the battery * @@ -36,8 +37,9 @@ class Battery: public BatteryInterface { * */ void update(); + // TODO move to cpp file + Battery(){}; private: - Battery(); /** * @brief Function to get the current voltage of the battery * @@ -57,6 +59,4 @@ class Battery: public BatteryInterface { */ int charging_pin; - - DisplayInterface& display; }; \ No newline at end of file diff --git a/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.cpp b/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.cpp index 38ec360..97526c0 100644 --- a/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.cpp +++ b/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.cpp @@ -18,21 +18,24 @@ void wifiHandler::WiFiEvent(WiFiEvent_t event){ } else { - this->display.clear_wifi_networks(); + // TODO Convert To callbacks + //this->display.clear_wifi_networks(); Serial.print(no_networks); Serial.print(" found\n"); - this->display.wifi_scan_complete( no_networks); + //this->display.wifi_scan_complete( no_networks); } break; case ARDUINO_EVENT_WIFI_STA_GOT_IP: case ARDUINO_EVENT_WIFI_STA_GOT_IP6: - display.update_wifi(true); + // TODO convert to callbacks + //display.update_wifi(true); this->update_credentials(temporary_ssid, temporary_password); break; case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: case ARDUINO_EVENT_WIFI_STA_LOST_IP: case ARDUINO_EVENT_WIFI_STA_STOP: - display.update_wifi(false); + // TODO Convert to Callbacks + //display.update_wifi(false); default: break; } @@ -86,9 +89,9 @@ void wifiHandler::scan() WiFi.scanNetworks(true); } -void wifiHandler::begin(DisplayInterface& display) +void wifiHandler::begin() { - this->display = display; + //this->display = display; mInstance = wifiHandler::getInstance(); WiFi.setHostname("OMOTE"); WiFi.mode(WIFI_STA); diff --git a/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.hpp b/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.hpp index 7b2f734..1ac8a08 100644 --- a/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.hpp +++ b/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.hpp @@ -11,7 +11,7 @@ class wifiHandler: public wifiHandlerInterface { * @brief Function to initialize the wifi handler * */ - void begin(DisplayInterface& display); + void begin(); /** * @brief Connect to the wifi using the provided credetials @@ -95,7 +95,6 @@ class wifiHandler: public wifiHandlerInterface { static char temporary_password[STRING_SIZE]; static char temporary_ssid[STRING_SIZE]; - DisplayInterface& display; wifiHandler(); diff --git a/Platformio/HAL/Targets/Simulator/HardwareSimulator.hpp b/Platformio/HAL/Targets/Simulator/HardwareSimulator.hpp index 612ad66..a3e0166 100644 --- a/Platformio/HAL/Targets/Simulator/HardwareSimulator.hpp +++ b/Platformio/HAL/Targets/Simulator/HardwareSimulator.hpp @@ -5,7 +5,7 @@ class HardwareSimulator : public HardwareInterface { public: - HardwareSimulator() = default; + HardwareSimulator() : HardwareInterface(nullptr){}; virtual void debugPrint(std::string message) override { std::cout << message; diff --git a/Platformio/platformio.ini b/Platformio/platformio.ini index 4e883c7..8064ff5 100644 --- a/Platformio/platformio.ini +++ b/Platformio/platformio.ini @@ -47,6 +47,7 @@ lib_deps = lib_archive = false build_src_filter = +<../OmoteUI/*> + +<../HAL/Interface/*> @@ -95,6 +96,9 @@ build_flags = ; ------------- Includes -------------------------------------------- -I HAL/Targets/ESP32 + -I HAL/Targets/ESP32/battery + -I HAL/Targets/ESP32/display + -I HAL/Targets/ESP32/wifiHandler build_unflags = -std=gnu++11 @@ -135,6 +139,6 @@ lib_deps = build_src_filter = + +<../HAL/Targets/Simulator/*> - +<../OmoteUI/*> + ${env.build_src_filter} ; Force compile LVGL demo, remove when working on your own project