Added Averager
This commit is contained in:
@@ -5,25 +5,65 @@ namespace analog_orp {
|
||||
|
||||
static const char *const TAG = "sensor.analog_orp";
|
||||
|
||||
FloatAverager::FloatAverager(size_t size): size_(size), vector_(size_){
|
||||
this->index_ = 0;
|
||||
}
|
||||
|
||||
void FloatAverager::add_value(float value){
|
||||
vector_.at(index_++) = value;
|
||||
if(index_ == size_){
|
||||
index_ = 0;
|
||||
has_filled_once_ = true;
|
||||
}
|
||||
}
|
||||
float FloatAverager::create_average(){
|
||||
float sum = 0;
|
||||
for(auto point : vector_){
|
||||
sum += point;
|
||||
}
|
||||
return sum / (float) size_;
|
||||
}
|
||||
bool FloatAverager::isFull(){
|
||||
return index_ == 0;
|
||||
}
|
||||
bool FloatAverager::has_filled_once(){
|
||||
return has_filled_once_;
|
||||
}
|
||||
|
||||
void ChlorineSensor::set_pin(InternalGPIOPin *pin){
|
||||
this->pin = pin;
|
||||
this->pin_ = pin;
|
||||
}
|
||||
|
||||
void ChlorineSensor::set_zero_point(int zero_point){
|
||||
this->zero_point_int = zero_point;
|
||||
this->zero_point = ((float) zero_point / 1024.0f) * 3.3f; // Base Value for Clorine in Volt
|
||||
this->zero_point_int_ = zero_point;
|
||||
this->zero_point_ = ((float) zero_point / 1024.0f) * 3.3f; // Base Value for Clorine in Volt
|
||||
}
|
||||
|
||||
void ChlorineSensor::set_print_raw(bool print_raw){
|
||||
this->print_raw = print_raw;
|
||||
this->print_raw_ = print_raw;
|
||||
}
|
||||
void ChlorineSensor::set_inverted(bool inverted){
|
||||
this->inverted = inverted;
|
||||
this->inverted_ = inverted;
|
||||
}
|
||||
|
||||
void ChlorineSensor::init_averager(int mesurements, int send_average_every){
|
||||
this->averager_ = new FloatAverager(mesurements);
|
||||
this->send_averager_every_ = send_average_every;
|
||||
}
|
||||
|
||||
void ChlorineSensor::add_read_callback(std::function<void(float)> &&callback){
|
||||
this->on_value_read_.add(std::move(callback));
|
||||
}
|
||||
void ChlorineSensor::add_average_change_callback(std::function<void(float)> &&callback){
|
||||
this->on_average_changed_.add(std::move(callback));
|
||||
}
|
||||
void ChlorineSensor::add_new_value_callback(std::function<void(float)> &&callback){
|
||||
this->new_value_callback_.add(std::move(callback));
|
||||
}
|
||||
|
||||
void ChlorineSensor::setup(){
|
||||
ESP_LOGCONFIG(TAG, "Setting up AnalogOrp '%s'...", this->get_name().c_str());
|
||||
pin->setup();
|
||||
pin_->setup();
|
||||
ESP_LOGCONFIG(TAG, "AnalogOrp '%s' setup finished!", this->get_name().c_str());
|
||||
}
|
||||
|
||||
@@ -31,6 +71,31 @@ void ChlorineSensor::setup(){
|
||||
void ChlorineSensor::update() {
|
||||
float value_v = this->sample();
|
||||
ESP_LOGV(TAG, "'%s': Got orp value=%.4fmV", this->get_name().c_str(), value_v);
|
||||
if(averager_ != nullptr){
|
||||
averager_->add_value(value_v);
|
||||
on_value_read_.call(value_v);
|
||||
|
||||
if(++averager_send_counter_ >= send_averager_every_) averager_send_counter_ = 0;
|
||||
|
||||
if(!averager_->has_filled_once()) return;
|
||||
float average = averager_->create_average();
|
||||
on_average_changed_.call(average);
|
||||
|
||||
if(send_averager_every_ == -1){
|
||||
if(averager_->isFull()){
|
||||
this->publish_state(average);
|
||||
}
|
||||
}
|
||||
else{
|
||||
if(averager_->isFull()){
|
||||
this->new_value_callback_.call(average);
|
||||
}
|
||||
if(averager_send_counter_ == 0){
|
||||
this->publish_state(average);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
this->publish_state(value_v);
|
||||
}
|
||||
|
||||
@@ -39,10 +104,10 @@ float ChlorineSensor::get_setup_priority() const { return setup_priority::DATA;
|
||||
void ChlorineSensor::dump_config(){
|
||||
LOG_SENSOR("", "AnalogOrp Sensor", this);
|
||||
LOG_UPDATE_INTERVAL(this);
|
||||
LOG_PIN(" Pin: ", pin);
|
||||
ESP_LOGCONFIG(TAG, " Zero Point: %d", zero_point_int);
|
||||
ESP_LOGCONFIG(TAG, " Inverted: %s", inverted ? "true" : "false");
|
||||
ESP_LOGCONFIG(TAG, " Print Raw: %s", print_raw ? "true" : "false");
|
||||
LOG_PIN(" Pin: ", pin_);
|
||||
ESP_LOGCONFIG(TAG, " Zero Point: %d", zero_point_int_);
|
||||
ESP_LOGCONFIG(TAG, " Inverted: %s", inverted_ ? "true" : "false");
|
||||
ESP_LOGCONFIG(TAG, " Print Raw: %s", print_raw_ ? "true" : "false");
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -50,15 +115,15 @@ Nullwert : 680
|
||||
*/
|
||||
float ChlorineSensor::sample(){
|
||||
|
||||
float raw_read = (float) analogRead(pin->get_pin());
|
||||
if(print_raw){
|
||||
float raw_read = (float) analogRead(pin_->get_pin());
|
||||
if(print_raw_){
|
||||
ESP_LOGD(TAG, "analogRead read a raw value of: %.0f", raw_read);
|
||||
}
|
||||
|
||||
float raw_voltage = (raw_read / 1024.0f) * 3.3f;
|
||||
float messured_voltage = raw_voltage - zero_point;
|
||||
float messured_voltage = raw_voltage - zero_point_;
|
||||
|
||||
if(inverted){
|
||||
if(inverted_){
|
||||
messured_voltage = messured_voltage * -1.0f;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user