diff --git a/Platformio/.vscode/settings.json b/Platformio/.vscode/settings.json index df05048..1b6f6c9 100644 --- a/Platformio/.vscode/settings.json +++ b/Platformio/.vscode/settings.json @@ -1,6 +1,7 @@ { "cmake.configureOnOpen": false, "files.associations": { + "*.json": "jsonc", "random": "cpp", "array": "cpp", "atomic": "cpp", @@ -50,7 +51,8 @@ "stdexcept": "cpp", "streambuf": "cpp", "cinttypes": "cpp", - "typeinfo": "cpp" + "typeinfo": "cpp", + "bit": "cpp" }, "cmake.sourceDirectory": "${workspaceFolder}/.pio/libdeps/esp32/Adafruit BusIO", "editor.formatOnSave": false, diff --git a/Platformio/HAL/Interface/BatteryInterface.h b/Platformio/HAL/Interface/BatteryInterface.h index 43c264b..0f600b9 100644 --- a/Platformio/HAL/Interface/BatteryInterface.h +++ b/Platformio/HAL/Interface/BatteryInterface.h @@ -3,20 +3,7 @@ class BatteryInterface { public: - struct batteryStatus { - /// @brief Percent of battery remaining (0-100] - int percentage; - /// @brief Voltage of battery in millivolts - int voltage; - /// @brief True - Battery is Charging - /// False - Battery discharging - bool isCharging; - }; - - 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; + BatteryInterface() = default; + virtual int getPercentage() = 0; + virtual bool isCharging() = 0; }; \ No newline at end of file diff --git a/Platformio/HAL/Interface/HardwareInterface.cpp b/Platformio/HAL/Interface/HardwareInterface.cpp index ff8861a..9c779b4 100644 --- a/Platformio/HAL/Interface/HardwareInterface.cpp +++ b/Platformio/HAL/Interface/HardwareInterface.cpp @@ -1,6 +1,16 @@ #include "HardwareInterface.h" HardwareInterface::HardwareInterface(std::shared_ptr aBattery) -: mBattery(aBattery){ - +: mBattery(std::move(aBattery)){ + } + +std::optional HardwareInterface::getBatteryStatus(){ + if(mBattery){ + HardwareInterface::batteryStatus currentStatus; + currentStatus.percentage = mBattery->getPercentage(); + currentStatus.isCharging = mBattery->isCharging(); + return currentStatus; + } + return std::nullopt; +} \ No newline at end of file diff --git a/Platformio/HAL/Interface/HardwareInterface.h b/Platformio/HAL/Interface/HardwareInterface.h index e2e0d60..7a065cc 100644 --- a/Platformio/HAL/Interface/HardwareInterface.h +++ b/Platformio/HAL/Interface/HardwareInterface.h @@ -11,19 +11,21 @@ class HardwareInterface { public: - HardwareInterface(std::shared_ptr aBattery); + 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(); + + HardwareInterface(std::shared_ptr aBattery = nullptr); virtual void init() = 0; virtual void sendIR() = 0; virtual void MQTTPublish(const char *topic, const char *payload) = 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/Targets/ESP32/HardwareRevX.cpp b/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp index b505659..6572534 100644 --- a/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp +++ b/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp @@ -49,6 +49,10 @@ void HardwareRevX::initIO() { gpio_deep_sleep_hold_dis(); } +HardwareRevX::HardwareRevX():HardwareInterface(std::make_shared(ADC_BAT,CRG_STAT)){ + +} + HardwareRevX::WakeReason getWakeReason() { // Find out wakeup cause if (esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_EXT1) { diff --git a/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp b/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp index eeb4757..42ccf06 100644 --- a/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp +++ b/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp @@ -27,13 +27,12 @@ public: static std::shared_ptr getInstance() { if (!mInstance) { - mInstance = std::make_shared(); + mInstance = std::shared_ptr(new HardwareRevX()); } return mInstance; } static std::weak_ptr getRefrence() { return getInstance(); } - HardwareRevX() : HardwareInterface(std::make_shared()){}; // HardwareInterface virtual void init() override; virtual void sendIR() override; @@ -72,6 +71,8 @@ protected: TaskHandle_t batteryUpdateTskHndl = nullptr; private: + HardwareRevX(); + // Static Wrappers Needed to Satisfy C APIs static void wiFiEventImpl(WiFiEvent_t event) { mInstance->handleWifiEvent(event); diff --git a/Platformio/HAL/Targets/ESP32/battery/battery.cpp b/Platformio/HAL/Targets/ESP32/battery/battery.cpp index 31877c2..8bc621d 100644 --- a/Platformio/HAL/Targets/ESP32/battery/battery.cpp +++ b/Platformio/HAL/Targets/ESP32/battery/battery.cpp @@ -1,14 +1,15 @@ #include "battery.hpp" #include -void Battery::setup( int adc_pin, int charging_pin) +Battery::Battery(int adc_pin, int charging_pin): BatteryInterface(), + mAdcPin(adc_pin), + mChargingPin(charging_pin) { - //this->display = display; - this->adc_pin = adc_pin; - this->charging_pin = charging_pin; + mAdcPin = adc_pin; + mChargingPin = charging_pin; // Power Pin Definition - pinMode(this->charging_pin, INPUT_PULLUP); - pinMode(this->adc_pin, INPUT); + pinMode(mChargingPin, INPUT_PULLUP); + pinMode(mAdcPin, INPUT); } int Battery::getPercentage() @@ -18,29 +19,15 @@ int Battery::getPercentage() bool Battery::isCharging() { - return !digitalRead(this->charging_pin); + return !digitalRead(mChargingPin); } bool Battery::isConnected() { - return ((!this->isCharging()) && (this->getVoltage() < 4350)); + return ((!isCharging()) && (getVoltage() < 4350)); } int Battery::getVoltage() { - return analogRead(this->adc_pin)*2*3300/4095 + 350; -} - -void Battery::update() -{ - // TODO make Callback into UI - //display.update_battery(this->getPercentage(), this->isCharging(), this->isConnected()); -} - -BatteryInterface::batteryStatus Battery::getBatteryPercentage(){ - BatteryInterface::batteryStatus currentStatus; - currentStatus.isCharging = isCharging(); - currentStatus.percentage = getPercentage(); - currentStatus.voltage = getVoltage(); - return currentStatus; -} + return analogRead(mAdcPin)*2*3300/4095 + 350; +} \ No newline at end of file diff --git a/Platformio/HAL/Targets/ESP32/battery/battery.hpp b/Platformio/HAL/Targets/ESP32/battery/battery.hpp index 8bdf659..f09898e 100644 --- a/Platformio/HAL/Targets/ESP32/battery/battery.hpp +++ b/Platformio/HAL/Targets/ESP32/battery/battery.hpp @@ -5,58 +5,54 @@ class Battery: public BatteryInterface { public: - virtual BatteryInterface::batteryStatus getBatteryPercentage(); - - - void setup(int adc_pin, int charging_pin); /** * @brief Get the Percentage of the battery - * - * @return int Percentage of the battery + * + * @return int Percentage of the battery */ - int getPercentage(); + virtual int getPercentage() override; /** * @brief Function to determine if the battery is charging or not - * - * @return true Battery is currently charging + * + * @return true Battery is currently charging * @return false Battery is currently not charging */ - bool isCharging(); + virtual bool isCharging() override; /** - * @brief Function to determine if the battery is connected - * - * @return true Battery is connected + * @brief Function to determine if the battery is connected + * + * @return true Battery is connected * @return false Battery is not connected */ bool isConnected(); - /** - * @brief Function to update the battery status. This should be called on a regular basis - * - */ - void update(); - // TODO move to cpp file - Battery(){}; + Battery(int adc_pin, int charging_pin); + + // Not sure why this is needed but shared_ptr seems to really + // need it possibly a compiler template handling limitation + // none the less we really should not use it. + Battery() = default; private: + /** - * @brief Function to get the current voltage of the battery - * - * @return int Voltage of the battery in mV + * @brief Function to get the current voltage of the battery + * + * @return int Voltage of the battery in mV */ int getVoltage(); /** * @brief Variable to store which pin should be used for ADC - * + * */ - int adc_pin; + int mAdcPin; /** - * @brief Variable to store which pin is used to inidicate if the battery is currently charging or not - * + * @brief Variable to store which pin is used to indicate if the battery is currently charging or not + * */ - int charging_pin; + int mChargingPin; }; \ No newline at end of file diff --git a/Platformio/HAL/Targets/Simulator/HardwareSimulator.hpp b/Platformio/HAL/Targets/Simulator/HardwareSimulator.hpp index a3e0166..9c83967 100644 --- a/Platformio/HAL/Targets/Simulator/HardwareSimulator.hpp +++ b/Platformio/HAL/Targets/Simulator/HardwareSimulator.hpp @@ -19,11 +19,10 @@ public: virtual void init() override; - virtual BatteryInterface::batteryStatus getBatteryPercentage() { - BatteryInterface::batteryStatus fakeStatus; + virtual std::optional getBatteryStatus() override { + HardwareInterface::batteryStatus fakeStatus; fakeStatus.isCharging = false; fakeStatus.percentage = 100; - fakeStatus.voltage = 4200; return fakeStatus; } };