Power improvements (#114)

This commit is contained in:
Ken Van Hoeylandt
2024-12-09 21:58:30 +01:00
committed by GitHub
parent e4206e8637
commit da81256622
16 changed files with 370 additions and 96 deletions
@@ -8,5 +8,4 @@
extern bool m5stack_bootstrap();
extern bool m5stack_lvgl_init();
extern const tt::hal::Power m5stack_power;
extern const tt::hal::sdcard::SdCard m5stack_sdcard;
-38
View File
@@ -1,38 +0,0 @@
#include "hal/Power.h"
#include "M5Unified.hpp"
/**
* M5.Power by default doesn't have a check to see if charging is enabled.
* However, it's always enabled by default after boot, so we cover that here:
*/
static bool charging_enabled = true;
static bool is_charging() {
return M5.Power.isCharging() == m5::Power_Class::is_charging;
}
static bool is_charging_enabled() {
return charging_enabled;
}
static void set_charging_enabled(bool enabled) {
charging_enabled = enabled; // Local shadow copy because M5 API doesn't provide a function for it
M5.Power.setBatteryCharge(enabled);
}
static uint8_t get_charge_level() {
uint16_t scaled = (uint16_t)M5.Power.getBatteryLevel() * 255 / 100;
return (uint8_t)scaled;
}
static int32_t get_current() {
return M5.Power.getBatteryCurrent();
}
extern const tt::hal::Power m5stack_power = {
.isCharging = &is_charging,
.isChargingEnabled = &is_charging_enabled,
.setChargingEnabled = &set_charging_enabled,
.getChargeLevel = &get_charge_level,
.getCurrent = &get_current
};
@@ -0,0 +1,50 @@
#include "M5stackPower.h"
#include "M5Unified.h"
#define TAG "m5stack_power"
bool M5stackPower::supportsMetric(MetricType type) const {
switch (type) {
case IS_CHARGING:
case CURRENT:
case BATTERY_VOLTAGE:
case CHARGE_LEVEL:
return true;
}
return false; // Safety guard for when new enum values are introduced
}
bool M5stackPower::getMetric(Power::MetricType type, Power::MetricData& data) {
switch (type) {
case IS_CHARGING:
data.valueAsBool = M5.Power.isCharging();
return true;
case CURRENT:
data.valueAsInt32 = M5.Power.getBatteryCurrent();
return true;
case BATTERY_VOLTAGE:
data.valueAsUint32 = M5.Power.getBatteryVoltage();
return true;
case CHARGE_LEVEL:
data.valueAsUint8 = M5.Power.getBatteryLevel();
return true;
}
return false; // Safety guard for when new enum values are introduced
}
void M5stackPower::setAllowedToCharge(bool canCharge) {
M5.Power.setBatteryCharge(canCharge);
}
static std::shared_ptr<Power> power;
std::shared_ptr<Power> m5stack_get_power() {
if (power == nullptr) {
power = std::make_shared<M5stackPower>();
}
return power;
}
@@ -0,0 +1,23 @@
#pragma once
#include "hal/Power.h"
#include <memory>
using namespace tt::hal;
class M5stackPower : public Power {
public:
M5stackPower() {}
~M5stackPower() {}
bool supportsMetric(MetricType type) const override;
bool getMetric(Power::MetricType type, Power::MetricData& data) override;
bool supportsChargeControl() const { return true; }
bool isAllowedToCharge() const { return true; } /** We can call setChargingAllowed() but the actual value is unknown as it resets when re-plugging USB */
void setAllowedToCharge(bool canCharge);
};
std::shared_ptr<Power> m5stack_get_power();