ESPHome  2024.4.1
cs5460a.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include <cinttypes>
9 
10 namespace esphome {
11 namespace cs5460a {
12 
14  CMD_SYNC0 = 0xfe,
15  CMD_SYNC1 = 0xff,
18  CMD_POWER_UP = 0xa0,
22  CMD_READ = 0x00,
23  CMD_WRITE = 0x40,
24 };
25 
27  REG_CONFIG = 0x00,
28  REG_IDCOFF = 0x01,
29  REG_IGN = 0x02,
30  REG_VDCOFF = 0x03,
31  REG_VGN = 0x04,
34  REG_I = 0x07,
35  REG_V = 0x08,
36  REG_P = 0x09,
37  REG_E = 0x0a,
38  REG_IRMS = 0x0b,
39  REG_VRMS = 0x0c,
40  REG_TBC = 0x0d,
41  REG_POFF = 0x0e,
42  REG_STATUS = 0x0f,
43  REG_IACOFF = 0x10,
44  REG_VACOFF = 0x11,
45  REG_MASK = 0x1a,
46  REG_CONTROL = 0x1c,
47 };
48 
54 };
55 
56 class CS5460AComponent : public Component,
57  public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW,
58  spi::CLOCK_PHASE_LEADING, spi::DATA_RATE_1MHZ> {
59  public:
60  void set_samples(uint32_t samples) { samples_ = samples; }
61  void set_phase_offset(int8_t phase_offset) { phase_offset_ = phase_offset; }
62  void set_pga_gain(CS5460APGAGain pga_gain) { pga_gain_ = pga_gain; }
63  void set_gains(float current_gain, float voltage_gain) {
64  current_gain_ = current_gain;
65  voltage_gain_ = voltage_gain;
66  }
67  void set_hpf_enable(bool current_hpf, bool voltage_hpf) {
68  current_hpf_ = current_hpf;
69  voltage_hpf_ = voltage_hpf;
70  }
71  void set_pulse_energy_wh(float pulse_energy_wh) { pulse_energy_wh_ = pulse_energy_wh; }
72  void set_current_sensor(sensor::Sensor *current_sensor) { current_sensor_ = current_sensor; }
73  void set_voltage_sensor(sensor::Sensor *voltage_sensor) { voltage_sensor_ = voltage_sensor; }
74  void set_power_sensor(sensor::Sensor *power_sensor) { power_sensor_ = power_sensor; }
75 
76  void restart() { restart_(); }
77 
78  void setup() override;
79  void loop() override {}
80  float get_setup_priority() const override { return setup_priority::DATA; }
81  void dump_config() override;
82 
83  protected:
84  uint32_t samples_;
85  int8_t phase_offset_;
95 
96  void write_register_(enum CS5460ARegister addr, uint32_t value);
97  uint32_t read_register_(uint8_t addr);
98  bool softreset_();
99  void hw_init_();
100  void restart_();
101  void started_();
102  void schedule_next_check_();
103  bool check_status_();
104 
108  float pulse_freq_;
109  uint32_t expect_data_ts_;
110  uint32_t prev_raw_current_{0};
111  uint32_t prev_raw_energy_{0};
112 };
113 
114 template<typename... Ts> class CS5460ARestartAction : public Action<Ts...> {
115  public:
116  CS5460ARestartAction(CS5460AComponent *cs5460a) : cs5460a_(cs5460a) {}
117 
118  void play(Ts... x) override { cs5460a_->restart(); }
119 
120  protected:
122 };
123 
124 } // namespace cs5460a
125 } // namespace esphome
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
void set_phase_offset(int8_t phase_offset)
Definition: cs5460a.h:61
CS5460ARestartAction(CS5460AComponent *cs5460a)
Definition: cs5460a.h:116
void set_hpf_enable(bool current_hpf, bool voltage_hpf)
Definition: cs5460a.h:67
void set_pulse_energy_wh(float pulse_energy_wh)
Definition: cs5460a.h:71
CS5460APGAGain
Enum listing the current channel aplifiergain settings for the CS5460A.
Definition: cs5460a.h:51
uint16_t x
Definition: tt21100.cpp:17
void set_power_sensor(sensor::Sensor *power_sensor)
Definition: cs5460a.h:74
float get_setup_priority() const override
Definition: cs5460a.h:80
The SPIDevice is what components using the SPI will create.
Definition: spi.h:408
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:73
void set_current_sensor(sensor::Sensor *current_sensor)
Definition: cs5460a.h:72
sensor::Sensor * voltage_sensor_
Definition: cs5460a.h:93
sensor::Sensor * current_sensor_
Definition: cs5460a.h:92
sensor::Sensor * power_sensor_
Definition: cs5460a.h:94
void set_gains(float current_gain, float voltage_gain)
Definition: cs5460a.h:63
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void set_pga_gain(CS5460APGAGain pga_gain)
Definition: cs5460a.h:62
void set_samples(uint32_t samples)
Definition: cs5460a.h:60
Base-class for all sensors.
Definition: sensor.h:57
void play(Ts... x) override
Definition: cs5460a.h:118