ESPHome  1.17.1
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_irq_pin(uint8_t irq_pin) {
13  has_irq_ = true;
14  irq_pin_number_ = irq_pin;
15  }
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->has_irq_) {
28  auto pin = GPIOPin(this->irq_pin_number_, INPUT);
29  this->irq_pin_ = &pin;
30  this->irq_pin_->setup();
31  }
32  this->set_timeout(100, [this]() {
33  this->ade_write_<uint8_t>(0x0010, 0x04);
34  this->ade_write_<uint8_t>(0x00FE, 0xAD);
35  this->ade_write_<uint16_t>(0x0120, 0x0030);
36  this->is_setup_ = true;
37  });
38  }
39 
40  void dump_config() override;
41 
42  void update() override;
43 
44  protected:
45  template<typename T> bool ade_write_(uint16_t reg, T value) {
46  std::vector<uint8_t> data;
47  data.push_back(reg >> 8);
48  data.push_back(reg >> 0);
49  for (int i = sizeof(T) - 1; i >= 0; i--)
50  data.push_back(value >> (i * 8));
51  return this->write_bytes_raw(data);
52  }
53  template<typename T> optional<T> ade_read_(uint16_t reg) {
54  uint8_t hi = reg >> 8;
55  uint8_t lo = reg >> 0;
56  if (!this->write_bytes_raw({hi, lo}))
57  return {};
58  auto ret = this->read_bytes_raw<sizeof(T)>();
59  if (!ret.has_value())
60  return {};
61  T result = 0;
62  for (int i = 0, j = sizeof(T) - 1; i < sizeof(T); i++, j--)
63  result |= T((*ret)[i]) << (j * 8);
64  return result;
65  }
66 
67  bool has_irq_ = false;
68  uint8_t irq_pin_number_;
69  GPIOPin *irq_pin_{nullptr};
70  bool is_setup_{false};
76 };
77 
78 } // namespace ade7953
79 } // namespace esphome
bool write_bytes_raw(const uint8_t *data, uint8_t len)
Definition: i2c.h:245
sensor::Sensor * current_a_sensor_
Definition: ade7953.h:72
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:174
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:210
sensor::Sensor * current_b_sensor_
Definition: ade7953.h:73
void set_timeout(uint32_t timeout, std::function< void()> &&f)
Definition: component.cpp:106
sensor::Sensor * active_power_b_sensor_
Definition: ade7953.h:75
void dump_config() override
Definition: ade7953.cpp:9
void setup() override
Definition: ade7953.h:26
virtual void setup()
Setup the pin mode.
optional< T > ade_read_(uint16_t reg)
Definition: ade7953.h:53
void set_current_a_sensor(sensor::Sensor *current_a_sensor)
Definition: ade7953.h:17
Library based on https://github.com/miguelbalboa/rfid and adapted to ESPHome by . ...
Definition: a4988.cpp:4
void set_irq_pin(uint8_t irq_pin)
Definition: ade7953.h:12
Base-class for all sensors.
Definition: sensor.h:33
bool ade_write_(uint16_t reg, T value)
Definition: ade7953.h:45
void set_current_b_sensor(sensor::Sensor *current_b_sensor)
Definition: ade7953.h:18
sensor::Sensor * voltage_sensor_
Definition: ade7953.h:71
sensor::Sensor * active_power_a_sensor_
Definition: ade7953.h:74
All components doing communication on the I2C bus should subclass I2CDevice.
Definition: i2c.h:163
A high-level abstraction class that can expose a pin together with useful options like pinMode...
Definition: esphal.h:69
void update() override
Definition: ade7953.cpp:30