From f00945b7a8f03572b92386d1c634ad367ef83b53 Mon Sep 17 00:00:00 2001 From: Michiel van Turnhout Date: Tue, 23 Jul 2019 20:21:27 +0200 Subject: [PATCH] added data to send function --- esphome/components/canbus/__init__.py | 26 +++++++++++++-------- esphome/components/canbus/automation.cpp | 4 ++-- esphome/components/canbus/automation.h | 24 ++++++++------------ esphome/components/canbus/canbus.cpp | 21 +++++++++++++++-- esphome/components/canbus/canbus.h | 29 ++++++++++++++++-------- 5 files changed, 65 insertions(+), 39 deletions(-) diff --git a/esphome/components/canbus/__init__.py b/esphome/components/canbus/__init__.py index 6484b86049..3e6f9607cc 100644 --- a/esphome/components/canbus/__init__.py +++ b/esphome/components/canbus/__init__.py @@ -1,8 +1,13 @@ +import re + import esphome.codegen as cg import esphome.config_validation as cv +from esphome.automation import LambdaAction from esphome import automation from esphome.automation import maybe_simple_id -from esphome.core import CORE, coroutine, coroutine_with_priority +from esphome.core import CORE, EsphomeError, Lambda, coroutine, coroutine_with_priority +from esphome.components import sensor +from esphome.py_compat import text_type from esphome.const import CONF_ID IS_PLATFORM_COMPONENT = True @@ -12,6 +17,8 @@ CONF_CAN_ID = 'can_id' CONF_CAN_DATA = 'can_data' CONF_SENDER_ID = 'sender_id' +CONF_CANBUS_SEND_ACTION = 'canbus.send' + canbus_ns = cg.esphome_ns.namespace('canbus') CanbusComponent = canbus_ns.class_('CanbusComponent', cg.Component) @@ -26,7 +33,7 @@ SendAction = canbus_ns.class_('SendAction', automation.Action) CANBUS_ACTION_SCHEMA = maybe_simple_id({ cv.Required(CONF_CANBUS_ID): cv.use_id(CanbusComponent), cv.Required(CONF_CAN_ID): cv.int_range(min=1, max=4096), - #cv.Required(CONF_CAN_DATA): cv.All(), + cv.Required(CONF_CAN_DATA): cv.templatable(cv.int_), }) @@ -48,15 +55,14 @@ def register_canbus(var, config): yield setup_canbus_core_(var, config) -@automation.register_action('canbus.send', SendAction, CANBUS_ACTION_SCHEMA) -def canbus_send_to_code(config, action_id, template_arg, args): +@automation.register_action(CONF_CANBUS_SEND_ACTION, SendAction, CANBUS_ACTION_SCHEMA) +def canbus_action_to_code(config, action_id, template_arg, args): canbus = yield cg.get_variable(config[CONF_CANBUS_ID]) - print "--------------" - for arg in config: - print arg - print "--------------" - #component = yield cg.get_variable(config[CONF_ID]) - yield cg.new_Pvariable(action_id, template_arg, canbus, config[CONF_CAN_ID]) + #args_ = yield cg.get_variable(config[CONF_CAN_DATA]) + templ = yield cg.templatable(config[CONF_CAN_DATA], args, cg.float_) + send_id_ = yield cg.templatable(config[CONF_CAN_ID], args, cg.uint16) + var = yield cg.new_Pvariable(action_id, template_arg, canbus, send_id_) + cg.add(var.set_data(templ)) @coroutine_with_priority(100.0) diff --git a/esphome/components/canbus/automation.cpp b/esphome/components/canbus/automation.cpp index ba9bb7e05a..5f3ebc08f6 100644 --- a/esphome/components/canbus/automation.cpp +++ b/esphome/components/canbus/automation.cpp @@ -2,9 +2,9 @@ #include "esphome/core/log.h" namespace esphome { -namespace switch_ { +namespace canbus { -static const char *TAG = "switch.automation"; +static const char *TAG = "canbus.automation"; } // namespace switch_ } // namespace esphome diff --git a/esphome/components/canbus/automation.h b/esphome/components/canbus/automation.h index 2ede6cec58..60c42e337b 100644 --- a/esphome/components/canbus/automation.h +++ b/esphome/components/canbus/automation.h @@ -9,27 +9,21 @@ namespace canbus { template class SendAction : public Action { public: - explicit SendAction(Canbus *a_canbus, int can_id) : canbus_(a_canbus), can_id_(can_id) { } + explicit SendAction(Canbus *parent, int can_id) : parent_(parent), can_id_(can_id) {} + + TEMPLATABLE_VALUE(float, data) void play(Ts... x) override { - uint8_t data[8]={0}; - data[1] = 10; - this->canbus_->send(this->can_id_,data); } - + auto call = this->parent_->make_call(this->can_id_); + //unsigned uint const * p = reinterpret_cast(&f); + call.set_data(this->data_.optional_value(x...)); +// call.perform(this->parent_, this->can_id_); + } protected: - Canbus *canbus_; + Canbus *parent_; int can_id_; }; -template class SendStateAction : public Action { - public: - SendStateAction(Canbus *a_canbus) : canbus_(a_canbus) {} - TEMPLATABLE_VALUE(bool, state) - void play(Ts... x) override { this->canbus_->send_state(this->state_.value(x...)); } - - protected: - Canbus *canbus_; -}; } // namespace canbus } // namespace esphome diff --git a/esphome/components/canbus/canbus.cpp b/esphome/components/canbus/canbus.cpp index b6f1f19548..e49dc2dca0 100644 --- a/esphome/components/canbus/canbus.cpp +++ b/esphome/components/canbus/canbus.cpp @@ -17,13 +17,30 @@ void Canbus::setup() { void Canbus::dump_config() { ESP_LOGCONFIG(TAG, "Canbus: sender_id=%d", this->sender_id_); } void Canbus::send(int can_id, uint8_t *data) { - ESP_LOGD(TAG, "send: sender_id=%d, id=%d, data=%d", this->sender_id_, can_id, data[0]); - this->send_internal_(can_id, data); + int size = (sizeof data/ sizeof *data); + ESP_LOGD(TAG, "send: sender_id=%d, can_id=%d, data=%d data_size=%d", this->sender_id_, can_id, data[0],size); + //this->send_internal_(can_id, data); }; void Canbus::loop() { // check harware inputbuffer and process to esphome outputs } +CanCall &CanCall::set_data(optional data) { + this->float_data_ = data; + return *this; +} +CanCall &CanCall::set_data(float data) { + this->float_data_ = data; + return *this; +} + +void CanCall::perform() { + ESP_LOGD(TAG,"parent_id=%d can_id= %d data=%f",this->parent_->sender_id_,this->can_id_,this->float_data_); + uint8_t *p = reinterpret_cast(&this->float_data_); + //here we start the canbus->send + this->parent_->send(this->can_id_,p); +} + } // namespace canbus } // namespace esphome \ No newline at end of file diff --git a/esphome/components/canbus/canbus.h b/esphome/components/canbus/canbus.h index bb10f287d5..bce8b2b833 100644 --- a/esphome/components/canbus/canbus.h +++ b/esphome/components/canbus/canbus.h @@ -2,24 +2,30 @@ #include "esphome/core/component.h" #include "esphome/core/automation.h" +#include "esphome/core/optional.h" #include "esphome/components/binary_sensor/binary_sensor.h" namespace esphome { namespace canbus { -class CanbusSensor { - public: - void set_can_id(int can_id) { this->can_id_ = can_id; } +class Canbus; - private: - int can_id_{0}; -}; - -class CanbusBinarySensor : public CanbusSensor, public binary_sensor::BinarySensor { - friend class Canbus; +class CanCall { + public: + explicit CanCall(Canbus *parent, int can_id) : parent_(parent), can_id_(can_id) {} + CanCall &set_data(optional data); + CanCall &set_data(float data); + void perform(); + protected: + Canbus *parent_; + int can_id_; + optional float_data_; + optional bool_data_; + optional long_data; }; class Canbus : public Component { + friend CanCall; public: /* special address description flags for the CAN_ID */ static const uint32_t CAN_EFF_FLAG = 0x80000000UL; /* EFF/SFF is set in the MSB */ @@ -79,14 +85,17 @@ class Canbus : public Component { void loop() override; void send(int can_id, uint8_t *data); - void register_can_device(CanbusSensor *component){}; void set_sender_id(int sender_id) { this->sender_id_ = sender_id; } + CanCall make_call(int can_id){ return CanCall(this, can_id); } + protected: int sender_id_{0}; virtual bool send_internal_(int can_id, uint8_t *data); virtual bool setup_internal_(); virtual ERROR set_bitrate_(const CAN_SPEED canSpeed); }; + + } // namespace canbus } // namespace esphome \ No newline at end of file