add batteryStatus info and allow access to it through the interface.

Change-Id: Ieb748b1478d532f52ffff9edc783de3cbcf9f303
This commit is contained in:
Matthew Colvin 2023-08-01 15:45:51 -05:00 committed by MatthewColvin
parent b09d4f7331
commit a0a46f729f
4 changed files with 45 additions and 28 deletions

View file

@ -1,30 +1,29 @@
#pragma once #pragma once
#include "HardwareAbstractionInterface.h" #include "HardwareAbstractionInterface.h"
#include <string>
#include <iostream> #include <iostream>
#include <string>
class HardwareSimulator : class HardwareSimulator : public HardwareAbstractionInterface {
public HardwareAbstractionInterface public:
{ HardwareSimulator() = default;
public:
HardwareSimulator() = default; virtual void debugPrint(std::string message) override {
std::cout << message;
}
virtual void debugPrint(std::string message) override { virtual void sendIR() override {}
std::cout << message;
}
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 void init() override { lv_init(); }
virtual void init() override {
lv_init();
}
virtual batteryStatus getBatteryPercentage() {
batteryStatus fakeStatus;
fakeStatus.isCharging = false;
fakeStatus.percentage = 100;
fakeStatus.voltage = 4200;
return fakeStatus;
}
}; };

View file

@ -7,10 +7,21 @@
class HardwareAbstractionInterface { class HardwareAbstractionInterface {
public: 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;
};
HardwareAbstractionInterface() = default; HardwareAbstractionInterface() = default;
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 batteryStatus getBatteryPercentage() = 0;
virtual void debugPrint(std::string message) = 0; virtual void debugPrint(std::string message) = 0;
}; };

View file

@ -99,6 +99,10 @@ void HardwareRevX::MQTTPublish(const char *topic, const char *payload) {
#endif #endif
} }
HardwareAbstractionInterface::batteryStatus HardwareRevX::getBatteryPercentage(){
return battery;
}
void HardwareRevX::initLVGL() { void HardwareRevX::initLVGL() {
lv_init(); lv_init();
@ -424,11 +428,11 @@ void HardwareRevX::startTasks() {
void HardwareRevX::updateBatteryTask([[maybe_unused]] void *aData) { void HardwareRevX::updateBatteryTask([[maybe_unused]] void *aData) {
while (true) { while (true) {
mInstance->battery_voltage = mInstance->battery.voltage =
analogRead(ADC_BAT) * 2 * 3300 / 4095 + 350; // 350mV ADC offset analogRead(ADC_BAT) * 2 * 3300 / 4095 + 350; // 350mV ADC offset
mInstance->battery_percentage = mInstance->battery.percentage =
constrain(map(mInstance->battery_voltage, 3700, 4200, 0, 100), 0, 100); constrain(map(mInstance->battery.voltage, 3700, 4200, 0, 100), 0, 100);
mInstance->battery_ischarging = !digitalRead(CRG_STAT); mInstance->battery.isCharging = !digitalRead(CRG_STAT);
// Check if battery is charging, fully charged or disconnected // Check if battery is charging, fully charged or disconnected
vTaskDelay(1000 / portTICK_PERIOD_MS); vTaskDelay(1000 / portTICK_PERIOD_MS);
// Update battery at 1Hz // Update battery at 1Hz
@ -436,6 +440,9 @@ void HardwareRevX::updateBatteryTask([[maybe_unused]] void *aData) {
} }
void HardwareRevX::loopHandler() { void HardwareRevX::loopHandler() {
// TODO Move the backlight handling into task that spawns when the backlight
// setting changes and then gets deleted when the setting is achieved.
// Update Backlight brightness // Update Backlight brightness
static int fadeInTimer = millis(); // fadeInTimer = time after setup static int fadeInTimer = millis(); // fadeInTimer = time after setup
if (millis() < if (millis() <
@ -448,6 +455,7 @@ void HardwareRevX::loopHandler() {
ledcWrite(5, backlight_brightness); // Backlight on ledcWrite(5, backlight_brightness); // Backlight on
} }
// TODO move to debug task
// Blink debug LED at 1 Hz // Blink debug LED at 1 Hz
digitalWrite(USER_LED, millis() % 1000 > 500); digitalWrite(USER_LED, millis() % 1000 > 500);

View file

@ -34,12 +34,13 @@ public:
} }
HardwareRevX() : HardwareAbstractionInterface(){}; HardwareRevX() : HardwareAbstractionInterface(){};
// HardwareAbstractionInterface // HardwareAbstractionInterface
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();
protected: protected:
@ -114,9 +115,7 @@ private:
IRsend IrSender = IRsend(IR_LED, true); IRsend IrSender = IRsend(IR_LED, true);
IRrecv IrReceiver = IRrecv(IR_RX); IRrecv IrReceiver = IRrecv(IR_RX);
int battery_voltage = 0; HardwareAbstractionInterface::batteryStatus battery;
int battery_percentage = 100;
bool battery_ischarging = false;
// LVGL Screen Buffers // LVGL Screen Buffers
lv_disp_draw_buf_t mdraw_buf; lv_disp_draw_buf_t mdraw_buf;