ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
max9611.cpp
Go to the documentation of this file.
1#include "max9611.h"
3#include "esphome/core/log.h"
5
6namespace esphome::max9611 {
7using namespace esphome::i2c;
8// Map the gain register to in uV/LSB
10 float lsb = 0.0;
12 lsb = 107.50;
13 } else if (gain == MAX9611_MULTIPLEXER_CSA_GAIN4) {
14 lsb = 26.88;
15 } else if (gain == MAX9611_MULTIPLEXER_CSA_GAIN8) {
16 lsb = 13.44;
17 }
18 return lsb;
19}
20ESPHOME_LOG_TAG(TAG, "max9611");
21static const uint8_t SETUP_DELAY = 4; // Wait 2 integration periods.
22static const float VOUT_LSB = 14.0 / 1000.0; // 14mV/LSB
23static const float TEMP_LSB = 0.48; // 0.48C/LSB
24static const float MICRO_VOLTS_PER_VOLT = 1000000.0;
25
27 // Perform dummy-read
28 uint8_t value;
29 this->read(&value, 1);
30 // Configuration Stage.
31 // First send an integration request with the specified gain
32 const uint8_t setup_dat[] = {CONTROL_REGISTER_1_ADRR, static_cast<uint8_t>(gain_)};
33 // Then send a request that samples all channels as fast as possible, using the last provided gain
35
36 if (this->write(reinterpret_cast<const uint8_t *>(&setup_dat), sizeof(setup_dat)) != ErrorCode::ERROR_OK) {
37 ESP_LOGE(TAG, "GAIN SET failed");
38 return;
39 }
40 delay(SETUP_DELAY);
41 if (this->write(reinterpret_cast<const uint8_t *>(&fast_mode_dat), sizeof(fast_mode_dat)) != ErrorCode::ERROR_OK) {
42 ESP_LOGE(TAG, "FAST MODE SET failed");
43 return;
44 }
45}
47 ESP_LOGCONFIG(TAG,
48 "MAX9611:\n"
49 " CSA Gain Register: %x",
50 gain_);
51 LOG_I2C_DEVICE(this);
52}
54 // Setup read from 0x0 register base
55 const uint8_t reg_base = 0x0;
56 const ErrorCode write_result = this->write(&reg_base, 1);
57 // Just read the entire register map in a bulk read, faster than individually querying register.
58 const ErrorCode read_result = this->read(register_map_, sizeof(register_map_));
59 if (write_result != ErrorCode::ERROR_OK || read_result != ErrorCode::ERROR_OK) {
60 ESP_LOGW(TAG, "Update failed");
61 return;
62 }
63 uint16_t csa_register = ((register_map_[CSA_DATA_BYTE_MSB_ADRR] << 8) | (register_map_[CSA_DATA_BYTE_LSB_ADRR])) >> 4;
64 uint16_t rs_register = ((register_map_[RS_DATA_BYTE_MSB_ADRR] << 8) | (register_map_[RS_DATA_BYTE_LSB_ADRR])) >> 4;
65 uint16_t t_register = ((register_map_[TEMP_DATA_BYTE_MSB_ADRR] << 8) | (register_map_[TEMP_DATA_BYTE_LSB_ADRR])) >> 7;
66 float voltage = rs_register * VOUT_LSB;
67 float shunt_voltage = (csa_register * gain_to_lsb(gain_)) / MICRO_VOLTS_PER_VOLT;
68 float temp = sign_extend<9>(t_register) * TEMP_LSB;
69 float amps = shunt_voltage / current_resistor_;
70 float watts = amps * voltage;
71
72 if (voltage_sensor_ != nullptr) {
74 }
75 if (current_sensor_ != nullptr) {
77 }
78 if (watt_sensor_ != nullptr) {
80 }
81 if (temperature_sensor_ != nullptr) {
83 }
84
85 ESP_LOGD(TAG, "V: %f, A: %f, W: %f, Deg C: %f", voltage, amps, watts, temp);
86}
87} // namespace esphome::max9611
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:195
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:175
sensor::Sensor * current_sensor_
Definition max9611.h:51
sensor::Sensor * temperature_sensor_
Definition max9611.h:53
sensor::Sensor * watt_sensor_
Definition max9611.h:52
sensor::Sensor * voltage_sensor_
Definition max9611.h:50
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
AlsGain501 gain
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:13
ESPHOME_LOG_TAG(TAG, "i2c")
float gain_to_lsb(MAX9611Multiplexer gain)
Definition max9611.cpp:9
@ TEMP_DATA_BYTE_LSB_ADRR
Definition max9611.h:31
@ CONTROL_REGISTER_1_ADRR
Definition max9611.h:32
@ RS_DATA_BYTE_MSB_ADRR
Definition max9611.h:24
@ CSA_DATA_BYTE_MSB_ADRR
Definition max9611.h:22
@ RS_DATA_BYTE_LSB_ADRR
Definition max9611.h:25
@ CSA_DATA_BYTE_LSB_ADRR
Definition max9611.h:23
@ TEMP_DATA_BYTE_MSB_ADRR
Definition max9611.h:30
@ MAX9611_MULTIPLEXER_CSA_GAIN4
Definition max9611.h:12
@ MAX9611_MULTIPLEXER_CSA_GAIN1
Definition max9611.h:11
@ MAX9611_MULTIPLEXER_FAST_MODE
Definition max9611.h:18
@ MAX9611_MULTIPLEXER_CSA_GAIN8
Definition max9611.h:13
constexpr int32_t sign_extend(uint32_t value)
Sign-extend the low BITS bits of value, e.g. sign_extend<24>(0xFFFFFE) == -2. Higher bits are ignored...
Definition helpers.h:1012
void HOT delay(uint32_t ms)
Definition hal.cpp:85