ESPHome  2024.11.1
ade7953_base.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/hal.h"
6 
7 #include <vector>
8 
9 namespace esphome {
10 namespace ade7953_base {
11 
12 static const uint8_t PGA_V_8 =
13  0x007; // PGA_V, (R/W) Default: 0x00, Unsigned, Voltage channel gain configuration (Bits[2:0])
14 static const uint8_t PGA_IA_8 =
15  0x008; // PGA_IA, (R/W) Default: 0x00, Unsigned, Current Channel A gain configuration (Bits[2:0])
16 static const uint8_t PGA_IB_8 =
17  0x009; // PGA_IB, (R/W) Default: 0x00, Unsigned, Current Channel B gain configuration (Bits[2:0])
18 
19 static const uint32_t AIGAIN_32 =
20  0x380; // AIGAIN, (R/W) Default: 0x400000, Unsigned,Current channel gain (Current Channel A)(32 bit)
21 static const uint32_t AVGAIN_32 = 0x381; // AVGAIN, (R/W) Default: 0x400000, Unsigned,Voltage channel gain(32 bit)
22 static const uint32_t AWGAIN_32 =
23  0x382; // AWGAIN, (R/W) Default: 0x400000, Unsigned,Active power gain (Current Channel A)(32 bit)
24 static const uint32_t AVARGAIN_32 =
25  0x383; // AVARGAIN, (R/W) Default: 0x400000, Unsigned, Reactive power gain (Current Channel A)(32 bit)
26 static const uint32_t AVAGAIN_32 =
27  0x384; // AVAGAIN, (R/W) Default: 0x400000, Unsigned,Apparent power gain (Current Channel A)(32 bit)
28 
29 static const uint32_t BIGAIN_32 =
30  0x38C; // BIGAIN, (R/W) Default: 0x400000, Unsigned,Current channel gain (Current Channel B)(32 bit)
31 static const uint32_t BVGAIN_32 = 0x38D; // BVGAIN, (R/W) Default: 0x400000, Unsigned,Voltage channel gain(32 bit)
32 static const uint32_t BWGAIN_32 =
33  0x38E; // BWGAIN, (R/W) Default: 0x400000, Unsigned,Active power gain (Current Channel B)(32 bit)
34 static const uint32_t BVARGAIN_32 =
35  0x38F; // BVARGAIN, (R/W) Default: 0x400000, Unsigned, Reactive power gain (Current Channel B)(32 bit)
36 static const uint32_t BVAGAIN_32 =
37  0x390; // BVAGAIN, (R/W) Default: 0x400000, Unsigned,Apparent power gain (Current Channel B)(32 bit)
38 
39 class ADE7953 : public PollingComponent, public sensor::Sensor {
40  public:
41  void set_irq_pin(InternalGPIOPin *irq_pin) { irq_pin_ = irq_pin; }
42 
43  // Set PGA input gains: 0 1x, 1 2x, 0b10 4x
44  void set_pga_v(uint8_t pga_v) { pga_v_ = pga_v; }
45  void set_pga_ia(uint8_t pga_ia) { pga_ia_ = pga_ia; }
46  void set_pga_ib(uint8_t pga_ib) { pga_ib_ = pga_ib; }
47 
48  // Set input gains
49  void set_vgain(uint32_t vgain) { vgain_ = vgain; }
50  void set_aigain(uint32_t aigain) { aigain_ = aigain; }
51  void set_bigain(uint32_t bigain) { bigain_ = bigain; }
52  void set_awgain(uint32_t awgain) { awgain_ = awgain; }
53  void set_bwgain(uint32_t bwgain) { bwgain_ = bwgain; }
54 
55  void set_use_acc_energy_regs(bool use_acc_energy_regs) { use_acc_energy_regs_ = use_acc_energy_regs; }
56 
57  void set_voltage_sensor(sensor::Sensor *voltage_sensor) { voltage_sensor_ = voltage_sensor; }
58  void set_frequency_sensor(sensor::Sensor *frequency_sensor) { frequency_sensor_ = frequency_sensor; }
59 
60  void set_power_factor_a_sensor(sensor::Sensor *power_factor_a) { power_factor_a_sensor_ = power_factor_a; }
61  void set_power_factor_b_sensor(sensor::Sensor *power_factor_b) { power_factor_b_sensor_ = power_factor_b; }
62 
63  void set_current_a_sensor(sensor::Sensor *current_a_sensor) { current_a_sensor_ = current_a_sensor; }
64  void set_current_b_sensor(sensor::Sensor *current_b_sensor) { current_b_sensor_ = current_b_sensor; }
65 
66  void set_apparent_power_a_sensor(sensor::Sensor *apparent_power_a) { apparent_power_a_sensor_ = apparent_power_a; }
67  void set_apparent_power_b_sensor(sensor::Sensor *apparent_power_b) { apparent_power_b_sensor_ = apparent_power_b; }
68 
69  void set_active_power_a_sensor(sensor::Sensor *active_power_a_sensor) {
70  active_power_a_sensor_ = active_power_a_sensor;
71  }
72  void set_active_power_b_sensor(sensor::Sensor *active_power_b_sensor) {
73  active_power_b_sensor_ = active_power_b_sensor;
74  }
75 
76  void set_reactive_power_a_sensor(sensor::Sensor *reactive_power_a) { reactive_power_a_sensor_ = reactive_power_a; }
77  void set_reactive_power_b_sensor(sensor::Sensor *reactive_power_b) { reactive_power_b_sensor_ = reactive_power_b; }
78 
79  void setup() override;
80 
81  void dump_config() override;
82 
83  void update() override;
84 
85  protected:
87  bool is_setup_{false};
100  uint8_t pga_v_;
101  uint8_t pga_ia_;
102  uint8_t pga_ib_;
103  uint32_t vgain_;
104  uint32_t aigain_;
105  uint32_t bigain_;
106  uint32_t awgain_;
107  uint32_t bwgain_;
108  bool use_acc_energy_regs_{false};
109  uint32_t last_update_;
110 
111  virtual bool ade_write_8(uint16_t reg, uint8_t value) = 0;
112 
113  virtual bool ade_write_16(uint16_t reg, uint16_t value) = 0;
114 
115  virtual bool ade_write_32(uint16_t reg, uint32_t value) = 0;
116 
117  virtual bool ade_read_8(uint16_t reg, uint8_t *value) = 0;
118 
119  virtual bool ade_read_16(uint16_t reg, uint16_t *value) = 0;
120 
121  virtual bool ade_read_32(uint16_t reg, uint32_t *value) = 0;
122 };
123 
124 } // namespace ade7953_base
125 } // namespace esphome
sensor::Sensor * apparent_power_b_sensor_
Definition: ade7953_base.h:93
sensor::Sensor * frequency_sensor_
Definition: ade7953_base.h:89
void set_frequency_sensor(sensor::Sensor *frequency_sensor)
Definition: ade7953_base.h:58
void set_pga_ia(uint8_t pga_ia)
Definition: ade7953_base.h:45
void set_irq_pin(InternalGPIOPin *irq_pin)
Definition: ade7953_base.h:41
sensor::Sensor * voltage_sensor_
Definition: ade7953_base.h:88
virtual bool ade_read_32(uint16_t reg, uint32_t *value)=0
void set_bwgain(uint32_t bwgain)
Definition: ade7953_base.h:53
void set_reactive_power_b_sensor(sensor::Sensor *reactive_power_b)
Definition: ade7953_base.h:77
void set_use_acc_energy_regs(bool use_acc_energy_regs)
Definition: ade7953_base.h:55
void set_bigain(uint32_t bigain)
Definition: ade7953_base.h:51
sensor::Sensor * reactive_power_a_sensor_
Definition: ade7953_base.h:96
sensor::Sensor * power_factor_b_sensor_
Definition: ade7953_base.h:99
void set_awgain(uint32_t awgain)
Definition: ade7953_base.h:52
void set_active_power_a_sensor(sensor::Sensor *active_power_a_sensor)
Definition: ade7953_base.h:69
This class simplifies creating components that periodically check a state.
Definition: component.h:283
void set_pga_ib(uint8_t pga_ib)
Definition: ade7953_base.h:46
virtual bool ade_write_32(uint16_t reg, uint32_t value)=0
void set_aigain(uint32_t aigain)
Definition: ade7953_base.h:50
void set_current_a_sensor(sensor::Sensor *current_a_sensor)
Definition: ade7953_base.h:63
void set_power_factor_a_sensor(sensor::Sensor *power_factor_a)
Definition: ade7953_base.h:60
void set_reactive_power_a_sensor(sensor::Sensor *reactive_power_a)
Definition: ade7953_base.h:76
void set_active_power_b_sensor(sensor::Sensor *active_power_b_sensor)
Definition: ade7953_base.h:72
void set_power_factor_b_sensor(sensor::Sensor *power_factor_b)
Definition: ade7953_base.h:61
void set_voltage_sensor(sensor::Sensor *voltage_sensor)
Definition: ade7953_base.h:57
sensor::Sensor * active_power_b_sensor_
Definition: ade7953_base.h:95
virtual bool ade_read_8(uint16_t reg, uint8_t *value)=0
sensor::Sensor * power_factor_a_sensor_
Definition: ade7953_base.h:98
void set_current_b_sensor(sensor::Sensor *current_b_sensor)
Definition: ade7953_base.h:64
InternalGPIOPin * irq_pin_
Definition: ade7953_base.h:86
virtual bool ade_write_16(uint16_t reg, uint16_t value)=0
sensor::Sensor * reactive_power_b_sensor_
Definition: ade7953_base.h:97
void set_apparent_power_b_sensor(sensor::Sensor *apparent_power_b)
Definition: ade7953_base.h:67
sensor::Sensor * current_b_sensor_
Definition: ade7953_base.h:91
sensor::Sensor * active_power_a_sensor_
Definition: ade7953_base.h:94
void set_pga_v(uint8_t pga_v)
Definition: ade7953_base.h:44
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
virtual bool ade_read_16(uint16_t reg, uint16_t *value)=0
Base-class for all sensors.
Definition: sensor.h:57
sensor::Sensor * apparent_power_a_sensor_
Definition: ade7953_base.h:92
void set_vgain(uint32_t vgain)
Definition: ade7953_base.h:49
void set_apparent_power_a_sensor(sensor::Sensor *apparent_power_a)
Definition: ade7953_base.h:66
sensor::Sensor * current_a_sensor_
Definition: ade7953_base.h:90
virtual bool ade_write_8(uint16_t reg, uint8_t value)=0