From 93b11bb7d53093cec0206251a2c440ccdcb775da Mon Sep 17 00:00:00 2001 From: NP v/d Spek Date: Thu, 19 Sep 2024 16:41:45 +0200 Subject: [PATCH] couple of other fixes --- esphome/components/espnow/.gitignore | 5 +++ esphome/components/espnow/__init__.py | 10 +++-- esphome/components/espnow/automations,h | 57 ------------------------- esphome/components/espnow/espnow.cpp | 12 +++--- esphome/components/espnow/espnow.h | 50 +++++++++++++++++++++- esphome/components/espnow/test.yaml | 2 +- 6 files changed, 67 insertions(+), 69 deletions(-) create mode 100644 esphome/components/espnow/.gitignore delete mode 100644 esphome/components/espnow/automations,h diff --git a/esphome/components/espnow/.gitignore b/esphome/components/espnow/.gitignore new file mode 100644 index 0000000000..d8b4157aef --- /dev/null +++ b/esphome/components/espnow/.gitignore @@ -0,0 +1,5 @@ +# Gitignore settings for ESPHome +# This is an example and may include too much for your use-case. +# You can modify this file to suit your needs. +/.esphome/ +/secrets.yaml diff --git a/esphome/components/espnow/__init__.py b/esphome/components/espnow/__init__.py index d38a41f46b..ec924fdf65 100644 --- a/esphome/components/espnow/__init__.py +++ b/esphome/components/espnow/__init__.py @@ -91,16 +91,20 @@ async def to_code(config): for conf in config.get(CONF_ON_SENT, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) - await automation.build_automation(trigger, [(ESPNowPacket, "it")], conf) + await automation.build_automation( + trigger, [(ESPNowPacketPtrConst, "packet"), (bool, "status")], conf + ) for conf in config.get(CONF_ON_RECEIVE, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) - await automation.build_automation(trigger, [(ESPNowPacket, "it")], conf) + await automation.build_automation( + trigger, [(ESPNowPacketPtrConst, "packet")], conf + ) for conf in config.get(CONF_ON_NEW_PEER, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) await automation.build_automation( - trigger, [(ESPNowPacket, "it"), (bool, "status")], conf + trigger, [(ESPNowPacketPtrConst, "packet")], conf ) for conf in config.get(CONF_PEERS, []): diff --git a/esphome/components/espnow/automations,h b/esphome/components/espnow/automations,h deleted file mode 100644 index 350b4eff74..0000000000 --- a/esphome/components/espnow/automations,h +++ /dev/null @@ -1,57 +0,0 @@ -#pragma once - -#include "esphome/components/esphome/esphome.h" -#include "esphome/core/automation.h" -#include "esphome/core/component.h" - -namespace esphome { -namespace espnow { - -template class NewPeerAction : public Action, public Parented { - public: - template void set_mac(V mac) { this->mac_ = mac; } - void play(Ts... x) override { - auto mac = this->mac_.value(x...); - parent_->add_peer(mac); - } - - protected: - TemplatableValue mac_{}; -}; - -template class DelPeerAction : public Action, public Parented { - public: - template void set_mac(V mac) { this->mac_ = mac; } - void play(Ts... x) override { - auto mac = this->mac_.value(x...); - parent_->del_peer(mac); - } - - protected: - TemplatableValue mac_{}; -}; - -class ESPNowSentTrigger : public Trigger { - public: - explicit ESPNowSentTrigger(ESPNowComponent *parent) { - parent->get_default_protocol()->add_on_sent_callback( - [this](ESPNowPacket *packet, bool status) { this->trigger(packet, status); }); - } -}; - -class ESPNowReceiveTrigger : public Trigger { - public: - explicit ESPNowReceiveTrigger(ESPNowComponent *parent) { - parent->get_default_protocol()->add_on_receive_callback([this](ESPNowPacket *packet) { this->trigger(packet); }); - } -}; - -class ESPNowNewPeerTrigger : public Trigger { - public: - explicit ESPNowNewPeerTrigger(ESPNowComponent *parent) { - parent->get_default_protocol()->add_on_peer_callback([this](ESPNowPacket *packet) { this->trigger(packet); }); - } -}; - -} -} diff --git a/esphome/components/espnow/espnow.cpp b/esphome/components/espnow/espnow.cpp index 693688d3f8..3c82162777 100644 --- a/esphome/components/espnow/espnow.cpp +++ b/esphome/components/espnow/espnow.cpp @@ -114,7 +114,7 @@ void ESPNowComponent::dump_config() { packet->content(5), packet->content(6), packet->content(7), packet->content(8), packet->content(9), packet->size()); - ESP_LOGI(TAG, "test: A:%06x R:%02x C:%04x S:%02d", packet->app_id(), packet->packet_id(), packet->crc16(), + ESP_LOGI(TAG, "test: A:%06x R:%02x C:%04x S:%02d", packet->protocol_id(), packet->packet_id(), packet->crc(), packet->size()); ESP_LOGI(TAG, "test: is_valid: %s", packet->is_valid() ? "Yes" : "No"); // ESP_LOGCONFIG(TAG, " WiFi Channel: %n", WiFi.channel()); @@ -232,11 +232,11 @@ esp_err_t ESPNowComponent::del_peer(uint64_t addr) { } ESPNowDefaultProtocol *ESPNowComponent::get_default_protocol() { - if (this->protocols_[ESPNOW_DEFAULT_APP_ID] == nullptr) { + if (this->protocols_[ESPNOW_MAIN_PROTOCOL_ID] == nullptr) { ESPNowDefaultProtocol *tmp = new ESPNowDefaultProtocol(); this->register_protocol(tmp); } - return (ESPNowDefaultProtocol *) this->protocols_[ESPNOW_DEFAULT_APP_ID]; + return (ESPNowDefaultProtocol *) this->protocols_[ESPNOW_MAIN_PROTOCOL_ID]; } ESPNowProtocol *ESPNowComponent::get_protocol_(uint32_t protocol) { @@ -317,7 +317,7 @@ bool ESPNowComponent::write(ESPNowPacket *packet) { ESP_LOGW(TAG, "Packet is invalid. maybe you need to ::calc_crc(). the packat before writing."); } else if (this->use_sent_check_) { xQueueSendToBack(this->send_queue_, packet->retrieve(), 10); - ESP_LOGVV(TAG, "Send (0x%04x.%d): 0x%12x. Buffer Used: %d", packet->packet_id, packet->attempts(), packet->peer(), + ESP_LOGVV(TAG, "Send (0x%04x.%d): 0x%12x. Buffer Used: %d", packet->packet_id(), packet->attempts(), packet->peer(), this->send_queue_used()); return true; } else { @@ -376,7 +376,7 @@ void ESPNowComponent::runner() { ESP_LOGV(TAG, "S: 0x%04x.%d. Wait for conformation. M: %s", packet->packet_id(), packet->attempts(), packet->content_bytes()); } else { - ESP_LOGE(TAG, "S: 0x%04x.%d B: %d.", packet->packet_id(), packet->attempts(), this.send_queue_used()); + ESP_LOGE(TAG, "S: 0x%04x.%d B: %d.", packet->packet_id(), packet->attempts(), this->send_queue_used()); this->unlock(); } } @@ -398,7 +398,7 @@ void ESPNowComponent::on_data_sent(const uint8_t *mac_addr, esp_now_send_status_ ESP_LOGE(TAG, "sent packet failed (0x%04x.%d)", packet->packet_id(), packet->attempts()); } else if (packet->peer() != mac64) { ESP_LOGE(TAG, " Invalid mac address. (0x%04x.%d) expected: 0x%12x got: 0x%12x", packet->packet_id(), - packet->retrys, packet->peer(), mac64); + packet->attempts(), packet->peer(), mac64); } else { ESP_LOGV(TAG, "Confirm sent (0x%04x.%d)", packet->packet_id(), packet->attempts()); global_esp_now->defer([packet]() { diff --git a/esphome/components/espnow/espnow.h b/esphome/components/espnow/espnow.h index b94463e7c5..08db1c81d5 100644 --- a/esphome/components/espnow/espnow.h +++ b/esphome/components/espnow/espnow.h @@ -37,7 +37,7 @@ static const uint8_t MAX_ESPNOW_DATA_SIZE = 240; static const uint8_t MAX_NUMBER_OF_RETRYS = 5; static const uint32_t TRANSPORT_HEADER = 0xC19983; -static const uint32_t ESPNOW_DEFAULT_APP_ID = 0x11CFAF; +static const uint32_t ESPNOW_MAIN_PROTOCOL_ID = 0x11CFAF; static uint8_t last_ref_id = 0; @@ -188,7 +188,7 @@ class ESPNowDefaultProtocol : public ESPNowProtocol { void on_sent(ESPNowPacket *packet, bool status) override { this->on_sent_.call(packet, status); }; void on_new_peer(ESPNowPacket *packet) override { this->on_new_peer_.call(packet); }; - uint32_t get_protocol_id() override { return ESPNOW_DEFAULT_APP_ID; }; + uint32_t get_protocol_id() override { return ESPNOW_MAIN_PROTOCOL_ID; }; void add_on_sent_callback(std::function &&callback) { this->on_sent_.add(std::move(callback)); @@ -305,6 +305,52 @@ template class SendAction : public Action, public Parente std::vector data_static_{}; }; +template class NewPeerAction : public Action, public Parented { + public: + template void set_mac(V mac) { this->mac_ = mac; } + void play(Ts... x) override { + auto mac = this->mac_.value(x...); + parent_->add_peer(mac); + } + + protected: + TemplatableValue mac_{}; +}; + +template class DelPeerAction : public Action, public Parented { + public: + template void set_mac(V mac) { this->mac_ = mac; } + void play(Ts... x) override { + auto mac = this->mac_.value(x...); + parent_->del_peer(mac); + } + + protected: + TemplatableValue mac_{}; +}; + +class ESPNowSentTrigger : public Trigger { + public: + explicit ESPNowSentTrigger(ESPNowComponent *parent) { + parent->get_default_protocol()->add_on_sent_callback( + [this](ESPNowPacket *packet, bool status) { this->trigger(packet, status); }); + } +}; + +class ESPNowReceiveTrigger : public Trigger { + public: + explicit ESPNowReceiveTrigger(ESPNowComponent *parent) { + parent->get_default_protocol()->add_on_receive_callback([this](ESPNowPacket *packet) { this->trigger(packet); }); + } +}; + +class ESPNowNewPeerTrigger : public Trigger { + public: + explicit ESPNowNewPeerTrigger(ESPNowComponent *parent) { + parent->get_default_protocol()->add_on_peer_callback([this](ESPNowPacket *packet) { this->trigger(packet); }); + } +}; + extern ESPNowComponent *global_esp_now; } // namespace espnow diff --git a/esphome/components/espnow/test.yaml b/esphome/components/espnow/test.yaml index b04fc016ff..988b441e52 100644 --- a/esphome/components/espnow/test.yaml +++ b/esphome/components/espnow/test.yaml @@ -19,7 +19,7 @@ espnow: on_receive: - logger.log: format: "Received: %s RSSI: %d" - args: [it->data().data(), it->rssi()] + args: [packet->contest_bytes(), packet->rssi()] binary_sensor: - platform: gpio