Update Battery Interface by adding it to hardwareInterface

Remove Display out of some classes and leave comments to replace for callbacks
I dont know about the function of this code but it compiles :)
This commit is contained in:
Matthew Colvin 2023-08-10 21:44:18 -05:00 committed by MatthewColvin
parent c4547917c8
commit cd603a2a83
12 changed files with 67 additions and 52 deletions

View file

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

View file

@ -0,0 +1,6 @@
#include "HardwareInterface.h"
HardwareInterface::HardwareInterface(std::shared_ptr<BatteryInterface> aBattery)
: mBattery(aBattery){
}

View file

@ -3,17 +3,28 @@
#pragma once
#include <lvgl.h>
#include <memory>
#include <optional>
#include <string>
#include "BatteryInterface.h"
class HardwareInterface {
public:
HardwareInterface() = default;
HardwareInterface(std::shared_ptr<BatteryInterface> 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<BatteryInterface::batteryStatus> getBatteryStatus() {
if(mBattery){
return mBattery->getBatteryPercentage();
}
return std::nullopt;
}
private:
std::shared_ptr<BatteryInterface> mBattery;
};

View file

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

View file

@ -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() {

View file

@ -5,6 +5,7 @@
#include <WiFi.h>
#include "Wire.h"
#include "lvgl.h"
#include "battery.hpp"
#include <Adafruit_FT6206.h>
#include <IRrecv.h>
#include <IRremoteESP8266.h>
@ -32,12 +33,11 @@ public:
}
static std::weak_ptr<HardwareRevX> getRefrence() { return getInstance(); }
HardwareRevX() : HardwareInterface(){};
HardwareRevX() : HardwareInterface(std::make_shared<Battery>()){};
// 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;

View file

@ -1,9 +1,9 @@
#include "battery.hpp"
#include <Arduino.h>
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());
}
}
BatteryInterface::batteryStatus Battery::getBatteryPercentage(){
BatteryInterface::batteryStatus currentStatus;
currentStatus.isCharging = isCharging();
currentStatus.percentage = getPercentage();
currentStatus.voltage = getVoltage();
return currentStatus;
}

View file

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

View file

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

View file

@ -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();

View file

@ -5,7 +5,7 @@
class HardwareSimulator : public HardwareInterface {
public:
HardwareSimulator() = default;
HardwareSimulator() : HardwareInterface(nullptr){};
virtual void debugPrint(std::string message) override {
std::cout << message;

View file

@ -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 =
+<simMain.cpp>
+<../HAL/Targets/Simulator/*>
+<../OmoteUI/*>
${env.build_src_filter}
; Force compile LVGL demo, remove when working on your own project