couple of other fixes

This commit is contained in:
NP v/d Spek 2024-09-19 16:41:45 +02:00
parent 5301df8c52
commit 93b11bb7d5
6 changed files with 67 additions and 69 deletions

5
esphome/components/espnow/.gitignore vendored Normal file
View file

@ -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

View file

@ -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, []):

View file

@ -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<typename... Ts> class NewPeerAction : public Action<Ts...>, public Parented<ESPNowComponent> {
public:
template<typename V> 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<uint64_t, Ts...> mac_{};
};
template<typename... Ts> class DelPeerAction : public Action<Ts...>, public Parented<ESPNowComponent> {
public:
template<typename V> 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<uint64_t, Ts...> mac_{};
};
class ESPNowSentTrigger : public Trigger<ESPNowPacket *, bool> {
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<ESPNowPacket *> {
public:
explicit ESPNowReceiveTrigger(ESPNowComponent *parent) {
parent->get_default_protocol()->add_on_receive_callback([this](ESPNowPacket *packet) { this->trigger(packet); });
}
};
class ESPNowNewPeerTrigger : public Trigger<ESPNowPacket *> {
public:
explicit ESPNowNewPeerTrigger(ESPNowComponent *parent) {
parent->get_default_protocol()->add_on_peer_callback([this](ESPNowPacket *packet) { this->trigger(packet); });
}
};
}
}

View file

@ -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]() {

View file

@ -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<void(ESPNowPacket *, bool status)> &&callback) {
this->on_sent_.add(std::move(callback));
@ -305,6 +305,52 @@ template<typename... Ts> class SendAction : public Action<Ts...>, public Parente
std::vector<uint8_t> data_static_{};
};
template<typename... Ts> class NewPeerAction : public Action<Ts...>, public Parented<ESPNowComponent> {
public:
template<typename V> 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<uint64_t, Ts...> mac_{};
};
template<typename... Ts> class DelPeerAction : public Action<Ts...>, public Parented<ESPNowComponent> {
public:
template<typename V> 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<uint64_t, Ts...> mac_{};
};
class ESPNowSentTrigger : public Trigger<ESPNowPacket *, bool> {
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<ESPNowPacket *> {
public:
explicit ESPNowReceiveTrigger(ESPNowComponent *parent) {
parent->get_default_protocol()->add_on_receive_callback([this](ESPNowPacket *packet) { this->trigger(packet); });
}
};
class ESPNowNewPeerTrigger : public Trigger<ESPNowPacket *> {
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

View file

@ -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