ESPHome  1.14.4
ade7953.h
Go to the documentation of this file.
1 #pragma once
2 
6 
7 namespace esphome {
8 namespace ade7953 {
9 
10 class ADE7953 : public i2c::I2CDevice, public PollingComponent {
11  public:
12  void set_voltage_sensor(sensor::Sensor *voltage_sensor) { voltage_sensor_ = voltage_sensor; }
13  void set_current_a_sensor(sensor::Sensor *current_a_sensor) { current_a_sensor_ = current_a_sensor; }
14  void set_current_b_sensor(sensor::Sensor *current_b_sensor) { current_b_sensor_ = current_b_sensor; }
15  void set_active_power_a_sensor(sensor::Sensor *active_power_a_sensor) {
16  active_power_a_sensor_ = active_power_a_sensor;
17  }
18  void set_active_power_b_sensor(sensor::Sensor *active_power_b_sensor) {
19  active_power_b_sensor_ = active_power_b_sensor;
20  }
21 
22  void setup() override {
23  this->set_timeout(100, [this]() {
24  this->ade_write_<uint8_t>(0x0010, 0x04);
25  this->ade_write_<uint8_t>(0x00FE, 0xAD);
26  this->ade_write_<uint16_t>(0x0120, 0x0030);
27  this->is_setup_ = true;
28  });
29  }
30 
31  void dump_config() override;
32 
33  void update() override;
34 
35  protected:
36  template<typename T> bool ade_write_(uint16_t reg, T value) {
37  std::vector<uint8_t> data;
38  data.push_back(reg >> 8);
39  data.push_back(reg >> 0);
40  for (int i = sizeof(T) - 1; i >= 0; i--)
41  data.push_back(value >> (i * 8));
42  return this->write_bytes_raw(data);
43  }
44  template<typename T> optional<T> ade_read_(uint16_t reg) {
45  uint8_t hi = reg >> 8;
46  uint8_t lo = reg >> 0;
47  if (!this->write_bytes_raw({hi, lo}))
48  return {};
49  auto ret = this->read_bytes_raw<sizeof(T)>();
50  if (!ret.has_value())
51  return {};
52  T result = 0;
53  for (int i = 0, j = sizeof(T) - 1; i < sizeof(T); i++, j--)
54  result |= T((*ret)[i]) << (j * 8);
55  return result;
56  }
57 
58  bool is_setup_{false};
64 };
65 
66 } // namespace ade7953
67 } // namespace esphome
bool write_bytes_raw(const uint8_t *data, uint8_t len)
Definition: i2c.h:234
sensor::Sensor * current_a_sensor_
Definition: ade7953.h:60
void set_voltage_sensor(sensor::Sensor *voltage_sensor)
Definition: ade7953.h:12
void set_active_power_a_sensor(sensor::Sensor *active_power_a_sensor)
Definition: ade7953.h:15
I2CRegister reg(uint8_t a_register)
Definition: i2c.h:174
void set_active_power_b_sensor(sensor::Sensor *active_power_b_sensor)
Definition: ade7953.h:18
This class simplifies creating components that periodically check a state.
Definition: component.h:208
sensor::Sensor * current_b_sensor_
Definition: ade7953.h:61
void set_timeout(uint32_t timeout, std::function< void()> &&f)
Definition: component.cpp:106
sensor::Sensor * active_power_b_sensor_
Definition: ade7953.h:63
void dump_config() override
Definition: ade7953.cpp:9
void setup() override
Definition: ade7953.h:22
optional< T > ade_read_(uint16_t reg)
Definition: ade7953.h:44
void set_current_a_sensor(sensor::Sensor *current_a_sensor)
Definition: ade7953.h:13
Definition: a4988.cpp:4
Base-class for all sensors.
Definition: sensor.h:30
bool ade_write_(uint16_t reg, T value)
Definition: ade7953.h:36
void set_current_b_sensor(sensor::Sensor *current_b_sensor)
Definition: ade7953.h:14
sensor::Sensor * voltage_sensor_
Definition: ade7953.h:59
sensor::Sensor * active_power_a_sensor_
Definition: ade7953.h:62
All components doing communication on the I2C bus should subclass I2CDevice.
Definition: i2c.h:163
void update() override
Definition: ade7953.cpp:27