78 lines
2.2 KiB
C++
78 lines
2.2 KiB
C++
#pragma once
|
|
|
|
#include "esphome/components/sensor/sensor.h"
|
|
#include "esphome/core/component.h"
|
|
#include "esphome/core/gpio.h"
|
|
#include "esphome/core/automation.h"
|
|
#include "Arduino.h"
|
|
|
|
namespace esphome {
|
|
namespace analog_orp {
|
|
|
|
class FloatAverager{
|
|
public:
|
|
FloatAverager(size_t size);
|
|
void add_value(float value);
|
|
bool isFull();
|
|
float create_average();
|
|
bool has_filled_once();
|
|
protected:
|
|
size_t size_;
|
|
std::vector<float> vector_;
|
|
size_t index_;
|
|
bool has_filled_once_ = false;
|
|
};
|
|
|
|
class ChlorineSensor: public PollingComponent, public sensor::Sensor {
|
|
public:
|
|
void set_pin(InternalGPIOPin *pin);
|
|
void set_zero_point(int zero_point);
|
|
void set_print_raw(bool print_raw);
|
|
void set_inverted(bool inverted);
|
|
void add_read_callback(std::function<void(float)> &&callback);
|
|
void add_average_change_callback(std::function<void(float)> &&callback);
|
|
void add_new_value_callback(std::function<void(float)> &&callback);
|
|
void init_averager(int mesurements, int send_average_every);
|
|
bool has_averager();
|
|
void setup() override;
|
|
void update() override;
|
|
float sample();
|
|
void dump_config() override;
|
|
float get_setup_priority() const;
|
|
protected:
|
|
InternalGPIOPin *pin_;
|
|
CallbackManager<void(float)> on_value_read_;
|
|
CallbackManager<void(float)> on_average_changed_;
|
|
CallbackManager<void(float)> new_value_callback_;
|
|
bool inverted_;
|
|
bool print_raw_;
|
|
float zero_point_;
|
|
int zero_point_int_;
|
|
FloatAverager *averager_;
|
|
bool use_averager_ = false;
|
|
int send_averager_every_ = -1;
|
|
int averager_send_counter_ = 0;
|
|
};
|
|
|
|
class ChlorineValueReadTrigger : public Trigger<float> {
|
|
public:
|
|
explicit ChlorineValueReadTrigger(ChlorineSensor *parent) {
|
|
parent->add_read_callback([this](int value) { this->trigger(value); });
|
|
}
|
|
};
|
|
class ChlorineAverageChangeTrigger : public Trigger<float> {
|
|
public:
|
|
explicit ChlorineAverageChangeTrigger(ChlorineSensor *parent) {
|
|
parent->add_average_change_callback([this](int value) { this->trigger(value); });
|
|
}
|
|
};
|
|
class ChlorineNewValueTrigger : public Trigger<float> {
|
|
public:
|
|
explicit ChlorineNewValueTrigger(ChlorineSensor *parent) {
|
|
parent->add_new_value_callback([this](int value) { this->trigger(value); });
|
|
}
|
|
};
|
|
|
|
|
|
}
|
|
} |