ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
max17043.cpp
Go to the documentation of this file.
1#include "max17043.h"
2#include "esphome/core/log.h"
3
5
6// MAX17043 is a 1-Cell Fuel Gauge with ModelGauge and Low-Battery Alert
7// Consult the datasheet at https://www.analog.com/en/products/max17043.html
8
9ESPHOME_LOG_TAG(TAG, "max17043");
10
12 if (!this->ready_for_update_())
13 return;
14
15 uint16_t raw;
16 bool ok = true;
17
18 if (this->voltage_sensor_ != nullptr) {
19 if (this->read_byte_16(max170xx::REG_VCELL, &raw)) {
20 // 12 bits in the top of the word at 1.25 mV
21 this->voltage_sensor_->publish_state((raw >> 4) * 1.25e-3f);
22 } else {
23 ok = false;
24 }
25 }
26 if (this->battery_remaining_sensor_ != nullptr) {
27 if (this->read_byte_16(max170xx::REG_SOC, &raw)) {
28 float percent = (float) ((raw >> 8) + 0.003906f * (raw & 0x00ff));
30 } else {
31 ok = false;
32 }
33 }
34
35 this->status_comm_result(ok, LOG_STR("Register read failed"));
36}
37
39 ESP_LOGCONFIG(TAG, "MAX17043:");
40 LOG_I2C_DEVICE(this);
41 LOG_UPDATE_INTERVAL(this);
42 LOG_SENSOR(" ", "Battery Voltage", this->voltage_sensor_);
43 LOG_SENSOR(" ", "Battery Level", this->battery_remaining_sensor_);
44}
45
46} // namespace esphome::max17043
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.
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition i2c.h:281
sensor::Sensor * battery_remaining_sensor_
Definition max17043.h:20
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
ESPHOME_LOG_TAG(TAG, "max17043")