esphome/esphome/components/pulse_width/pulse_width.h
Otto Winter 8e75980ebd
Cleanup dashboard JS (#491)
* Cleanup dashboard JS

* Add vscode

* Save start_mark/end_mark

* Updates

* Updates

* Remove need for cv.nameable

It's a bit hacky but removes so much bloat from integrations

* Add enum helper

* Document APIs, and Improvements

* Fixes

* Fixes

* Update PULL_REQUEST_TEMPLATE.md

* Updates

* Updates

* Updates
2019-04-22 21:56:30 +02:00

44 lines
1.3 KiB
C++

#pragma once
#include "esphome/core/component.h"
#include "esphome/core/esphal.h"
#include "esphome/components/sensor/sensor.h"
namespace esphome {
namespace pulse_width {
/// Store data in a class that doesn't use multiple-inheritance (vtables in flash)
class PulseWidthSensorStore {
public:
void setup(GPIOPin *pin) {
pin->setup();
this->pin_ = pin->to_isr();
this->last_rise_ = micros();
pin->attach_interrupt(&PulseWidthSensorStore::gpio_intr, this, CHANGE);
}
static void gpio_intr(PulseWidthSensorStore *arg);
uint32_t get_pulse_width_us() const { return this->last_width_; }
float get_pulse_width_s() const { return this->last_width_ / 1e6f; }
uint32_t get_last_rise() const { return last_rise_; }
protected:
ISRInternalGPIOPin *pin_;
volatile uint32_t last_width_{0};
volatile uint32_t last_rise_{0};
};
class PulseWidthSensor : public sensor::Sensor, public PollingComponent {
public:
void set_pin(GPIOPin *pin) { pin_ = pin; }
void setup() override { this->store_.setup(this->pin_); }
void dump_config() override;
float get_setup_priority() const override { return setup_priority::DATA; }
void update() override;
protected:
PulseWidthSensorStore store_;
GPIOPin *pin_;
};
} // namespace pulse_width
} // namespace esphome