some refactoring

This commit is contained in:
Guido Schreuder 2024-02-24 22:04:10 +01:00
parent baad6e7d19
commit ee781d789c
7 changed files with 41 additions and 29 deletions

View file

@ -125,7 +125,9 @@ async def to_code(config):
cg.add(var.set_update_interval(config[CONF_POLL_INTERVAL].total_milliseconds)) 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])) cg.add(sensor_base.set_send_poll(config[CONF_TELEGRAM][CONF_SEND_POLL]))
if CONF_ADDRESS in config[CONF_TELEGRAM]: if CONF_ADDRESS in config[CONF_TELEGRAM]:
cg.add(sensor_base.set_address(config[CONF_TELEGRAM][CONF_ADDRESS])) cg.add(sensor_base.set_address(config[CONF_TELEGRAM][CONF_ADDRESS]))

View file

@ -32,7 +32,5 @@ async def to_code(config):
ebus = await cg.get_variable(config[CONF_EBUS_ID]) ebus = await cg.get_variable(config[CONF_EBUS_ID])
sens = await binary_sensor.new_binary_sensor(config) 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(sens.set_response_read_mask(config[CONF_TELEGRAM][CONF_DECODE][CONF_MASK]))
cg.add(ebus.add_sensor(sens))

View file

@ -217,7 +217,7 @@ void Ebus::process_received_char(unsigned char received_byte) {
this->handle_response_(this->receiving_telegram_); this->handle_response_(this->receiving_telegram_);
} }
void Ebus::add_send_response_handler(const std::function<uint8_t(Telegram &, uint8_t *)> &send_response_handler) { void Ebus::add_send_response_handler(std::function<std::vector<uint8_t>(Telegram &)> send_response_handler) {
send_response_handlers_.push_back(send_response_handler); send_response_handlers_.push_back(send_response_handler);
} }
@ -232,28 +232,27 @@ void Ebus::handle_response_(Telegram &telegram) {
} }
// response buffer // response buffer
uint8_t buf[RESPONSE_BUFFER_SIZE] = {0}; std::vector<uint8_t> reply;
int len = 0;
// find response // find response
for (auto const &handler : send_response_handlers_) { for (auto const &handler : send_response_handlers_) {
len = handler(telegram, buf); reply = handler(telegram);
if (len != 0) { if (reply.size() != 0) {
break; break;
} }
} }
// we found no reponse to send // we found no reponse to send
if (len == 0) { if (reply.size() == 0 || reply.size() > RESPONSE_BUFFER_SIZE) {
uart_send_char_(NACK); uart_send_char_(NACK);
return; return;
} }
uart_send_char_(ACK); uart_send_char_(ACK);
uint8_t crc = Elf::crc8_calc(len, 0); uint8_t crc = Elf::crc8_calc(reply.size(), 0);
uart_send_char_(len); uart_send_char_(reply.size());
for (int i = 0; i < len; i++) { for (int i = 0; i < reply.size(); i++) {
crc = uart_send_char_(buf[i], true, true, crc); crc = uart_send_char_(reply[i], true, true, crc);
} }
uart_send_char_(crc); uart_send_char_(crc);
} }

View file

