mirror of
https://github.com/esphome/esphome.git
synced 2025-03-13 22:28:14 +00:00
added trigger
This commit is contained in:
parent
9996124c54
commit
c592f57cce
@ -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):
|
||||
|
@ -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
|
@ -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 <typename... Ts>
|
||||
class CanbusSendAction : public Action<Ts...>, public Parented<Canbus> {
|
||||
public:
|
||||
void
|
||||
set_data_template(const std::function<std::vector<uint8_t>(Ts...)> func) {
|
||||
this->data_func_ = func;
|
||||
this->static_ = false;
|
||||
}
|
||||
void set_data_static(const std::vector<uint8_t> &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<std::vector<uint8_t>(Ts...)> data_func_{};
|
||||
std::vector<uint8_t> data_static_{};
|
||||
};
|
||||
|
||||
class CanbusTrigger : public Trigger<std::uint32_t>, 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
|
@ -35,7 +35,13 @@ void Canbus::send_data(uint32_t can_id, const std::vector<uint8_t> 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
|
||||
|
@ -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<CanbusTrigger *> 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 <typename... Ts>
|
||||
class CanbusSendAction : public Action<Ts...>, public Parented<Canbus> {
|
||||
public:
|
||||
void
|
||||
set_data_template(const std::function<std::vector<uint8_t>(Ts...)> func) {
|
||||
this->data_func_ = func;
|
||||
this->static_ = false;
|
||||
}
|
||||
void set_data_static(const std::vector<uint8_t> &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<std::vector<uint8_t>(Ts...)> data_func_{};
|
||||
std::vector<uint8_t> 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
|
||||
|
Loading…
x
Reference in New Issue
Block a user