mirror of
https://github.com/esphome/esphome.git
synced 2024-11-26 12:27:13 +01:00
CAN bus: support bit mask for on_frame can_id (#3196)
This commit is contained in:
parent
c054fb8a2c
commit
38259c96c9
@ -8,6 +8,7 @@ CODEOWNERS = ["@mvturnho", "@danielschramm"]
|
|||||||
IS_PLATFORM_COMPONENT = True
|
IS_PLATFORM_COMPONENT = True
|
||||||
|
|
||||||
CONF_CAN_ID = "can_id"
|
CONF_CAN_ID = "can_id"
|
||||||
|
CONF_CAN_ID_MASK = "can_id_mask"
|
||||||
CONF_USE_EXTENDED_ID = "use_extended_id"
|
CONF_USE_EXTENDED_ID = "use_extended_id"
|
||||||
CONF_CANBUS_ID = "canbus_id"
|
CONF_CANBUS_ID = "canbus_id"
|
||||||
CONF_BIT_RATE = "bit_rate"
|
CONF_BIT_RATE = "bit_rate"
|
||||||
@ -38,7 +39,7 @@ canbus_ns = cg.esphome_ns.namespace("canbus")
|
|||||||
CanbusComponent = canbus_ns.class_("CanbusComponent", cg.Component)
|
CanbusComponent = canbus_ns.class_("CanbusComponent", cg.Component)
|
||||||
CanbusTrigger = canbus_ns.class_(
|
CanbusTrigger = canbus_ns.class_(
|
||||||
"CanbusTrigger",
|
"CanbusTrigger",
|
||||||
automation.Trigger.template(cg.std_vector.template(cg.uint8)),
|
automation.Trigger.template(cg.std_vector.template(cg.uint8), cg.uint32),
|
||||||
cg.Component,
|
cg.Component,
|
||||||
)
|
)
|
||||||
CanSpeed = canbus_ns.enum("CAN_SPEED")
|
CanSpeed = canbus_ns.enum("CAN_SPEED")
|
||||||
@ -72,6 +73,9 @@ CANBUS_SCHEMA = cv.Schema(
|
|||||||
{
|
{
|
||||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(CanbusTrigger),
|
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(CanbusTrigger),
|
||||||
cv.Required(CONF_CAN_ID): cv.int_range(min=0, max=0x1FFFFFFF),
|
cv.Required(CONF_CAN_ID): cv.int_range(min=0, max=0x1FFFFFFF),
|
||||||
|
cv.Optional(CONF_CAN_ID_MASK, default=0x1FFFFFFF): cv.int_range(
|
||||||
|
min=0, max=0x1FFFFFFF
|
||||||
|
),
|
||||||
cv.Optional(CONF_USE_EXTENDED_ID, default=False): cv.boolean,
|
cv.Optional(CONF_USE_EXTENDED_ID, default=False): cv.boolean,
|
||||||
},
|
},
|
||||||
validate_id,
|
validate_id,
|
||||||
@ -90,11 +94,16 @@ async def setup_canbus_core_(var, config):
|
|||||||
|
|
||||||
for conf in config.get(CONF_ON_FRAME, []):
|
for conf in config.get(CONF_ON_FRAME, []):
|
||||||
can_id = conf[CONF_CAN_ID]
|
can_id = conf[CONF_CAN_ID]
|
||||||
|
can_id_mask = conf[CONF_CAN_ID_MASK]
|
||||||
ext_id = conf[CONF_USE_EXTENDED_ID]
|
ext_id = conf[CONF_USE_EXTENDED_ID]
|
||||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var, can_id, ext_id)
|
trigger = cg.new_Pvariable(
|
||||||
|
conf[CONF_TRIGGER_ID], var, can_id, can_id_mask, ext_id
|
||||||
|
)
|
||||||
await cg.register_component(trigger, conf)
|
await cg.register_component(trigger, conf)
|
||||||
await automation.build_automation(
|
await automation.build_automation(
|
||||||
trigger, [(cg.std_vector.template(cg.uint8), "x")], conf
|
trigger,
|
||||||
|
[(cg.std_vector.template(cg.uint8), "x"), (cg.uint32, "can_id")],
|
||||||
|
conf,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -126,7 +135,6 @@ async def canbus_action_to_code(config, action_id, template_arg, args):
|
|||||||
if CONF_CAN_ID in config:
|
if CONF_CAN_ID in config:
|
||||||
can_id = await cg.templatable(config[CONF_CAN_ID], args, cg.uint32)
|
can_id = await cg.templatable(config[CONF_CAN_ID], args, cg.uint32)
|
||||||
cg.add(var.set_can_id(can_id))
|
cg.add(var.set_can_id(can_id))
|
||||||
|
|
||||||
use_extended_id = await cg.templatable(
|
use_extended_id = await cg.templatable(
|
||||||
config[CONF_USE_EXTENDED_ID], args, cg.uint32
|
config[CONF_USE_EXTENDED_ID], args, cg.uint32
|
||||||
)
|
)
|
||||||
|
@ -78,8 +78,9 @@ void Canbus::loop() {
|
|||||||
|
|
||||||
// fire all triggers
|
// fire all triggers
|
||||||
for (auto *trigger : this->triggers_) {
|
for (auto *trigger : this->triggers_) {
|
||||||
if ((trigger->can_id_ == can_message.can_id) && (trigger->use_extended_id_ == can_message.use_extended_id)) {
|
if ((trigger->can_id_ == (can_message.can_id & trigger->can_id_mask_)) &&
|
||||||
trigger->trigger(data);
|
(trigger->use_extended_id_ == can_message.use_extended_id)) {
|
||||||
|
trigger->trigger(data, can_message.can_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -116,17 +116,19 @@ template<typename... Ts> class CanbusSendAction : public Action<Ts...>, public P
|
|||||||
std::vector<uint8_t> data_static_{};
|
std::vector<uint8_t> data_static_{};
|
||||||
};
|
};
|
||||||
|
|
||||||
class CanbusTrigger : public Trigger<std::vector<uint8_t>>, public Component {
|
class CanbusTrigger : public Trigger<std::vector<uint8_t>, uint32_t>, public Component {
|
||||||
friend class Canbus;
|
friend class Canbus;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit CanbusTrigger(Canbus *parent, const std::uint32_t can_id, const bool use_extended_id)
|
explicit CanbusTrigger(Canbus *parent, const std::uint32_t can_id, const std::uint32_t can_id_mask,
|
||||||
: parent_(parent), can_id_(can_id), use_extended_id_(use_extended_id){};
|
const bool use_extended_id)
|
||||||
|
: parent_(parent), can_id_(can_id), can_id_mask_(can_id_mask), use_extended_id_(use_extended_id){};
|
||||||
void setup() override { this->parent_->add_trigger(this); }
|
void setup() override { this->parent_->add_trigger(this); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Canbus *parent_;
|
Canbus *parent_;
|
||||||
uint32_t can_id_;
|
uint32_t can_id_;
|
||||||
|
uint32_t can_id_mask_;
|
||||||
bool use_extended_id_;
|
bool use_extended_id_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -2551,6 +2551,25 @@ canbus:
|
|||||||
lambda: "return x[0] == 0x11;"
|
lambda: "return x[0] == 0x11;"
|
||||||
then:
|
then:
|
||||||
light.toggle: ${roomname}_lights
|
light.toggle: ${roomname}_lights
|
||||||
|
- can_id: 0b00000000000000000000001000000
|
||||||
|
can_id_mask: 0b11111000000000011111111000000
|
||||||
|
use_extended_id: true
|
||||||
|
then:
|
||||||
|
- lambda: |-
|
||||||
|
auto pdo_id = can_id >> 14;
|
||||||
|
switch (pdo_id)
|
||||||
|
{
|
||||||
|
case 117:
|
||||||
|
ESP_LOGD("canbus", "exhaust_fan_duty");
|
||||||
|
break;
|
||||||
|
case 118:
|
||||||
|
ESP_LOGD("canbus", "supply_fan_duty");
|
||||||
|
break;
|
||||||
|
case 119:
|
||||||
|
ESP_LOGD("canbus", "supply_fan_flow");
|
||||||
|
break;
|
||||||
|
// to be continued...
|
||||||
|
}
|
||||||
- platform: esp32_can
|
- platform: esp32_can
|
||||||
id: esp32_internal_can
|
id: esp32_internal_can
|
||||||
rx_pin: GPIO04
|
rx_pin: GPIO04
|
||||||
@ -2570,6 +2589,25 @@ canbus:
|
|||||||
lambda: "return x[0] == 0x11;"
|
lambda: "return x[0] == 0x11;"
|
||||||
then:
|
then:
|
||||||
light.toggle: ${roomname}_lights
|
light.toggle: ${roomname}_lights
|
||||||
|
- can_id: 0b00000000000000000000001000000
|
||||||
|
can_id_mask: 0b11111000000000011111111000000
|
||||||
|
use_extended_id: true
|
||||||
|
then:
|
||||||
|
- lambda: |-
|
||||||
|
auto pdo_id = can_id >> 14;
|
||||||
|
switch (pdo_id)
|
||||||
|
{
|
||||||
|
case 117:
|
||||||
|
ESP_LOGD("canbus", "exhaust_fan_duty");
|
||||||
|
break;
|
||||||
|
case 118:
|
||||||
|
ESP_LOGD("canbus", "supply_fan_duty");
|
||||||
|
break;
|
||||||
|
case 119:
|
||||||
|
ESP_LOGD("canbus", "supply_fan_flow");
|
||||||
|
break;
|
||||||
|
// to be continued...
|
||||||
|
}
|
||||||
|
|
||||||
teleinfo:
|
teleinfo:
|
||||||
id: myteleinfo
|
id: myteleinfo
|
||||||
|
Loading…
Reference in New Issue
Block a user