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,
"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,

View file

@ -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;
};

View file

@ -1,6 +1,16 @@
#include "HardwareInterface.h"
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,19 +11,21 @@
class HardwareInterface {
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 sendIR() = 0;
virtual void MQTTPublish(const char *topic, const char *payload) = 0;
virtual void debugPrint(std::string message) = 0;
virtual std::optional<BatteryInterface::batteryStatus> getBatteryStatus() {
if(mBattery){
return mBattery->getBatteryPercentage();
}
return std::nullopt;
}
private:
std::shared_ptr<BatteryInterface> mBattery;

View file

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

View file

@ -27,13 +27,12 @@ public:
static std::shared_ptr<HardwareRevX> getInstance() {
if (!mInstance) {
mInstance = std::make_shared<HardwareRevX>();
mInstance = std::shared_ptr<HardwareRevX>(new HardwareRevX());
}
return mInstance;
}
static std::weak_ptr<HardwareRevX> getRefrence() { return getInstance(); }
HardwareRevX() : HardwareInterface(std::make_shared<Battery>()){};
// 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);

View file

@ -1,14 +1,15 @@
#include "battery.hpp"
#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;
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;
}

View file

@ -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;
};

View file

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