Power improvements (#114)
This commit is contained in:
committed by
GitHub
parent
e4206e8637
commit
da81256622
@@ -1,5 +1,5 @@
|
||||
idf_component_register(
|
||||
SRC_DIRS "Source" "Source/hal"
|
||||
INCLUDE_DIRS "Source"
|
||||
REQUIRES Tactility esp_lvgl_port esp_lcd esp_lcd_touch_gt911 driver vfs fatfs
|
||||
REQUIRES Tactility esp_lvgl_port esp_lcd esp_lcd_touch_gt911 driver vfs fatfs esp_adc
|
||||
)
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
#include "hal/TdeckDisplayConstants.h"
|
||||
#include <driver/spi_common.h>
|
||||
#include <soc/gpio_num.h>
|
||||
#include <driver/ledc.h>
|
||||
|
||||
#define TAG "tdeck"
|
||||
|
||||
@@ -19,7 +18,6 @@
|
||||
#define TDECK_LCD_BACKLIGHT_LEDC_DUTY_RES LEDC_TIMER_8_BIT
|
||||
#define TDECK_LCD_BACKLIGHT_LEDC_FREQUENCY (4000)
|
||||
|
||||
|
||||
static bool init_spi() {
|
||||
spi_bus_config_t bus_config = {
|
||||
.mosi_io_num = TDECK_SPI_PIN_MOSI,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "hal/Configuration.h"
|
||||
#include "hal/TdeckDisplay.h"
|
||||
#include "hal/TdeckKeyboard.h"
|
||||
#include "hal/TdeckPower.h"
|
||||
#include "hal/sdcard/Sdcard.h"
|
||||
|
||||
bool tdeck_init_power();
|
||||
@@ -16,7 +17,7 @@ extern const tt::hal::Configuration lilygo_tdeck = {
|
||||
.createDisplay = createDisplay,
|
||||
.createKeyboard = createKeyboard,
|
||||
.sdcard = &tdeck_sdcard,
|
||||
.power = nullptr,
|
||||
.power = tdeck_get_power,
|
||||
.i2c = {
|
||||
tt::hal::i2c::Configuration {
|
||||
.name = "Internal",
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
#include "TdeckPower.h"
|
||||
|
||||
#include "Log.h"
|
||||
#include "CoreDefines.h"
|
||||
|
||||
#define TAG "power"
|
||||
|
||||
/**
|
||||
* The ratio of the voltage divider is supposedly 2.0, but when we set that, the ADC reports a bit over 4.45V
|
||||
* when charging the device.
|
||||
* There was also supposedly a +0.11 sag compensation related to "display under-voltage" according to Meshtastic firmware.
|
||||
* Either Meshtastic implemented it incorrectly OR there is simply a 5-10% deviation in accuracy.
|
||||
* The latter is feasible as the selected resistors for the voltage divider might not have been matched appropriately.
|
||||
*/
|
||||
#define ADC_MULTIPLIER 1.89f
|
||||
|
||||
#define ADC_REF_VOLTAGE 3.3f
|
||||
#define BATTERY_VOLTAGE_MIN 3.2f
|
||||
#define BATTERY_VOLTAGE_MAX 4.2f
|
||||
|
||||
static adc_oneshot_unit_init_cfg_t adcConfig = {
|
||||
.unit_id = ADC_UNIT_1,
|
||||
.clk_src = ADC_RTC_CLK_SRC_DEFAULT,
|
||||
.ulp_mode = ADC_ULP_MODE_DISABLE,
|
||||
};
|
||||
|
||||
static adc_oneshot_chan_cfg_t adcChannelConfig = {
|
||||
.atten = ADC_ATTEN_DB_12,
|
||||
.bitwidth = ADC_BITWIDTH_DEFAULT,
|
||||
};
|
||||
|
||||
static uint8_t estimateChargeLevelFromVoltage(uint32_t milliVolt) {
|
||||
float volts = TT_MIN((float)milliVolt / 1000.f, BATTERY_VOLTAGE_MAX);
|
||||
float voltage_percentage = (volts - BATTERY_VOLTAGE_MIN) / (BATTERY_VOLTAGE_MAX - BATTERY_VOLTAGE_MIN);
|
||||
float voltage_factor = TT_MIN(1.0f, voltage_percentage);
|
||||
auto charge_level = (uint8_t) (voltage_factor * 100.f);
|
||||
TT_LOG_V(TAG, "mV = %lu, scaled = %.2f, factor = %.2f, result = %d", milliVolt, volts, voltage_factor, charge_level);
|
||||
return charge_level;
|
||||
}
|
||||
|
||||
TdeckPower::TdeckPower() {
|
||||
if (adc_oneshot_new_unit(&adcConfig, &adcHandle) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "ADC config failed");
|
||||
return;
|
||||
}
|
||||
|
||||
if (adc_oneshot_config_channel(adcHandle, ADC_CHANNEL_3, &adcChannelConfig) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "ADC channel config failed");
|
||||
|
||||
adc_oneshot_del_unit(adcHandle);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
TdeckPower::~TdeckPower() {
|
||||
if (adcHandle) {
|
||||
adc_oneshot_del_unit(adcHandle);
|
||||
}
|
||||
}
|
||||
|
||||
bool TdeckPower::supportsMetric(MetricType type) const {
|
||||
switch (type) {
|
||||
case BATTERY_VOLTAGE:
|
||||
case CHARGE_LEVEL:
|
||||
return true;
|
||||
case IS_CHARGING:
|
||||
case CURRENT:
|
||||
return false;
|
||||
}
|
||||
|
||||
return false; // Safety guard for when new enum values are introduced
|
||||
}
|
||||
|
||||
bool TdeckPower::getMetric(Power::MetricType type, Power::MetricData& data) {
|
||||
switch (type) {
|
||||
case BATTERY_VOLTAGE:
|
||||
return readBatteryVoltageSampled(data.valueAsUint32);
|
||||
case CHARGE_LEVEL:
|
||||
if (readBatteryVoltageSampled(data.valueAsUint32)) {
|
||||
data.valueAsUint32 = estimateChargeLevelFromVoltage(data.valueAsUint32);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
case IS_CHARGING:
|
||||
case CURRENT:
|
||||
return false;
|
||||
}
|
||||
|
||||
return false; // Safety guard for when new enum values are introduced
|
||||
}
|
||||
|
||||
bool TdeckPower::readBatteryVoltageOnce(uint32_t& output) {
|
||||
int raw;
|
||||
if (adc_oneshot_read(adcHandle, ADC_CHANNEL_3, &raw) == ESP_OK) {
|
||||
output = ADC_MULTIPLIER * ((1000.f * ADC_REF_VOLTAGE) / 4096.f) * (float)raw;
|
||||
TT_LOG_V(TAG, "Raw = %d, voltage = %lu", raw, output);
|
||||
return true;
|
||||
} else {
|
||||
TT_LOG_E(TAG, "Read failed");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
#define MAX_VOLTAGE_SAMPLES 15
|
||||
|
||||
bool TdeckPower::readBatteryVoltageSampled(uint32_t& output) {
|
||||
size_t samples_read = 0;
|
||||
uint32_t sample_accumulator = 0;
|
||||
uint32_t sample_read_buffer;
|
||||
|
||||
for (size_t i = 0; i < MAX_VOLTAGE_SAMPLES; ++i) {
|
||||
if (readBatteryVoltageOnce(sample_read_buffer)) {
|
||||
sample_accumulator += sample_read_buffer;
|
||||
samples_read++;
|
||||
}
|
||||
}
|
||||
|
||||
if (samples_read > 0) {
|
||||
output = sample_accumulator / samples_read;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static std::shared_ptr<Power> power;
|
||||
|
||||
std::shared_ptr<Power> tdeck_get_power() {
|
||||
if (power == nullptr) {
|
||||
power = std::make_shared<TdeckPower>();
|
||||
}
|
||||
return power;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include "hal/Power.h"
|
||||
#include <esp_adc/adc_oneshot.h>
|
||||
#include <memory>
|
||||
|
||||
using namespace tt::hal;
|
||||
|
||||
class TdeckPower : public Power {
|
||||
|
||||
adc_oneshot_unit_handle_t adcHandle = nullptr;
|
||||
|
||||
public:
|
||||
|
||||
TdeckPower();
|
||||
~TdeckPower();
|
||||
|
||||
bool supportsMetric(MetricType type) const override;
|
||||
bool getMetric(Power::MetricType type, Power::MetricData& data) override;
|
||||
|
||||
private:
|
||||
|
||||
bool readBatteryVoltageSampled(uint32_t& output);
|
||||
bool readBatteryVoltageOnce(uint32_t& output);
|
||||
};
|
||||
|
||||
std::shared_ptr<Power> tdeck_get_power();
|
||||
@@ -1,12 +1,13 @@
|
||||
#include "M5stackCore2.h"
|
||||
#include "M5stackShared.h"
|
||||
#include "hal/M5stackPower.h"
|
||||
|
||||
extern const tt::hal::Configuration m5stack_core2 = {
|
||||
.initBoot = &m5stack_bootstrap,
|
||||
.initLvgl = &m5stack_lvgl_init,
|
||||
.initBoot = m5stack_bootstrap,
|
||||
.initLvgl = m5stack_lvgl_init,
|
||||
.createDisplay = createDisplay,
|
||||
.sdcard = &m5stack_sdcard,
|
||||
.power = &m5stack_power,
|
||||
.power = m5stack_get_power,
|
||||
.i2c = {
|
||||
tt::hal::i2c::Configuration {
|
||||
.name = "Internal",
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
#include "M5stackCoreS3.h"
|
||||
#include "M5stackShared.h"
|
||||
#include "hal/M5stackPower.h"
|
||||
|
||||
const tt::hal::Configuration m5stack_cores3 = {
|
||||
.initBoot = &m5stack_bootstrap,
|
||||
.initLvgl = &m5stack_lvgl_init,
|
||||
.initBoot = m5stack_bootstrap,
|
||||
.initLvgl = m5stack_lvgl_init,
|
||||
.createDisplay = createDisplay,
|
||||
.sdcard = &m5stack_sdcard,
|
||||
.power = &m5stack_power,
|
||||
.power = m5stack_get_power,
|
||||
.i2c = {
|
||||
tt::hal::i2c::Configuration {
|
||||
.name = "Internal",
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
Reference in New Issue
Block a user