Further update battery Interface and downstream code

This commit is contained in:
Matthew Colvin 2023-08-11 12:10:34 -05:00 committed by MatthewColvin
parent cd603a2a83
commit 85343d9bd4
9 changed files with 72 additions and 84 deletions

View file

@ -1,6 +1,7 @@
{ {
"cmake.configureOnOpen": false, "cmake.configureOnOpen": false,
"files.associations": { "files.associations": {
"*.json": "jsonc",
"random": "cpp", "random": "cpp",
"array": "cpp", "array": "cpp",
"atomic": "cpp", "atomic": "cpp",
@ -50,7 +51,8 @@
"stdexcept": "cpp", "stdexcept": "cpp",
"streambuf": "cpp", "streambuf": "cpp",
"cinttypes": "cpp", "cinttypes": "cpp",
"typeinfo": "cpp" "typeinfo": "cpp",
"bit": "cpp"
}, },
"cmake.sourceDirectory": "${workspaceFolder}/.pio/libdeps/esp32/Adafruit BusIO", "cmake.sourceDirectory": "${workspaceFolder}/.pio/libdeps/esp32/Adafruit BusIO",
"editor.formatOnSave": false, "editor.formatOnSave": false,

View file

@ -3,20 +3,7 @@
class BatteryInterface { class BatteryInterface {
public: public:
struct batteryStatus { BatteryInterface() = default;
/// @brief Percent of battery remaining (0-100] virtual int getPercentage() = 0;
int percentage; virtual bool isCharging() = 0;
/// @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;
}; };

View file

@ -1,6 +1,16 @@
#include "HardwareInterface.h" #include "HardwareInterface.h"
HardwareInterface::HardwareInterface(std::shared_ptr<BatteryInterface> aBattery) HardwareInterface::HardwareInterface(std::shared_ptr<BatteryInterface> aBattery)
: mBattery(aBattery){ : mBattery(std::move(aBattery)){
} }
std::optional<HardwareInterface::batteryStatus> HardwareInterface::getBatteryStatus(){
if(mBattery){
HardwareInterface::batteryStatus currentStatus;
currentStatus.percentage = mBattery->getPercentage();
currentStatus.isCharging = mBattery->isCharging();
return currentStatus;
}
return std::nullopt;
}

View file

@ -11,20 +11,22 @@
class HardwareInterface { class HardwareInterface {
public: public:
HardwareInterface(std::shared_ptr<BatteryInterface> 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<batteryStatus> getBatteryStatus();
HardwareInterface(std::shared_ptr<BatteryInterface> aBattery = nullptr);
virtual void init() = 0; virtual void init() = 0;
virtual void sendIR() = 0; virtual void sendIR() = 0;
virtual void MQTTPublish(const char *topic, const char *payload) = 0; virtual void MQTTPublish(const char *topic, const char *payload) = 0;
virtual void debugPrint(std::string message) = 0; virtual void debugPrint(std::string message) = 0;
virtual std::optional<BatteryInterface::batteryStatus> getBatteryStatus() {
if(mBattery){
return mBattery->getBatteryPercentage();
}
return std::nullopt;
}
private: private:
std::shared_ptr<BatteryInterface> mBattery; std::shared_ptr<BatteryInterface> mBattery;
}; };

View file

@ -49,6 +49,10 @@ void HardwareRevX::initIO() {
gpio_deep_sleep_hold_dis(); gpio_deep_sleep_hold_dis();
} }
HardwareRevX::HardwareRevX():HardwareInterface(std::make_shared<Battery>(ADC_BAT,CRG_STAT)){
}
HardwareRevX::WakeReason getWakeReason() { HardwareRevX::WakeReason getWakeReason() {
// Find out wakeup cause // Find out wakeup cause
if (esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_EXT1) { if (esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_EXT1) {

View file

@ -27,13 +27,12 @@ public:
static std::shared_ptr<HardwareRevX> getInstance() { static std::shared_ptr<HardwareRevX> getInstance() {
if (!mInstance) { if (!mInstance) {
mInstance = std::make_shared<HardwareRevX>(); mInstance = std::shared_ptr<HardwareRevX>(new HardwareRevX());
} }
return mInstance; return mInstance;
} }
static std::weak_ptr<HardwareRevX> getRefrence() { return getInstance(); } static std::weak_ptr<HardwareRevX> getRefrence() { return getInstance(); }
HardwareRevX() : HardwareInterface(std::make_shared<Battery>()){};
// HardwareInterface // HardwareInterface
virtual void init() override; virtual void init() override;
virtual void sendIR() override; virtual void sendIR() override;
@ -72,6 +71,8 @@ protected:
TaskHandle_t batteryUpdateTskHndl = nullptr; TaskHandle_t batteryUpdateTskHndl = nullptr;
private: private:
HardwareRevX();
// Static Wrappers Needed to Satisfy C APIs // Static Wrappers Needed to Satisfy C APIs
static void wiFiEventImpl(WiFiEvent_t event) { static void wiFiEventImpl(WiFiEvent_t event) {
mInstance->handleWifiEvent(event); mInstance->handleWifiEvent(event);

View file

@ -1,14 +1,15 @@
#include "battery.hpp" #include "battery.hpp"
#include <Arduino.h> #include <Arduino.h>
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; mAdcPin = adc_pin;
this->adc_pin = adc_pin; mChargingPin = charging_pin;
this->charging_pin = charging_pin;
// Power Pin Definition // Power Pin Definition
pinMode(this->charging_pin, INPUT_PULLUP); pinMode(mChargingPin, INPUT_PULLUP);
pinMode(this->adc_pin, INPUT); pinMode(mAdcPin, INPUT);
} }
int Battery::getPercentage() int Battery::getPercentage()
@ -18,29 +19,15 @@ int Battery::getPercentage()
bool Battery::isCharging() bool Battery::isCharging()
{ {
return !digitalRead(this->charging_pin); return !digitalRead(mChargingPin);
} }
bool Battery::isConnected() bool Battery::isConnected()
{ {
return ((!this->isCharging()) && (this->getVoltage() < 4350)); return ((!isCharging()) && (getVoltage() < 4350));
} }
int Battery::getVoltage() int Battery::getVoltage()
{ {
return analogRead(this->adc_pin)*2*3300/4095 + 350; return analogRead(mAdcPin)*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;
} }

View file

@ -5,16 +5,12 @@
class Battery: public BatteryInterface { class Battery: public BatteryInterface {
public: public:
virtual BatteryInterface::batteryStatus getBatteryPercentage();
void setup(int adc_pin, int charging_pin);
/** /**
* @brief Get the Percentage of the battery * @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 * @brief Function to determine if the battery is charging or not
@ -22,7 +18,7 @@ class Battery: public BatteryInterface {
* @return true Battery is currently charging * @return true Battery is currently charging
* @return false Battery is currently not charging * @return false Battery is currently not charging
*/ */
bool isCharging(); virtual bool isCharging() override;
/** /**
* @brief Function to determine if the battery is connected * @brief Function to determine if the battery is connected
@ -32,14 +28,14 @@ class Battery: public BatteryInterface {
*/ */
bool isConnected(); bool isConnected();
/** Battery(int adc_pin, int charging_pin);
* @brief Function to update the battery status. This should be called on a regular basis
* // Not sure why this is needed but shared_ptr seems to really
*/ // need it possibly a compiler template handling limitation
void update(); // none the less we really should not use it.
// TODO move to cpp file Battery() = default;
Battery(){};
private: private:
/** /**
* @brief Function to get the current voltage of the battery * @brief Function to get the current voltage of the battery
* *
@ -51,12 +47,12 @@ class Battery: public BatteryInterface {
* @brief Variable to store which pin should be used for ADC * @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;
}; };

View file

@ -19,11 +19,10 @@ public:
virtual void init() override; virtual void init() override;
virtual BatteryInterface::batteryStatus getBatteryPercentage() { virtual std::optional<HardwareInterface::batteryStatus> getBatteryStatus() override {
BatteryInterface::batteryStatus fakeStatus; HardwareInterface::batteryStatus fakeStatus;
fakeStatus.isCharging = false; fakeStatus.isCharging = false;
fakeStatus.percentage = 100; fakeStatus.percentage = 100;
fakeStatus.voltage = 4200;
return fakeStatus; return fakeStatus;
} }
}; };