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