ESPHome  2022.6.3
pulse_width.cpp
Go to the documentation of this file.
1 #include "pulse_width.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace pulse_width {
6 
7 static const char *const TAG = "pulse_width";
8 
10  const bool new_level = arg->pin_.digital_read();
11  const uint32_t now = micros();
12  if (new_level) {
13  arg->last_rise_ = now;
14  } else {
15  arg->last_width_ = (now - arg->last_rise_);
16  }
17 }
18 
20  LOG_SENSOR("", "Pulse Width", this)
21  LOG_UPDATE_INTERVAL(this)
22  LOG_PIN(" Pin: ", this->pin_);
23 }
25  float width = this->store_.get_pulse_width_s();
26  ESP_LOGCONFIG(TAG, "'%s' - Got pulse width %.3f s", this->name_.c_str(), width);
27  this->publish_state(width);
28 }
29 
30 } // namespace pulse_width
31 } // namespace esphome
uint32_t IRAM_ATTR HOT micros()
Definition: core.cpp:28
Store data in a class that doesn't use multiple-inheritance (vtables in flash)
Definition: pulse_width.h:11
Definition: a4988.cpp:4
static void gpio_intr(PulseWidthSensorStore *arg)
Definition: pulse_width.cpp:9