From c592f57cce2ce43c51d9bf3f7d70ef24c2818ea8 Mon Sep 17 00:00:00 2001 From: Michiel van Turnhout Date: Wed, 7 Aug 2019 15:22:39 +0200 Subject: [PATCH] added trigger --- esphome/components/canbus/__init__.py | 22 +++++----- esphome/components/canbus/automation.cpp | 10 ----- esphome/components/canbus/automation.h | 55 ------------------------ esphome/components/canbus/canbus.cpp | 8 +++- esphome/components/canbus/canbus.h | 50 +++++++++++++++++++++ 5 files changed, 68 insertions(+), 77 deletions(-) delete mode 100644 esphome/components/canbus/automation.cpp delete mode 100644 esphome/components/canbus/automation.h diff --git a/esphome/components/canbus/__init__.py b/esphome/components/canbus/__init__.py index f9e3abd008..afa6d1669c 100644 --- a/esphome/components/canbus/__init__.py +++ b/esphome/components/canbus/__init__.py @@ -32,9 +32,8 @@ def validate_raw_data(value): canbus_ns = cg.esphome_ns.namespace('canbus') CanbusComponent = canbus_ns.class_('CanbusComponent', cg.Component) -CanbusTrigger = canbus_ns.class_('CanbusTrigger', - automation.Trigger.template(cg.std_string), - cg.Component) +CanbusTrigger = canbus_ns.class_('CanbusTrigger', automation.Trigger.template(), + cg.Component) CanSpeed = canbus_ns.enum('CAN_SPEED') CAN_SPEEDS = { @@ -60,10 +59,10 @@ CONFIG_SCHEMA = cv.Schema({ cv.GenerateID(): cv.declare_id(CanbusComponent), cv.Required(CONF_SENDER_ID): cv.int_range(min=0, max=255), cv.Optional(CONF_BIT_RATE, default='125KBPS'): cv.enum(CAN_SPEEDS, upper=True), - # cv.Optional(CONF_ON_MESSAGE): automation.validate_automation({ - # cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(CanbusTrigger), - # cv.GenerateID(CONF_CAN_ID): cv.int_range(min=1, max=4096), - # }), + cv.Optional(CONF_ON_MESSAGE): automation.validate_automation({ + cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(CanbusTrigger), + cv.GenerateID(CONF_CAN_ID): cv.int_range(min=1, max=4096), + }), }).extend(cv.COMPONENT_SCHEMA) # Actions @@ -86,10 +85,11 @@ def setup_canbus_core_(var, config): if CONF_BIT_RATE in config: bitrate = CAN_SPEEDS[config[CONF_BIT_RATE]] cg.add(var.set_bitrate(bitrate)) - # for conf in config.get(CONF_ON_MESSAGE, []): - # trig = cg.new_Pvariable(conf[CONF_TRIGGER_ID], conf[CONF_CAN_ID]) - # yield cg.register_component(trig, conf) - # yield automation.build_automation(trig, [(cg.uint32, 'x')], conf) + for conf in config.get(CONF_ON_MESSAGE, []): + trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var, conf[CONF_CAN_ID]) + yield cg.register_component(trigger, conf) + yield automation.build_automation(trigger, [], conf) + @coroutine def register_canbus(var, config): diff --git a/esphome/components/canbus/automation.cpp b/esphome/components/canbus/automation.cpp deleted file mode 100644 index 5f3ebc08f6..0000000000 --- a/esphome/components/canbus/automation.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "automation.h" -#include "esphome/core/log.h" - -namespace esphome { -namespace canbus { - -static const char *TAG = "canbus.automation"; - -} // namespace switch_ -} // namespace esphome diff --git a/esphome/components/canbus/automation.h b/esphome/components/canbus/automation.h deleted file mode 100644 index 07f5740f0a..0000000000 --- a/esphome/components/canbus/automation.h +++ /dev/null @@ -1,55 +0,0 @@ -#pragma once - -#include "esphome/components/canbus/canbus.h" -#include "esphome/core/automation.h" -#include "esphome/core/component.h" -#include "esphome/core/log.h" - -namespace esphome { -namespace canbus { - -template -class CanbusSendAction : public Action, public Parented { -public: - void - set_data_template(const std::function(Ts...)> func) { - this->data_func_ = func; - this->static_ = false; - } - void set_data_static(const std::vector &data) { - this->data_static_ = data; - this->static_ = true; - } - - void set_can_id(uint32_t can_id) { this->can_id_ = can_id; } - - void play(Ts... x) override { - if (this->static_) { - this->parent_->send_data(this->can_id_, this->data_static_); - } else { - auto val = this->data_func_(x...); - this->parent_->send_data(this->can_id_, val); - } - } - -protected: - uint32_t can_id_; - bool static_{false}; - std::function(Ts...)> data_func_{}; - std::vector data_static_{}; -}; - -class CanbusTrigger : public Trigger, public Component { -public: - explicit CanbusTrigger(const std::uint32_t &can_id); - - void setup() override; - void dump_config() override; - float get_setup_priority() const override; - -protected: - uint32_t can_id_; -}; - -} // namespace canbus -} // namespace esphome diff --git a/esphome/components/canbus/canbus.cpp b/esphome/components/canbus/canbus.cpp index ab4089e87c..3f1ee48950 100644 --- a/esphome/components/canbus/canbus.cpp +++ b/esphome/components/canbus/canbus.cpp @@ -35,7 +35,13 @@ void Canbus::send_data(uint32_t can_id, const std::vector data) { } void Canbus::loop() { - // check harware inputbuffer and process to esphome outputs + struct can_frame can_message; + this->read_message_(&can_message); + for(auto trigger: this->triggers_){ + if(trigger->can_id_ == can_message.can_id) { + trigger->trigger(); + } + } } } // namespace canbus diff --git a/esphome/components/canbus/canbus.h b/esphome/components/canbus/canbus.h index b1bb1f45ca..4e9585817c 100644 --- a/esphome/components/canbus/canbus.h +++ b/esphome/components/canbus/canbus.h @@ -7,6 +7,8 @@ namespace esphome { namespace canbus { +class CanbusTrigger; + /* CAN payload length and DLC definitions according to ISO 11898-1 */ static const uint8_t CAN_MAX_DLC = 8; static const uint8_t CAN_MAX_DLEN = 8; @@ -66,12 +68,60 @@ class Canbus : public Component { void set_sender_id(int sender_id) { this->sender_id_ = sender_id; } void set_bitrate(CAN_SPEED bit_rate) { this->bit_rate_ = bit_rate; } + void add_trigger(CanbusTrigger *trigger) {this->triggers_.push_back(trigger);}; + protected: + std::vector triggers_{}; uint32_t sender_id_{0}; CAN_SPEED bit_rate_{CAN_125KBPS}; virtual bool setup_internal_(); virtual ERROR send_message_(const struct can_frame *frame); + virtual ERROR read_message_(struct can_frame *frame); +}; + +template +class CanbusSendAction : public Action, public Parented { +public: + void + set_data_template(const std::function(Ts...)> func) { + this->data_func_ = func; + this->static_ = false; + } + void set_data_static(const std::vector &data) { + this->data_static_ = data; + this->static_ = true; + } + + void set_can_id(uint32_t can_id) { this->can_id_ = can_id; } + + void play(Ts... x) override { + if (this->static_) { + this->parent_->send_data(this->can_id_, this->data_static_); + } else { + auto val = this->data_func_(x...); + this->parent_->send_data(this->can_id_, val); + } + } + +protected: + uint32_t can_id_; + bool static_{false}; + std::function(Ts...)> data_func_{}; + std::vector data_static_{}; +}; + +class CanbusTrigger : public Trigger<> , public Component { + friend class Canbus; +public: + explicit CanbusTrigger(Canbus *parent, const std::uint32_t can_id):parent_(parent), can_id_(can_id){}; + void setup() override { + this->parent_->add_trigger(this); + } + +protected: + Canbus *parent_; + uint32_t can_id_; }; } // namespace canbus