add possibility to provide different conversion times for Bus Voltage… (#6327)

Co-authored-by: Kevin Hübner <k.huebner@ceyoniq.com>
This commit is contained in:
kev300 2024-03-13 04:25:38 +01:00 committed by GitHub
parent 64a47f840e
commit de43678525
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 27 additions and 7 deletions

View file

@ -55,10 +55,10 @@ void INA226Component::setup() {
config.avg_samples = this->adc_avg_samples_;
// Bus Voltage Conversion Time VBUSCT Bit Settings [8:6] (100 -> 1.1ms, 111 -> 8.244 ms)
config.bus_voltage_conversion_time = this->adc_time_;
config.bus_voltage_conversion_time = this->adc_time_voltage_;
// Shunt Voltage Conversion Time VSHCT Bit Settings [5:3] (100 -> 1.1ms, 111 -> 8.244 ms)
config.shunt_voltage_conversion_time = this->adc_time_;
config.shunt_voltage_conversion_time = this->adc_time_current_;
// Mode Settings [2:0] Combinations (111 -> Shunt and Bus, Continuous)
config.mode = 0b111;
@ -93,7 +93,8 @@ void INA226Component::dump_config() {
}
LOG_UPDATE_INTERVAL(this);
ESP_LOGCONFIG(TAG, " ADC Conversion Time: %d", INA226_ADC_TIMES[this->adc_time_ & 0b111]);
ESP_LOGCONFIG(TAG, " ADC Conversion Time Bus Voltage: %d", INA226_ADC_TIMES[this->adc_time_voltage_ & 0b111]);
ESP_LOGCONFIG(TAG, " ADC Conversion Time Shunt Voltage: %d", INA226_ADC_TIMES[this->adc_time_current_ & 0b111]);
ESP_LOGCONFIG(TAG, " ADC Averaging Samples: %d", INA226_ADC_AVG_SAMPLES[this->adc_avg_samples_ & 0b111]);
LOG_SENSOR(" ", "Bus Voltage", this->bus_voltage_sensor_);

View file

@ -50,7 +50,8 @@ class INA226Component : public PollingComponent, public i2c::I2CDevice {
void set_shunt_resistance_ohm(float shunt_resistance_ohm) { shunt_resistance_ohm_ = shunt_resistance_ohm; }
void set_max_current_a(float max_current_a) { max_current_a_ = max_current_a; }
void set_adc_time(AdcTime time) { adc_time_ = time; }
void set_adc_time_voltage(AdcTime time) { adc_time_voltage_ = time; }
void set_adc_time_current(AdcTime time) { adc_time_current_ = time; }
void set_adc_avg_samples(AdcAvgSamples samples) { adc_avg_samples_ = samples; }
void set_bus_voltage_sensor(sensor::Sensor *bus_voltage_sensor) { bus_voltage_sensor_ = bus_voltage_sensor; }
@ -61,7 +62,8 @@ class INA226Component : public PollingComponent, public i2c::I2CDevice {
protected:
float shunt_resistance_ohm_;
float max_current_a_;
AdcTime adc_time_{AdcTime::ADC_TIME_1100US};
AdcTime adc_time_voltage_{AdcTime::ADC_TIME_1100US};
AdcTime adc_time_current_{AdcTime::ADC_TIME_1100US};
AdcAvgSamples adc_avg_samples_{AdcAvgSamples::ADC_AVG_SAMPLES_4};
uint32_t calibration_lsb_;
sensor::Sensor *bus_voltage_sensor_{nullptr};

View file

@ -16,6 +16,7 @@ from esphome.const import (
UNIT_VOLT,
UNIT_AMPERE,
UNIT_WATT,
CONF_VOLTAGE,
)
DEPENDENCIES = ["i2c"]
@ -92,7 +93,15 @@ CONFIG_SCHEMA = (
cv.Optional(CONF_MAX_CURRENT, default=3.2): cv.All(
cv.current, cv.Range(min=0.0)
),
cv.Optional(CONF_ADC_TIME, default="1100 us"): validate_adc_time,
cv.Optional(CONF_ADC_TIME, default="1100 us"): cv.Any(
validate_adc_time,
cv.Schema(
{
cv.Required(CONF_VOLTAGE): validate_adc_time,
cv.Required(CONF_CURRENT): validate_adc_time,
}
),
),
cv.Optional(CONF_ADC_AVERAGING, default=4): cv.enum(
ADC_AVG_SAMPLES, int=True
),
@ -110,7 +119,15 @@ async def to_code(config):
cg.add(var.set_shunt_resistance_ohm(config[CONF_SHUNT_RESISTANCE]))
cg.add(var.set_max_current_a(config[CONF_MAX_CURRENT]))
cg.add(var.set_adc_time(config[CONF_ADC_TIME]))
adc_time_config = config[CONF_ADC_TIME]
if isinstance(adc_time_config, dict):
cg.add(var.set_adc_time_voltage(adc_time_config[CONF_VOLTAGE]))
cg.add(var.set_adc_time_current(adc_time_config[CONF_CURRENT]))
else:
cg.add(var.set_adc_time_voltage(adc_time_config))
cg.add(var.set_adc_time_current(adc_time_config))
cg.add(var.set_adc_avg_samples(config[CONF_ADC_AVERAGING]))
if CONF_BUS_VOLTAGE in config: