Core2 M5Unified independence + general fixes (#142)
- Change init sequence: I2C is now initialized before "power" init phase, to allow for I2C power control - I2c HAL: Added read/write support for registers - Added axp192 code from https://github.com/tuupola/axp192 to Core2 implementation - Fixes for Core2Power - Fix for root project CMakeLists.txt
This commit is contained in:
committed by
GitHub
parent
9581978fc7
commit
3214923425
@@ -1,11 +1,10 @@
|
||||
#include "Core2Power.h"
|
||||
|
||||
#include "utility/AXP192_Class.hpp"
|
||||
#include "TactilityCore.h"
|
||||
#include "axp192/axp192.h"
|
||||
|
||||
#define TAG "core2_power"
|
||||
|
||||
extern m5::AXP192_Class axpDevice;
|
||||
extern axp192_t axpDevice;
|
||||
|
||||
bool Core2Power::supportsMetric(MetricType type) const {
|
||||
switch (type) {
|
||||
@@ -22,35 +21,81 @@ bool Core2Power::supportsMetric(MetricType type) const {
|
||||
|
||||
bool Core2Power::getMetric(Power::MetricType type, Power::MetricData& data) {
|
||||
switch (type) {
|
||||
case BATTERY_VOLTAGE:
|
||||
data.valueAsUint32 = (uint32_t)TT_MAX((axpDevice.getBatteryVoltage() * 1000.f), 0.0f);
|
||||
return true;
|
||||
case CHARGE_LEVEL: {
|
||||
auto level = axpDevice.getBatteryLevel();
|
||||
if (level > 0) {
|
||||
data.valueAsUint8 = (uint8_t)level;
|
||||
case BATTERY_VOLTAGE: {
|
||||
float voltage;
|
||||
if (axp192_read(&axpDevice, AXP192_BATTERY_VOLTAGE, &voltage) == ESP_OK) {
|
||||
data.valueAsUint32 = (uint32_t)TT_MAX((voltage * 1000.f), 0.0f);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
case CHARGE_LEVEL: {
|
||||
float vbat, charge_current;
|
||||
if (
|
||||
axp192_read(&axpDevice, AXP192_BATTERY_VOLTAGE, &vbat) == ESP_OK &&
|
||||
axp192_read(&axpDevice, AXP192_CHARGE_CURRENT, &charge_current) == ESP_OK
|
||||
) {
|
||||
float max_voltage = 4.20f;
|
||||
float min_voltage = 2.69f; // From M5Unified
|
||||
float voltage_correction = (charge_current > 0.01f) ? -0.1f : 0.f; // Roughly 0.1V drop when ccharging
|
||||
float corrected_voltage = vbat + voltage_correction;
|
||||
if (corrected_voltage > 2.69f) {
|
||||
float charge_factor = (corrected_voltage - min_voltage) / (max_voltage - min_voltage);
|
||||
data.valueAsUint8 = (uint8_t)(charge_factor * 100.f);
|
||||
} else {
|
||||
data.valueAsUint8 = 0;
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
case IS_CHARGING: {
|
||||
float charge_current;
|
||||
if (axp192_read(&axpDevice, AXP192_CHARGE_CURRENT, &charge_current) == ESP_OK) {
|
||||
data.valueAsBool = charge_current > 0.001f;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
case CURRENT: {
|
||||
float charge_current, discharge_current;
|
||||
if (
|
||||
axp192_read(&axpDevice, AXP192_CHARGE_CURRENT, &charge_current) == ESP_OK &&
|
||||
axp192_read(&axpDevice, AXP192_DISCHARGE_CURRENT, &discharge_current) == ESP_OK
|
||||
) {
|
||||
if (charge_current > 0.0f) {
|
||||
data.valueAsInt32 = (int32_t) (charge_current * 1000.0f);
|
||||
} else {
|
||||
data.valueAsInt32 = -(int32_t) (discharge_current * 1000.0f);
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
case IS_CHARGING:
|
||||
data.valueAsBool = axpDevice.isCharging();
|
||||
return true;
|
||||
case CURRENT:
|
||||
return false;
|
||||
}
|
||||
|
||||
return false; // Safety guard for when new enum values are introduced
|
||||
}
|
||||
|
||||
bool Core2Power::isAllowedToCharge() const {
|
||||
return allowedToCharge;
|
||||
uint8_t buffer;
|
||||
if (axp192_read(&axpDevice, AXP192_CHARGE_CONTROL_1, &buffer) == ESP_OK) {
|
||||
return buffer & 0x80;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void Core2Power::setAllowedToCharge(bool canCharge) {
|
||||
allowedToCharge = canCharge;
|
||||
axpDevice.setBatteryCharge(canCharge);
|
||||
uint8_t buffer;
|
||||
if (axp192_read(&axpDevice, AXP192_CHARGE_CONTROL_1, &buffer) == ESP_OK) {
|
||||
buffer = (buffer & 0x7F) + (canCharge ? 0x80 : 0x00);
|
||||
axp192_write(&axpDevice, AXP192_CHARGE_CONTROL_1, buffer);
|
||||
}
|
||||
}
|
||||
|
||||
static std::shared_ptr<Power> power;
|
||||
|
||||
@@ -7,10 +7,6 @@ using namespace tt::hal;
|
||||
|
||||
class Core2Power : public Power {
|
||||
|
||||
private:
|
||||
|
||||
bool allowedToCharge = true;
|
||||
|
||||
public:
|
||||
|
||||
Core2Power() = default;
|
||||
|
||||
Reference in New Issue
Block a user