@ -3,6 +3,7 @@
#include <cstdint> #include <cstdint>
#include <functional> #include <functional>
#include <list> #include <list>
#include <vector>
#include "telegram.h" #include "telegram.h"
@ -36,7 +37,7 @@ class Ebus {
} }
void process_received_char(unsigned char received_byte); void process_received_char(unsigned char received_byte);
void add_send_response_handler(const std::function<uint8_t(Telegram &, uint8_t *)> &send_response_handler); void add_send_response_handler(std::function<std::vector<uint8_t>(Telegram &)> send_response_handler);
protected: protected:
uint8_t primary_address_; uint8_t primary_address_;
@ -47,7 +48,7 @@ class Ebus {
EbusState state_ = EbusState::ARBITRATION; EbusState state_ = EbusState::ARBITRATION;
Telegram receiving_telegram_; Telegram receiving_telegram_;
SendCommand active_command_; SendCommand active_command_;
std::list<std::function<uint8_t(Telegram &, uint8_t *)>> send_response_handlers_; std::list<std::function<std::vector<uint8_t>(Telegram &)>> send_response_handlers_;
std::function<void(const char *, int16_t)> uart_send_; std::function<void(const char *, int16_t)> uart_send_;
std::function<void(Telegram &)> queue_received_telegram_; std::function<void(Telegram &)> queue_received_telegram_;

View file

@ -49,8 +49,6 @@ void EbusComponent::add_sender(EbusSender *sender) {
if (this->primary_address_ == SYN) { if (this->primary_address_ == SYN) {
return; return;
} }
sender->set_primary_address(this->primary_address_);
this->senders_.push_back(sender); 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); }
@ -59,6 +57,7 @@ void EbusComponent::setup_queues_() {
this->history_queue_ = xQueueCreate(this->history_queue_size_, sizeof(Telegram)); this->history_queue_ = xQueueCreate(this->history_queue_size_, sizeof(Telegram));
this->command_queue_ = xQueueCreate(this->command_queue_size_, sizeof(Telegram)); this->command_queue_ = xQueueCreate(this->command_queue_size_, sizeof(Telegram));
} }
void EbusComponent::setup_ebus_() { void EbusComponent::setup_ebus_() {
this->ebus_ = make_unique<Ebus>(); this->ebus_ = make_unique<Ebus>();
this->ebus_->set_primary_address(this->primary_address_); 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<uint8_t> 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) { this->ebus_->set_dequeue_command_function([&](void *const command) {
BaseType_t x_higher_priority_task_woken = pdFALSE; BaseType_t x_higher_priority_task_woken = pdFALSE;
if (xQueueReceiveFromISR(this->command_queue_, command, &x_higher_priority_task_woken)) { if (xQueueReceiveFromISR(this->command_queue_, command, &x_higher_priority_task_woken)) {
@ -187,7 +197,7 @@ optional<SendCommand> EbusSensorBase::prepare_command() {
if (this->send_poll_) { if (this->send_poll_) {
command = SendCommand( // 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; return command;
} }

View file

@ -18,28 +18,28 @@ namespace ebus {
static const char *const TAG = "ebus"; static const char *const TAG = "ebus";
class EbusComponent;
class EbusReceiver { class EbusReceiver {
public: public:
EbusReceiver() {}
virtual void process_received(Telegram) = 0; virtual void process_received(Telegram) = 0;
std::vector<uint8_t> reply(Telegram telegram) {
std::vector<uint8_t> reply = {0xe3, 'E', 'S', 'P', 'H', 'M', 0x12, 0x34, 0x56, 0x78};
return reply;
};
}; };
class EbusSender { class EbusSender {
public: 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<SendCommand> prepare_command() = 0; virtual optional<SendCommand> prepare_command() = 0;
protected:
uint8_t primary_address_;
uint8_t address_ = SYN;
}; };
class EbusSensorBase : public EbusReceiver, public EbusSender, public Component { class EbusSensorBase : public EbusReceiver, public EbusSender, public Component {
public: public:
void dump_config() override; 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_send_poll(bool send_poll) { this->send_poll_ = send_poll; }
void set_command(uint16_t command) { this->command_ = command; } void set_command(uint16_t command) { this->command_ = command; }
void set_payload(const std::vector<uint8_t> &payload) { this->payload_ = payload; } void set_payload(const std::vector<uint8_t> &payload) { this->payload_ = payload; }
@ -52,6 +52,8 @@ class EbusSensorBase : public EbusReceiver, public EbusSender, public Component
bool is_mine(Telegram &telegram); bool is_mine(Telegram &telegram);
protected: protected:
EbusComponent *parent_;
uint8_t address_ = SYN;
bool send_poll_; bool send_poll_;
uint16_t command_; uint16_t command_;
std::vector<uint8_t> payload_{}; std::vector<uint8_t> payload_{};
@ -66,6 +68,7 @@ class EbusComponent : public PollingComponent {
void setup() override; void setup() override;
void set_primary_address(uint8_t /*primary_address*/); 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_tries(uint8_t /*max_tries*/);
void set_max_lock_counter(uint8_t /*max_lock_counter*/); void set_max_lock_counter(uint8_t /*max_lock_counter*/);
void set_uart_num(uint8_t /*uart_num*/); void set_uart_num(uint8_t /*uart_num*/);

View file

@ -37,10 +37,9 @@ async def to_code(config):
ebus = await cg.get_variable(config[CONF_EBUS_ID]) ebus = await cg.get_variable(config[CONF_EBUS_ID])
sens = await sensor.new_sensor(config) 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_bytes(config[CONF_TELEGRAM][CONF_DECODE][CONF_BYTES]))
cg.add( cg.add(
sens.set_response_read_divider(config[CONF_TELEGRAM][CONF_DECODE][CONF_DIVIDER]) sens.set_response_read_divider(config[CONF_TELEGRAM][CONF_DECODE][CONF_DIVIDER])
) )
cg.add(ebus.add_sensor(sens))