From e1a8f60ddb437bc63a81c76a844fe23774d45e15 Mon Sep 17 00:00:00 2001 From: Guido Schreuder Date: Mon, 19 Feb 2024 13:57:24 +0100 Subject: [PATCH] ran clang-format --- esphome/components/ebus/ebus.cpp | 259 +++++++++--------- esphome/components/ebus/ebus.h | 11 +- esphome/components/ebus/ebus_component.cpp | 98 +++---- esphome/components/ebus/ebus_component.h | 21 +- .../components/ebus/sensor/ebus_sensor.cpp | 40 +-- esphome/components/ebus/sensor/ebus_sensor.h | 7 +- esphome/components/ebus/telegram.cpp | 68 ++--- esphome/components/ebus/telegram.h | 125 ++++----- 8 files changed, 270 insertions(+), 359 deletions(-) diff --git a/esphome/components/ebus/ebus.cpp b/esphome/components/ebus/ebus.cpp index 05d2891eaa..3c9016d885 100644 --- a/esphome/components/ebus/ebus.cpp +++ b/esphome/components/ebus/ebus.cpp @@ -47,9 +47,7 @@ uint8_t Ebus::uart_send_char(uint8_t cr, bool esc, bool run_crc, uint8_t crc_ini return Elf::crc8_calc(buffer[1], crc); } -void Ebus::uart_send_char(uint8_t cr, bool esc) { - this->uart_send_char(cr, esc, false, 0); -} +void Ebus::uart_send_char(uint8_t cr, bool esc) { this->uart_send_char(cr, esc, false, 0); } void Ebus::uart_send_remaining_request_part(SendCommand &command) { this->uart_send_char(command.getZZ()); @@ -57,7 +55,7 @@ void Ebus::uart_send_remaining_request_part(SendCommand &command) { this->uart_send_char(command.getSB()); this->uart_send_char(command.getNN()); for (int i = 0; i < command.getNN(); i++) { - this->uart_send_char((uint8_t)command.get_request_byte(i)); + this->uart_send_char((uint8_t) command.get_request_byte(i)); } this->uart_send_char(command.get_crc()); } @@ -92,121 +90,131 @@ void Ebus::process_received_char(unsigned char received_byte) { } switch (this->receiving_telegram_.get_state()) { - case TelegramState::waitForSyn: - if (received_byte == SYN) { - this->receiving_telegram_.set_state(TelegramState::waitForArbitration); - } - break; - case TelegramState::waitForArbitration: - if (received_byte != SYN) { - this->receiving_telegram_.push_req_data(received_byte); - this->receiving_telegram_.set_state(TelegramState::waitForRequestData); - } - break; - case TelegramState::waitForRequestData: - if (received_byte == SYN) { - if (this->receiving_telegram_.getZZ() == ESC) { - this->receiving_telegram_.set_state(TelegramState::endArbitration); + case TelegramState::waitForSyn: + if (received_byte == SYN) { + this->receiving_telegram_.set_state(TelegramState::waitForArbitration); + } + break; + case TelegramState::waitForArbitration: + if (received_byte != SYN) { + this->receiving_telegram_.push_req_data(received_byte); + this->receiving_telegram_.set_state(TelegramState::waitForRequestData); + } + break; + case TelegramState::waitForRequestData: + if (received_byte == SYN) { + if (this->receiving_telegram_.getZZ() == ESC) { + this->receiving_telegram_.set_state(TelegramState::endArbitration); + } else { + this->receiving_telegram_.set_state(TelegramState::endErrorUnexpectedSyn); + } } else { + this->receiving_telegram_.push_req_data(received_byte); + if (this->receiving_telegram_.is_request_complete()) { + this->receiving_telegram_.set_state(this->receiving_telegram_.is_ack_expected() + ? TelegramState::waitForRequestAck + : TelegramState::endCompleted); + } + } + break; + case TelegramState::waitForRequestAck: + switch (received_byte) { + case ACK: + this->receiving_telegram_.set_state(this->receiving_telegram_.is_response_expected() + ? TelegramState::waitForResponseData + : TelegramState::endCompleted); + break; + case NACK: + this->receiving_telegram_.set_state(TelegramState::endErrorRequestNackReceived); + break; + default: + this->receiving_telegram_.set_state(TelegramState::endErrorRequestNoAck); + } + break; + case TelegramState::waitForResponseData: + if (received_byte == SYN) { this->receiving_telegram_.set_state(TelegramState::endErrorUnexpectedSyn); + } else { + this->receiving_telegram_.push_response_data(received_byte); + if (this->receiving_telegram_.is_response_complete()) { + this->receiving_telegram_.set_state(TelegramState::waitForResponseAck); + } } - } else { - this->receiving_telegram_.push_req_data(received_byte); - if (this->receiving_telegram_.is_request_complete()) { - this->receiving_telegram_.set_state(this->receiving_telegram_.is_ack_expected() ? TelegramState::waitForRequestAck : TelegramState::endCompleted); - } - } - break; - case TelegramState::waitForRequestAck: - switch (received_byte) { - case ACK: - this->receiving_telegram_.set_state(this->receiving_telegram_.is_response_expected() ? TelegramState::waitForResponseData : TelegramState::endCompleted); break; - case NACK: - this->receiving_telegram_.set_state(TelegramState::endErrorRequestNackReceived); + case TelegramState::waitForResponseAck: + switch (received_byte) { + case ACK: + this->receiving_telegram_.set_state(TelegramState::endCompleted); + break; + case NACK: + this->receiving_telegram_.set_state(TelegramState::endErrorResponseNackReceived); + break; + default: + this->receiving_telegram_.set_state(TelegramState::endErrorResponseNoAck); + } break; default: - this->receiving_telegram_.set_state(TelegramState::endErrorRequestNoAck); - } - break; - case TelegramState::waitForResponseData: - if (received_byte == SYN) { - this->receiving_telegram_.set_state(TelegramState::endErrorUnexpectedSyn); - } else { - this->receiving_telegram_.push_response_data(received_byte); - if (this->receiving_telegram_.is_response_complete()) { - this->receiving_telegram_.set_state(TelegramState::waitForResponseAck); - } - } - break; - case TelegramState::waitForResponseAck: - switch (received_byte) { - case ACK: - this->receiving_telegram_.set_state(TelegramState::endCompleted); break; - case NACK: - this->receiving_telegram_.set_state(TelegramState::endErrorResponseNackReceived); - break; - default: - this->receiving_telegram_.set_state(TelegramState::endErrorResponseNoAck); - } - break; - default: - break; } switch (this->active_command_.get_state()) { - case TelegramState::waitForSend: - if (received_byte == SYN && state == EbusState::normal && this->lock_counter_ == 0) { - this->active_command_.set_state(TelegramState::waitForArbitration); - this->uart_send_char(this->active_command_.getQQ()); - } - break; - case TelegramState::waitForArbitration: - if (received_byte == this->active_command_.getQQ()) { - // we won arbitration - this->uart_send_remaining_request_part(this->active_command_); - if (this->active_command_.is_ack_expected()) { - this->active_command_.set_state(TelegramState::waitForCommandAck); + case TelegramState::waitForSend: + if (received_byte == SYN && state == EbusState::normal && this->lock_counter_ == 0) { + this->active_command_.set_state(TelegramState::waitForArbitration); + this->uart_send_char(this->active_command_.getQQ()); + } + break; + case TelegramState::waitForArbitration: + if (received_byte == this->active_command_.getQQ()) { + // we won arbitration + this->uart_send_remaining_request_part(this->active_command_); + if (this->active_command_.is_ack_expected()) { + this->active_command_.set_state(TelegramState::waitForCommandAck); + } else { + this->active_command_.set_state(TelegramState::endCompleted); + this->lock_counter_ = this->max_lock_counter_; + } + } else if (Elf::get_priority_class(received_byte) == Elf::get_priority_class(this->active_command_.getQQ())) { + // eligible for round 2 + this->active_command_.set_state(TelegramState::waitForArbitration2nd); } else { + // lost arbitration, try again later if retries left + this->active_command_.set_state(this->active_command_.can_retry(this->max_tries_) + ? TelegramState::waitForSend + : TelegramState::endSendFailed); + } + break; + case TelegramState::waitForArbitration2nd: + if (received_byte == SYN) { + this->uart_send_char(this->active_command_.getQQ()); + } else if (received_byte == this->active_command_.getQQ()) { + // won round 2 + this->uart_send_remaining_request_part(this->active_command_); + if (this->active_command_.is_ack_expected()) { + this->active_command_.set_state(TelegramState::waitForCommandAck); + } else { + this->active_command_.set_state(TelegramState::endCompleted); + this->lock_counter_ = this->max_lock_counter_; + } + } else { + // try again later if retries left + this->active_command_.set_state(this->active_command_.can_retry(this->max_tries_) + ? TelegramState::waitForSend + : TelegramState::endSendFailed); + } + break; + case TelegramState::waitForCommandAck: + if (received_byte == ACK) { this->active_command_.set_state(TelegramState::endCompleted); this->lock_counter_ = this->max_lock_counter_; + } else if (received_byte == SYN) { // timeout waiting for ACK signaled by AUTO-SYN + this->active_command_.set_state(this->active_command_.can_retry(this->max_tries_) + ? TelegramState::waitForSend + : TelegramState::endSendFailed); } - } else if (Elf::get_priority_class(received_byte) == Elf::get_priority_class(this->active_command_.getQQ())) { - // eligible for round 2 - this->active_command_.set_state(TelegramState::waitForArbitration2nd); - } else { - // lost arbitration, try again later if retries left - this->active_command_.set_state(this->active_command_.can_retry(this->max_tries_) ? TelegramState::waitForSend : TelegramState::endSendFailed); - } - break; - case TelegramState::waitForArbitration2nd: - if (received_byte == SYN) { - this->uart_send_char(this->active_command_.getQQ()); - } else if (received_byte == this->active_command_.getQQ()) { - // won round 2 - this->uart_send_remaining_request_part(this->active_command_); - if (this->active_command_.is_ack_expected()) { - this->active_command_.set_state(TelegramState::waitForCommandAck); - } else { - this->active_command_.set_state(TelegramState::endCompleted); - this->lock_counter_ = this->max_lock_counter_; - } - } else { - // try again later if retries left - this->active_command_.set_state(this->active_command_.can_retry(this->max_tries_) ? TelegramState::waitForSend : TelegramState::endSendFailed); - } - break; - case TelegramState::waitForCommandAck: - if (received_byte == ACK) { - this->active_command_.set_state(TelegramState::endCompleted); - this->lock_counter_ = this->max_lock_counter_; - } else if (received_byte == SYN) { // timeout waiting for ACK signaled by AUTO-SYN - this->active_command_.set_state(this->active_command_.can_retry(this->max_tries_) ? TelegramState::waitForSend : TelegramState::endSendFailed); - } - break; - default: - break; + break; + default: + break; } // responses to our commands are stored in receiving_telegram_ @@ -223,7 +231,6 @@ void Ebus::process_received_char(unsigned char received_byte) { // Handle our responses this->handle_response(this->receiving_telegram_); - } void Ebus::add_send_response_handler(std::function send_response_handler) { @@ -245,7 +252,7 @@ void Ebus::handle_response(Telegram &telegram) { int len = 0; // find response - for (auto const& handler : send_response_handlers_) { + for (auto const &handler : send_response_handlers_) { len = handler(telegram, buf); if (len != 0) { break; @@ -274,23 +281,23 @@ unsigned char Elf::crc8_calc(unsigned char data, unsigned char crc_init) { crc = crc_init; for (int i = 0; i < 8; i++) { if (crc & 0x80) { - polynom = (unsigned char)0x9B; + polynom = (unsigned char) 0x9B; } else { - polynom = (unsigned char)0; + polynom = (unsigned char) 0; } - crc = (unsigned char)((crc & ~0x80) << 1); + crc = (unsigned char) ((crc & ~0x80) << 1); if (data & 0x80) { - crc = (unsigned char)(crc | 1); + crc = (unsigned char) (crc | 1); } - crc = (unsigned char)(crc ^ polynom); - data = (unsigned char)(data << 1); + crc = (unsigned char) (crc ^ polynom); + data = (unsigned char) (data << 1); } return (crc); } unsigned char Elf::crc8_array(unsigned char data[], unsigned int length) { unsigned char uc_crc; - uc_crc = (unsigned char)0; + uc_crc = (unsigned char) 0; for (int i = 0; i < length; i++) { uc_crc = crc8_calc(data[i], uc_crc); } @@ -304,24 +311,20 @@ bool Elf::is_primary(uint8_t address) { int Elf::is_primary_nibble(uint8_t nibble) { switch (nibble) { - case 0b0000: - case 0b0001: - case 0b0011: - case 0b0111: - case 0b1111: - return true; - default: - return false; + case 0b0000: + case 0b0001: + case 0b0011: + case 0b0111: + case 0b1111: + return true; + default: + return false; } } -uint8_t Elf::get_priority_class(uint8_t address) { - return (address & 0x0F); -} +uint8_t Elf::get_priority_class(uint8_t address) { return (address & 0x0F); } -uint8_t Elf::get_sub_address(uint8_t address) { - return (address >> 4); -} +uint8_t Elf::get_sub_address(uint8_t address) { return (address >> 4); } uint8_t Elf::to_secondary(uint8_t address) { if (is_primary(address)) { diff --git a/esphome/components/ebus/ebus.h b/esphome/components/ebus/ebus.h index 376541dbc3..c7a5f429c3 100644 --- a/esphome/components/ebus/ebus.h +++ b/esphome/components/ebus/ebus.h @@ -15,9 +15,8 @@ typedef struct { uint8_t max_lock_counter; } ebus_config_t; - class Elf { -public: + public: static unsigned char crc8_calc(unsigned char data, unsigned char crc_init); static unsigned char crc8_array(unsigned char data[], unsigned int length); static bool is_primary(uint8_t address); @@ -27,9 +26,8 @@ public: static uint8_t to_secondary(uint8_t address); }; - class Ebus { -public: + public: explicit Ebus(ebus_config_t &config); void set_uart_send_function(std::function uart_send); void set_queue_received_telegram_function(std::function queue_received_telegram); @@ -37,7 +35,7 @@ public: void process_received_char(unsigned char receivedByte); void add_send_response_handler(std::function); -protected: + protected: uint8_t primary_address_; uint8_t max_tries_; uint8_t max_lock_counter_; @@ -50,12 +48,11 @@ protected: std::function uart_send_; std::function queue_received_telegram_; - std::function dequeue_command_; + std::function dequeue_command_; uint8_t uart_send_char(uint8_t cr, bool esc, bool run_crc, uint8_t crc_init); void uart_send_char(uint8_t cr, bool esc = true); void uart_send_remaining_request_part(SendCommand &command); void handle_response(Telegram &telegram); - }; } // namespace ebus diff --git a/esphome/components/ebus/ebus_component.cpp b/esphome/components/ebus/ebus_component.cpp index 0ac4bc9a16..eec0aadff3 100644 --- a/esphome/components/ebus/ebus_component.cpp +++ b/esphome/components/ebus/ebus_component.cpp @@ -25,24 +25,12 @@ void EbusComponent::setup() { this->setup_tasks(); } -void EbusComponent::set_primary_address(uint8_t primary_address) { - this->primary_address_ = primary_address; -} -void EbusComponent::set_max_tries(uint8_t max_tries) { - this->max_tries_ = max_tries; -} -void EbusComponent::set_max_lock_counter(uint8_t max_lock_counter) { - this->max_lock_counter_ = max_lock_counter; -} -void EbusComponent::set_uart_num(uint8_t uart_num) { - this->uart_num_ = uart_num; -} -void EbusComponent::set_uart_tx_pin(uint8_t uart_tx_pin) { - this->uart_tx_pin_ = uart_tx_pin; -} -void EbusComponent::set_uart_rx_pin(uint8_t uart_rx_pin) { - this->uart_rx_pin_ = uart_rx_pin; -} +void EbusComponent::set_primary_address(uint8_t primary_address) { this->primary_address_ = primary_address; } +void EbusComponent::set_max_tries(uint8_t max_tries) { this->max_tries_ = max_tries; } +void EbusComponent::set_max_lock_counter(uint8_t max_lock_counter) { this->max_lock_counter_ = max_lock_counter; } +void EbusComponent::set_uart_num(uint8_t uart_num) { this->uart_num_ = uart_num; } +void EbusComponent::set_uart_tx_pin(uint8_t uart_tx_pin) { this->uart_tx_pin_ = uart_tx_pin; } +void EbusComponent::set_uart_rx_pin(uint8_t uart_rx_pin) { this->uart_rx_pin_ = uart_rx_pin; } void EbusComponent::set_history_queue_size(uint8_t history_queue_size) { this->history_queue_size_ = history_queue_size; } @@ -54,36 +42,33 @@ void EbusComponent::add_sender(EbusSender *sender) { sender->set_primary_address(this->primary_address_); this->senders_.push_back(sender); } -void EbusComponent::add_receiver(EbusReceiver *receiver) { - this->receivers_.push_back(receiver); -} +void EbusComponent::add_receiver(EbusReceiver *receiver) { this->receivers_.push_back(receiver); } 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() { - ebus_config_t ebus_config = ebus_config_t { - .primary_address = this->primary_address_, - .max_tries = this->max_tries_, - .max_lock_counter = this->max_lock_counter_, + ebus_config_t ebus_config = ebus_config_t{ + .primary_address = this->primary_address_, + .max_tries = this->max_tries_, + .max_lock_counter = this->max_lock_counter_, }; this->ebus = new Ebus(ebus_config); - this->ebus->set_uart_send_function( [&](const char * buffer, int16_t length) { - return uart_write_bytes(this->uart_num_, buffer, length); - } ); + this->ebus->set_uart_send_function( + [&](const char *buffer, int16_t length) { return uart_write_bytes(this->uart_num_, buffer, length); }); - this->ebus->set_queue_received_telegram_function( [&](Telegram &telegram) { + this->ebus->set_queue_received_telegram_function([&](Telegram &telegram) { BaseType_t xHigherPriorityTaskWoken; xHigherPriorityTaskWoken = pdFALSE; xQueueSendToBackFromISR(this->history_queue_, &telegram, &xHigherPriorityTaskWoken); if (xHigherPriorityTaskWoken) { portYIELD_FROM_ISR(); } - } ); + }); - this->ebus->set_dequeue_command_function( [&](void *const command) { + this->ebus->set_dequeue_command_function([&](void *const command) { BaseType_t xHigherPriorityTaskWoken = pdFALSE; if (xQueueReceiveFromISR(this->command_queue_, command, &xHigherPriorityTaskWoken)) { if (xHigherPriorityTaskWoken) { @@ -92,8 +77,7 @@ void EbusComponent::setup_ebus() { return true; } return false; - } ); - + }); } void EbusComponent::setup_uart() { @@ -101,23 +85,19 @@ void EbusComponent::setup_uart() { portENTER_CRITICAL(&mux); uart_config_t uart_config = { - .baud_rate = 2400, - .data_bits = UART_DATA_8_BITS, - .parity = UART_PARITY_DISABLE, - .stop_bits = UART_STOP_BITS_1, - .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, - .rx_flow_ctrl_thresh = 2, - .use_ref_tick = true, + .baud_rate = 2400, + .data_bits = UART_DATA_8_BITS, + .parity = UART_PARITY_DISABLE, + .stop_bits = UART_STOP_BITS_1, + .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, + .rx_flow_ctrl_thresh = 2, + .use_ref_tick = true, }; ESP_ERROR_CHECK(uart_param_config(this->uart_num_, &uart_config)); - ESP_ERROR_CHECK(uart_set_pin( - this->uart_num_, - this->uart_tx_pin_, - this->uart_rx_pin_, - UART_PIN_NO_CHANGE, - UART_PIN_NO_CHANGE)); + ESP_ERROR_CHECK( + uart_set_pin(this->uart_num_, this->uart_tx_pin_, this->uart_rx_pin_, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE)); ESP_ERROR_CHECK(uart_driver_install(this->uart_num_, 256, 0, 0, NULL, 0)); @@ -125,12 +105,12 @@ void EbusComponent::setup_uart() { } void EbusComponent::setup_tasks() { - xTaskCreate(&process_received_bytes, "ebus_process_received_bytes", 2048, (void*) this, 10, NULL); - xTaskCreate(&process_received_messages, "ebus_process_received_messages", 2560, (void*) this, 5, NULL); + xTaskCreate(&process_received_bytes, "ebus_process_received_bytes", 2048, (void *) this, 10, NULL); + xTaskCreate(&process_received_messages, "ebus_process_received_messages", 2560, (void *) this, 5, NULL); } void EbusComponent::process_received_bytes(void *pvParameter) { - EbusComponent* instance = static_cast(pvParameter); + EbusComponent *instance = static_cast(pvParameter); while (1) { uint8_t receivedByte; @@ -143,14 +123,15 @@ void EbusComponent::process_received_bytes(void *pvParameter) { } void EbusComponent::process_received_messages(void *pvParameter) { - EbusComponent* instance = static_cast(pvParameter); + EbusComponent *instance = static_cast(pvParameter); Telegram telegram; while (1) { if (xQueueReceive(instance->history_queue_, &telegram, pdMS_TO_TICKS(1000))) { instance->handle_message(telegram); // TODO: this comment is kept as reference on how to debug stack overflows. Could be generalized. - // ESP_LOGD(TAG, "Task: %s, Stack Highwater Mark: %d", pcTaskGetTaskName(NULL), uxTaskGetStackHighWaterMark(NULL)); + // ESP_LOGD(TAG, "Task: %s, Stack Highwater Mark: %d", pcTaskGetTaskName(NULL), + // uxTaskGetStackHighWaterMark(NULL)); taskYIELD(); } } @@ -159,28 +140,23 @@ void EbusComponent::process_received_messages(void *pvParameter) { void EbusComponent::handle_message(Telegram &telegram) { if (telegram.get_state() != TelegramState::endCompleted) { ESP_LOGD(TAG, "Message received with invalid state: %s, QQ:%02X, ZZ:%02X, Command:%02X%02X", - telegram.get_state_string(), - telegram.getQQ(), - telegram.getZZ(), - telegram.getPB(), - telegram.getSB()); + telegram.get_state_string(), telegram.getQQ(), telegram.getZZ(), telegram.getPB(), telegram.getSB()); return; } - for (auto const& receiver : this->receivers_) { + for (auto const &receiver : this->receivers_) { receiver->process_received(telegram); } } void EbusComponent::update() { - for (auto const& sender : this->senders_) { + for (auto const &sender : this->senders_) { optional command = sender->prepare_command(); if (command.has_value()) { xQueueSendToBack(this->command_queue_, &command.value(), portMAX_DELAY); } } - } -} // namespace ebus -} // namespace esphome +} // namespace ebus +} // namespace esphome diff --git a/esphome/components/ebus/ebus_component.h b/esphome/components/ebus/ebus_component.h index 308e92c5d2..d468c6265d 100644 --- a/esphome/components/ebus/ebus_component.h +++ b/esphome/components/ebus/ebus_component.h @@ -8,29 +8,27 @@ #include #include - namespace esphome { namespace ebus { - static const char *const TAG = "ebus"; +static const char *const TAG = "ebus"; class EbusReceiver { -public: + public: EbusReceiver() {} virtual void process_received(Telegram) = 0; }; class EbusSender { -public: + public: EbusSender() {} virtual void set_primary_address(uint8_t) = 0; virtual optional prepare_command() = 0; }; class EbusComponent : public PollingComponent { -public: - EbusComponent() { - } + public: + EbusComponent() {} void dump_config() override; void setup() override; @@ -49,7 +47,7 @@ public: void update(); -protected: + protected: uint8_t primary_address_; uint8_t max_tries_; uint8_t max_lock_counter_; @@ -65,7 +63,7 @@ protected: std::list senders_; std::list receivers_; - Ebus* ebus; + Ebus *ebus; void setup_queues(); void setup_ebus(); @@ -75,8 +73,7 @@ protected: static void process_received_bytes(void *); static void process_received_messages(void *); void handle_message(Telegram &); - }; -} // namespace ebus -} // namespace esphome +} // namespace ebus +} // namespace esphome diff --git a/esphome/components/ebus/sensor/ebus_sensor.cpp b/esphome/components/ebus/sensor/ebus_sensor.cpp index 4b58f7d12e..9139b5f2cf 100644 --- a/esphome/components/ebus/sensor/ebus_sensor.cpp +++ b/esphome/components/ebus/sensor/ebus_sensor.cpp @@ -23,41 +23,21 @@ void EbusSensor::dump_config() { ESP_LOGCONFIG(TAG, " command: 0x%04x", this->command_); }; -void EbusSensor::set_primary_address(uint8_t primary_address) { - this->primary_address_ = primary_address; -} -void EbusSensor::set_source(uint8_t source) { - this->source_ = source; -} -void EbusSensor::set_destination(uint8_t destination) { - this->destination_ = destination; -} -void EbusSensor::set_command(uint16_t command) { - this->command_ = command; -} -void EbusSensor::set_payload(const std::vector &payload) { - this->payload_ = payload; -} -void EbusSensor::set_response_read_position(uint8_t response_position) { - this->response_position_ = response_position; -} -void EbusSensor::set_response_read_bytes(uint8_t response_bytes) { - this->response_bytes_ = response_bytes; -} -void EbusSensor::set_response_read_divider(float response_divider) { - this->response_divider_ = response_divider; -} +void EbusSensor::set_primary_address(uint8_t primary_address) { this->primary_address_ = primary_address; } +void EbusSensor::set_source(uint8_t source) { this->source_ = source; } +void EbusSensor::set_destination(uint8_t destination) { this->destination_ = destination; } +void EbusSensor::set_command(uint16_t command) { this->command_ = command; } +void EbusSensor::set_payload(const std::vector &payload) { this->payload_ = payload; } +void EbusSensor::set_response_read_position(uint8_t response_position) { this->response_position_ = response_position; } +void EbusSensor::set_response_read_bytes(uint8_t response_bytes) { this->response_bytes_ = response_bytes; } +void EbusSensor::set_response_read_divider(float response_divider) { this->response_divider_ = response_divider; } optional EbusSensor::prepare_command() { optional command; if (this->destination_ != SYN) { command = SendCommand( // - this->primary_address_, - Elf::to_secondary(this->destination_), - GET_BYTE(this->command_, 1), - GET_BYTE(this->command_, 0), - this->payload_.size(), - &this->payload_[0]); + this->primary_address_, Elf::to_secondary(this->destination_), GET_BYTE(this->command_, 1), + GET_BYTE(this->command_, 0), this->payload_.size(), &this->payload_[0]); } return command; } diff --git a/esphome/components/ebus/sensor/ebus_sensor.h b/esphome/components/ebus/sensor/ebus_sensor.h index 7e31b18071..0b4319d2bb 100644 --- a/esphome/components/ebus/sensor/ebus_sensor.h +++ b/esphome/components/ebus/sensor/ebus_sensor.h @@ -7,9 +7,8 @@ namespace esphome { namespace ebus { class EbusSensor : public EbusReceiver, public EbusSender, public sensor::Sensor, public Component { -public: - EbusSensor() { - } + public: + EbusSensor() {} void dump_config() override; @@ -31,7 +30,7 @@ public: float to_float(Telegram &telegram, uint8_t start, uint8_t length, float divider); bool is_mine(Telegram &telegram); -protected: + protected: uint8_t primary_address_; uint8_t source_ = SYN; uint8_t destination_ = SYN; diff --git a/esphome/components/ebus/telegram.cpp b/esphome/components/ebus/telegram.cpp index e8672db2ac..725d4fd188 100644 --- a/esphome/components/ebus/telegram.cpp +++ b/esphome/components/ebus/telegram.cpp @@ -3,20 +3,17 @@ namespace esphome { namespace ebus { -TelegramBase::TelegramBase() { -} +TelegramBase::TelegramBase() {} -void TelegramBase::set_state(TelegramState new_state) { - this->state = new_state; -} +void TelegramBase::set_state(TelegramState new_state) { this->state = new_state; } -TelegramState TelegramBase::get_state() { - return this->state; -} +TelegramState TelegramBase::get_state() { return this->state; } -#define X(name, int) case int: return ""#name""; -const char * TelegramBase::get_state_string() { - switch((int8_t) this->state) { +#define X(name, int) \ + case int: \ + return "" #name ""; +const char *TelegramBase::get_state_string() { + switch ((int8_t) this->state) { TELEGRAM_STATE_TABLE default: return "[INVALID STATE]"; @@ -24,7 +21,6 @@ const char * TelegramBase::get_state_string() { } #undef X - void TelegramBase::push_buffer(uint8_t cr, uint8_t *buffer, uint8_t *pos, uint8_t *crc, int max_pos) { if (*pos < max_pos) { *crc = Elf::crc8_calc(cr, *crc); @@ -58,30 +54,19 @@ int16_t TelegramBase::get_request_byte(uint8_t pos) { return this->request_buffer[OFFSET_DATA + pos]; } -uint8_t TelegramBase::get_request_crc() { - return this->request_buffer[OFFSET_DATA + this->getNN()]; -} +uint8_t TelegramBase::get_request_crc() { return this->request_buffer[OFFSET_DATA + this->getNN()]; } void TelegramBase::push_req_data(uint8_t cr) { this->push_buffer(cr, request_buffer, &request_buffer_pos, &request_rolling_crc, OFFSET_DATA + getNN()); } -bool TelegramBase::is_ack_expected() { - return (this->get_type() != TelegramType::Broadcast); -} +bool TelegramBase::is_ack_expected() { return (this->get_type() != TelegramType::Broadcast); } -bool TelegramBase::is_response_expected() { - return (this->get_type() == TelegramType::PrimarySecondary); -} +bool TelegramBase::is_response_expected() { return (this->get_type() == TelegramType::PrimarySecondary); } -bool TelegramBase::is_finished() { - return this->state < TelegramState::unknown; -} +bool TelegramBase::is_finished() { return this->state < TelegramState::unknown; } - -Telegram::Telegram() { - this->state = TelegramState::waitForSyn; -} +Telegram::Telegram() { this->state = TelegramState::waitForSyn; } int16_t Telegram::get_response_byte(uint8_t pos) { if (pos > this->getResponseNN() || pos >= MAX_DATA_LENGTH) { @@ -90,19 +75,17 @@ int16_t Telegram::get_response_byte(uint8_t pos) { return this->response_buffer[RESPONSE_OFFSET + pos]; } -uint8_t Telegram::get_response_crc() { - return this->response_buffer[RESPONSE_OFFSET + this->getResponseNN()]; -} +uint8_t Telegram::get_response_crc() { return this->response_buffer[RESPONSE_OFFSET + this->getResponseNN()]; } void Telegram::push_response_data(uint8_t cr) { - this->push_buffer(cr, response_buffer, &response_buffer_pos, &response_rolling_crc, RESPONSE_OFFSET + getResponseNN()); + this->push_buffer(cr, response_buffer, &response_buffer_pos, &response_rolling_crc, + RESPONSE_OFFSET + getResponseNN()); } bool Telegram::is_response_complete() { return (this->state > TelegramState::waitForSyn || this->state == TelegramState::endCompleted) && (this->response_buffer_pos > RESPONSE_OFFSET) && - (this->response_buffer_pos == (RESPONSE_OFFSET + this->getResponseNN() + 1)) && - !this->wait_for_escaped_char_; + (this->response_buffer_pos == (RESPONSE_OFFSET + this->getResponseNN() + 1)) && !this->wait_for_escaped_char_; } bool Telegram::is_response_valid() { @@ -111,17 +94,14 @@ bool Telegram::is_response_valid() { bool Telegram::is_request_complete() { return (this->state > TelegramState::waitForSyn || this->state == TelegramState::endCompleted) && - (this->request_buffer_pos > OFFSET_DATA) && - (this->request_buffer_pos == (OFFSET_DATA + this->getNN() + 1)) && !this->wait_for_escaped_char_; + (this->request_buffer_pos > OFFSET_DATA) && (this->request_buffer_pos == (OFFSET_DATA + this->getNN() + 1)) && + !this->wait_for_escaped_char_; } bool Telegram::is_request_valid() { return this->is_request_complete() && this->get_request_crc() == this->request_rolling_crc; } - -SendCommand::SendCommand() { - this->state = TelegramState::endCompleted; -} +SendCommand::SendCommand() { this->state = TelegramState::endCompleted; } SendCommand::SendCommand(uint8_t QQ, uint8_t ZZ, uint8_t PB, uint8_t SB, uint8_t NN, uint8_t *data) { this->state = TelegramState::waitForSend; @@ -136,13 +116,9 @@ SendCommand::SendCommand(uint8_t QQ, uint8_t ZZ, uint8_t PB, uint8_t SB, uint8_t this->push_req_data(this->request_rolling_crc); } -bool SendCommand::can_retry(int8_t max_tries) { - return this->tries_count_++ < max_tries; -} +bool SendCommand::can_retry(int8_t max_tries) { return this->tries_count_++ < max_tries; } -uint8_t SendCommand::get_crc() { - return this->request_rolling_crc; -} +uint8_t SendCommand::get_crc() { return this->request_rolling_crc; } } // namespace ebus } // namespace esphome diff --git a/esphome/components/ebus/telegram.h b/esphome/components/ebus/telegram.h index 359fc0aff1..25b303a951 100644 --- a/esphome/components/ebus/telegram.h +++ b/esphome/components/ebus/telegram.h @@ -16,75 +16,62 @@ enum TelegramType : int8_t { }; #define TELEGRAM_STATE_TABLE \ -X(waitForSyn, 1) \ -X(waitForSend, 2) \ -X(waitForRequestData, 3) \ -X(waitForRequestAck, 4) \ -X(waitForResponseData, 5) \ -X(waitForResponseAck, 6) \ -X(waitForArbitration, 7) \ -X(waitForArbitration2nd, 8) \ -X(waitForCommandAck, 9) \ -X(unknown, 0) \ -X(endErrorUnexpectedSyn, -1) \ -X(endErrorRequestNackReceived, -2) \ -X(endErrorResponseNackReceived, -3) \ -X(endErrorResponseNoAck, -4) \ -X(endErrorRequestNoAck, -5) \ -X(endArbitration, -6) \ -X(endCompleted, -16) \ -X(endSendFailed, -17) + X(waitForSyn, 1) \ + X(waitForSend, 2) \ + X(waitForRequestData, 3) \ + X(waitForRequestAck, 4) \ + X(waitForResponseData, 5) \ + X(waitForResponseAck, 6) \ + X(waitForArbitration, 7) \ + X(waitForArbitration2nd, 8) \ + X(waitForCommandAck, 9) \ + X(unknown, 0) \ + X(endErrorUnexpectedSyn, -1) \ + X(endErrorRequestNackReceived, -2) \ + X(endErrorResponseNackReceived, -3) \ + X(endErrorResponseNoAck, -4) \ + X(endErrorRequestNoAck, -5) \ + X(endArbitration, -6) \ + X(endCompleted, -16) \ + X(endSendFailed, -17) #define X(name, int) name = int, -enum TelegramState : int8_t { - TELEGRAM_STATE_TABLE -}; +enum TelegramState : int8_t { TELEGRAM_STATE_TABLE }; #undef X - const uint8_t SYN = 0xAA; - const uint8_t ESC = 0xA9; - const uint8_t ACK = 0x00; - const uint8_t NACK = 0xFF; +const uint8_t SYN = 0xAA; +const uint8_t ESC = 0xA9; +const uint8_t ACK = 0x00; +const uint8_t NACK = 0xFF; - const uint8_t BROADCAST_ADDRESS = 0xFE; - - /* Specification says: - 1. In primary and secondary telegram part, standardised commands must be limited to 10 used data bytes. - 2. In primary and secondary telegram part, the sum of mfr.-specific telegram used data bytes must not exceed 14. - We use 16 to be on the safe side for now. - */ - const uint8_t MAX_DATA_LENGTH = 16; - const uint8_t OFFSET_QQ = 0; - const uint8_t OFFSET_ZZ = 1; - const uint8_t OFFSET_PB = 2; - const uint8_t OFFSET_SB = 3; - const uint8_t OFFSET_NN = 4; - const uint8_t OFFSET_DATA = 5; - const uint8_t REQUEST_BUFFER_SIZE = (OFFSET_DATA + MAX_DATA_LENGTH + 1); - const uint8_t RESPONSE_BUFFER_SIZE = (MAX_DATA_LENGTH + 2); - const uint8_t RESPONSE_OFFSET = 1; - const uint8_t INVALID_RESPONSE_BYTE = -1; +const uint8_t BROADCAST_ADDRESS = 0xFE; +/* Specification says: + 1. In primary and secondary telegram part, standardised commands must be limited to 10 used data bytes. + 2. In primary and secondary telegram part, the sum of mfr.-specific telegram used data bytes must not exceed 14. + We use 16 to be on the safe side for now. +*/ +const uint8_t MAX_DATA_LENGTH = 16; +const uint8_t OFFSET_QQ = 0; +const uint8_t OFFSET_ZZ = 1; +const uint8_t OFFSET_PB = 2; +const uint8_t OFFSET_SB = 3; +const uint8_t OFFSET_NN = 4; +const uint8_t OFFSET_DATA = 5; +const uint8_t REQUEST_BUFFER_SIZE = (OFFSET_DATA + MAX_DATA_LENGTH + 1); +const uint8_t RESPONSE_BUFFER_SIZE = (MAX_DATA_LENGTH + 2); +const uint8_t RESPONSE_OFFSET = 1; +const uint8_t INVALID_RESPONSE_BYTE = -1; class TelegramBase { -public: + public: TelegramBase(); - uint8_t getQQ() { - return this->request_buffer[OFFSET_QQ]; - } - uint8_t getZZ() { - return this->request_buffer[OFFSET_ZZ]; - } - uint8_t getPB() { - return this->request_buffer[OFFSET_PB]; - } - uint8_t getSB() { - return this->request_buffer[OFFSET_SB]; - } - uint16_t getCommand() { - return ((uint16_t) getPB()) << 8 | getSB(); - } + uint8_t getQQ() { return this->request_buffer[OFFSET_QQ]; } + uint8_t getZZ() { return this->request_buffer[OFFSET_ZZ]; } + uint8_t getPB() { return this->request_buffer[OFFSET_PB]; } + uint8_t getSB() { return this->request_buffer[OFFSET_SB]; } + uint16_t getCommand() { return ((uint16_t) getPB()) << 8 | getSB(); } uint8_t getNN() { uint8_t nn = this->request_buffer[OFFSET_NN]; if (nn >= MAX_DATA_LENGTH) { @@ -95,7 +82,7 @@ public: void set_state(TelegramState new_state); TelegramState get_state(); - const char * get_state_string(); + const char *get_state_string(); TelegramType get_type(); int16_t get_request_byte(uint8_t pos); @@ -105,19 +92,18 @@ public: bool is_response_expected(); bool is_finished(); -protected: + protected: TelegramState state; - uint8_t request_buffer[REQUEST_BUFFER_SIZE] = {ESC, ESC}; // initialize QQ and ZZ with ESC char to distinguish from valid primary 0 + uint8_t request_buffer[REQUEST_BUFFER_SIZE] = { + ESC, ESC}; // initialize QQ and ZZ with ESC char to distinguish from valid primary 0 uint8_t request_buffer_pos = 0; uint8_t request_rolling_crc = 0; bool wait_for_escaped_char_ = false; void push_buffer(uint8_t cr, uint8_t *buffer, uint8_t *pos, uint8_t *crc, int max_pos); - }; - class Telegram : public TelegramBase { -public: + public: Telegram(); uint8_t getResponseNN() { @@ -137,25 +123,22 @@ public: bool is_request_complete(); bool is_request_valid(); -protected: + protected: uint8_t response_buffer[RESPONSE_BUFFER_SIZE] = {0}; uint8_t response_buffer_pos = 0; uint8_t response_rolling_crc = 0; - }; class SendCommand : public TelegramBase { -public: + public: SendCommand(); SendCommand(uint8_t QQ, uint8_t ZZ, uint8_t PB, uint8_t SB, uint8_t NN, uint8_t *data); bool can_retry(int8_t max_tries); uint8_t get_crc(); -protected: + protected: uint8_t tries_count_ = 0; - }; - } // namespace ebus } // namespace esphome