1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-29 22:24:26 +00:00

Revert HLW8012 to use pulse counter (#537)

This commit is contained in:
Otto Winter
2019-05-10 22:13:43 +02:00
committed by GitHub
parent 7bab279c6a
commit 23dcfe5075
5 changed files with 29 additions and 27 deletions

View File

@@ -28,7 +28,9 @@ void ICACHE_RAM_ATTR PulseCounterStorage::gpio_intr(PulseCounterStorage *arg) {
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->pin->attach_interrupt(PulseCounterStorage::gpio_intr, this, CHANGE);
return true;
@@ -42,7 +44,9 @@ pulse_counter_t PulseCounterStorage::read_raw_value() {
#endif
#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;
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() {
ESP_LOGCONFIG(TAG, "Setting up pulse counter '%s'...", this->name_.c_str());
this->pin_->setup();
this->storage_.pin = this->pin_;
if (!this->storage_.pulse_counter_setup()) {
if (!this->storage_.pulse_counter_setup(this->pin_)) {
this->mark_failed();
return;
}

View File

@@ -25,7 +25,7 @@ using pulse_counter_t = int32_t;
#endif
struct PulseCounterStorage {
bool pulse_counter_setup();
bool pulse_counter_setup(GPIOPin *pin);
pulse_counter_t read_raw_value();
static void gpio_intr(PulseCounterStorage *arg);
@@ -42,9 +42,9 @@ struct PulseCounterStorage {
#ifdef ARDUINO_ARCH_ESP8266
ISRInternalGPIOPin *isr_pin;
#endif
PulseCounterCountMode rising_edge_mode{};
PulseCounterCountMode falling_edge_mode{};
uint32_t filter_us{};
PulseCounterCountMode rising_edge_mode{PULSE_COUNTER_INCREMENT};
PulseCounterCountMode falling_edge_mode{PULSE_COUNTER_DISABLE};
uint32_t filter_us{0};
pulse_counter_t last_value{0};
};