Refactor MAX17043 driver

This commit is contained in:
Theo Arends 2023-07-24 12:29:43 +02:00
parent c5be85f443
commit f626539c18
2 changed files with 36 additions and 61 deletions

View File

@ -711,9 +711,8 @@
// #define TC74_MAX_SENSORS 8 // Support non-default/multiple I2C addresses // #define TC74_MAX_SENSORS 8 // Support non-default/multiple I2C addresses
// #define TC74_I2C_PROBE_ADDRESSES { 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F } // Addresses to probe/support // #define TC74_I2C_PROBE_ADDRESSES { 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F } // Addresses to probe/support
// #define TC74_MAX_FAILCOUNT 8 // Maximum failed polls before it's marked inactive until reprobing later // #define TC74_MAX_FAILCOUNT 8 // Maximum failed polls before it's marked inactive until reprobing later
// #define USE_MAX17043 // [I2cDriver110] Enable MAX17043 sensor (I2C address 0x36) (+0k9 code)
// #define USE_PCA9557 // [I2cDriver81] Enable PCA9557 8-bit I/O Expander (I2C addresses 0x18 - 0x1F) (+2k5 code) // #define USE_PCA9557 // [I2cDriver81] Enable PCA9557 8-bit I/O Expander (I2C addresses 0x18 - 0x1F) (+2k5 code)
// #define USE_MAX17043 // [I2cDriver83] Enable MAX17043 fuel-gauge systems Lipo batteries sensor (I2C address 0x36) (+0k9 code)
// #define USE_RTC_CHIPS // Enable RTC chip support and NTP server - Select only one // #define USE_RTC_CHIPS // Enable RTC chip support and NTP server - Select only one
// #define USE_DS3231 // [I2cDriver26] Enable DS3231 RTC (I2C address 0x68) (+1k2 code) // #define USE_DS3231 // [I2cDriver26] Enable DS3231 RTC (I2C address 0x68) (+1k2 code)

View File

@ -19,13 +19,6 @@
#ifdef USE_I2C #ifdef USE_I2C
#ifdef USE_MAX17043 #ifdef USE_MAX17043
#define XI2C_83 83 // See I2CDEVICES.md
#define SENSOR_NAME "MAX17043"
#include "DFRobot_MAX17043.h"
/*********************************************************************************************\ /*********************************************************************************************\
* MAX17043 fuel-gauge for 3.7 Volt Lipo batteries * MAX17043 fuel-gauge for 3.7 Volt Lipo batteries
* *
@ -34,7 +27,7 @@
* The alert flag and alert threshold are not required for MQTT, the alert pin is not used * The alert flag and alert threshold are not required for MQTT, the alert pin is not used
* by this sensor driver. * by this sensor driver.
* *
* Wirering and other information: * Wiring and other information:
* *
* \lib\lib_i2c\DFRobot_MAX17043\resources * \lib\lib_i2c\DFRobot_MAX17043\resources
* *
@ -46,83 +39,66 @@
* *
* https://www.aliexpress.us/item/2251832479401925.html * https://www.aliexpress.us/item/2251832479401925.html
* *
\*********************************************************************************************/ \*********************************************************************************************/
#define XSNS_110 110 #define XSNS_110 110
#define XI2C_83 83 // See I2CDEVICES.md
const char *mqttId = "MAX17043"; #include "DFRobot_MAX17043.h"
DFRobot_MAX17043 gauge; // Class to read from MAX17043 DFRobot_MAX17043 max17043_gauge; // Class to read from MAX17043
bool max17043 = false;
struct MAX17043 /*********************************************************************************************/
{
float voltage = 0.0; // Battery voltage in Volt
float percentage = 0.0; // Battery remaining charge in percent
} *max17043 = nullptr;
/*********************************************************************************************/
void Max17043Init(void) { void Max17043Init(void) {
if (I2cSetDevice(MAX17043_ADDRESS)) { if (I2cSetDevice(MAX17043_ADDRESS)) {
I2cSetActiveFound(MAX17043_ADDRESS, "MAX17043"); if (max17043_gauge.begin() == 0) {
if (gauge.begin() == 0) { max17043 = true;
max17043 = (MAX17043 *)calloc(1, sizeof(struct MAX17043)); I2cSetActiveFound(MAX17043_ADDRESS, "MAX17043");
} }
} }
} }
void Max17043Read(void) { void Max17043Show(bool json) {
float voltage = max17043_gauge.readVoltage() / 1000.0; // Battery voltage in Volt
float percentage = 0.0; // During charging the percentage might be (slightly) above 100%. To avoid strange numbers
// in the statistics the percentage provided by this driver will not go above 100%
max17043->voltage = gauge.readVoltage()/1000.0; float percentage = max17043_gauge.readPercentage(); // Battery remaining charge in percent
if (percentage > 100.0) { percentage = 100.0; }
// During charging the percentage might be (slightly) above 100%. To avoid stange numbers
// in the statistics we the percentage provided by this driver will not go above 100%
percentage = gauge.readPercentage();
if (percentage > 100.0) {
max17043->percentage = 100.0;
}
else {
max17043->percentage = percentage;
}
}
void Max17043Json(void) {
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_VOLTAGE "\":%3_f,\"" D_JSON_BATTPERCENT "\":%2_f}"), mqttId, &max17043->voltage, &max17043->percentage );
}
if (json) {
ResponseAppend_P(PSTR(",\"MAX17043\":{\"" D_JSON_VOLTAGE "\":%3_f,\"" D_JSON_BATTPERCENT "\":%2_f}"), &voltage, &percentage );
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
void Max17043Show(void) { } else {
WSContentSend_PD(PSTR("{s}%s " D_VOLTAGE "{m}%1_f" D_UNIT_VOLT "{e}"), SENSOR_NAME, &max17043->voltage); // WSContentSend_Voltage("MAX17043", voltage);
WSContentSend_PD(PSTR("{s}%s " D_BATTERY_CHARGE "{m}%1_f %% {e}"), SENSOR_NAME, &max17043->percentage); WSContentSend_PD(PSTR("{s}MAX17043 " D_VOLTAGE "{m}%1_f" D_UNIT_VOLT "{e}"), &voltage);
} WSContentSend_PD(PSTR("{s}MAX17043 " D_BATTERY_CHARGE "{m}%1_f %% {e}"), &percentage);
#endif // USE_WEBSERVER #endif
}
}
/*********************************************************************************************\ /*********************************************************************************************\
* Interface * Interface
\*********************************************************************************************/ \*********************************************************************************************/
bool Xsns110(uint32_t function) { bool Xsns110(uint32_t function) {
if (!I2cEnabled(MAX17043_ADDRESS)) { return false; } if (!I2cEnabled(MAX17043_ADDRESS)) { return false; }
if (FUNC_INIT == function) { if (FUNC_INIT == function) {
Max17043Init(); Max17043Init();
} }
else if (max17043 != nullptr) { else if (max17043) {
switch (function) { switch (function) {
case FUNC_EVERY_SECOND: case FUNC_JSON_APPEND:
Max17043Read(); Max17043Show(1);
break;
case FUNC_JSON_APPEND:
Max17043Json();
break;
#ifdef USE_WEBSERVER
case FUNC_WEB_SENSOR:
Max17043Show();
break; break;
#endif // USE_WEBSERVER #ifdef USE_WEBSERVER
case FUNC_WEB_SENSOR:
Max17043Show(0);
break;
#endif // USE_WEBSERVER
} }
} }
return false; return false;