mirror of
https://github.com/esphome/esphome.git
synced 2024-11-23 23:48:11 +01:00
ran clang-format
This commit is contained in:
parent
241f4363f5
commit
e1a8f60ddb
8 changed files with 270 additions and 359 deletions
|
@ -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);
|
return Elf::crc8_calc(buffer[1], crc);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Ebus::uart_send_char(uint8_t cr, bool esc) {
|
void Ebus::uart_send_char(uint8_t cr, bool esc) { this->uart_send_char(cr, esc, false, 0); }
|
||||||
this->uart_send_char(cr, esc, false, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Ebus::uart_send_remaining_request_part(SendCommand &command) {
|
void Ebus::uart_send_remaining_request_part(SendCommand &command) {
|
||||||
this->uart_send_char(command.getZZ());
|
this->uart_send_char(command.getZZ());
|
||||||
|
@ -113,14 +111,18 @@ void Ebus::process_received_char(unsigned char received_byte) {
|
||||||
} else {
|
} else {
|
||||||
this->receiving_telegram_.push_req_data(received_byte);
|
this->receiving_telegram_.push_req_data(received_byte);
|
||||||
if (this->receiving_telegram_.is_request_complete()) {
|
if (this->receiving_telegram_.is_request_complete()) {
|
||||||
this->receiving_telegram_.set_state(this->receiving_telegram_.is_ack_expected() ? TelegramState::waitForRequestAck : TelegramState::endCompleted);
|
this->receiving_telegram_.set_state(this->receiving_telegram_.is_ack_expected()
|
||||||
|
? TelegramState::waitForRequestAck
|
||||||
|
: TelegramState::endCompleted);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case TelegramState::waitForRequestAck:
|
case TelegramState::waitForRequestAck:
|
||||||
switch (received_byte) {
|
switch (received_byte) {
|
||||||
case ACK:
|
case ACK:
|
||||||
this->receiving_telegram_.set_state(this->receiving_telegram_.is_response_expected() ? TelegramState::waitForResponseData : TelegramState::endCompleted);
|
this->receiving_telegram_.set_state(this->receiving_telegram_.is_response_expected()
|
||||||
|
? TelegramState::waitForResponseData
|
||||||
|
: TelegramState::endCompleted);
|
||||||
break;
|
break;
|
||||||
case NACK:
|
case NACK:
|
||||||
this->receiving_telegram_.set_state(TelegramState::endErrorRequestNackReceived);
|
this->receiving_telegram_.set_state(TelegramState::endErrorRequestNackReceived);
|
||||||
|
@ -177,7 +179,9 @@ void Ebus::process_received_char(unsigned char received_byte) {
|
||||||
this->active_command_.set_state(TelegramState::waitForArbitration2nd);
|
this->active_command_.set_state(TelegramState::waitForArbitration2nd);
|
||||||
} else {
|
} else {
|
||||||
// lost arbitration, try again later if retries left
|
// 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);
|
this->active_command_.set_state(this->active_command_.can_retry(this->max_tries_)
|
||||||
|
? TelegramState::waitForSend
|
||||||
|
: TelegramState::endSendFailed);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case TelegramState::waitForArbitration2nd:
|
case TelegramState::waitForArbitration2nd:
|
||||||
|
@ -194,7 +198,9 @@ void Ebus::process_received_char(unsigned char received_byte) {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// try again later if retries left
|
// try again later if retries left
|
||||||
this->active_command_.set_state(this->active_command_.can_retry(this->max_tries_) ? TelegramState::waitForSend : TelegramState::endSendFailed);
|
this->active_command_.set_state(this->active_command_.can_retry(this->max_tries_)
|
||||||
|
? TelegramState::waitForSend
|
||||||
|
: TelegramState::endSendFailed);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case TelegramState::waitForCommandAck:
|
case TelegramState::waitForCommandAck:
|
||||||
|
@ -202,7 +208,9 @@ void Ebus::process_received_char(unsigned char received_byte) {
|
||||||
this->active_command_.set_state(TelegramState::endCompleted);
|
this->active_command_.set_state(TelegramState::endCompleted);
|
||||||
this->lock_counter_ = this->max_lock_counter_;
|
this->lock_counter_ = this->max_lock_counter_;
|
||||||
} else if (received_byte == SYN) { // timeout waiting for ACK signaled by AUTO-SYN
|
} 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);
|
this->active_command_.set_state(this->active_command_.can_retry(this->max_tries_)
|
||||||
|
? TelegramState::waitForSend
|
||||||
|
: TelegramState::endSendFailed);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
@ -223,7 +231,6 @@ void Ebus::process_received_char(unsigned char received_byte) {
|
||||||
|
|
||||||
// Handle our responses
|
// Handle our responses
|
||||||
this->handle_response(this->receiving_telegram_);
|
this->handle_response(this->receiving_telegram_);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Ebus::add_send_response_handler(std::function<uint8_t(Telegram &, uint8_t *)> send_response_handler) {
|
void Ebus::add_send_response_handler(std::function<uint8_t(Telegram &, uint8_t *)> send_response_handler) {
|
||||||
|
@ -315,13 +322,9 @@ int Elf::is_primary_nibble(uint8_t nibble) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t Elf::get_priority_class(uint8_t address) {
|
uint8_t Elf::get_priority_class(uint8_t address) { return (address & 0x0F); }
|
||||||
return (address & 0x0F);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t Elf::get_sub_address(uint8_t address) {
|
uint8_t Elf::get_sub_address(uint8_t address) { return (address >> 4); }
|
||||||
return (address >> 4);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t Elf::to_secondary(uint8_t address) {
|
uint8_t Elf::to_secondary(uint8_t address) {
|
||||||
if (is_primary(address)) {
|
if (is_primary(address)) {
|
||||||
|
|
|
@ -15,7 +15,6 @@ typedef struct {
|
||||||
uint8_t max_lock_counter;
|
uint8_t max_lock_counter;
|
||||||
} ebus_config_t;
|
} ebus_config_t;
|
||||||
|
|
||||||
|
|
||||||
class Elf {
|
class Elf {
|
||||||
public:
|
public:
|
||||||
static unsigned char crc8_calc(unsigned char data, unsigned char crc_init);
|
static unsigned char crc8_calc(unsigned char data, unsigned char crc_init);
|
||||||
|
@ -27,7 +26,6 @@ public:
|
||||||
static uint8_t to_secondary(uint8_t address);
|
static uint8_t to_secondary(uint8_t address);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class Ebus {
|
class Ebus {
|
||||||
public:
|
public:
|
||||||
explicit Ebus(ebus_config_t &config);
|
explicit Ebus(ebus_config_t &config);
|
||||||
|
@ -55,7 +53,6 @@ protected:
|
||||||
void uart_send_char(uint8_t cr, bool esc = true);
|
void uart_send_char(uint8_t cr, bool esc = true);
|
||||||
void uart_send_remaining_request_part(SendCommand &command);
|
void uart_send_remaining_request_part(SendCommand &command);
|
||||||
void handle_response(Telegram &telegram);
|
void handle_response(Telegram &telegram);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ebus
|
} // namespace ebus
|
||||||
|
|
|
@ -25,24 +25,12 @@ void EbusComponent::setup() {
|
||||||
this->setup_tasks();
|
this->setup_tasks();
|
||||||
}
|
}
|
||||||
|
|
||||||
void EbusComponent::set_primary_address(uint8_t primary_address) {
|
void EbusComponent::set_primary_address(uint8_t primary_address) { this->primary_address_ = 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_max_tries(uint8_t max_tries) {
|
void EbusComponent::set_uart_num(uint8_t uart_num) { this->uart_num_ = uart_num; }
|
||||||
this->max_tries_ = max_tries;
|
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_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) {
|
void EbusComponent::set_history_queue_size(uint8_t history_queue_size) {
|
||||||
this->history_queue_size_ = history_queue_size;
|
this->history_queue_size_ = history_queue_size;
|
||||||
}
|
}
|
||||||
|
@ -54,9 +42,7 @@ void EbusComponent::add_sender(EbusSender *sender) {
|
||||||
sender->set_primary_address(this->primary_address_);
|
sender->set_primary_address(this->primary_address_);
|
||||||
this->senders_.push_back(sender);
|
this->senders_.push_back(sender);
|
||||||
}
|
}
|
||||||
void EbusComponent::add_receiver(EbusReceiver *receiver) {
|
void EbusComponent::add_receiver(EbusReceiver *receiver) { this->receivers_.push_back(receiver); }
|
||||||
this->receivers_.push_back(receiver);
|
|
||||||
}
|
|
||||||
|
|
||||||
void EbusComponent::setup_queues() {
|
void EbusComponent::setup_queues() {
|
||||||
this->history_queue_ = xQueueCreate(this->history_queue_size_, sizeof(Telegram));
|
this->history_queue_ = xQueueCreate(this->history_queue_size_, sizeof(Telegram));
|
||||||
|
@ -70,9 +56,8 @@ void EbusComponent::setup_ebus() {
|
||||||
};
|
};
|
||||||
this->ebus = new Ebus(ebus_config);
|
this->ebus = new Ebus(ebus_config);
|
||||||
|
|
||||||
this->ebus->set_uart_send_function( [&](const char * buffer, int16_t length) {
|
this->ebus->set_uart_send_function(
|
||||||
return uart_write_bytes(this->uart_num_, buffer, length);
|
[&](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;
|
BaseType_t xHigherPriorityTaskWoken;
|
||||||
|
@ -93,7 +78,6 @@ void EbusComponent::setup_ebus() {
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void EbusComponent::setup_uart() {
|
void EbusComponent::setup_uart() {
|
||||||
|
@ -112,12 +96,8 @@ void EbusComponent::setup_uart() {
|
||||||
|
|
||||||
ESP_ERROR_CHECK(uart_param_config(this->uart_num_, &uart_config));
|
ESP_ERROR_CHECK(uart_param_config(this->uart_num_, &uart_config));
|
||||||
|
|
||||||
ESP_ERROR_CHECK(uart_set_pin(
|
ESP_ERROR_CHECK(
|
||||||
this->uart_num_,
|
uart_set_pin(this->uart_num_, this->uart_tx_pin_, this->uart_rx_pin_, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
|
||||||
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));
|
ESP_ERROR_CHECK(uart_driver_install(this->uart_num_, 256, 0, 0, NULL, 0));
|
||||||
|
|
||||||
|
@ -150,7 +130,8 @@ void EbusComponent::process_received_messages(void *pvParameter) {
|
||||||
if (xQueueReceive(instance->history_queue_, &telegram, pdMS_TO_TICKS(1000))) {
|
if (xQueueReceive(instance->history_queue_, &telegram, pdMS_TO_TICKS(1000))) {
|
||||||
instance->handle_message(telegram);
|
instance->handle_message(telegram);
|
||||||
// TODO: this comment is kept as reference on how to debug stack overflows. Could be generalized.
|
// 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();
|
taskYIELD();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -159,11 +140,7 @@ void EbusComponent::process_received_messages(void *pvParameter) {
|
||||||
void EbusComponent::handle_message(Telegram &telegram) {
|
void EbusComponent::handle_message(Telegram &telegram) {
|
||||||
if (telegram.get_state() != TelegramState::endCompleted) {
|
if (telegram.get_state() != TelegramState::endCompleted) {
|
||||||
ESP_LOGD(TAG, "Message received with invalid state: %s, QQ:%02X, ZZ:%02X, Command:%02X%02X",
|
ESP_LOGD(TAG, "Message received with invalid state: %s, QQ:%02X, ZZ:%02X, Command:%02X%02X",
|
||||||
telegram.get_state_string(),
|
telegram.get_state_string(), telegram.getQQ(), telegram.getZZ(), telegram.getPB(), telegram.getSB());
|
||||||
telegram.getQQ(),
|
|
||||||
telegram.getZZ(),
|
|
||||||
telegram.getPB(),
|
|
||||||
telegram.getSB());
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -179,7 +156,6 @@ void EbusComponent::update() {
|
||||||
xQueueSendToBack(this->command_queue_, &command.value(), portMAX_DELAY);
|
xQueueSendToBack(this->command_queue_, &command.value(), portMAX_DELAY);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace ebus
|
} // namespace ebus
|
||||||
|
|
|
@ -8,7 +8,6 @@
|
||||||
#include <driver/uart.h>
|
#include <driver/uart.h>
|
||||||
#include <soc/uart_reg.h>
|
#include <soc/uart_reg.h>
|
||||||
|
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace ebus {
|
namespace ebus {
|
||||||
|
|
||||||
|
@ -29,8 +28,7 @@ public:
|
||||||
|
|
||||||
class EbusComponent : public PollingComponent {
|
class EbusComponent : public PollingComponent {
|
||||||
public:
|
public:
|
||||||
EbusComponent() {
|
EbusComponent() {}
|
||||||
}
|
|
||||||
|
|
||||||
void dump_config() override;
|
void dump_config() override;
|
||||||
void setup() override;
|
void setup() override;
|
||||||
|
@ -75,7 +73,6 @@ protected:
|
||||||
static void process_received_bytes(void *);
|
static void process_received_bytes(void *);
|
||||||
static void process_received_messages(void *);
|
static void process_received_messages(void *);
|
||||||
void handle_message(Telegram &);
|
void handle_message(Telegram &);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ebus
|
} // namespace ebus
|
||||||
|
|
|
@ -23,41 +23,21 @@ void EbusSensor::dump_config() {
|
||||||
ESP_LOGCONFIG(TAG, " command: 0x%04x", this->command_);
|
ESP_LOGCONFIG(TAG, " command: 0x%04x", this->command_);
|
||||||
};
|
};
|
||||||
|
|
||||||
void EbusSensor::set_primary_address(uint8_t primary_address) {
|
void EbusSensor::set_primary_address(uint8_t primary_address) { this->primary_address_ = 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_source(uint8_t source) {
|
void EbusSensor::set_command(uint16_t command) { this->command_ = command; }
|
||||||
this->source_ = source;
|
void EbusSensor::set_payload(const std::vector<uint8_t> &payload) { this->payload_ = payload; }
|
||||||
}
|
void EbusSensor::set_response_read_position(uint8_t response_position) { this->response_position_ = response_position; }
|
||||||
void EbusSensor::set_destination(uint8_t destination) {
|
void EbusSensor::set_response_read_bytes(uint8_t response_bytes) { this->response_bytes_ = response_bytes; }
|
||||||
this->destination_ = destination;
|
void EbusSensor::set_response_read_divider(float response_divider) { this->response_divider_ = response_divider; }
|
||||||
}
|
|
||||||
void EbusSensor::set_command(uint16_t command) {
|
|
||||||
this->command_ = command;
|
|
||||||
}
|
|
||||||
void EbusSensor::set_payload(const std::vector<uint8_t> &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<SendCommand> EbusSensor::prepare_command() {
|
optional<SendCommand> EbusSensor::prepare_command() {
|
||||||
optional<SendCommand> command;
|
optional<SendCommand> command;
|
||||||
if (this->destination_ != SYN) {
|
if (this->destination_ != SYN) {
|
||||||
command = SendCommand( //
|
command = SendCommand( //
|
||||||
this->primary_address_,
|
this->primary_address_, Elf::to_secondary(this->destination_), GET_BYTE(this->command_, 1),
|
||||||
Elf::to_secondary(this->destination_),
|
GET_BYTE(this->command_, 0), this->payload_.size(), &this->payload_[0]);
|
||||||
GET_BYTE(this->command_, 1),
|
|
||||||
GET_BYTE(this->command_, 0),
|
|
||||||
this->payload_.size(),
|
|
||||||
&this->payload_[0]);
|
|
||||||
}
|
}
|
||||||
return command;
|
return command;
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,7 @@ namespace ebus {
|
||||||
|
|
||||||
class EbusSensor : public EbusReceiver, public EbusSender, public sensor::Sensor, public Component {
|
class EbusSensor : public EbusReceiver, public EbusSender, public sensor::Sensor, public Component {
|
||||||
public:
|
public:
|
||||||
EbusSensor() {
|
EbusSensor() {}
|
||||||
}
|
|
||||||
|
|
||||||
void dump_config() override;
|
void dump_config() override;
|
||||||
|
|
||||||
|
|
|
@ -3,18 +3,15 @@
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace ebus {
|
namespace ebus {
|
||||||
|
|
||||||
TelegramBase::TelegramBase() {
|
TelegramBase::TelegramBase() {}
|
||||||
}
|
|
||||||
|
|
||||||
void TelegramBase::set_state(TelegramState new_state) {
|
void TelegramBase::set_state(TelegramState new_state) { this->state = new_state; }
|
||||||
this->state = new_state;
|
|
||||||
}
|
|
||||||
|
|
||||||
TelegramState TelegramBase::get_state() {
|
TelegramState TelegramBase::get_state() { return this->state; }
|
||||||
return this->state;
|
|
||||||
}
|
|
||||||
|
|
||||||
#define X(name, int) case int: return ""#name"";
|
#define X(name, int) \
|
||||||
|
case int: \
|
||||||
|
return "" #name "";
|
||||||
const char *TelegramBase::get_state_string() {
|
const char *TelegramBase::get_state_string() {
|
||||||
switch ((int8_t) this->state) {
|
switch ((int8_t) this->state) {
|
||||||
TELEGRAM_STATE_TABLE
|
TELEGRAM_STATE_TABLE
|
||||||
|
@ -24,7 +21,6 @@ const char * TelegramBase::get_state_string() {
|
||||||
}
|
}
|
||||||
#undef X
|
#undef X
|
||||||
|
|
||||||
|
|
||||||
void TelegramBase::push_buffer(uint8_t cr, uint8_t *buffer, uint8_t *pos, uint8_t *crc, int max_pos) {
|
void TelegramBase::push_buffer(uint8_t cr, uint8_t *buffer, uint8_t *pos, uint8_t *crc, int max_pos) {
|
||||||
if (*pos < max_pos) {
|
if (*pos < max_pos) {
|
||||||
*crc = Elf::crc8_calc(cr, *crc);
|
*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];
|
return this->request_buffer[OFFSET_DATA + pos];
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t TelegramBase::get_request_crc() {
|
uint8_t TelegramBase::get_request_crc() { return this->request_buffer[OFFSET_DATA + this->getNN()]; }
|
||||||
return this->request_buffer[OFFSET_DATA + this->getNN()];
|
|
||||||
}
|
|
||||||
|
|
||||||
void TelegramBase::push_req_data(uint8_t cr) {
|
void TelegramBase::push_req_data(uint8_t cr) {
|
||||||
this->push_buffer(cr, request_buffer, &request_buffer_pos, &request_rolling_crc, OFFSET_DATA + getNN());
|
this->push_buffer(cr, request_buffer, &request_buffer_pos, &request_rolling_crc, OFFSET_DATA + getNN());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TelegramBase::is_ack_expected() {
|
bool TelegramBase::is_ack_expected() { return (this->get_type() != TelegramType::Broadcast); }
|
||||||
return (this->get_type() != TelegramType::Broadcast);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool TelegramBase::is_response_expected() {
|
bool TelegramBase::is_response_expected() { return (this->get_type() == TelegramType::PrimarySecondary); }
|
||||||
return (this->get_type() == TelegramType::PrimarySecondary);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool TelegramBase::is_finished() {
|
bool TelegramBase::is_finished() { return this->state < TelegramState::unknown; }
|
||||||
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) {
|
int16_t Telegram::get_response_byte(uint8_t pos) {
|
||||||
if (pos > this->getResponseNN() || pos >= MAX_DATA_LENGTH) {
|
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];
|
return this->response_buffer[RESPONSE_OFFSET + pos];
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t Telegram::get_response_crc() {
|
uint8_t Telegram::get_response_crc() { return this->response_buffer[RESPONSE_OFFSET + this->getResponseNN()]; }
|
||||||
return this->response_buffer[RESPONSE_OFFSET + this->getResponseNN()];
|
|
||||||
}
|
|
||||||
|
|
||||||
void Telegram::push_response_data(uint8_t cr) {
|
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() {
|
bool Telegram::is_response_complete() {
|
||||||
return (this->state > TelegramState::waitForSyn || this->state == TelegramState::endCompleted) &&
|
return (this->state > TelegramState::waitForSyn || this->state == TelegramState::endCompleted) &&
|
||||||
(this->response_buffer_pos > RESPONSE_OFFSET) &&
|
(this->response_buffer_pos > RESPONSE_OFFSET) &&
|
||||||
(this->response_buffer_pos == (RESPONSE_OFFSET + this->getResponseNN() + 1)) &&
|
(this->response_buffer_pos == (RESPONSE_OFFSET + this->getResponseNN() + 1)) && !this->wait_for_escaped_char_;
|
||||||
!this->wait_for_escaped_char_;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Telegram::is_response_valid() {
|
bool Telegram::is_response_valid() {
|
||||||
|
@ -111,17 +94,14 @@ bool Telegram::is_response_valid() {
|
||||||
|
|
||||||
bool Telegram::is_request_complete() {
|
bool Telegram::is_request_complete() {
|
||||||
return (this->state > TelegramState::waitForSyn || this->state == TelegramState::endCompleted) &&
|
return (this->state > TelegramState::waitForSyn || this->state == TelegramState::endCompleted) &&
|
||||||
(this->request_buffer_pos > OFFSET_DATA) &&
|
(this->request_buffer_pos > OFFSET_DATA) && (this->request_buffer_pos == (OFFSET_DATA + this->getNN() + 1)) &&
|
||||||
(this->request_buffer_pos == (OFFSET_DATA + this->getNN() + 1)) && !this->wait_for_escaped_char_;
|
!this->wait_for_escaped_char_;
|
||||||
}
|
}
|
||||||
bool Telegram::is_request_valid() {
|
bool Telegram::is_request_valid() {
|
||||||
return this->is_request_complete() && this->get_request_crc() == this->request_rolling_crc;
|
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) {
|
SendCommand::SendCommand(uint8_t QQ, uint8_t ZZ, uint8_t PB, uint8_t SB, uint8_t NN, uint8_t *data) {
|
||||||
this->state = TelegramState::waitForSend;
|
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);
|
this->push_req_data(this->request_rolling_crc);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SendCommand::can_retry(int8_t max_tries) {
|
bool SendCommand::can_retry(int8_t max_tries) { return this->tries_count_++ < max_tries; }
|
||||||
return this->tries_count_++ < max_tries;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t SendCommand::get_crc() {
|
uint8_t SendCommand::get_crc() { return this->request_rolling_crc; }
|
||||||
return this->request_rolling_crc;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace ebus
|
} // namespace ebus
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
|
|
@ -36,9 +36,7 @@ X(endCompleted, -16) \
|
||||||
X(endSendFailed, -17)
|
X(endSendFailed, -17)
|
||||||
|
|
||||||
#define X(name, int) name = int,
|
#define X(name, int) name = int,
|
||||||
enum TelegramState : int8_t {
|
enum TelegramState : int8_t { TELEGRAM_STATE_TABLE };
|
||||||
TELEGRAM_STATE_TABLE
|
|
||||||
};
|
|
||||||
#undef X
|
#undef X
|
||||||
|
|
||||||
const uint8_t SYN = 0xAA;
|
const uint8_t SYN = 0xAA;
|
||||||
|
@ -65,26 +63,15 @@ enum TelegramState : int8_t {
|
||||||
const uint8_t RESPONSE_OFFSET = 1;
|
const uint8_t RESPONSE_OFFSET = 1;
|
||||||
const uint8_t INVALID_RESPONSE_BYTE = -1;
|
const uint8_t INVALID_RESPONSE_BYTE = -1;
|
||||||
|
|
||||||
|
|
||||||
class TelegramBase {
|
class TelegramBase {
|
||||||
public:
|
public:
|
||||||
TelegramBase();
|
TelegramBase();
|
||||||
|
|
||||||
uint8_t getQQ() {
|
uint8_t getQQ() { return this->request_buffer[OFFSET_QQ]; }
|
||||||
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 getZZ() {
|
uint8_t getSB() { return this->request_buffer[OFFSET_SB]; }
|
||||||
return this->request_buffer[OFFSET_ZZ];
|
uint16_t getCommand() { return ((uint16_t) getPB()) << 8 | getSB(); }
|
||||||
}
|
|
||||||
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 getNN() {
|
||||||
uint8_t nn = this->request_buffer[OFFSET_NN];
|
uint8_t nn = this->request_buffer[OFFSET_NN];
|
||||||
if (nn >= MAX_DATA_LENGTH) {
|
if (nn >= MAX_DATA_LENGTH) {
|
||||||
|
@ -107,15 +94,14 @@ public:
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
TelegramState state;
|
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_buffer_pos = 0;
|
||||||
uint8_t request_rolling_crc = 0;
|
uint8_t request_rolling_crc = 0;
|
||||||
bool wait_for_escaped_char_ = false;
|
bool wait_for_escaped_char_ = false;
|
||||||
void push_buffer(uint8_t cr, uint8_t *buffer, uint8_t *pos, uint8_t *crc, int max_pos);
|
void push_buffer(uint8_t cr, uint8_t *buffer, uint8_t *pos, uint8_t *crc, int max_pos);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class Telegram : public TelegramBase {
|
class Telegram : public TelegramBase {
|
||||||
public:
|
public:
|
||||||
Telegram();
|
Telegram();
|
||||||
|
@ -141,7 +127,6 @@ protected:
|
||||||
uint8_t response_buffer[RESPONSE_BUFFER_SIZE] = {0};
|
uint8_t response_buffer[RESPONSE_BUFFER_SIZE] = {0};
|
||||||
uint8_t response_buffer_pos = 0;
|
uint8_t response_buffer_pos = 0;
|
||||||
uint8_t response_rolling_crc = 0;
|
uint8_t response_rolling_crc = 0;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class SendCommand : public TelegramBase {
|
class SendCommand : public TelegramBase {
|
||||||
|
@ -153,9 +138,7 @@ public:
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
uint8_t tries_count_ = 0;
|
uint8_t tries_count_ = 0;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
} // namespace ebus
|
} // namespace ebus
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
|
Loading…
Reference in a new issue