mirror of
https://github.com/esphome/esphome.git
synced 2024-11-29 18:24:13 +01:00
Revert HLW8012 to use pulse counter (#537)
This commit is contained in:
parent
7bab279c6a
commit
23dcfe5075
5 changed files with 29 additions and 27 deletions
|
@ -13,8 +13,8 @@ void HLW8012Component::setup() {
|
||||||
ESP_LOGCONFIG(TAG, "Setting up HLW8012...");
|
ESP_LOGCONFIG(TAG, "Setting up HLW8012...");
|
||||||
this->sel_pin_->setup();
|
this->sel_pin_->setup();
|
||||||
this->sel_pin_->digital_write(this->current_mode_);
|
this->sel_pin_->digital_write(this->current_mode_);
|
||||||
this->cf_store_.setup(this->cf_pin_);
|
this->cf_store_.pulse_counter_setup(this->cf_pin_);
|
||||||
this->cf1_store_.setup(this->cf1_pin_);
|
this->cf1_store_.pulse_counter_setup(this->cf1_pin_);
|
||||||
}
|
}
|
||||||
void HLW8012Component::dump_config() {
|
void HLW8012Component::dump_config() {
|
||||||
ESP_LOGCONFIG(TAG, "HLW8012:");
|
ESP_LOGCONFIG(TAG, "HLW8012:");
|
||||||
|
@ -32,18 +32,18 @@ void HLW8012Component::dump_config() {
|
||||||
float HLW8012Component::get_setup_priority() const { return setup_priority::DATA; }
|
float HLW8012Component::get_setup_priority() const { return setup_priority::DATA; }
|
||||||
void HLW8012Component::update() {
|
void HLW8012Component::update() {
|
||||||
// HLW8012 has 50% duty cycle
|
// HLW8012 has 50% duty cycle
|
||||||
const uint32_t last_rise_cf = this->cf_store_.get_last_rise();
|
pulse_counter::pulse_counter_t raw_cf = this->cf_store_.read_raw_value();
|
||||||
const uint32_t last_rise_cf1 = this->cf1_store_.get_last_rise();
|
pulse_counter::pulse_counter_t raw_cf1 = this->cf1_store_.read_raw_value();
|
||||||
const uint32_t now = micros();
|
float cf_hz = raw_cf / (this->get_update_interval() / 1000.0f);
|
||||||
float full_cycle_cf = this->cf_store_.get_pulse_width_s() * 2;
|
if (raw_cf <= 1) {
|
||||||
float full_cycle_cf1 = this->cf1_store_.get_pulse_width_s() * 2;
|
// don't count single pulse as power
|
||||||
float cf_hz = 0.0f, cf1_hz = 0.0f;
|
cf_hz = 0.0f;
|
||||||
auto update_interval_micros = static_cast<uint32_t>(this->update_interval_ * 1e3f);
|
}
|
||||||
|
float cf1_hz = raw_cf1 / (this->get_update_interval() / 1000.0f);
|
||||||
if (full_cycle_cf != 0.0f && now - last_rise_cf < update_interval_micros * 3)
|
if (raw_cf1 <= 1) {
|
||||||
cf_hz = 1.0f / full_cycle_cf;
|
// don't count single pulse as anything
|
||||||
if (full_cycle_cf1 != 0.0f && now - last_rise_cf1 < update_interval_micros * 3)
|
cf1_hz = 0.0f;
|
||||||
cf1_hz = 1.0f / full_cycle_cf1;
|
}
|
||||||
|
|
||||||
if (this->nth_value_++ < 2) {
|
if (this->nth_value_++ < 2) {
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
#include "esphome/core/component.h"
|
#include "esphome/core/component.h"
|
||||||
#include "esphome/core/esphal.h"
|
#include "esphome/core/esphal.h"
|
||||||
#include "esphome/components/sensor/sensor.h"
|
#include "esphome/components/sensor/sensor.h"
|
||||||
#include "esphome/components/pulse_width/pulse_width.h"
|
#include "esphome/components/pulse_counter/pulse_counter_sensor.h"
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace hlw8012 {
|
namespace hlw8012 {
|
||||||
|
@ -34,9 +34,9 @@ class HLW8012Component : public PollingComponent {
|
||||||
float voltage_divider_{2351};
|
float voltage_divider_{2351};
|
||||||
GPIOPin *sel_pin_;
|
GPIOPin *sel_pin_;
|
||||||
GPIOPin *cf_pin_;
|
GPIOPin *cf_pin_;
|
||||||
pulse_width::PulseWidthSensorStore cf_store_;
|
pulse_counter::PulseCounterStorage cf_store_;
|
||||||
GPIOPin *cf1_pin_;
|
GPIOPin *cf1_pin_;
|
||||||
pulse_width::PulseWidthSensorStore cf1_store_;
|
pulse_counter::PulseCounterStorage cf1_store_;
|
||||||
sensor::Sensor *voltage_sensor_{nullptr};
|
sensor::Sensor *voltage_sensor_{nullptr};
|
||||||
sensor::Sensor *current_sensor_{nullptr};
|
sensor::Sensor *current_sensor_{nullptr};
|
||||||
sensor::Sensor *power_sensor_{nullptr};
|
sensor::Sensor *power_sensor_{nullptr};
|
||||||
|
|
|
@ -6,7 +6,7 @@ from esphome.const import CONF_CHANGE_MODE_EVERY, CONF_CURRENT, \
|
||||||
CONF_CURRENT_RESISTOR, CONF_ID, CONF_POWER, CONF_SEL_PIN, CONF_VOLTAGE, CONF_VOLTAGE_DIVIDER, \
|
CONF_CURRENT_RESISTOR, CONF_ID, CONF_POWER, CONF_SEL_PIN, CONF_VOLTAGE, CONF_VOLTAGE_DIVIDER, \
|
||||||
ICON_FLASH, UNIT_VOLT, UNIT_AMPERE, UNIT_WATT
|
ICON_FLASH, UNIT_VOLT, UNIT_AMPERE, UNIT_WATT
|
||||||
|
|
||||||
AUTO_LOAD = ['pulse_width']
|
AUTO_LOAD = ['pulse_counter']
|
||||||
|
|
||||||
hlw8012_ns = cg.esphome_ns.namespace('hlw8012')
|
hlw8012_ns = cg.esphome_ns.namespace('hlw8012')
|
||||||
HLW8012Component = hlw8012_ns.class_('HLW8012Component', cg.PollingComponent)
|
HLW8012Component = hlw8012_ns.class_('HLW8012Component', cg.PollingComponent)
|
||||||
|
|
|
@ -28,7 +28,9 @@ void ICACHE_RAM_ATTR PulseCounterStorage::gpio_intr(PulseCounterStorage *arg) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bool PulseCounterStorage::pulse_counter_setup() {
|
bool PulseCounterStorage::pulse_counter_setup(GPIOPin *pin) {
|
||||||
|
this->pin = pin;
|
||||||
|
this->pin->setup();
|
||||||
this->isr_pin = this->pin->to_isr();
|
this->isr_pin = this->pin->to_isr();
|
||||||
this->pin->attach_interrupt(PulseCounterStorage::gpio_intr, this, CHANGE);
|
this->pin->attach_interrupt(PulseCounterStorage::gpio_intr, this, CHANGE);
|
||||||
return true;
|
return true;
|
||||||
|
@ -42,7 +44,9 @@ pulse_counter_t PulseCounterStorage::read_raw_value() {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef ARDUINO_ARCH_ESP32
|
#ifdef ARDUINO_ARCH_ESP32
|
||||||
bool PulseCounterStorage::pulse_counter_setup() {
|
bool PulseCounterStorage::pulse_counter_setup(GPIOPin *pin) {
|
||||||
|
this->pin = pin;
|
||||||
|
this->pin->setup();
|
||||||
this->pcnt_unit = next_pcnt_unit;
|
this->pcnt_unit = next_pcnt_unit;
|
||||||
next_pcnt_unit = pcnt_unit_t(int(next_pcnt_unit) + 1); // NOLINT
|
next_pcnt_unit = pcnt_unit_t(int(next_pcnt_unit) + 1); // NOLINT
|
||||||
|
|
||||||
|
@ -133,9 +137,7 @@ pulse_counter_t PulseCounterStorage::read_raw_value() {
|
||||||
|
|
||||||
void PulseCounterSensor::setup() {
|
void PulseCounterSensor::setup() {
|
||||||
ESP_LOGCONFIG(TAG, "Setting up pulse counter '%s'...", this->name_.c_str());
|
ESP_LOGCONFIG(TAG, "Setting up pulse counter '%s'...", this->name_.c_str());
|
||||||
this->pin_->setup();
|
if (!this->storage_.pulse_counter_setup(this->pin_)) {
|
||||||
this->storage_.pin = this->pin_;
|
|
||||||
if (!this->storage_.pulse_counter_setup()) {
|
|
||||||
this->mark_failed();
|
this->mark_failed();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,7 @@ using pulse_counter_t = int32_t;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct PulseCounterStorage {
|
struct PulseCounterStorage {
|
||||||
bool pulse_counter_setup();
|
bool pulse_counter_setup(GPIOPin *pin);
|
||||||
pulse_counter_t read_raw_value();
|
pulse_counter_t read_raw_value();
|
||||||
|
|
||||||
static void gpio_intr(PulseCounterStorage *arg);
|
static void gpio_intr(PulseCounterStorage *arg);
|
||||||
|
@ -42,9 +42,9 @@ struct PulseCounterStorage {
|
||||||
#ifdef ARDUINO_ARCH_ESP8266
|
#ifdef ARDUINO_ARCH_ESP8266
|
||||||
ISRInternalGPIOPin *isr_pin;
|
ISRInternalGPIOPin *isr_pin;
|
||||||
#endif
|
#endif
|
||||||
PulseCounterCountMode rising_edge_mode{};
|
PulseCounterCountMode rising_edge_mode{PULSE_COUNTER_INCREMENT};
|
||||||
PulseCounterCountMode falling_edge_mode{};
|
PulseCounterCountMode falling_edge_mode{PULSE_COUNTER_DISABLE};
|
||||||
uint32_t filter_us{};
|
uint32_t filter_us{0};
|
||||||
pulse_counter_t last_value{0};
|
pulse_counter_t last_value{0};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue