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" #include "DisplayInterface.h"
class BatteryInterface { class BatteryInterface {
@ -11,10 +12,11 @@ class BatteryInterface {
/// False - Battery discharging /// False - Battery discharging
bool isCharging; bool isCharging;
}; };
virtual void setup(DisplayInterface& display, int adc_pin, int charging_pin) = 0; virtual BatteryInterface::batteryStatus getBatteryPercentage() = 0;
virtual int getPercentage() = 0; //virtual void setup(DisplayInterface& display, int adc_pin, int charging_pin) = 0;
virtual bool isCharging() = 0; //virtual int getPercentage() = 0;
virtual bool isConnected() = 0; //virtual bool isCharging() = 0;
virtual void update() = 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 #pragma once
#include <lvgl.h> #include <lvgl.h>
#include <memory>
#include <optional>
#include <string> #include <string>
#include "BatteryInterface.h" #include "BatteryInterface.h"
class HardwareInterface { class HardwareInterface {
public: public:
HardwareInterface() = default; HardwareInterface(std::shared_ptr<BatteryInterface> aBattery);
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 BatteryInterface::batteryStatus getBatteryPercentage() = 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:
std::shared_ptr<BatteryInterface> mBattery;
}; };

View file

@ -4,7 +4,7 @@
class wifiHandlerInterface{ class wifiHandlerInterface{
public: public:
virtual void begin(DisplayInterface& display) = 0; virtual void begin() = 0;
virtual void connect(const char* SSID, const char* password) = 0; virtual void connect(const char* SSID, const char* password) = 0;
virtual void disconnect() = 0; virtual void disconnect() = 0;
virtual bool isConnected() = 0; virtual bool isConnected() = 0;

View file

@ -99,10 +99,6 @@ void HardwareRevX::MQTTPublish(const char *topic, const char *payload) {
#endif #endif
} }
BatteryInterface::batteryStatus HardwareRevX::getBatteryPercentage() {
return battery;
}
void HardwareRevX::initLVGL() { void HardwareRevX::initLVGL() {
lv_init(); lv_init();
@ -420,23 +416,10 @@ void HardwareRevX::setupWifi() {
} }
void HardwareRevX::startTasks() { void HardwareRevX::startTasks() {
if (xTaskCreate(&HardwareRevX::updateBatteryTask, "Battery Percent Update", // if (xTaskCreate(&HardwareRevX::updateBatteryTask, "Battery Percent Update",
1024, nullptr, 5, &batteryUpdateTskHndl) != pdPASS) { // 1024, nullptr, 5, &batteryUpdateTskHndl) != pdPASS) {
debugPrint("ERROR Could not Create Battery Update Task!"); // 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
}
} }
void HardwareRevX::loopHandler() { void HardwareRevX::loopHandler() {

View file

@ -5,6 +5,7 @@
#include <WiFi.h> #include <WiFi.h>
#include "Wire.h" #include "Wire.h"
#include "lvgl.h" #include "lvgl.h"
#include "battery.hpp"
#include <Adafruit_FT6206.h> #include <Adafruit_FT6206.h>
#include <IRrecv.h> #include <IRrecv.h>
#include <IRremoteESP8266.h> #include <IRremoteESP8266.h>
@ -32,12 +33,11 @@ public:
} }
static std::weak_ptr<HardwareRevX> getRefrence() { return getInstance(); } static std::weak_ptr<HardwareRevX> getRefrence() { return getInstance(); }
HardwareRevX() : HardwareInterface(){}; HardwareRevX() : HardwareInterface(std::make_shared<Battery>()){};
// HardwareInterface // HardwareInterface
virtual void init() override; virtual void init() override;
virtual void sendIR() override; virtual void sendIR() override;
virtual void MQTTPublish(const char *topic, const char *payload) override; virtual void MQTTPublish(const char *topic, const char *payload) override;
virtual batteryStatus getBatteryPercentage() override;
virtual void debugPrint(std::string aDebugMessage) override; virtual void debugPrint(std::string aDebugMessage) override;
void loopHandler(); void loopHandler();
@ -69,7 +69,6 @@ protected:
// Tasks // Tasks
void startTasks(); void startTasks();
static void updateBatteryTask([[maybe_unused]] void *aData);
TaskHandle_t batteryUpdateTskHndl = nullptr; TaskHandle_t batteryUpdateTskHndl = nullptr;
private: private:
@ -113,7 +112,7 @@ private:
IRsend IrSender = IRsend(IR_LED, true); IRsend IrSender = IRsend(IR_LED, true);
IRrecv IrReceiver = IRrecv(IR_RX); IRrecv IrReceiver = IRrecv(IR_RX);
HardwareInterface::batteryStatus battery; Battery battery;
// LVGL Screen Buffers // LVGL Screen Buffers
lv_disp_draw_buf_t mdraw_buf; lv_disp_draw_buf_t mdraw_buf;

View file

@ -1,9 +1,9 @@
#include "battery.hpp" #include "battery.hpp"
#include <Arduino.h> #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->adc_pin = adc_pin;
this->charging_pin = charging_pin; this->charging_pin = charging_pin;
// Power Pin Definition // Power Pin Definition
@ -33,6 +33,14 @@ int Battery::getVoltage()
void Battery::update() 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 { class Battery: public BatteryInterface {
public: 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 * @brief Get the Percentage of the battery
* *
@ -36,8 +37,9 @@ class Battery: public BatteryInterface {
* *
*/ */
void update(); void update();
// TODO move to cpp file
Battery(){};
private: private:
Battery();
/** /**
* @brief Function to get the current voltage of the battery * @brief Function to get the current voltage of the battery
* *
@ -57,6 +59,4 @@ class Battery: public BatteryInterface {
*/ */
int charging_pin; int charging_pin;
DisplayInterface& display;
}; };

View file

@ -18,21 +18,24 @@ void wifiHandler::WiFiEvent(WiFiEvent_t event){
} }
else else
{ {
this->display.clear_wifi_networks(); // TODO Convert To callbacks
//this->display.clear_wifi_networks();
Serial.print(no_networks); Serial.print(no_networks);
Serial.print(" found\n"); Serial.print(" found\n");
this->display.wifi_scan_complete( no_networks); //this->display.wifi_scan_complete( no_networks);
} }
break; break;
case ARDUINO_EVENT_WIFI_STA_GOT_IP: case ARDUINO_EVENT_WIFI_STA_GOT_IP:
case ARDUINO_EVENT_WIFI_STA_GOT_IP6: 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); this->update_credentials(temporary_ssid, temporary_password);
break; break;
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
case ARDUINO_EVENT_WIFI_STA_LOST_IP: case ARDUINO_EVENT_WIFI_STA_LOST_IP:
case ARDUINO_EVENT_WIFI_STA_STOP: case ARDUINO_EVENT_WIFI_STA_STOP:
display.update_wifi(false); // TODO Convert to Callbacks
//display.update_wifi(false);
default: default:
break; break;
} }
@ -86,9 +89,9 @@ void wifiHandler::scan()
WiFi.scanNetworks(true); WiFi.scanNetworks(true);
} }
void wifiHandler::begin(DisplayInterface& display) void wifiHandler::begin()
{ {
this->display = display; //this->display = display;
mInstance = wifiHandler::getInstance(); mInstance = wifiHandler::getInstance();
WiFi.setHostname("OMOTE"); WiFi.setHostname("OMOTE");
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);

View file

@ -11,7 +11,7 @@ class wifiHandler: public wifiHandlerInterface {
* @brief Function to initialize the wifi handler * @brief Function to initialize the wifi handler
* *
*/ */
void begin(DisplayInterface& display); void begin();
/** /**
* @brief Connect to the wifi using the provided credetials * @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_password[STRING_SIZE];
static char temporary_ssid[STRING_SIZE]; static char temporary_ssid[STRING_SIZE];
DisplayInterface& display;
wifiHandler(); wifiHandler();

View file

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

View file

@ -47,6 +47,7 @@ lib_deps =
lib_archive = false lib_archive = false
build_src_filter = build_src_filter =
+<../OmoteUI/*> +<../OmoteUI/*>
+<../HAL/Interface/*>
@ -95,6 +96,9 @@ build_flags =
; ------------- Includes -------------------------------------------- ; ------------- Includes --------------------------------------------
-I HAL/Targets/ESP32 -I HAL/Targets/ESP32
-I HAL/Targets/ESP32/battery
-I HAL/Targets/ESP32/display
-I HAL/Targets/ESP32/wifiHandler
build_unflags = build_unflags =
-std=gnu++11 -std=gnu++11
@ -135,6 +139,6 @@ lib_deps =
build_src_filter = build_src_filter =
+<simMain.cpp> +<simMain.cpp>
+<../HAL/Targets/Simulator/*> +<../HAL/Targets/Simulator/*>
+<../OmoteUI/*> ${env.build_src_filter}
; Force compile LVGL demo, remove when working on your own project ; Force compile LVGL demo, remove when working on your own project