mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 06:33:51 +00:00 
			
		
		
		
	Add spi support for ade7953 (#5439)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
		
				
					committed by
					
						 GitHub
						GitHub
					
				
			
			
				
	
			
			
			
						parent
						
							7ac9caa169
						
					
				
				
					commit
					defe8ac97b
				
			
							
								
								
									
										196
									
								
								esphome/components/ade7953_base/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										196
									
								
								esphome/components/ade7953_base/__init__.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,196 @@ | ||||
| import esphome.codegen as cg | ||||
| import esphome.config_validation as cv | ||||
| from esphome.components import sensor | ||||
| from esphome import pins | ||||
| from esphome.const import ( | ||||
|     CONF_IRQ_PIN, | ||||
|     CONF_VOLTAGE, | ||||
|     CONF_FREQUENCY, | ||||
|     DEVICE_CLASS_CURRENT, | ||||
|     DEVICE_CLASS_APPARENT_POWER, | ||||
|     DEVICE_CLASS_POWER, | ||||
|     DEVICE_CLASS_REACTIVE_POWER, | ||||
|     DEVICE_CLASS_POWER_FACTOR, | ||||
|     DEVICE_CLASS_VOLTAGE, | ||||
|     DEVICE_CLASS_FREQUENCY, | ||||
|     STATE_CLASS_MEASUREMENT, | ||||
|     UNIT_VOLT, | ||||
|     UNIT_HERTZ, | ||||
|     UNIT_AMPERE, | ||||
|     UNIT_VOLT_AMPS, | ||||
|     UNIT_WATT, | ||||
|     UNIT_VOLT_AMPS_REACTIVE, | ||||
|     UNIT_PERCENT, | ||||
| ) | ||||
|  | ||||
| CONF_CURRENT_A = "current_a" | ||||
| CONF_CURRENT_B = "current_b" | ||||
| CONF_ACTIVE_POWER_A = "active_power_a" | ||||
| CONF_ACTIVE_POWER_B = "active_power_b" | ||||
| CONF_APPARENT_POWER_A = "apparent_power_a" | ||||
| CONF_APPARENT_POWER_B = "apparent_power_b" | ||||
| CONF_REACTIVE_POWER_A = "reactive_power_a" | ||||
| CONF_REACTIVE_POWER_B = "reactive_power_b" | ||||
| CONF_POWER_FACTOR_A = "power_factor_a" | ||||
| CONF_POWER_FACTOR_B = "power_factor_b" | ||||
| CONF_VOLTAGE_PGA_GAIN = "voltage_pga_gain" | ||||
| CONF_CURRENT_PGA_GAIN_A = "current_pga_gain_a" | ||||
| CONF_CURRENT_PGA_GAIN_B = "current_pga_gain_b" | ||||
| CONF_VOLTAGE_GAIN = "voltage_gain" | ||||
| CONF_CURRENT_GAIN_A = "current_gain_a" | ||||
| CONF_CURRENT_GAIN_B = "current_gain_b" | ||||
| CONF_ACTIVE_POWER_GAIN_A = "active_power_gain_a" | ||||
| CONF_ACTIVE_POWER_GAIN_B = "active_power_gain_b" | ||||
| PGA_GAINS = { | ||||
|     "1x": 0b000, | ||||
|     "2x": 0b001, | ||||
|     "4x": 0b010, | ||||
|     "8x": 0b011, | ||||
|     "16x": 0b100, | ||||
|     "22x": 0b101, | ||||
| } | ||||
|  | ||||
| ade7953_base_ns = cg.esphome_ns.namespace("ade7953_base") | ||||
| ADE7953 = ade7953_base_ns.class_("ADE7953", cg.PollingComponent) | ||||
|  | ||||
| ADE7953_CONFIG_SCHEMA = cv.Schema( | ||||
|     { | ||||
|         cv.Optional(CONF_IRQ_PIN): pins.internal_gpio_input_pin_schema, | ||||
|         cv.Optional(CONF_VOLTAGE): sensor.sensor_schema( | ||||
|             unit_of_measurement=UNIT_VOLT, | ||||
|             accuracy_decimals=1, | ||||
|             device_class=DEVICE_CLASS_VOLTAGE, | ||||
|             state_class=STATE_CLASS_MEASUREMENT, | ||||
|         ), | ||||
|         cv.Optional(CONF_FREQUENCY): sensor.sensor_schema( | ||||
|             unit_of_measurement=UNIT_HERTZ, | ||||
|             accuracy_decimals=2, | ||||
|             device_class=DEVICE_CLASS_FREQUENCY, | ||||
|             state_class=STATE_CLASS_MEASUREMENT, | ||||
|         ), | ||||
|         cv.Optional(CONF_CURRENT_A): sensor.sensor_schema( | ||||
|             unit_of_measurement=UNIT_AMPERE, | ||||
|             accuracy_decimals=2, | ||||
|             device_class=DEVICE_CLASS_CURRENT, | ||||
|             state_class=STATE_CLASS_MEASUREMENT, | ||||
|         ), | ||||
|         cv.Optional(CONF_CURRENT_B): sensor.sensor_schema( | ||||
|             unit_of_measurement=UNIT_AMPERE, | ||||
|             accuracy_decimals=2, | ||||
|             device_class=DEVICE_CLASS_CURRENT, | ||||
|             state_class=STATE_CLASS_MEASUREMENT, | ||||
|         ), | ||||
|         cv.Optional(CONF_ACTIVE_POWER_A): sensor.sensor_schema( | ||||
|             unit_of_measurement=UNIT_WATT, | ||||
|             accuracy_decimals=1, | ||||
|             device_class=DEVICE_CLASS_POWER, | ||||
|             state_class=STATE_CLASS_MEASUREMENT, | ||||
|         ), | ||||
|         cv.Optional(CONF_ACTIVE_POWER_B): sensor.sensor_schema( | ||||
|             unit_of_measurement=UNIT_WATT, | ||||
|             accuracy_decimals=1, | ||||
|             device_class=DEVICE_CLASS_POWER, | ||||
|             state_class=STATE_CLASS_MEASUREMENT, | ||||
|         ), | ||||
|         cv.Optional(CONF_APPARENT_POWER_A): sensor.sensor_schema( | ||||
|             unit_of_measurement=UNIT_VOLT_AMPS, | ||||
|             accuracy_decimals=1, | ||||
|             device_class=DEVICE_CLASS_APPARENT_POWER, | ||||
|             state_class=STATE_CLASS_MEASUREMENT, | ||||
|         ), | ||||
|         cv.Optional(CONF_APPARENT_POWER_B): sensor.sensor_schema( | ||||
|             unit_of_measurement=UNIT_VOLT_AMPS, | ||||
|             accuracy_decimals=1, | ||||
|             device_class=DEVICE_CLASS_APPARENT_POWER, | ||||
|             state_class=STATE_CLASS_MEASUREMENT, | ||||
|         ), | ||||
|         cv.Optional(CONF_REACTIVE_POWER_A): sensor.sensor_schema( | ||||
|             unit_of_measurement=UNIT_VOLT_AMPS_REACTIVE, | ||||
|             accuracy_decimals=1, | ||||
|             device_class=DEVICE_CLASS_REACTIVE_POWER, | ||||
|             state_class=STATE_CLASS_MEASUREMENT, | ||||
|         ), | ||||
|         cv.Optional(CONF_REACTIVE_POWER_B): sensor.sensor_schema( | ||||
|             unit_of_measurement=UNIT_VOLT_AMPS_REACTIVE, | ||||
|             accuracy_decimals=1, | ||||
|             device_class=DEVICE_CLASS_REACTIVE_POWER, | ||||
|             state_class=STATE_CLASS_MEASUREMENT, | ||||
|         ), | ||||
|         cv.Optional(CONF_POWER_FACTOR_A): sensor.sensor_schema( | ||||
|             unit_of_measurement=UNIT_PERCENT, | ||||
|             accuracy_decimals=2, | ||||
|             device_class=DEVICE_CLASS_POWER_FACTOR, | ||||
|             state_class=STATE_CLASS_MEASUREMENT, | ||||
|         ), | ||||
|         cv.Optional(CONF_POWER_FACTOR_B): sensor.sensor_schema( | ||||
|             unit_of_measurement=UNIT_PERCENT, | ||||
|             accuracy_decimals=2, | ||||
|             device_class=DEVICE_CLASS_POWER_FACTOR, | ||||
|             state_class=STATE_CLASS_MEASUREMENT, | ||||
|         ), | ||||
|         cv.Optional( | ||||
|             CONF_VOLTAGE_PGA_GAIN, | ||||
|             default="1x", | ||||
|         ): cv.one_of(*PGA_GAINS, lower=True), | ||||
|         cv.Optional( | ||||
|             CONF_CURRENT_PGA_GAIN_A, | ||||
|             default="1x", | ||||
|         ): cv.one_of(*PGA_GAINS, lower=True), | ||||
|         cv.Optional( | ||||
|             CONF_CURRENT_PGA_GAIN_B, | ||||
|             default="1x", | ||||
|         ): cv.one_of(*PGA_GAINS, lower=True), | ||||
|         cv.Optional(CONF_VOLTAGE_GAIN, default=0x400000): cv.hex_int_range( | ||||
|             min=0x100000, max=0x800000 | ||||
|         ), | ||||
|         cv.Optional(CONF_CURRENT_GAIN_A, default=0x400000): cv.hex_int_range( | ||||
|             min=0x100000, max=0x800000 | ||||
|         ), | ||||
|         cv.Optional(CONF_CURRENT_GAIN_B, default=0x400000): cv.hex_int_range( | ||||
|             min=0x100000, max=0x800000 | ||||
|         ), | ||||
|         cv.Optional(CONF_ACTIVE_POWER_GAIN_A, default=0x400000): cv.hex_int_range( | ||||
|             min=0x100000, max=0x800000 | ||||
|         ), | ||||
|         cv.Optional(CONF_ACTIVE_POWER_GAIN_B, default=0x400000): cv.hex_int_range( | ||||
|             min=0x100000, max=0x800000 | ||||
|         ), | ||||
|     } | ||||
| ).extend(cv.polling_component_schema("60s")) | ||||
|  | ||||
|  | ||||
| async def register_ade7953(var, config): | ||||
|     await cg.register_component(var, config) | ||||
|  | ||||
|     if irq_pin_config := config.get(CONF_IRQ_PIN): | ||||
|         irq_pin = await cg.gpio_pin_expression(irq_pin_config) | ||||
|         cg.add(var.set_irq_pin(irq_pin)) | ||||
|  | ||||
|     cg.add(var.set_pga_v(PGA_GAINS[config.get(CONF_VOLTAGE_PGA_GAIN)])) | ||||
|     cg.add(var.set_pga_ia(PGA_GAINS[config.get(CONF_CURRENT_PGA_GAIN_A)])) | ||||
|     cg.add(var.set_pga_ib(PGA_GAINS[config.get(CONF_CURRENT_PGA_GAIN_B)])) | ||||
|     cg.add(var.set_vgain(config.get(CONF_VOLTAGE_GAIN))) | ||||
|     cg.add(var.set_aigain(config.get(CONF_CURRENT_GAIN_A))) | ||||
|     cg.add(var.set_bigain(config.get(CONF_CURRENT_GAIN_B))) | ||||
|     cg.add(var.set_awgain(config.get(CONF_ACTIVE_POWER_GAIN_A))) | ||||
|     cg.add(var.set_bwgain(config.get(CONF_ACTIVE_POWER_GAIN_B))) | ||||
|  | ||||
|     for key in [ | ||||
|         CONF_VOLTAGE, | ||||
|         CONF_FREQUENCY, | ||||
|         CONF_CURRENT_A, | ||||
|         CONF_CURRENT_B, | ||||
|         CONF_POWER_FACTOR_A, | ||||
|         CONF_POWER_FACTOR_B, | ||||
|         CONF_APPARENT_POWER_A, | ||||
|         CONF_APPARENT_POWER_B, | ||||
|         CONF_ACTIVE_POWER_A, | ||||
|         CONF_ACTIVE_POWER_B, | ||||
|         CONF_REACTIVE_POWER_A, | ||||
|         CONF_REACTIVE_POWER_B, | ||||
|     ]: | ||||
|         if key not in config: | ||||
|             continue | ||||
|         conf = config[key] | ||||
|         sens = await sensor.new_sensor(conf) | ||||
|         cg.add(getattr(var, f"set_{key}_sensor")(sens)) | ||||
							
								
								
									
										129
									
								
								esphome/components/ade7953_base/ade7953_base.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										129
									
								
								esphome/components/ade7953_base/ade7953_base.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,129 @@ | ||||
| #include "ade7953_base.h" | ||||
| #include "esphome/core/log.h" | ||||
|  | ||||
| namespace esphome { | ||||
| namespace ade7953_base { | ||||
|  | ||||
| static const char *const TAG = "ade7953"; | ||||
|  | ||||
| void ADE7953::setup() { | ||||
|   if (this->irq_pin_ != nullptr) { | ||||
|     this->irq_pin_->setup(); | ||||
|   } | ||||
|  | ||||
|   // The chip might take up to 100ms to initialise | ||||
|   this->set_timeout(100, [this]() { | ||||
|     // this->ade_write_8(0x0010, 0x04); | ||||
|     this->ade_write_8(0x00FE, 0xAD); | ||||
|     this->ade_write_16(0x0120, 0x0030); | ||||
|     // Set gains | ||||
|     this->ade_write_8(PGA_V_8, pga_v_); | ||||
|     this->ade_write_8(PGA_IA_8, pga_ia_); | ||||
|     this->ade_write_8(PGA_IB_8, pga_ib_); | ||||
|     this->ade_write_32(AVGAIN_32, vgain_); | ||||
|     this->ade_write_32(AIGAIN_32, aigain_); | ||||
|     this->ade_write_32(BIGAIN_32, bigain_); | ||||
|     this->ade_write_32(AWGAIN_32, awgain_); | ||||
|     this->ade_write_32(BWGAIN_32, bwgain_); | ||||
|     // Read back gains for debugging | ||||
|     this->ade_read_8(PGA_V_8, &pga_v_); | ||||
|     this->ade_read_8(PGA_IA_8, &pga_ia_); | ||||
|     this->ade_read_8(PGA_IB_8, &pga_ib_); | ||||
|     this->ade_read_32(AVGAIN_32, &vgain_); | ||||
|     this->ade_read_32(AIGAIN_32, &aigain_); | ||||
|     this->ade_read_32(BIGAIN_32, &bigain_); | ||||
|     this->ade_read_32(AWGAIN_32, &awgain_); | ||||
|     this->ade_read_32(BWGAIN_32, &bwgain_); | ||||
|     this->is_setup_ = true; | ||||
|   }); | ||||
| } | ||||
|  | ||||
| void ADE7953::dump_config() { | ||||
|   LOG_PIN("  IRQ Pin: ", irq_pin_); | ||||
|   LOG_UPDATE_INTERVAL(this); | ||||
|   LOG_SENSOR("  ", "Voltage Sensor", this->voltage_sensor_); | ||||
|   LOG_SENSOR("  ", "Current A Sensor", this->current_a_sensor_); | ||||
|   LOG_SENSOR("  ", "Current B Sensor", this->current_b_sensor_); | ||||
|   LOG_SENSOR("  ", "Power Factor A Sensor", this->power_factor_a_sensor_); | ||||
|   LOG_SENSOR("  ", "Power Factor B Sensor", this->power_factor_b_sensor_); | ||||
|   LOG_SENSOR("  ", "Apparent Power A Sensor", this->apparent_power_a_sensor_); | ||||
|   LOG_SENSOR("  ", "Apparent Power B Sensor", this->apparent_power_b_sensor_); | ||||
|   LOG_SENSOR("  ", "Active Power A Sensor", this->active_power_a_sensor_); | ||||
|   LOG_SENSOR("  ", "Active Power B Sensor", this->active_power_b_sensor_); | ||||
|   LOG_SENSOR("  ", "Rective Power A Sensor", this->reactive_power_a_sensor_); | ||||
|   LOG_SENSOR("  ", "Reactive Power B Sensor", this->reactive_power_b_sensor_); | ||||
|   ESP_LOGCONFIG(TAG, "  PGA_V_8: 0x%X", pga_v_); | ||||
|   ESP_LOGCONFIG(TAG, "  PGA_IA_8: 0x%X", pga_ia_); | ||||
|   ESP_LOGCONFIG(TAG, "  PGA_IB_8: 0x%X", pga_ib_); | ||||
|   ESP_LOGCONFIG(TAG, "  VGAIN_32: 0x%08jX", (uintmax_t) vgain_); | ||||
|   ESP_LOGCONFIG(TAG, "  AIGAIN_32: 0x%08jX", (uintmax_t) aigain_); | ||||
|   ESP_LOGCONFIG(TAG, "  BIGAIN_32: 0x%08jX", (uintmax_t) bigain_); | ||||
|   ESP_LOGCONFIG(TAG, "  AWGAIN_32: 0x%08jX", (uintmax_t) awgain_); | ||||
|   ESP_LOGCONFIG(TAG, "  BWGAIN_32: 0x%08jX", (uintmax_t) bwgain_); | ||||
| } | ||||
|  | ||||
| #define ADE_PUBLISH_(name, val, factor) \ | ||||
|   if (err == 0 && this->name##_sensor_) { \ | ||||
|     float value = (val) / (factor); \ | ||||
|     this->name##_sensor_->publish_state(value); \ | ||||
|   } | ||||
| #define ADE_PUBLISH(name, val, factor) ADE_PUBLISH_(name, val, factor) | ||||
|  | ||||
| void ADE7953::update() { | ||||
|   if (!this->is_setup_) | ||||
|     return; | ||||
|  | ||||
|   bool err; | ||||
|  | ||||
|   uint32_t interrupts_a = 0; | ||||
|   uint32_t interrupts_b = 0; | ||||
|   if (this->irq_pin_ != nullptr) { | ||||
|     // Read and reset interrupts | ||||
|     this->ade_read_32(0x032E, &interrupts_a); | ||||
|     this->ade_read_32(0x0331, &interrupts_b); | ||||
|   } | ||||
|  | ||||
|   uint32_t val; | ||||
|   uint16_t val_16; | ||||
|  | ||||
|   // Power factor | ||||
|   err = this->ade_read_16(0x010A, &val_16); | ||||
|   ADE_PUBLISH(power_factor_a, (int16_t) val_16, (0x7FFF / 100.0f)); | ||||
|   err = this->ade_read_16(0x010B, &val_16); | ||||
|   ADE_PUBLISH(power_factor_b, (int16_t) val_16, (0x7FFF / 100.0f)); | ||||
|  | ||||
|   // Apparent power | ||||
|   err = this->ade_read_32(0x0310, &val); | ||||
|   ADE_PUBLISH(apparent_power_a, (int32_t) val, 154.0f); | ||||
|   err = this->ade_read_32(0x0311, &val); | ||||
|   ADE_PUBLISH(apparent_power_b, (int32_t) val, 154.0f); | ||||
|  | ||||
|   // Active power | ||||
|   err = this->ade_read_32(0x0312, &val); | ||||
|   ADE_PUBLISH(active_power_a, (int32_t) val, 154.0f); | ||||
|   err = this->ade_read_32(0x0313, &val); | ||||
|   ADE_PUBLISH(active_power_b, (int32_t) val, 154.0f); | ||||
|  | ||||
|   // Reactive power | ||||
|   err = this->ade_read_32(0x0314, &val); | ||||
|   ADE_PUBLISH(reactive_power_a, (int32_t) val, 154.0f); | ||||
|   err = this->ade_read_32(0x0315, &val); | ||||
|   ADE_PUBLISH(reactive_power_b, (int32_t) val, 154.0f); | ||||
|  | ||||
|   // Current | ||||
|   err = this->ade_read_32(0x031A, &val); | ||||
|   ADE_PUBLISH(current_a, (uint32_t) val, 100000.0f); | ||||
|   err = this->ade_read_32(0x031B, &val); | ||||
|   ADE_PUBLISH(current_b, (uint32_t) val, 100000.0f); | ||||
|  | ||||
|   // Voltage | ||||
|   err = this->ade_read_32(0x031C, &val); | ||||
|   ADE_PUBLISH(voltage, (uint32_t) val, 26000.0f); | ||||
|  | ||||
|   // Frequency | ||||
|   err = this->ade_read_16(0x010E, &val_16); | ||||
|   ADE_PUBLISH(frequency, 223750.0f, 1 + val_16); | ||||
| } | ||||
|  | ||||
| }  // namespace ade7953_base | ||||
| }  // namespace esphome | ||||
							
								
								
									
										121
									
								
								esphome/components/ade7953_base/ade7953_base.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										121
									
								
								esphome/components/ade7953_base/ade7953_base.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,121 @@ | ||||
| #pragma once | ||||
|  | ||||
| #include "esphome/core/component.h" | ||||
| #include "esphome/core/hal.h" | ||||
| #include "esphome/components/sensor/sensor.h" | ||||
|  | ||||
| #include <vector> | ||||
|  | ||||
| namespace esphome { | ||||
| namespace ade7953_base { | ||||
|  | ||||
| static const uint8_t PGA_V_8 = | ||||
|     0x007;  // PGA_V,  (R/W) Default: 0x00, Unsigned, Voltage channel gain configuration (Bits[2:0]) | ||||
| static const uint8_t PGA_IA_8 = | ||||
|     0x008;  // PGA_IA, (R/W) Default: 0x00, Unsigned, Current Channel A gain configuration (Bits[2:0]) | ||||
| static const uint8_t PGA_IB_8 = | ||||
|     0x009;  // PGA_IB, (R/W) Default: 0x00, Unsigned, Current Channel B gain configuration (Bits[2:0]) | ||||
|  | ||||
| static const uint32_t AIGAIN_32 = | ||||
|     0x380;  // AIGAIN, (R/W)   Default: 0x400000, Unsigned,Current channel gain (Current Channel A)(32 bit) | ||||
| static const uint32_t AVGAIN_32 = 0x381;  // AVGAIN, (R/W)   Default: 0x400000, Unsigned,Voltage channel gain(32 bit) | ||||
| static const uint32_t AWGAIN_32 = | ||||
|     0x382;  // AWGAIN, (R/W)   Default: 0x400000, Unsigned,Active power gain (Current Channel A)(32 bit) | ||||
| static const uint32_t AVARGAIN_32 = | ||||
|     0x383;  // AVARGAIN, (R/W) Default: 0x400000, Unsigned, Reactive power gain (Current Channel A)(32 bit) | ||||
| static const uint32_t AVAGAIN_32 = | ||||
|     0x384;  // AVAGAIN, (R/W)  Default: 0x400000, Unsigned,Apparent power gain (Current Channel A)(32 bit) | ||||
|  | ||||
| static const uint32_t BIGAIN_32 = | ||||
|     0x38C;  // BIGAIN, (R/W)   Default: 0x400000, Unsigned,Current channel gain (Current Channel B)(32 bit) | ||||
| static const uint32_t BVGAIN_32 = 0x38D;  // BVGAIN, (R/W)   Default: 0x400000, Unsigned,Voltage channel gain(32 bit) | ||||
| static const uint32_t BWGAIN_32 = | ||||
|     0x38E;  // BWGAIN, (R/W)   Default: 0x400000, Unsigned,Active power gain (Current Channel B)(32 bit) | ||||
| static const uint32_t BVARGAIN_32 = | ||||
|     0x38F;  // BVARGAIN, (R/W) Default: 0x400000, Unsigned, Reactive power gain (Current Channel B)(32 bit) | ||||
| static const uint32_t BVAGAIN_32 = | ||||
|     0x390;  // BVAGAIN, (R/W)  Default: 0x400000, Unsigned,Apparent power gain (Current Channel B)(32 bit) | ||||
|  | ||||
| class ADE7953 : public PollingComponent, public sensor::Sensor { | ||||
|  public: | ||||
|   void set_irq_pin(InternalGPIOPin *irq_pin) { irq_pin_ = irq_pin; } | ||||
|  | ||||
|   // Set PGA input gains: 0 1x, 1 2x, 0b10 4x | ||||
|   void set_pga_v(uint8_t pga_v) { pga_v_ = pga_v; } | ||||
|   void set_pga_ia(uint8_t pga_ia) { pga_ia_ = pga_ia; } | ||||
|   void set_pga_ib(uint8_t pga_ib) { pga_ib_ = pga_ib; } | ||||
|  | ||||
|   // Set input gains | ||||
|   void set_vgain(uint32_t vgain) { vgain_ = vgain; } | ||||
|   void set_aigain(uint32_t aigain) { aigain_ = aigain; } | ||||
|   void set_bigain(uint32_t bigain) { bigain_ = bigain; } | ||||
|   void set_awgain(uint32_t awgain) { awgain_ = awgain; } | ||||
|   void set_bwgain(uint32_t bwgain) { bwgain_ = bwgain; } | ||||
|  | ||||
|   void set_voltage_sensor(sensor::Sensor *voltage_sensor) { voltage_sensor_ = voltage_sensor; } | ||||
|   void set_frequency_sensor(sensor::Sensor *frequency_sensor) { frequency_sensor_ = frequency_sensor; } | ||||
|  | ||||
|   void set_power_factor_a_sensor(sensor::Sensor *power_factor_a) { power_factor_a_sensor_ = power_factor_a; } | ||||
|   void set_power_factor_b_sensor(sensor::Sensor *power_factor_b) { power_factor_b_sensor_ = power_factor_b; } | ||||
|  | ||||
|   void set_current_a_sensor(sensor::Sensor *current_a_sensor) { current_a_sensor_ = current_a_sensor; } | ||||
|   void set_current_b_sensor(sensor::Sensor *current_b_sensor) { current_b_sensor_ = current_b_sensor; } | ||||
|  | ||||
|   void set_apparent_power_a_sensor(sensor::Sensor *apparent_power_a) { apparent_power_a_sensor_ = apparent_power_a; } | ||||
|   void set_apparent_power_b_sensor(sensor::Sensor *apparent_power_b) { apparent_power_b_sensor_ = apparent_power_b; } | ||||
|  | ||||
|   void set_active_power_a_sensor(sensor::Sensor *active_power_a_sensor) { | ||||
|     active_power_a_sensor_ = active_power_a_sensor; | ||||
|   } | ||||
|   void set_active_power_b_sensor(sensor::Sensor *active_power_b_sensor) { | ||||
|     active_power_b_sensor_ = active_power_b_sensor; | ||||
|   } | ||||
|  | ||||
|   void set_reactive_power_a_sensor(sensor::Sensor *reactive_power_a) { reactive_power_a_sensor_ = reactive_power_a; } | ||||
|   void set_reactive_power_b_sensor(sensor::Sensor *reactive_power_b) { reactive_power_b_sensor_ = reactive_power_b; } | ||||
|  | ||||
|   void setup() override; | ||||
|  | ||||
|   void dump_config() override; | ||||
|  | ||||
|   void update() override; | ||||
|  | ||||
|  protected: | ||||
|   InternalGPIOPin *irq_pin_{nullptr}; | ||||
|   bool is_setup_{false}; | ||||
|   sensor::Sensor *voltage_sensor_{nullptr}; | ||||
|   sensor::Sensor *frequency_sensor_{nullptr}; | ||||
|   sensor::Sensor *current_a_sensor_{nullptr}; | ||||
|   sensor::Sensor *current_b_sensor_{nullptr}; | ||||
|   sensor::Sensor *apparent_power_a_sensor_{nullptr}; | ||||
|   sensor::Sensor *apparent_power_b_sensor_{nullptr}; | ||||
|   sensor::Sensor *active_power_a_sensor_{nullptr}; | ||||
|   sensor::Sensor *active_power_b_sensor_{nullptr}; | ||||
|   sensor::Sensor *reactive_power_a_sensor_{nullptr}; | ||||
|   sensor::Sensor *reactive_power_b_sensor_{nullptr}; | ||||
|   sensor::Sensor *power_factor_a_sensor_{nullptr}; | ||||
|   sensor::Sensor *power_factor_b_sensor_{nullptr}; | ||||
|   uint8_t pga_v_; | ||||
|   uint8_t pga_ia_; | ||||
|   uint8_t pga_ib_; | ||||
|   uint32_t vgain_; | ||||
|   uint32_t aigain_; | ||||
|   uint32_t bigain_; | ||||
|   uint32_t awgain_; | ||||
|   uint32_t bwgain_; | ||||
|  | ||||
|   virtual bool ade_write_8(uint16_t reg, uint8_t value) = 0; | ||||
|  | ||||
|   virtual bool ade_write_16(uint16_t reg, uint16_t value) = 0; | ||||
|  | ||||
|   virtual bool ade_write_32(uint16_t reg, uint32_t value) = 0; | ||||
|  | ||||
|   virtual bool ade_read_8(uint16_t reg, uint8_t *value) = 0; | ||||
|  | ||||
|   virtual bool ade_read_16(uint16_t reg, uint16_t *value) = 0; | ||||
|  | ||||
|   virtual bool ade_read_32(uint16_t reg, uint32_t *value) = 0; | ||||
| }; | ||||
|  | ||||
| }  // namespace ade7953_base | ||||
| }  // namespace esphome | ||||
		Reference in New Issue
	
	Block a user