ESPHome  2024.11.0
hlw8012.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/hal.h"
7 
8 #include <cinttypes>
9 
10 namespace esphome {
11 namespace hlw8012 {
12 
14 
19 };
20 
21 #ifdef HAS_PCNT
22 #define USE_PCNT true
23 #else
24 #define USE_PCNT false
25 #endif
26 
28  public:
30  : cf_store_(*pulse_counter::get_storage(USE_PCNT)), cf1_store_(*pulse_counter::get_storage(USE_PCNT)) {}
31 
32  void setup() override;
33  void dump_config() override;
34  float get_setup_priority() const override;
35  void update() override;
36 
37  void set_initial_mode(HLW8012InitialMode initial_mode) {
39  }
40  void set_sensor_model(HLW8012SensorModels sensor_model) { sensor_model_ = sensor_model; }
41  void set_change_mode_every(uint32_t change_mode_every) { change_mode_every_ = change_mode_every; }
42  void set_current_resistor(float current_resistor) { current_resistor_ = current_resistor; }
43  void set_voltage_divider(float voltage_divider) { voltage_divider_ = voltage_divider; }
44  void set_sel_pin(GPIOPin *sel_pin) { sel_pin_ = sel_pin; }
45  void set_cf_pin(InternalGPIOPin *cf_pin) { cf_pin_ = cf_pin; }
46  void set_cf1_pin(InternalGPIOPin *cf1_pin) { cf1_pin_ = cf1_pin; }
47  void set_voltage_sensor(sensor::Sensor *voltage_sensor) { voltage_sensor_ = voltage_sensor; }
48  void set_current_sensor(sensor::Sensor *current_sensor) { current_sensor_ = current_sensor; }
49  void set_power_sensor(sensor::Sensor *power_sensor) { power_sensor_ = power_sensor; }
50  void set_energy_sensor(sensor::Sensor *energy_sensor) { energy_sensor_ = energy_sensor; }
51 
52  protected:
53  uint32_t nth_value_{0};
54  bool current_mode_{false};
55  uint32_t change_mode_at_{0};
56  uint32_t change_mode_every_{8};
57  float current_resistor_{0.001};
58  float voltage_divider_{2351};
60  uint64_t cf_total_pulses_{0};
70 
71  float voltage_multiplier_{0.0f};
72  float current_multiplier_{0.0f};
73  float power_multiplier_{0.0f};
74 };
75 
76 } // namespace hlw8012
77 } // namespace esphome
void set_power_sensor(sensor::Sensor *power_sensor)
Definition: hlw8012.h:49
void set_initial_mode(HLW8012InitialMode initial_mode)
Definition: hlw8012.h:37
void set_current_sensor(sensor::Sensor *current_sensor)
Definition: hlw8012.h:48
void set_voltage_divider(float voltage_divider)
Definition: hlw8012.h:43
PulseCounterStorageBase * get_storage(bool hw_pcnt)
void set_sel_pin(GPIOPin *sel_pin)
Definition: hlw8012.h:44
This class simplifies creating components that periodically check a state.
Definition: component.h:283
sensor::Sensor * energy_sensor_
Definition: hlw8012.h:69
sensor::Sensor * power_sensor_
Definition: hlw8012.h:68
InternalGPIOPin * cf_pin_
Definition: hlw8012.h:62
void set_energy_sensor(sensor::Sensor *energy_sensor)
Definition: hlw8012.h:50
void set_cf_pin(InternalGPIOPin *cf_pin)
Definition: hlw8012.h:45
pulse_counter::PulseCounterStorageBase & cf_store_
Definition: hlw8012.h:63
HLW8012SensorModels sensor_model_
Definition: hlw8012.h:59
void set_current_resistor(float current_resistor)
Definition: hlw8012.h:42
void set_change_mode_every(uint32_t change_mode_every)
Definition: hlw8012.h:41
sensor::Sensor * voltage_sensor_
Definition: hlw8012.h:66
float get_setup_priority() const override
Definition: hlw8012.cpp:50
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
sensor::Sensor * current_sensor_
Definition: hlw8012.h:67
pulse_counter::PulseCounterStorageBase & cf1_store_
Definition: hlw8012.h:65
Base-class for all sensors.
Definition: sensor.h:57
InternalGPIOPin * cf1_pin_
Definition: hlw8012.h:64
void set_voltage_sensor(sensor::Sensor *voltage_sensor)
Definition: hlw8012.h:47
void set_cf1_pin(InternalGPIOPin *cf1_pin)
Definition: hlw8012.h:46
void set_sensor_model(HLW8012SensorModels sensor_model)
Definition: hlw8012.h:40