[P139] Fix ESP8266 build + show device state

This commit is contained in:
TD-er
2025-01-24 19:37:51 +01:00
parent 0a684b02b7
commit 65320c8dee
9 changed files with 177 additions and 97 deletions
+1
View File
@@ -259,6 +259,7 @@ uint16_t AXP2101::registerToVoltage(uint8_t data,
}
// Values in mVolt, raw ADC data is expressed in 0.5 mV
// LUT is based on 10k NTC with 50 uA current
constexpr uint16_t axp2101_TS_LUT[] {
3150, // -20
2508, // -15
-12
View File
@@ -108,18 +108,6 @@ const __FlashStringHelper* toString(AXP2101_chargingDetail_e charge) {
return F("");
}
const __FlashStringHelper* toString(AXP2101_CV_charger_voltage_e voltage) {
switch (voltage) {
case AXP2101_CV_charger_voltage_e::limit_4_00V: return F("4.0");
case AXP2101_CV_charger_voltage_e::limit_4_10V: return F("4.1");
case AXP2101_CV_charger_voltage_e::limit_4_20V: return F("4.2");
case AXP2101_CV_charger_voltage_e::limit_4_35V: return F("4.35");
case AXP2101_CV_charger_voltage_e::limit_4_40V: return F("4.4");
case AXP2101_CV_charger_voltage_e::reserved: break;
}
return F("");
}
AXP2101_registers_e AXP2101_intToRegister(int reg) {
switch (reg) {
case 0: return AXP2101_registers_e::dcdc1;
-1
View File
@@ -294,7 +294,6 @@ const __FlashStringHelper* toString(AXP2101_chargeled_d led);
const __FlashStringHelper* toString(AXP2101_chargingState_e state);
const __FlashStringHelper* toString(AXP2101_chipid_e chip);
const __FlashStringHelper* toString(AXP2101_chargingDetail_e charge);
const __FlashStringHelper* toString(AXP2101_CV_charger_voltage_e voltage);
class AXP2101_settings { // Voltages in mV, range 0..3700, max. depending on the AXP2101 pin/port used.
public:
@@ -2,11 +2,18 @@
#include "../WebServer/Markup.h"
FormSelectorOptions::FormSelectorOptions(int optionCount)
: _optionCount(optionCount)
{
classname = F("wide");
}
FormSelectorOptions::FormSelectorOptions(
int optionCount,
const int indices[],
const String attr[]) : _optionCount(optionCount)
{
classname = F("wide");
_indices = new int[optionCount];
if (attr != nullptr) {
@@ -28,6 +35,7 @@ FormSelectorOptions::FormSelectorOptions(
const int indices[],
const String attr[]) : _optionCount(optionCount)
{
classname = F("wide");
_names_str = new String[optionCount];
if (indices != nullptr) {
@@ -58,6 +66,7 @@ FormSelectorOptions::FormSelectorOptions(
const int indices[],
const String attr[]) : _optionCount(optionCount)
{
classname = F("wide");
_names_f = new const __FlashStringHelper *[optionCount];
if (indices != nullptr) {
+2 -2
View File
@@ -6,7 +6,7 @@
class FormSelectorOptions {
public:
FormSelectorOptions(int optionCount) : _optionCount(optionCount) {}
FormSelectorOptions(int optionCount);
FormSelectorOptions(int optionCount,
const int indices[],
@@ -49,7 +49,7 @@ public:
bool reloadonchange = false;
bool enabled = true;
const __FlashStringHelper * classname = F("wide");
const __FlashStringHelper * classname;
#if FEATURE_TOOLTIPS
String tooltip;
#endif // if FEATURE_TOOLTIPS
+70 -41
View File
@@ -4,7 +4,7 @@
# ifdef ESP32
#include "../PluginStructs/P139_data_struct_formselectors.h"
# include "../PluginStructs/P139_data_struct_formselectors.h"
// **************************************************************************/
// Constructors
@@ -47,8 +47,9 @@ String P139_data_struct::loadSettings(struct EventStruct *event) {
// applySettings: Write the current settings to AXP2101
// **************************************************************************/
void P139_data_struct::applySettings(struct EventStruct *event) {
if (!isInitialized())
if (!isInitialized()) {
return;
}
uint8_t count = 0;
for (int s = 0; s < AXP2101_settings_count; ++s) {
@@ -103,7 +104,7 @@ void P139_data_struct::applySettings(struct EventStruct *event) {
axp2101->setTS_disabled(_settings.getTS_disabled());
// axp2101->set_IRQ_enable_0(0b11110000); // Disable temperature checks
// axp2101->set_IRQ_enable_0(0b11110000); // Disable temperature checks
axp2101->setConstChargeCurrentLimit(_settings.getConstChargeCurrentLimit());
}
@@ -153,9 +154,9 @@ void P139_data_struct::webform_load(struct EventStruct *event) {
{
// Reg 63: Iterm Charger Settings and Control
// 0 .. 200 mA in 25 mA steps + enable checkbox
AXP2101_PreChargeCurrentLimit_FormSelector selector(_settings.getTerminationChargeCurrentLimit());
// TODO TD-er: Must add 'enabled' checkbox
AXP2101_TerminationChargeCurrentLimit_FormSelector selector(_settings.getTerminationChargeCurrentLimit());
addFormCheckBox(F("Enable CV Charging"), F("iterm_en"), _settings.getTerminationChargeCurrentLimitEnable());
addFormNote(F("When enabled, the last part of the charge cycle is done using constant voltage (CV)"));
}
{
@@ -178,7 +179,7 @@ void P139_data_struct::webform_load(struct EventStruct *event) {
}
addFormCheckBox(F("Disable TS pin"), F("dis_TS"), _settings.getTS_disabled());
addFormNote(F("Make sure to disable TS pin when no NTC is used, or else the battery will not charge"));
addFormNote(F("Make sure to disable TS pin when no battery temperature sensor is used, or else the battery will not charge"));
addFormCheckBox(F("Generate events"), F("events"), P139_GET_GENERATE_EVENTS);
@@ -275,13 +276,42 @@ void P139_data_struct::webform_load(struct EventStruct *event) {
static_cast<int>(pin));
}
}
html_end_table();
addFormNote(F("Check your device documentation for what is connected to each output."));
if (isInitialized()) {
addFormSubHeader(F("Current State"));
const AXP2101_registers_e registers[] = {
AXP2101_registers_e::vbat,
AXP2101_registers_e::vbus,
AXP2101_registers_e::vsys,
AXP2101_registers_e::battemp,
AXP2101_registers_e::chiptemp
};
for (size_t i = 0; i < NR_ELEMENTS(registers); ++i) {
addRowLabel(toString(registers[i], true));
if ((registers[i] == AXP2101_registers_e::battemp) ||
(registers[i] == AXP2101_registers_e::chiptemp))
{
addHtmlFloat(read_value(registers[i]));
addUnit(F("°C"));
} else {
addHtmlInt(static_cast<int>(read_value(registers[i])));
addUnit(F("mV"));
}
}
addRowLabel(F("Charging State"));
addHtml(toString(axp2101->getChargingDetail()));
}
}
}
void P139_data_struct::webform_save(struct EventStruct *event) {
for (uint8_t i = 0; i < P139_NR_OUTPUT_VALUES; ++i) {
sensorTypeHelper_saveOutputSelector(event, P139_CONFIG_BASE + i, i,
@@ -300,37 +330,36 @@ void P139_data_struct::webform_save(struct EventStruct *event) {
}
}
_settings.setChargeLed(static_cast<AXP2101_chargeled_d>(getFormItemInt(F("led"))));
_settings.setChargeLed(AXP2101_ChargeLED_FormSelector::get());
// Reg 61: Iprechg Charger Settings
_settings.setPreChargeCurrentLimit(getFormItemInt(F("iprechg")));
_settings.setPreChargeCurrentLimit(AXP2101_PreChargeCurrentLimit_FormSelector::get());
// Reg 62: ICC Charger Settings
_settings.setConstChargeCurrentLimit(getFormItemInt(F("iccchg")));
_settings.setConstChargeCurrentLimit(AXP2101_ConstChargeCurrentLimit_FormSelector::get());
// Reg 63: Iterm Charger Settings and Control
_settings.setTerminationChargeCurrentLimit(
getFormItemInt(F("iterm")),
AXP2101_TerminationChargeCurrentLimit_FormSelector::get(),
isFormItemChecked(F("iterm_en")));
// Reg 64: CV Charger Voltage Settings
_settings.setCV_chargeVoltage(static_cast<AXP2101_CV_charger_voltage_e>(getFormItemInt(F("cv_volt"))));
_settings.setCV_chargeVoltage(AXP2101_CV_charger_voltage_FormSelector::get());
// Reg 14: Minimum System Voltage Control
_settings.setLinear_Charger_Vsys_dpm(static_cast<AXP2101_Linear_Charger_Vsys_dpm_e>(getFormItemInt(F("min_vsys"))));
_settings.setLinear_Charger_Vsys_dpm(AXP2101_Linear_Charger_Vsys_dpm_FormSelector::get());
// Reg 15: Input Voltage Limit
_settings.setVin_DPM(static_cast<AXP2101_VINDPM_e>(getFormItemInt(F("vin_dpm"))));
_settings.setVin_DPM(AXP2101_Vin_DPM_FormSelector::get());
// Reg 16: Input Current Limit
_settings.setInputCurrentLimit(static_cast<AXP2101_InputCurrentLimit_e>(getFormItemInt(F("cur_lim_in"))));
_settings.setInputCurrentLimit(AXP2101_InputCurrentLimit_FormSelector::get());
_settings.setTS_disabled(isFormItemChecked(F("dis_TS")));
saveSettings(event);
}
// **************************************************************************/
// plugin_read: Read the values and send to controller(s)
// **************************************************************************/
@@ -353,32 +382,32 @@ float P139_data_struct::read_value(AXP2101_registers_e value) {
if (isInitialized()) {
switch (value)
{
case AXP2101_registers_e::chargeled:
return static_cast<float>(axp2101->getChargeLed());
case AXP2101_registers_e::batcharge:
return static_cast<float>(axp2101->getBatCharge());
case AXP2101_registers_e::charging:
return static_cast<float>(axp2101->getChargingState());
case AXP2101_registers_e::batpresent:
return static_cast<float>(axp2101->isBatteryDetected());
case AXP2101_registers_e::chipid:
return static_cast<float>(axp2101->getChipIDRaw());
case AXP2101_registers_e::chargedet:
return static_cast<float>(axp2101->getChargingDetail());
case AXP2101_registers_e::chargeled:
return static_cast<float>(axp2101->getChargeLed());
case AXP2101_registers_e::batcharge:
return static_cast<float>(axp2101->getBatCharge());
case AXP2101_registers_e::charging:
return static_cast<float>(axp2101->getChargingState());
case AXP2101_registers_e::batpresent:
return static_cast<float>(axp2101->isBatteryDetected());
case AXP2101_registers_e::chipid:
return static_cast<float>(axp2101->getChipIDRaw());
case AXP2101_registers_e::chargedet:
return static_cast<float>(axp2101->getChargingDetail());
case AXP2101_registers_e::vbat:
case AXP2101_registers_e::vbus:
case AXP2101_registers_e::vsys:
return static_cast<float>(axp2101->getADCVoltage(value));
case AXP2101_registers_e::vbat:
case AXP2101_registers_e::vbus:
case AXP2101_registers_e::vsys:
return static_cast<float>(axp2101->getADCVoltage(value));
case AXP2101_registers_e::battemp:
return axp2101->TS_registerToTemp(axp2101->getADCVoltage(value));
case AXP2101_registers_e::battemp:
return axp2101->TS_registerToTemp(axp2101->getADCVoltage(value));
case AXP2101_registers_e::chiptemp:
return (22.0f + (7274 - axp2101->getADCVoltage(value)) / 20.0f);
case AXP2101_registers_e::chiptemp:
return 22.0f + (7274 - axp2101->getADCVoltage(value)) / 20.0f;
default:
return static_cast<float>(axp2101->getPortVoltage(value));
default:
return static_cast<float>(axp2101->getPortVoltage(value));
}
}
return 0.0f;
@@ -634,8 +663,8 @@ bool P139_data_struct::plugin_get_config_value(struct EventStruct *event,
const AXP2101_registers_e reg = AXP2101_intToRegister(r);
if (equals(command, toString(reg, false))) { // Voltage (mV) / numeric state
if (reg == AXP2101_registers_e::battemp ||
reg == AXP2101_registers_e::chiptemp)
if ((reg == AXP2101_registers_e::battemp) ||
(reg == AXP2101_registers_e::chiptemp))
{
string = floatToString(read_value(reg), 2);
} else {
-1
View File
@@ -35,7 +35,6 @@
# define P139_CONST_MAX_LDO 3700 // Max. output voltage
struct P139_data_struct : public PluginTaskData_base {
public:
@@ -15,6 +15,11 @@ AXP2101_ChargeLED_FormSelector::AXP2101_ChargeLED_FormSelector(
static_cast<int>(selected));
}
AXP2101_chargeled_d AXP2101_ChargeLED_FormSelector::get()
{
return static_cast<AXP2101_chargeled_d>(getFormItemInt(F("led")));
}
AXP2101_chargeled_d get_AXP2101_chargeled_d(int index) {
if (index < 0) { return AXP2101_chargeled_d::Off; }
@@ -42,9 +47,15 @@ AXP2101_PreChargeCurrentLimit_FormSelector::AXP2101_PreChargeCurrentLimit_FormSe
int selected) : FormSelectorOptions(9)
{
addFormSelector(
F("Term Charge Current"), F("iterm"),
F("Pre Charge Current"), F("iprechg"),
static_cast<int>(selected));
addUnit(F("mA"));
addFormNote(F("When V_bat < 3V, the battery will be charged using pre-charge current"));
}
int AXP2101_PreChargeCurrentLimit_FormSelector::get()
{
return getFormItemInt(F("iprechg"));
}
int get_AXP2101_0_to_200mA_ChargeCurrentLimit(int index) {
@@ -79,6 +90,11 @@ AXP2101_ConstChargeCurrentLimit_FormSelector::AXP2101_ConstChargeCurrentLimit_Fo
addUnit(F("mA"));
}
int AXP2101_ConstChargeCurrentLimit_FormSelector::get()
{
return getFormItemInt(F("iccchg"));
}
int get_AXP2101_ConstChargeCurrentLimit(int index) {
if (index < 0) { return 0; }
const int res = index <= 8
@@ -107,9 +123,15 @@ AXP2101_TerminationChargeCurrentLimit_FormSelector::AXP2101_TerminationChargeCur
int selected) : FormSelectorOptions(9)
{
addFormSelector(
F("Pre Charge Current"), F("iprechg"),
F("Termination Charge Current"), F("iterm"),
static_cast<int>(selected));
addUnit(F("mA"));
addFormNote(F("Charge current threshold to switch to CV charging or stop charging, when battery is nearly full"));
}
int AXP2101_TerminationChargeCurrentLimit_FormSelector::get()
{
return getFormItemInt(F("iterm"));
}
String AXP2101_TerminationChargeCurrentLimit_FormSelector::getOptionString(int index) const
@@ -133,13 +155,18 @@ AXP2101_CV_charger_voltage_FormSelector::AXP2101_CV_charger_voltage_FormSelector
F("CV Charger Voltage"), F("cv_volt"),
static_cast<int>(selected));
addUnit(F("V"));
addFormNote(F("Target charge voltage of battery"));
}
AXP2101_CV_charger_voltage_e AXP2101_CV_charger_voltage_FormSelector::get()
{
return static_cast<AXP2101_CV_charger_voltage_e>(getFormItemInt(F("cv_volt")));
}
AXP2101_CV_charger_voltage_e get_AXP2101_CV_charger_voltage_e(int index) {
if (index < 0) { return AXP2101_CV_charger_voltage_e::reserved; }
constexpr int offset = static_cast<int>(AXP2101_CV_charger_voltage_e::limit_4_00V);
constexpr int max = static_cast<int>(AXP2101_CV_charger_voltage_e::MAX);
index += offset;
constexpr int max = static_cast<int>(AXP2101_CV_charger_voltage_e::MAX);
index += 1;
if (index >= max) { return AXP2101_CV_charger_voltage_e::reserved; }
return static_cast<AXP2101_CV_charger_voltage_e>(index);
@@ -147,7 +174,13 @@ AXP2101_CV_charger_voltage_e get_AXP2101_CV_charger_voltage_e(int index) {
String AXP2101_CV_charger_voltage_FormSelector::getOptionString(int index) const
{
return toString(get_AXP2101_CV_charger_voltage_e(index));
const AXP2101_CV_charger_voltage_e val = get_AXP2101_CV_charger_voltage_e(index);
int decimal = index;
if (AXP2101_CV_charger_voltage_e::limit_4_35V == val) {
index = 35;
}
return concat(F("4."), index);
}
int AXP2101_CV_charger_voltage_FormSelector::getIndexValue(int index) const
@@ -166,6 +199,12 @@ AXP2101_Linear_Charger_Vsys_dpm_FormSelector::AXP2101_Linear_Charger_Vsys_dpm_Fo
F("Minimum System Voltage"), F("min_vsys"),
static_cast<int>(selected));
addUnit(F("V"));
addFormNote(F("Minimum system voltage to allow charging the battery"));
}
AXP2101_Linear_Charger_Vsys_dpm_e AXP2101_Linear_Charger_Vsys_dpm_FormSelector::get()
{
return static_cast<AXP2101_Linear_Charger_Vsys_dpm_e>(getFormItemInt(F("min_vsys")));
}
String AXP2101_Linear_Charger_Vsys_dpm_FormSelector::getOptionString(int index) const
@@ -186,9 +225,15 @@ AXP2101_Vin_DPM_FormSelector::AXP2101_Vin_DPM_FormSelector(
FormSelectorOptions(static_cast<int>(AXP2101_VINDPM_e::MAX))
{
addFormSelector(
F("Min Vin DPM Voltage"), F("vin_dpm"),
F("Min Vin_dpm Voltage"), F("vin_dpm"),
static_cast<int>(selected));
addUnit(F("V"));
addFormNote(F("When Vbus reaches Vin_dpm, the charge current will decrease until zero"));
}
AXP2101_VINDPM_e AXP2101_Vin_DPM_FormSelector::get()
{
return static_cast<AXP2101_VINDPM_e>(getFormItemInt(F("vin_dpm")));
}
String AXP2101_Vin_DPM_FormSelector::getOptionString(int index) const
@@ -212,6 +257,13 @@ AXP2101_InputCurrentLimit_FormSelector::AXP2101_InputCurrentLimit_FormSelector(
F("Input Current Limit"), F("cur_lim_in"),
static_cast<int>(selected));
addUnit(F("mA"));
// If I_sys is over the input power supply capability, V_sys will drop.
// If V_bat is above V_sys, PMU will enter supplement mode.
}
AXP2101_InputCurrentLimit_e AXP2101_InputCurrentLimit_FormSelector::get()
{
return static_cast<AXP2101_InputCurrentLimit_e>(getFormItemInt(F("cur_lim_in")));
}
String AXP2101_InputCurrentLimit_FormSelector::getOptionString(int index) const
@@ -4,20 +4,21 @@
#ifdef USES_P139
#include <AXP2101_settings.h>
# include <AXP2101_settings.h>
// **********************************************************************
// Charge LED settings
// **********************************************************************
class AXP2101_ChargeLED_FormSelector : public FormSelectorOptions
{
class AXP2101_ChargeLED_FormSelector : public FormSelectorOptions {
public:
AXP2101_ChargeLED_FormSelector(AXP2101_chargeled_d selected);
virtual ~AXP2101_ChargeLED_FormSelector() {}
virtual String getOptionString(int index) const override;
virtual int getIndexValue(int index) const override;
static AXP2101_chargeled_d get();
virtual String getOptionString(int index) const override;
virtual int getIndexValue(int index) const override;
};
@@ -25,15 +26,16 @@ public:
// Reg 61: Iprechg Charger Settings
// 0 .. 200 mA in 25 mA steps
// **********************************************************************
class AXP2101_PreChargeCurrentLimit_FormSelector : public FormSelectorOptions
{
class AXP2101_PreChargeCurrentLimit_FormSelector : public FormSelectorOptions {
public:
AXP2101_PreChargeCurrentLimit_FormSelector(int selected);
virtual ~AXP2101_PreChargeCurrentLimit_FormSelector() {}
static int get();
virtual String getOptionString(int index) const override;
virtual int getIndexValue(int index) const override;
};
@@ -42,94 +44,95 @@ public:
// 0 .. 200 mA in 25 mA steps
// 200 ... 1000 mA in 100 mA steps
// **********************************************************************
class AXP2101_ConstChargeCurrentLimit_FormSelector : public FormSelectorOptions
{
class AXP2101_ConstChargeCurrentLimit_FormSelector : public FormSelectorOptions {
public:
AXP2101_ConstChargeCurrentLimit_FormSelector(int selected);
virtual ~AXP2101_ConstChargeCurrentLimit_FormSelector() {}
static int get();
virtual String getOptionString(int index) const override;
virtual int getIndexValue(int index) const override;
};
// **********************************************************************
// Reg 63: Iterm Charger Settings and Control
// 0 .. 200 mA in 25 mA steps + enable checkbox
// **********************************************************************
class AXP2101_TerminationChargeCurrentLimit_FormSelector : public FormSelectorOptions
{
class AXP2101_TerminationChargeCurrentLimit_FormSelector : public FormSelectorOptions {
public:
AXP2101_TerminationChargeCurrentLimit_FormSelector(int selected);
virtual ~AXP2101_TerminationChargeCurrentLimit_FormSelector() {}
static int get();
virtual String getOptionString(int index) const override;
virtual int getIndexValue(int index) const override;
};
// **********************************************************************
// Reg 64: CV Charger Voltage Settings
// **********************************************************************
class AXP2101_CV_charger_voltage_FormSelector : public FormSelectorOptions
{
class AXP2101_CV_charger_voltage_FormSelector : public FormSelectorOptions {
public:
AXP2101_CV_charger_voltage_FormSelector(AXP2101_CV_charger_voltage_e selected);
virtual ~AXP2101_CV_charger_voltage_FormSelector() {}
virtual String getOptionString(int index) const override;
virtual int getIndexValue(int index) const override;
static AXP2101_CV_charger_voltage_e get();
virtual String getOptionString(int index) const override;
virtual int getIndexValue(int index) const override;
};
// **********************************************************************
// Reg 14: Minimum System Voltage Control
// **********************************************************************
class AXP2101_Linear_Charger_Vsys_dpm_FormSelector : public FormSelectorOptions
{
class AXP2101_Linear_Charger_Vsys_dpm_FormSelector : public FormSelectorOptions {
public:
AXP2101_Linear_Charger_Vsys_dpm_FormSelector(AXP2101_Linear_Charger_Vsys_dpm_e selected);
virtual ~AXP2101_Linear_Charger_Vsys_dpm_FormSelector() {}
virtual String getOptionString(int index) const override;
static AXP2101_Linear_Charger_Vsys_dpm_e get();
virtual String getOptionString(int index) const override;
};
// **********************************************************************
// Reg 15: Input Voltage Limit
// **********************************************************************
class AXP2101_Vin_DPM_FormSelector : public FormSelectorOptions
{
class AXP2101_Vin_DPM_FormSelector : public FormSelectorOptions {
public:
AXP2101_Vin_DPM_FormSelector(AXP2101_VINDPM_e selected);
virtual ~AXP2101_Vin_DPM_FormSelector() {}
virtual String getOptionString(int index) const override;
static AXP2101_VINDPM_e get();
virtual String getOptionString(int index) const override;
};
// **********************************************************************
// Reg 16: Input Current Limit
// **********************************************************************
class AXP2101_InputCurrentLimit_FormSelector : public FormSelectorOptions
{
class AXP2101_InputCurrentLimit_FormSelector : public FormSelectorOptions {
public:
AXP2101_InputCurrentLimit_FormSelector(AXP2101_InputCurrentLimit_e selected);
virtual ~AXP2101_InputCurrentLimit_FormSelector() {}
virtual String getOptionString(int index) const override;
static AXP2101_InputCurrentLimit_e get();
virtual String getOptionString(int index) const override;
};
#endif
#endif // ifdef USES_P139