mirror of
https://github.com/esphome/esphome.git
synced 2025-03-15 15:18:16 +00:00
added data to send function
This commit is contained in:
parent
c9c5203b48
commit
f00945b7a8
@ -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)
|
||||
|
@ -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
|
||||
|
@ -9,27 +9,21 @@ namespace canbus {
|
||||
|
||||
template<typename... Ts> class SendAction : public Action<Ts...> {
|
||||
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<unsigned char const *>(&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<typename... Ts> class SendStateAction : public Action<Ts...> {
|
||||
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
|
||||
|
@ -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<float> 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<uint8_t *>(&this->float_data_);
|
||||
//here we start the canbus->send
|
||||
this->parent_->send(this->can_id_,p);
|
||||
}
|
||||
|
||||
} // namespace canbus
|
||||
} // namespace esphome
|
@ -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<float> data);
|
||||
CanCall &set_data(float data);
|
||||
void perform();
|
||||
protected:
|
||||
Canbus *parent_;
|
||||
int can_id_;
|
||||
optional<float> float_data_;
|
||||
optional<bool> bool_data_;
|
||||
optional<long> 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
|
Loading…
x
Reference in New Issue
Block a user