mirror of
https://github.com/ByteWelder/Tactility.git
synced 2026-04-18 09:25:06 +00:00
Implement battery support for LilyGO T-HMI S3 (#511)
This commit is contained in:
parent
2426c387eb
commit
9fc0aa51d7
@ -11,9 +11,9 @@ using namespace tt::hal;
|
|||||||
|
|
||||||
static std::vector<std::shared_ptr<tt::hal::Device>> createDevices() {
|
static std::vector<std::shared_ptr<tt::hal::Device>> createDevices() {
|
||||||
return {
|
return {
|
||||||
createPower(),
|
|
||||||
createSdCard(),
|
createSdCard(),
|
||||||
createDisplay(),
|
createDisplay(),
|
||||||
|
std::make_shared<Power>(),
|
||||||
ButtonControl::createOneButtonControl(0)
|
ButtonControl::createOneButtonControl(0)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,90 @@
|
|||||||
#include "Power.h"
|
#include "Power.h"
|
||||||
|
|
||||||
#include <ChargeFromAdcVoltage.h>
|
#include <Tactility/Logger.h>
|
||||||
#include <EstimatedPower.h>
|
#include <driver/adc.h>
|
||||||
|
|
||||||
std::shared_ptr<tt::hal::power::PowerDevice> createPower() {
|
static const auto LOGGER = tt::Logger("Power");
|
||||||
ChargeFromAdcVoltage::Configuration configuration;
|
|
||||||
// 2.0 ratio, but +.11 added as display voltage sag compensation.
|
|
||||||
configuration.adcMultiplier = 2.11;
|
|
||||||
|
|
||||||
return std::make_shared<EstimatedPower>(configuration);
|
bool Power::adcInitCalibration() {
|
||||||
}
|
bool calibrated = false;
|
||||||
|
|
||||||
|
esp_err_t efuse_read_result = esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_TP_FIT);
|
||||||
|
if (efuse_read_result == ESP_ERR_NOT_SUPPORTED) {
|
||||||
|
LOGGER.warn("Calibration scheme not supported, skip software calibration");
|
||||||
|
} else if (efuse_read_result == ESP_ERR_INVALID_VERSION) {
|
||||||
|
LOGGER.warn("eFuse not burnt, skip software calibration");
|
||||||
|
} else if (efuse_read_result == ESP_OK) {
|
||||||
|
calibrated = true;
|
||||||
|
LOGGER.info("Calibration success");
|
||||||
|
esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, static_cast<adc_bits_width_t>(ADC_WIDTH_BIT_DEFAULT), 0, &adcCharacteristics);
|
||||||
|
} else {
|
||||||
|
LOGGER.warn("eFuse read failed, skipping calibration");
|
||||||
|
}
|
||||||
|
|
||||||
|
return calibrated;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t Power::adcReadValue() const {
|
||||||
|
int adc_raw = adc1_get_raw(ADC1_CHANNEL_4);
|
||||||
|
LOGGER.debug("Raw data: {}", adc_raw);
|
||||||
|
|
||||||
|
uint32_t voltage;
|
||||||
|
|
||||||
|
if (calibrated) {
|
||||||
|
voltage = esp_adc_cal_raw_to_voltage(adc_raw, &adcCharacteristics);
|
||||||
|
LOGGER.debug("Calibrated data: {} mV", voltage);
|
||||||
|
} else {
|
||||||
|
voltage = (adc_raw * 3300) / 4095; // fallback
|
||||||
|
LOGGER.debug("Estimated data: {} mV", voltage);
|
||||||
|
}
|
||||||
|
|
||||||
|
return voltage;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Power::ensureInitialized() {
|
||||||
|
if (!initialized) {
|
||||||
|
|
||||||
|
if (adc1_config_width(ADC_WIDTH_BIT_12) != ESP_OK) {
|
||||||
|
LOGGER.error("ADC1 config width failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (adc1_config_channel_atten(ADC1_CHANNEL_4, ADC_ATTEN_DB_11) != ESP_OK) {
|
||||||
|
LOGGER.error("ADC1 config attenuation failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
calibrated = adcInitCalibration();
|
||||||
|
|
||||||
|
initialized = true;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Power::supportsMetric(MetricType type) const {
|
||||||
|
switch (type) {
|
||||||
|
using enum MetricType;
|
||||||
|
case BatteryVoltage:
|
||||||
|
case ChargeLevel:
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Power::getMetric(MetricType type, MetricData& data) {
|
||||||
|
if (!ensureInitialized()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (type) {
|
||||||
|
case MetricType::BatteryVoltage:
|
||||||
|
data.valueAsUint32 = adcReadValue() * 2;
|
||||||
|
return true;
|
||||||
|
case MetricType::ChargeLevel:
|
||||||
|
data.valueAsUint8 = chargeFromAdcVoltage.estimateCharge(adcReadValue() * 2);
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -1,10 +1,33 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <memory>
|
#include <string>
|
||||||
#include <Tactility/hal/power/PowerDevice.h>
|
#include <esp_adc_cal.h>
|
||||||
#include <driver/gpio.h>
|
#include <driver/gpio.h>
|
||||||
|
#include <ChargeFromVoltage.h>
|
||||||
|
#include <Tactility/hal/power/PowerDevice.h>
|
||||||
|
|
||||||
constexpr auto THMI_S3_POWEREN_GPIO = GPIO_NUM_10;
|
constexpr auto THMI_S3_POWEREN_GPIO = GPIO_NUM_10;
|
||||||
constexpr auto THMI_S3_POWERON_GPIO = GPIO_NUM_14;
|
constexpr auto THMI_S3_POWERON_GPIO = GPIO_NUM_14;
|
||||||
|
|
||||||
std::shared_ptr<tt::hal::power::PowerDevice> createPower();
|
using tt::hal::power::PowerDevice;
|
||||||
|
|
||||||
|
class Power final : public PowerDevice {
|
||||||
|
|
||||||
|
ChargeFromVoltage chargeFromAdcVoltage = ChargeFromVoltage(3.3f, 4.2f);
|
||||||
|
bool initialized = false;
|
||||||
|
esp_adc_cal_characteristics_t adcCharacteristics;
|
||||||
|
bool calibrated = false;
|
||||||
|
|
||||||
|
bool adcInitCalibration();
|
||||||
|
uint32_t adcReadValue() const;
|
||||||
|
|
||||||
|
bool ensureInitialized();
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
std::string getName() const override { return "T-hmi Power"; }
|
||||||
|
std::string getDescription() const override { return "Power measurement via ADC"; }
|
||||||
|
|
||||||
|
bool supportsMetric(MetricType type) const override;
|
||||||
|
bool getMetric(MetricType type, MetricData& data) override;
|
||||||
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user