mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-05-19 20:53:26 +02:00
ac0d921413
* Socket refactor and SSL * esp-idf temp * Fixes * Echo component and noise * Add noise API transport support * Updates * ESP-IDF * Complete * Fixes * Fixes * Versions update * New i2c APIs * Complete i2c refactor * SPI migration * Revert ESP Preferences migration, too complex for now * OTA support * Remove echo again * Remove ssl again * GPIOFlags updates * Rename esphal and ICACHE_RAM_ATTR * Make ESP32 arduino compilable again * Fix GPIO flags * Complete pin registry refactor and fixes * Fixes to make test1 compile * Remove sdkconfig file * Ignore sdkconfig file * Fixes in reviewing * Make test2 compile * Make test4 compile * Make test5 compile * Run clang-format * Fix lint errors * Use esp-idf APIs instead of btStart * Another round of fixes * Start implementing ESP8266 * Make test3 compile * Guard esp8266 code * Lint * Reformat * Fixes * Fixes v2 * more fixes * ESP-IDF tidy target * Convert ARDUINO_ARCH_ESPxx * Update WiFiSignalSensor * Update time ifdefs * OTA needs millis from hal * RestartSwitch needs delay from hal * ESP-IDF Uart * Fix OTA blank password * Allow setting sdkconfig * Fix idf partitions and allow setting sdkconfig from yaml * Re-add read/write compat APIs and fix esp8266 uart * Fix esp8266 store log strings in flash * Fix ESP32 arduino preferences not initialized * Update ifdefs * Change how sdkconfig change is detected * Add checks to ci-custom and fix them * Run clang-format * Add esp-idf clang-tidy target and fix errors * Fixes from clang-tidy idf round 2 * Fixes from compiling tests with esp-idf * Run clang-format * Switch test5.yaml to esp-idf * Implement ESP8266 Preferences * Lint * Re-do PIO package version selection a bit * Fix arduinoespressif32 package version * Fix unit tests * Lint * Lint fixes * Fix readv/writev not defined * Fix graphing component * Re-add all old options from core/config.py Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
111 lines
3.0 KiB
C++
111 lines
3.0 KiB
C++
#include "nextion_sensor.h"
|
|
#include "esphome/core/util.h"
|
|
#include "esphome/core/log.h"
|
|
|
|
namespace esphome {
|
|
namespace nextion {
|
|
|
|
static const char *const TAG = "nextion_sensor";
|
|
|
|
void NextionSensor::process_sensor(const std::string &variable_name, int state) {
|
|
if (!this->nextion_->is_setup())
|
|
return;
|
|
|
|
if (this->wave_chan_id_ == UINT8_MAX && this->variable_name_ == variable_name) {
|
|
this->publish_state(state);
|
|
ESP_LOGD(TAG, "Processed sensor \"%s\" state %d", variable_name.c_str(), state);
|
|
}
|
|
}
|
|
|
|
void NextionSensor::add_to_wave_buffer(float state) {
|
|
this->needs_to_send_update_ = true;
|
|
|
|
int wave_state = (int) ((state / (float) this->wave_maxvalue_) * 100);
|
|
|
|
wave_buffer_.push_back(wave_state);
|
|
|
|
if (this->wave_buffer_.size() > this->wave_max_length_) {
|
|
this->wave_buffer_.erase(this->wave_buffer_.begin());
|
|
}
|
|
}
|
|
|
|
void NextionSensor::update() {
|
|
if (!this->nextion_->is_setup())
|
|
return;
|
|
|
|
if (this->wave_chan_id_ == UINT8_MAX) {
|
|
this->nextion_->add_to_get_queue(shared_from_this());
|
|
} else {
|
|
if (this->send_last_value_) {
|
|
this->add_to_wave_buffer(this->last_value_);
|
|
}
|
|
|
|
this->wave_update_();
|
|
}
|
|
}
|
|
|
|
void NextionSensor::set_state(float state, bool publish, bool send_to_nextion) {
|
|
if (!this->nextion_->is_setup())
|
|
return;
|
|
|
|
if (std::isnan(state))
|
|
return;
|
|
|
|
if (this->wave_chan_id_ == UINT8_MAX) {
|
|
if (send_to_nextion) {
|
|
if (this->nextion_->is_sleeping() || !this->visible_) {
|
|
this->needs_to_send_update_ = true;
|
|
} else {
|
|
this->needs_to_send_update_ = false;
|
|
|
|
if (this->precision_ > 0) {
|
|
double to_multiply = pow(10, this->precision_);
|
|
int state_value = (int) (state * to_multiply);
|
|
|
|
this->nextion_->add_no_result_to_queue_with_set(shared_from_this(), (int) state_value);
|
|
} else {
|
|
this->nextion_->add_no_result_to_queue_with_set(shared_from_this(), (int) state);
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
if (this->send_last_value_) {
|
|
this->last_value_ = state; // Update will handle setting the buffer
|
|
} else {
|
|
this->add_to_wave_buffer(state);
|
|
}
|
|
}
|
|
|
|
if (this->wave_chan_id_ == UINT8_MAX) {
|
|
if (publish) {
|
|
this->publish_state(state);
|
|
} else {
|
|
this->raw_state = state;
|
|
this->state = state;
|
|
this->has_state_ = true;
|
|
}
|
|
}
|
|
this->update_component_settings();
|
|
|
|
ESP_LOGN(TAG, "Wrote state for sensor \"%s\" state %lf", this->variable_name_.c_str(), state);
|
|
}
|
|
|
|
void NextionSensor::wave_update_() {
|
|
if (this->nextion_->is_sleeping() || this->wave_buffer_.empty()) {
|
|
return;
|
|
}
|
|
|
|
#ifdef NEXTION_PROTOCOL_LOG
|
|
size_t buffer_to_send =
|
|
this->wave_buffer_.size() < 255 ? this->wave_buffer_.size() : 255; // ADDT command can only send 255
|
|
|
|
ESP_LOGN(TAG, "wave_update send %zu of %zu value(s) to wave nextion component id %d and wave channel id %d",
|
|
buffer_to_send, this->wave_buffer_.size(), this->component_id_, this->wave_chan_id_);
|
|
#endif
|
|
|
|
this->nextion_->add_addt_command_to_queue(shared_from_this());
|
|
}
|
|
|
|
} // namespace nextion
|
|
} // namespace esphome
|