8static const char *
const TAG =
"ina226";
28static const uint8_t INA226_REGISTER_CONFIG = 0x00;
29static const uint8_t INA226_REGISTER_SHUNT_VOLTAGE = 0x01;
30static const uint8_t INA226_REGISTER_BUS_VOLTAGE = 0x02;
31static const uint8_t INA226_REGISTER_POWER = 0x03;
32static const uint8_t INA226_REGISTER_CURRENT = 0x04;
33static const uint8_t INA226_REGISTER_CALIBRATION = 0x05;
35static const uint16_t INA226_ADC_TIMES[] = {140, 204, 332, 588, 1100, 2116, 4156, 8244};
36static const uint16_t INA226_ADC_AVG_SAMPLES[] = {1, 4, 16, 64, 128, 256, 512, 1024};
75 ESP_LOGV(TAG,
" Using LSB=%" PRIu32
" calibration=%" PRIu32, lsb, calibration);
77 if (!this->
write_byte_16(INA226_REGISTER_CALIBRATION, calibration)) {
84 ESP_LOGCONFIG(TAG,
"INA226:");
88 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
91 LOG_UPDATE_INTERVAL(
this);
94 " ADC Conversion Time Bus Voltage: %d\n"
95 " ADC Conversion Time Shunt Voltage: %d\n"
96 " ADC Averaging Samples: %d",
108 uint16_t raw_bus_voltage;
109 if (!this->
read_byte_16(INA226_REGISTER_BUS_VOLTAGE, &raw_bus_voltage)) {
115 bus_voltage_v *= 0.00125f;
120 uint16_t raw_shunt_voltage;
121 if (!this->
read_byte_16(INA226_REGISTER_SHUNT_VOLTAGE, &raw_shunt_voltage)) {
127 shunt_voltage_v *= 0.0000025f;
132 uint16_t raw_current;
133 if (!this->
read_byte_16(INA226_REGISTER_CURRENT, &raw_current)) {
145 if (!this->
read_byte_16(INA226_REGISTER_POWER, &raw_power)) {
149 float power_mw = int16_t(raw_power) * (this->
calibration_lsb_ * 25.0f / 1000.0f);
void mark_failed()
Mark this component as failed.
void status_set_warning()
void status_clear_warning()
bool read_byte_16(uint8_t a_register, uint16_t *data)
bool write_byte_16(uint8_t a_register, uint16_t data) const
AdcTime adc_time_voltage_
AdcTime adc_time_current_
sensor::Sensor * power_sensor_
sensor::Sensor * bus_voltage_sensor_
void dump_config() override
float shunt_resistance_ohm_
sensor::Sensor * current_sensor_
sensor::Sensor * shunt_voltage_sensor_
AdcAvgSamples adc_avg_samples_
uint32_t calibration_lsb_
int32_t twos_complement_(int32_t val, uint8_t bits)
void publish_state(float state)
Publish a new state to the front-end.
void HOT delay(uint32_t ms)
AdcAvgSamples avg_samples
AdcTime shunt_voltage_conversion_time
AdcTime bus_voltage_conversion_time