rework battery Interface and downstream usages of battery
This commit is contained in:
parent
43a930d7a8
commit
92c559df5e
10 changed files with 91 additions and 89 deletions
|
@ -1,23 +1,5 @@
|
|||
#include "HardwareAbstract.hpp"
|
||||
|
||||
HardwareAbstract::HardwareAbstract()
|
||||
{}
|
||||
|
||||
|
||||
std::optional<HardwareAbstract::batteryStatus> 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<void(HardwareAbstract::batteryStatus)> onBatteryStatusChangeHandler){
|
||||
mBatteryNotification.onNotify(std::move(onBatteryStatusChangeHandler));
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -1,45 +1,30 @@
|
|||
// OMOTE Hardware Abstraction
|
||||
// 2023 Matthew Colvin
|
||||
#pragma once
|
||||
#include "BatteryInterface.h"
|
||||
#include "DisplayAbstract.h"
|
||||
#include "wifiHandlerInterface.h"
|
||||
#include <functional>
|
||||
#include <lvgl.h>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "Notification.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
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<batteryStatus> 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<void(batteryStatus)> onBatteryStatusChangeHandler);
|
||||
|
||||
virtual std::shared_ptr<wifiHandlerInterface> 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<BatteryInterface> battery() = 0;
|
||||
virtual std::shared_ptr<DisplayAbstract> display() = 0;
|
||||
virtual std::shared_ptr<wifiHandlerInterface> wifi() = 0;
|
||||
|
||||
protected:
|
||||
Notification<batteryStatus> mBatteryNotification;
|
||||
|
||||
private:
|
||||
|
||||
};
|
9
Platformio/HAL/HardwareModules/BatteryInterface.cpp
Normal file
9
Platformio/HAL/HardwareModules/BatteryInterface.cpp
Normal file
|
@ -0,0 +1,9 @@
|
|||
#include "BatteryInterface.h"
|
||||
|
||||
void BatteryInterface::NotifyCurrentStatus(){
|
||||
mBatteryNotification.notify(getPercentage(),isCharging());
|
||||
}
|
||||
|
||||
void BatteryInterface::onBatteryStatusChange(std::function<void(int,bool)> batteryChangeHandler){
|
||||
mBatteryNotification.onNotify(std::move(batteryChangeHandler));
|
||||
}
|
|
@ -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<void(int,bool)>);
|
||||
private:
|
||||
Notification<int,bool> mBatteryNotification;
|
||||
};
|
|
@ -71,9 +71,9 @@ void HardwareRevX::init() {
|
|||
// Make sure ESP32 is running at full speed
|
||||
setCpuFrequencyMhz(240);
|
||||
|
||||
this->mDisplay = Display::getInstance(std::shared_ptr<HardwareAbstract>(this));
|
||||
this->mBattery = std::make_shared<Battery>(ADC_BAT,CRG_STAT);
|
||||
this->mWifiHandler = wifiHandler::getInstance(std::shared_ptr<HardwareAbstract>(this));
|
||||
mDisplay = Display::getInstance(std::shared_ptr<HardwareAbstract>());
|
||||
mBattery = std::make_shared<Battery>(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> HardwareRevX::getInstance(){
|
||||
if (!mInstance) {
|
||||
|
@ -113,7 +107,15 @@ std::shared_ptr<HardwareRevX> HardwareRevX::getInstance(){
|
|||
|
||||
std::shared_ptr<wifiHandlerInterface> HardwareRevX::wifi()
|
||||
{
|
||||
return this->mWifiHandler;
|
||||
return mWifiHandler;
|
||||
}
|
||||
|
||||
std::shared_ptr<BatteryInterface> HardwareRevX::battery(){
|
||||
return mBattery;
|
||||
}
|
||||
|
||||
std::shared_ptr<DisplayAbstract> 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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<BatteryInterface> battery() override;
|
||||
virtual std::shared_ptr<DisplayAbstract> display() override;
|
||||
virtual std::shared_ptr<wifiHandlerInterface> wifi() override;
|
||||
|
||||
/// @brief To be ran in loop out in main
|
||||
// TODO move to a freertos task
|
||||
void loopHandler();
|
||||
|
||||
std::shared_ptr<wifiHandlerInterface> wifi();
|
||||
|
||||
protected:
|
||||
// Init Functions to setup hardware
|
||||
void initIO();
|
||||
|
@ -60,9 +62,9 @@ protected:
|
|||
private:
|
||||
HardwareRevX();
|
||||
|
||||
std::shared_ptr<BatteryInterface> mBattery;
|
||||
std::shared_ptr<wifiHandlerInterface> mWifiHandler;
|
||||
std::shared_ptr<DisplayAbstract> mDisplay;
|
||||
std::shared_ptr<Battery> mBattery;
|
||||
std::shared_ptr<Display> mDisplay;
|
||||
std::shared_ptr<wifiHandler> mWifiHandler;
|
||||
// IMU Motion Detection
|
||||
LIS3DH IMU = LIS3DH(I2C_MODE, 0x19); // Default constructor is I2C, addr 0x19.
|
||||
int standbyTimer = SLEEP_TIMEOUT;
|
||||
|
|
|
@ -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>(wifiHandlerSim());
|
||||
mBattery = std::make_shared<BatterySimulator>();
|
||||
mDisplay = SDLDisplay::getInstance();
|
||||
mWifiHandler = std::make_shared<wifiHandlerSim>();
|
||||
SDL_CreateThread(tick_thread, "tick", NULL);
|
||||
}
|
||||
|
||||
std::shared_ptr<wifiHandlerInterface> HardwareSimulator::wifi()
|
||||
{
|
||||
return this->mWifiHandler;
|
||||
std::shared_ptr<BatteryInterface> HardwareSimulator::battery(){
|
||||
return mBattery;
|
||||
}
|
||||
std::shared_ptr<DisplayAbstract> HardwareSimulator::display(){
|
||||
return mDisplay;
|
||||
}
|
||||
std::shared_ptr<wifiHandlerInterface> HardwareSimulator::wifi(){
|
||||
return mWifiHandler;
|
||||
}
|
|
@ -1,13 +1,16 @@
|
|||
#pragma once
|
||||
#include "HardwareAbstract.hpp"
|
||||
#include "wifiHandlerSim.hpp"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#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<wifiHandlerInterface> wifi();
|
||||
virtual std::shared_ptr<BatteryInterface> battery() override;
|
||||
virtual std::shared_ptr<DisplayAbstract> display() override;
|
||||
virtual std::shared_ptr<wifiHandlerInterface> wifi() override;
|
||||
|
||||
virtual void init() override {};
|
||||
|
||||
virtual std::optional<HardwareAbstract::batteryStatus> getBatteryStatus() override {
|
||||
HardwareAbstract::batteryStatus fakeStatus;
|
||||
fakeStatus.isCharging = false;
|
||||
fakeStatus.percentage = 100;
|
||||
return fakeStatus;
|
||||
}
|
||||
private:
|
||||
std::shared_ptr<wifiHandlerSim> mWifiHandler;
|
||||
std::shared_ptr<BatterySimulator> mBattery;
|
||||
std::shared_ptr<SDLDisplay> mDisplay;
|
||||
std::shared_ptr<wifiHandlerSim> mWifiHandler;
|
||||
};
|
||||
|
|
9
Platformio/HAL/Targets/Simulator/batterySimulator.hpp
Normal file
9
Platformio/HAL/Targets/Simulator/batterySimulator.hpp
Normal file
|
@ -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; }
|
||||
|
||||
};
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue