ESPHome  2022.6.3
ade7953.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/hal.h"
7 
8 namespace esphome {
9 namespace ade7953 {
10 
11 class ADE7953 : public i2c::I2CDevice, public PollingComponent {
12  public:
13  void set_irq_pin(InternalGPIOPin *irq_pin) { irq_pin_ = irq_pin; }
14  void set_voltage_sensor(sensor::Sensor *voltage_sensor) { voltage_sensor_ = voltage_sensor; }
15  void set_current_a_sensor(sensor::Sensor *current_a_sensor) { current_a_sensor_ = current_a_sensor; }
16  void set_current_b_sensor(sensor::Sensor *current_b_sensor) { current_b_sensor_ = current_b_sensor; }
17  void set_active_power_a_sensor(sensor::Sensor *active_power_a_sensor) {
18  active_power_a_sensor_ = active_power_a_sensor;
19  }
20  void set_active_power_b_sensor(sensor::Sensor *active_power_b_sensor) {
21  active_power_b_sensor_ = active_power_b_sensor;
22  }
23 
24  void setup() override {
25  if (this->irq_pin_ != nullptr) {
26  this->irq_pin_->setup();
27  }
28  this->set_timeout(100, [this]() {
29  this->ade_write_8_(0x0010, 0x04);
30  this->ade_write_8_(0x00FE, 0xAD);
31  this->ade_write_16_(0x0120, 0x0030);
32  this->is_setup_ = true;
33  });
34  }
35 
36  void dump_config() override;
37 
38  void update() override;
39 
40  protected:
41  i2c::ErrorCode ade_write_8_(uint16_t reg, uint8_t value) {
42  std::vector<uint8_t> data;
43  data.push_back(reg >> 8);
44  data.push_back(reg >> 0);
45  data.push_back(value);
46  return write(data.data(), data.size());
47  }
48  i2c::ErrorCode ade_write_16_(uint16_t reg, uint16_t value) {
49  std::vector<uint8_t> data;
50  data.push_back(reg >> 8);
51  data.push_back(reg >> 0);
52  data.push_back(value >> 8);
53  data.push_back(value >> 0);
54  return write(data.data(), data.size());
55  }
56  i2c::ErrorCode ade_write_32_(uint16_t reg, uint32_t value) {
57  std::vector<uint8_t> data;
58  data.push_back(reg >> 8);
59  data.push_back(reg >> 0);
60  data.push_back(value >> 24);
61  data.push_back(value >> 16);
62  data.push_back(value >> 8);
63  data.push_back(value >> 0);
64  return write(data.data(), data.size());
65  }
66  i2c::ErrorCode ade_read_32_(uint16_t reg, uint32_t *value) {
67  uint8_t reg_data[2];
68  reg_data[0] = reg >> 8;
69  reg_data[1] = reg >> 0;
70  i2c::ErrorCode err = write(reg_data, 2);
71  if (err != i2c::ERROR_OK)
72  return err;
73  uint8_t recv[4];
74  err = read(recv, 4);
75  if (err != i2c::ERROR_OK)
76  return err;
77  *value = 0;
78  *value |= ((uint32_t) recv[0]) << 24;
79  *value |= ((uint32_t) recv[1]) << 16;
80  *value |= ((uint32_t) recv[2]) << 8;
81  *value |= ((uint32_t) recv[3]);
82  return i2c::ERROR_OK;
83  }
84 
86  bool is_setup_{false};
92 };
93 
94 } // namespace ade7953
95 } // namespace esphome
sensor::Sensor * current_a_sensor_
Definition: ade7953.h:88
void set_voltage_sensor(sensor::Sensor *voltage_sensor)
Definition: ade7953.h:14
void set_active_power_a_sensor(sensor::Sensor *active_power_a_sensor)
Definition: ade7953.h:17
I2CRegister reg(uint8_t a_register)
Definition: i2c.h:46
ErrorCode read(uint8_t *data, size_t len)
Definition: i2c.h:48
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition: component.cpp:67
void set_active_power_b_sensor(sensor::Sensor *active_power_b_sensor)
Definition: ade7953.h:20
This class simplifies creating components that periodically check a state.
Definition: component.h:266
virtual void setup()=0
i2c::ErrorCode ade_write_16_(uint16_t reg, uint16_t value)
Definition: ade7953.h:48
sensor::Sensor * current_b_sensor_
Definition: ade7953.h:89
InternalGPIOPin * irq_pin_
Definition: ade7953.h:85
ErrorCode write(const uint8_t *data, uint8_t len, bool stop=true)
Definition: i2c.h:56
sensor::Sensor * active_power_b_sensor_
Definition: ade7953.h:91
void dump_config() override
Definition: ade7953.cpp:9
void setup() override
Definition: ade7953.h:24
void set_irq_pin(InternalGPIOPin *irq_pin)
Definition: ade7953.h:13
i2c::ErrorCode ade_write_8_(uint16_t reg, uint8_t value)
Definition: ade7953.h:41
void set_current_a_sensor(sensor::Sensor *current_a_sensor)
Definition: ade7953.h:15
Definition: a4988.cpp:4
Base-class for all sensors.
Definition: sensor.h:47
i2c::ErrorCode ade_write_32_(uint16_t reg, uint32_t value)
Definition: ade7953.h:56
void set_current_b_sensor(sensor::Sensor *current_b_sensor)
Definition: ade7953.h:16
sensor::Sensor * voltage_sensor_
Definition: ade7953.h:87
sensor::Sensor * active_power_a_sensor_
Definition: ade7953.h:90
i2c::ErrorCode ade_read_32_(uint16_t reg, uint32_t *value)
Definition: ade7953.h:66
void update() override
Definition: ade7953.cpp:28