ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
max17048.cpp
Go to the documentation of this file.
1#include "max17048.h"
2#include "esphome/core/log.h"
3
5
6// MAX17048/MAX17049 1 and 2 cell fuel gauges with ModelGauge
7// Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/MAX17048-MAX17049.pdf
8
9ESPHOME_LOG_TAG(TAG, "max17048");
10
11static constexpr uint8_t REG_CRATE = 0x16; // charge rate, signed
12
13static constexpr float VCELL_VOLTS_PER_LSB = 78.125e-6f;
14static constexpr float SOC_PERCENT_PER_LSB = 1.0f / 256.0f;
15static constexpr float CRATE_PERCENT_PER_HOUR_PER_LSB = 0.208f;
16
17void MAX17048Component::update() {
18 if (!this->ready_for_update_())
19 return;
20
21 bool ok = true;
22 uint16_t raw;
23 if (this->battery_voltage_sensor_ != nullptr) {
24 if (this->read_byte_16(max170xx::REG_VCELL, &raw)) {
25 this->battery_voltage_sensor_->publish_state(raw * VCELL_VOLTS_PER_LSB);
26 } else {
27 ok = false;
28 }
29 }
30 if (this->battery_level_sensor_ != nullptr) {
31 if (this->read_byte_16(max170xx::REG_SOC, &raw)) {
32 this->battery_level_sensor_->publish_state(raw * SOC_PERCENT_PER_LSB);
33 } else {
34 ok = false;
35 }
36 }
37 if (this->rate_sensor_ != nullptr) {
38 if (this->read_byte_16(REG_CRATE, &raw)) {
39 this->rate_sensor_->publish_state(static_cast<int16_t>(raw) * CRATE_PERCENT_PER_HOUR_PER_LSB);
40 } else {
41 ok = false;
42 }
43 }
44 this->status_comm_result(ok, LOG_STR("Register read failed"));
45}
46
48 ESP_LOGCONFIG(TAG, "MAX17048:");
49 LOG_I2C_DEVICE(this);
50 LOG_UPDATE_INTERVAL(this);
51 LOG_SENSOR(" ", "Battery Voltage", this->battery_voltage_sensor_);
52 LOG_SENSOR(" ", "Battery Level", this->battery_level_sensor_);
53 LOG_SENSOR(" ", "Rate", this->rate_sensor_);
54}
55
56} // namespace esphome::max17048
uint8_t raw[35]
Definition bl0939.h:0
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
virtual void dump_config()
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition i2c.h:281
ESPHOME_LOG_TAG(TAG, "max17048")