mirror of
https://github.com/esphome/esphome.git
synced 2024-11-30 10:44:13 +01:00
some refactoring
This commit is contained in:
parent
baad6e7d19
commit
ee781d789c
7 changed files with 41 additions and 29 deletions
|
@ -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]))
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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<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);
|
||||
}
|
||||
|
||||
|
@ -232,28 +232,27 @@ void Ebus::handle_response_(Telegram &telegram) {
|
|||
}
|
||||
|
||||
// response buffer
|
||||
uint8_t buf[RESPONSE_BUFFER_SIZE] = {0};
|
||||
int len = 0;
|
||||
std::vector<uint8_t> 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);
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <list>
|
||||
#include <vector>
|
||||
|
||||
#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<uint8_t(Telegram &, uint8_t *)> &send_response_handler);
|
||||
void add_send_response_handler(std::function<std::vector<uint8_t>(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<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(Telegram &)> queue_received_telegram_;
|
||||
|
|
|
@ -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<Ebus>();
|
||||
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) {
|
||||
BaseType_t x_higher_priority_task_woken = pdFALSE;
|
||||
if (xQueueReceiveFromISR(this->command_queue_, command, &x_higher_priority_task_woken)) {
|
||||
|
@ -187,7 +197,7 @@ optional<SendCommand> 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;
|
||||
}
|
||||
|
|
|
@ -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<uint8_t> reply(Telegram telegram) {
|
||||
std::vector<uint8_t> 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<SendCommand> 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<uint8_t> &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<uint8_t> 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*/);
|
||||
|
|
|
@ -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))
|
||||
|
|
Loading…
Reference in a new issue