ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
cs5460a.h
Go to the documentation of this file.
1#pragma once
2
7
8#include <cinttypes>
9
10namespace esphome {
11namespace cs5460a {
12
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,
47};
48
55
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 void dump_config() override;
81
82 protected:
83 uint32_t samples_;
94
95 void write_register_(enum CS5460ARegister addr, uint32_t value);
96 uint32_t read_register_(uint8_t addr);
97 bool softreset_();
98 void hw_init_();
99 void restart_();
100 void started_();
102 bool check_status_();
103
109 uint32_t prev_raw_current_{0};
110 uint32_t prev_raw_energy_{0};
111};
112
113template<typename... Ts> class CS5460ARestartAction : public Action<Ts...> {
114 public:
116
117 void play(Ts... x) override { cs5460a_->restart(); }
118
119 protected:
121};
122
123} // namespace cs5460a
124} // namespace esphome
void set_gains(float current_gain, float voltage_gain)
Definition cs5460a.h:63
void set_power_sensor(sensor::Sensor *power_sensor)
Definition cs5460a.h:74
void write_register_(enum CS5460ARegister addr, uint32_t value)
Definition cs5460a.cpp:9
void set_current_sensor(sensor::Sensor *current_sensor)
Definition cs5460a.h:72
sensor::Sensor * voltage_sensor_
Definition cs5460a.h:92
void set_phase_offset(int8_t phase_offset)
Definition cs5460a.h:61
void set_pulse_energy_wh(float pulse_energy_wh)
Definition cs5460a.h:71
uint32_t read_register_(uint8_t addr)
Definition cs5460a.cpp:16
void set_samples(uint32_t samples)
Definition cs5460a.h:60
sensor::Sensor * power_sensor_
Definition cs5460a.h:93
void set_voltage_sensor(sensor::Sensor *voltage_sensor)
Definition cs5460a.h:73
void set_hpf_enable(bool current_hpf, bool voltage_hpf)
Definition cs5460a.h:67
sensor::Sensor * current_sensor_
Definition cs5460a.h:91
void set_pga_gain(CS5460APGAGain pga_gain)
Definition cs5460a.h:62
void play(Ts... x) override
Definition cs5460a.h:117
CS5460ARestartAction(CS5460AComponent *cs5460a)
Definition cs5460a.h:115
Base-class for all sensors.
Definition sensor.h:59
The SPIDevice is what components using the SPI will create.
Definition spi.h:427
CS5460APGAGain
Enum listing the current channel aplifiergain settings for the CS5460A.
Definition cs5460a.h:51
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t x
Definition tt21100.cpp:5