ESPHome  2023.5.5
cs5460a.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 namespace esphome {
9 namespace cs5460a {
10 
12  CMD_SYNC0 = 0xfe,
13  CMD_SYNC1 = 0xff,
16  CMD_POWER_UP = 0xa0,
20  CMD_READ = 0x00,
21  CMD_WRITE = 0x40,
22 };
23 
25  REG_CONFIG = 0x00,
26  REG_IDCOFF = 0x01,
27  REG_IGN = 0x02,
28  REG_VDCOFF = 0x03,
29  REG_VGN = 0x04,
32  REG_I = 0x07,
33  REG_V = 0x08,
34  REG_P = 0x09,
35  REG_E = 0x0a,
36  REG_IRMS = 0x0b,
37  REG_VRMS = 0x0c,
38  REG_TBC = 0x0d,
39  REG_POFF = 0x0e,
40  REG_STATUS = 0x0f,
41  REG_IACOFF = 0x10,
42  REG_VACOFF = 0x11,
43  REG_MASK = 0x1a,
44  REG_CONTROL = 0x1c,
45 };
46 
52 };
53 
54 class CS5460AComponent : public Component,
55  public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW,
56  spi::CLOCK_PHASE_LEADING, spi::DATA_RATE_1MHZ> {
57  public:
58  void set_samples(uint32_t samples) { samples_ = samples; }
59  void set_phase_offset(int8_t phase_offset) { phase_offset_ = phase_offset; }
60  void set_pga_gain(CS5460APGAGain pga_gain) { pga_gain_ = pga_gain; }
61  void set_gains(float current_gain, float voltage_gain) {
62  current_gain_ = current_gain;
63  voltage_gain_ = voltage_gain;
64  }
65  void set_hpf_enable(bool current_hpf, bool voltage_hpf) {
66  current_hpf_ = current_hpf;
67  voltage_hpf_ = voltage_hpf;
68  }
69  void set_pulse_energy_wh(float pulse_energy_wh) { pulse_energy_wh_ = pulse_energy_wh; }
70  void set_current_sensor(sensor::Sensor *current_sensor) { current_sensor_ = current_sensor; }
71  void set_voltage_sensor(sensor::Sensor *voltage_sensor) { voltage_sensor_ = voltage_sensor; }
72  void set_power_sensor(sensor::Sensor *power_sensor) { power_sensor_ = power_sensor; }
73 
74  void restart() { restart_(); }
75 
76  void setup() override;
77  void loop() override {}
78  float get_setup_priority() const override { return setup_priority::DATA; }
79  void dump_config() override;
80 
81  protected:
82  uint32_t samples_;
83  int8_t phase_offset_;
93 
94  void write_register_(enum CS5460ARegister addr, uint32_t value);
95  uint32_t read_register_(uint8_t addr);
96  bool softreset_();
97  void hw_init_();
98  void restart_();
99  void started_();
100  void schedule_next_check_();
101  bool check_status_();
102 
106  float pulse_freq_;
107  uint32_t expect_data_ts_;
108  uint32_t prev_raw_current_{0};
109  uint32_t prev_raw_energy_{0};
110 };
111 
112 template<typename... Ts> class CS5460ARestartAction : public Action<Ts...> {
113  public:
114  CS5460ARestartAction(CS5460AComponent *cs5460a) : cs5460a_(cs5460a) {}
115 
116  void play(Ts... x) override { cs5460a_->restart(); }
117 
118  protected:
120 };
121 
122 } // namespace cs5460a
123 } // namespace esphome
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:18
void set_phase_offset(int8_t phase_offset)
Definition: cs5460a.h:59
CS5460ARestartAction(CS5460AComponent *cs5460a)
Definition: cs5460a.h:114
void set_hpf_enable(bool current_hpf, bool voltage_hpf)
Definition: cs5460a.h:65
void set_pulse_energy_wh(float pulse_energy_wh)
Definition: cs5460a.h:69
CS5460APGAGain
Enum listing the current channel aplifiergain settings for the CS5460A.
Definition: cs5460a.h:49
void set_power_sensor(sensor::Sensor *power_sensor)
Definition: cs5460a.h:72
float get_setup_priority() const override
Definition: cs5460a.h:78
uint32_t read_register_(uint8_t addr)
Definition: cs5460a.cpp:16
void write_register_(enum CS5460ARegister addr, uint32_t value)
Definition: cs5460a.cpp:9
void set_voltage_sensor(sensor::Sensor *voltage_sensor)
Definition: cs5460a.h:71
void set_current_sensor(sensor::Sensor *current_sensor)
Definition: cs5460a.h:70
sensor::Sensor * voltage_sensor_
Definition: cs5460a.h:91
sensor::Sensor * current_sensor_
Definition: cs5460a.h:90
sensor::Sensor * power_sensor_
Definition: cs5460a.h:92
void set_gains(float current_gain, float voltage_gain)
Definition: cs5460a.h:61
Definition: a4988.cpp:4
void set_pga_gain(CS5460APGAGain pga_gain)
Definition: cs5460a.h:60
void set_samples(uint32_t samples)
Definition: cs5460a.h:58
Base-class for all sensors.
Definition: sensor.h:57
void play(Ts... x) override
Definition: cs5460a.h:116