espHome-NBS-files/external_components/analog_orp/analog_orp.h

78 lines
2.2 KiB
C
Raw Normal View History

2024-05-10 10:37:19 +00:00
#pragma once
#include "esphome/components/sensor/sensor.h"
#include "esphome/core/component.h"
#include "esphome/core/gpio.h"
2024-05-11 08:54:14 +00:00
#include "esphome/core/automation.h"
#include "Arduino.h"
2024-05-10 10:37:19 +00:00
namespace esphome {
namespace analog_orp {
2024-05-11 08:54:14 +00:00
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;
};
2024-05-10 10:37:19 +00:00
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);
2024-05-11 08:54:14 +00:00
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();
2024-05-10 10:37:19 +00:00
void setup() override;
void update() override;
float sample();
void dump_config() override;
float get_setup_priority() const;
protected:
2024-05-11 08:54:14 +00:00
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); });
}
2024-05-10 10:37:19 +00:00
};
}
}