From ee781d789cb1ae8927a9cfd64d689060ed444471 Mon Sep 17 00:00:00 2001 From: Guido Schreuder Date: Sat, 24 Feb 2024 22:04:10 +0100 Subject: [PATCH] some refactoring --- esphome/components/ebus/__init__.py | 4 +++- .../components/ebus/binary_sensor/__init__.py | 4 +--- esphome/components/ebus/ebus.cpp | 19 +++++++++---------- esphome/components/ebus/ebus.h | 5 +++-- esphome/components/ebus/ebus_component.cpp | 16 +++++++++++++--- esphome/components/ebus/ebus_component.h | 19 +++++++++++-------- esphome/components/ebus/sensor/__init__.py | 3 +-- 7 files changed, 41 insertions(+), 29 deletions(-) diff --git a/esphome/components/ebus/__init__.py b/esphome/components/ebus/__init__.py index 9fde5dbf0a..c631660947 100644 --- a/esphome/components/ebus/__init__.py +++ b/esphome/components/ebus/__init__.py @@ -125,7 +125,9 @@ async def to_code(config): cg.add(var.set_update_interval(config[CONF_POLL_INTERVAL].total_milliseconds)) -def sensor_base_config(sensor_base, config): +def sensor_base_config(ebus, sensor_base, config): + cg.add(sensor_base.set_parent(ebus)) + cg.add(ebus.add_sensor(sensor_base)), cg.add(sensor_base.set_send_poll(config[CONF_TELEGRAM][CONF_SEND_POLL])) if CONF_ADDRESS in config[CONF_TELEGRAM]: cg.add(sensor_base.set_address(config[CONF_TELEGRAM][CONF_ADDRESS])) diff --git a/esphome/components/ebus/binary_sensor/__init__.py b/esphome/components/ebus/binary_sensor/__init__.py index 8172c2b86d..fb1290fb42 100644 --- a/esphome/components/ebus/binary_sensor/__init__.py +++ b/esphome/components/ebus/binary_sensor/__init__.py @@ -32,7 +32,5 @@ async def to_code(config): ebus = await cg.get_variable(config[CONF_EBUS_ID]) sens = await binary_sensor.new_binary_sensor(config) - sensor_base_config(sens, config) + sensor_base_config(ebus, sens, config) cg.add(sens.set_response_read_mask(config[CONF_TELEGRAM][CONF_DECODE][CONF_MASK])) - - cg.add(ebus.add_sensor(sens)) diff --git a/esphome/components/ebus/ebus.cpp b/esphome/components/ebus/ebus.cpp index 26a0232b45..6bbbf93d0e 100644 --- a/esphome/components/ebus/ebus.cpp +++ b/esphome/components/ebus/ebus.cpp @@ -217,7 +217,7 @@ void Ebus::process_received_char(unsigned char received_byte) { this->handle_response_(this->receiving_telegram_); } -void Ebus::add_send_response_handler(const std::function &send_response_handler) { +void Ebus::add_send_response_handler(std::function(Telegram &)> send_response_handler) { send_response_handlers_.push_back(send_response_handler); } @@ -232,28 +232,27 @@ void Ebus::handle_response_(Telegram &telegram) { } // response buffer - uint8_t buf[RESPONSE_BUFFER_SIZE] = {0}; - int len = 0; + std::vector reply; // find response for (auto const &handler : send_response_handlers_) { - len = handler(telegram, buf); - if (len != 0) { + reply = handler(telegram); + if (reply.size() != 0) { break; } } // we found no reponse to send - if (len == 0) { + if (reply.size() == 0 || reply.size() > RESPONSE_BUFFER_SIZE) { uart_send_char_(NACK); return; } uart_send_char_(ACK); - uint8_t crc = Elf::crc8_calc(len, 0); - uart_send_char_(len); - for (int i = 0; i < len; i++) { - crc = uart_send_char_(buf[i], true, true, crc); + uint8_t crc = Elf::crc8_calc(reply.size(), 0); + uart_send_char_(reply.size()); + for (int i = 0; i < reply.size(); i++) { + crc = uart_send_char_(reply[i], true, true, crc); } uart_send_char_(crc); } diff --git a/esphome/components/ebus/ebus.h b/esphome/components/ebus/ebus.h index 18ba794049..07d307cdef 100644 --- a/esphome/components/ebus/ebus.h +++ b/esphome/components/ebus/ebus.h @@ -3,6 +3,7 @@ #include #include #include +#include #include "telegram.h" @@ -36,7 +37,7 @@ class Ebus { } void process_received_char(unsigned char received_byte); - void add_send_response_handler(const std::function &send_response_handler); + void add_send_response_handler(std::function(Telegram &)> send_response_handler); protected: uint8_t primary_address_; @@ -47,7 +48,7 @@ class Ebus { EbusState state_ = EbusState::ARBITRATION; Telegram receiving_telegram_; SendCommand active_command_; - std::list> send_response_handlers_; + std::list(Telegram &)>> send_response_handlers_; std::function uart_send_; std::function queue_received_telegram_; diff --git a/esphome/components/ebus/ebus_component.cpp b/esphome/components/ebus/ebus_component.cpp index 9e6afdf93e..5b37481bab 100644 --- a/esphome/components/ebus/ebus_component.cpp +++ b/esphome/components/ebus/ebus_component.cpp @@ -49,8 +49,6 @@ void EbusComponent::add_sender(EbusSender *sender) { if (this->primary_address_ == SYN) { return; } - - sender->set_primary_address(this->primary_address_); this->senders_.push_back(sender); } void EbusComponent::add_receiver(EbusReceiver *receiver) { this->receivers_.push_back(receiver); } @@ -59,6 +57,7 @@ void EbusComponent::setup_queues_() { this->history_queue_ = xQueueCreate(this->history_queue_size_, sizeof(Telegram)); this->command_queue_ = xQueueCreate(this->command_queue_size_, sizeof(Telegram)); } + void EbusComponent::setup_ebus_() { this->ebus_ = make_unique(); this->ebus_->set_primary_address(this->primary_address_); @@ -77,6 +76,17 @@ void EbusComponent::setup_ebus_() { } }); + this->ebus_->add_send_response_handler([&](Telegram &telegram) { + std::vector reply = {}; + for (auto const &receiver : this->receivers_) { + reply = receiver->reply(telegram); + if (reply.size() != 0) { + break; + } + } + return reply; + }); + this->ebus_->set_dequeue_command_function([&](void *const command) { BaseType_t x_higher_priority_task_woken = pdFALSE; if (xQueueReceiveFromISR(this->command_queue_, command, &x_higher_priority_task_woken)) { @@ -187,7 +197,7 @@ optional EbusSensorBase::prepare_command() { if (this->send_poll_) { command = SendCommand( // - this->primary_address_, this->address_, this->command_, this->payload_.size(), &this->payload_[0]); + this->parent_->get_primary_address(), this->address_, this->command_, this->payload_.size(), &this->payload_[0]); } return command; } diff --git a/esphome/components/ebus/ebus_component.h b/esphome/components/ebus/ebus_component.h index 20ce8e73b1..ef7a2ab087 100644 --- a/esphome/components/ebus/ebus_component.h +++ b/esphome/components/ebus/ebus_component.h @@ -18,28 +18,28 @@ namespace ebus { static const char *const TAG = "ebus"; +class EbusComponent; + class EbusReceiver { public: - EbusReceiver() {} virtual void process_received(Telegram) = 0; + std::vector reply(Telegram telegram) { + std::vector reply = {0xe3, 'E', 'S', 'P', 'H', 'M', 0x12, 0x34, 0x56, 0x78}; + return reply; + }; }; class EbusSender { public: - EbusSender() {} - void set_primary_address(uint8_t primary_address) { this->primary_address_ = primary_address; } - void set_address(uint8_t address) { this->address_ = Elf::to_secondary(address); } virtual optional prepare_command() = 0; - - protected: - uint8_t primary_address_; - uint8_t address_ = SYN; }; class EbusSensorBase : public EbusReceiver, public EbusSender, public Component { public: void dump_config() override; + void set_parent(EbusComponent *parent) { this->parent_ = parent; } + void set_address(uint8_t address) { this->address_ = Elf::to_secondary(address); } void set_send_poll(bool send_poll) { this->send_poll_ = send_poll; } void set_command(uint16_t command) { this->command_ = command; } void set_payload(const std::vector &payload) { this->payload_ = payload; } @@ -52,6 +52,8 @@ class EbusSensorBase : public EbusReceiver, public EbusSender, public Component bool is_mine(Telegram &telegram); protected: + EbusComponent *parent_; + uint8_t address_ = SYN; bool send_poll_; uint16_t command_; std::vector payload_{}; @@ -66,6 +68,7 @@ class EbusComponent : public PollingComponent { void setup() override; void set_primary_address(uint8_t /*primary_address*/); + uint8_t get_primary_address() { return this->primary_address_; } void set_max_tries(uint8_t /*max_tries*/); void set_max_lock_counter(uint8_t /*max_lock_counter*/); void set_uart_num(uint8_t /*uart_num*/); diff --git a/esphome/components/ebus/sensor/__init__.py b/esphome/components/ebus/sensor/__init__.py index 19b3e46f82..bd70275b20 100644 --- a/esphome/components/ebus/sensor/__init__.py +++ b/esphome/components/ebus/sensor/__init__.py @@ -37,10 +37,9 @@ async def to_code(config): ebus = await cg.get_variable(config[CONF_EBUS_ID]) sens = await sensor.new_sensor(config) - sensor_base_config(sens, config) + sensor_base_config(ebus, sens, config) cg.add(sens.set_response_read_bytes(config[CONF_TELEGRAM][CONF_DECODE][CONF_BYTES])) cg.add( sens.set_response_read_divider(config[CONF_TELEGRAM][CONF_DECODE][CONF_DIVIDER]) ) - cg.add(ebus.add_sensor(sens))