mirror of
https://github.com/esphome/esphome.git
synced 2025-02-08 14:10:54 +00:00
update code with given CI suggestions
This commit is contained in:
parent
d193d44e13
commit
b22dacc0f2
@ -57,11 +57,11 @@ struct {
|
|||||||
|
|
||||||
void ESPNowProtocol::setup() { parent_->register_protocol(this); }
|
void ESPNowProtocol::setup() { parent_->register_protocol(this); }
|
||||||
|
|
||||||
bool ESPNowProtocol::write(const uint64_t mac_address, const uint8_t *data, uint8_t len) {
|
bool ESPNowProtocol::write(uint64_t mac_address, const uint8_t *data, uint8_t len) {
|
||||||
ESPNowPacket packet(mac_address, data, len, this->get_app_id());
|
ESPNowPacket packet(mac_address, data, len, this->get_app_id());
|
||||||
return this->parent_->write(packet);
|
return this->parent_->write(packet);
|
||||||
}
|
}
|
||||||
bool ESPNowProtocol::write(const uint64_t mac_address, const std::vector<uint8_t> data) {
|
bool ESPNowProtocol::write(uint64_t mac_address, std::vector<uint8_t> &data) {
|
||||||
ESPNowPacket packet(mac_address, (uint8_t *) data.data(), (uint8_t) data.size(), this->get_app_id());
|
ESPNowPacket packet(mac_address, (uint8_t *) data.data(), (uint8_t) data.size(), this->get_app_id());
|
||||||
return this->parent_->write(packet);
|
return this->parent_->write(packet);
|
||||||
}
|
}
|
||||||
@ -102,7 +102,7 @@ void ESPNowComponent::setup() {
|
|||||||
ESP_LOGI(TAG, "Setting up ESP-NOW...");
|
ESP_LOGI(TAG, "Setting up ESP-NOW...");
|
||||||
|
|
||||||
#ifdef USE_WIFI
|
#ifdef USE_WIFI
|
||||||
global_wifi_component.disable();
|
wifi::global_wifi_component.disable();
|
||||||
#else // Set device as a Wi-Fi Station
|
#else // Set device as a Wi-Fi Station
|
||||||
esp_event_loop_create_default();
|
esp_event_loop_create_default();
|
||||||
|
|
||||||
@ -181,13 +181,13 @@ esp_err_t ESPNowComponent::add_peer(uint64_t addr) {
|
|||||||
uint8_t mac[6];
|
uint8_t mac[6];
|
||||||
this->del_peer(addr);
|
this->del_peer(addr);
|
||||||
|
|
||||||
esp_now_peer_info_t peerInfo = {};
|
esp_now_peer_info_t peer_info = {};
|
||||||
memset(&peerInfo, 0, sizeof(esp_now_peer_info_t));
|
memset(&peer_info, 0, sizeof(esp_now_peer_info_t));
|
||||||
peerInfo.channel = this->wifi_channel_;
|
peer_info.channel = this->wifi_channel_;
|
||||||
peerInfo.encrypt = false;
|
peer_info.encrypt = false;
|
||||||
memcpy((void *) peerInfo.peer_addr, (void *) &addr, 6);
|
memcpy((void *) peer_info.peer_addr, (void *) &addr, 6);
|
||||||
|
|
||||||
return esp_now_add_peer(&peerInfo);
|
return esp_now_add_peer(&peer_info);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -199,7 +199,7 @@ esp_err_t ESPNowComponent::del_peer(uint64_t addr) {
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
ESPNowDefaultProtocol *ESPNowComponent::get_defaultProtocol_() {
|
ESPNowDefaultProtocol *ESPNowComponent::get_default_protocol() {
|
||||||
if (this->protocols_[ESPNOW_DEFAULT_APP_ID] == nullptr) {
|
if (this->protocols_[ESPNOW_DEFAULT_APP_ID] == nullptr) {
|
||||||
ESPNowDefaultProtocol *tmp = new ESPNowDefaultProtocol();
|
ESPNowDefaultProtocol *tmp = new ESPNowDefaultProtocol();
|
||||||
this->protocols_[ESPNOW_DEFAULT_APP_ID] = tmp;
|
this->protocols_[ESPNOW_DEFAULT_APP_ID] = tmp;
|
||||||
@ -240,7 +240,7 @@ void ESPNowComponent::on_data_received(const uint8_t *addr, const uint8_t *data,
|
|||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
ESPNowPacket packet;
|
ESPNowPacket packet;
|
||||||
wifi_pkt_rx_ctrl_t *rx_ctrl = NULL;
|
wifi_pkt_rx_ctrl_t *rx_ctrl = nullptr;
|
||||||
|
|
||||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 1)
|
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 1)
|
||||||
uint8_t *addr = recv_info->src_addr;
|
uint8_t *addr = recv_info->src_addr;
|
||||||
|
@ -28,15 +28,15 @@ class ESPNowProtocol : public Parented<ESPNowComponent> {
|
|||||||
|
|
||||||
void setup();
|
void setup();
|
||||||
|
|
||||||
virtual void on_receive(ESPNowPacket packet) { return; };
|
virtual void on_receive(ESPNowPacket packet){};
|
||||||
virtual void on_sent(ESPNowPacket packet, bool status) { return; };
|
virtual void on_sent(ESPNowPacket packet, bool status){};
|
||||||
virtual void on_new_peer(ESPNowPacket packet) { return; };
|
virtual void on_new_peer(ESPNowPacket packet){};
|
||||||
|
|
||||||
virtual uint32_t get_app_id() = 0;
|
virtual uint32_t get_app_id() = 0;
|
||||||
uint8_t get_next_ref_id() { return next_ref_id_++; }
|
uint8_t get_next_ref_id() { return next_ref_id_++; }
|
||||||
|
|
||||||
bool write(const uint64_t mac_address, const uint8_t *data, uint8_t len);
|
bool write(uint64_t mac_address, const uint8_t *data, uint8_t len);
|
||||||
bool write(const uint64_t mac_address, const std::vector<uint8_t> data);
|
bool write(uint64_t mac_address, std::vector<uint8_t> &data);
|
||||||
bool write(ESPNowPacket packet);
|
bool write(ESPNowPacket packet);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
@ -45,9 +45,9 @@ class ESPNowProtocol : public Parented<ESPNowComponent> {
|
|||||||
|
|
||||||
class ESPNowDefaultProtocol : public ESPNowProtocol {
|
class ESPNowDefaultProtocol : public ESPNowProtocol {
|
||||||
public:
|
public:
|
||||||
void on_receive(ESPNowPacket packet) { this->on_receive_.call(packet); };
|
void on_receive(ESPNowPacket packet) override { this->on_receive_.call(packet); };
|
||||||
void on_sent(ESPNowPacket packet, bool status) { this->on_sent_.call(packet, status); };
|
void on_sent(ESPNowPacket packet, bool status) override { this->on_sent_.call(packet, status); };
|
||||||
void on_new_peer(ESPNowPacket packet) { this->on_new_peer_.call(packet); };
|
void on_new_peer(ESPNowPacket packet) override { this->on_new_peer_.call(packet); };
|
||||||
|
|
||||||
uint32_t get_app_id() override { return ESPNOW_DEFAULT_APP_ID; };
|
uint32_t get_app_id() override { return ESPNOW_DEFAULT_APP_ID; };
|
||||||
|
|
||||||
@ -96,7 +96,7 @@ class ESPNowComponent : public Component {
|
|||||||
|
|
||||||
void register_protocol(ESPNowProtocol *protocol) {
|
void register_protocol(ESPNowProtocol *protocol) {
|
||||||
protocol->set_parent(this);
|
protocol->set_parent(this);
|
||||||
this->protocols_[protocol->get_app_id()] = std::move(protocol);
|
this->protocols_[protocol->get_app_id()] = protocol;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t add_peer(uint64_t addr);
|
esp_err_t add_peer(uint64_t addr);
|
||||||
@ -111,7 +111,7 @@ class ESPNowComponent : public Component {
|
|||||||
bool is_locked() { return this->lock_; }
|
bool is_locked() { return this->lock_; }
|
||||||
void unlock() { this->lock_ = false; }
|
void unlock() { this->lock_ = false; }
|
||||||
|
|
||||||
ESPNowDefaultProtocol *get_defaultProtocol_();
|
ESPNowDefaultProtocol *get_default_protocol();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool validate_channel_(uint8_t channel);
|
bool validate_channel_(uint8_t channel);
|
||||||
@ -151,10 +151,10 @@ template<typename... Ts> class SendAction : public Action<Ts...>, public Parente
|
|||||||
auto mac = this->mac_.value(x...);
|
auto mac = this->mac_.value(x...);
|
||||||
|
|
||||||
if (this->static_) {
|
if (this->static_) {
|
||||||
this->parent_->get_defaultProtocol_()->write(mac, this->data_static_);
|
this->parent_->get_default_protocol()->write(mac, this->data_static_);
|
||||||
} else {
|
} else {
|
||||||
auto val = this->data_func_(x...);
|
auto val = this->data_func_(x...);
|
||||||
this->parent_->get_defaultProtocol_()->write(mac, val);
|
this->parent_->get_default_protocol()->write(mac, val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -192,7 +192,7 @@ template<typename... Ts> class DelPeerAction : public Action<Ts...>, public Pare
|
|||||||
class ESPNowSentTrigger : public Trigger<ESPNowPacket, bool> {
|
class ESPNowSentTrigger : public Trigger<ESPNowPacket, bool> {
|
||||||
public:
|
public:
|
||||||
explicit ESPNowSentTrigger(ESPNowComponent *parent) {
|
explicit ESPNowSentTrigger(ESPNowComponent *parent) {
|
||||||
parent->get_defaultProtocol_()->add_on_sent_callback(
|
parent->get_default_protocol()->add_on_sent_callback(
|
||||||
[this](ESPNowPacket value, bool status) { this->trigger(value, status); });
|
[this](ESPNowPacket value, bool status) { this->trigger(value, status); });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -200,14 +200,14 @@ class ESPNowSentTrigger : public Trigger<ESPNowPacket, bool> {
|
|||||||
class ESPNowReceiveTrigger : public Trigger<ESPNowPacket> {
|
class ESPNowReceiveTrigger : public Trigger<ESPNowPacket> {
|
||||||
public:
|
public:
|
||||||
explicit ESPNowReceiveTrigger(ESPNowComponent *parent) {
|
explicit ESPNowReceiveTrigger(ESPNowComponent *parent) {
|
||||||
parent->get_defaultProtocol_()->add_on_receive_callback([this](ESPNowPacket value) { this->trigger(value); });
|
parent->get_default_protocol()->add_on_receive_callback([this](ESPNowPacket value) { this->trigger(value); });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class ESPNowNewPeerTrigger : public Trigger<ESPNowPacket> {
|
class ESPNowNewPeerTrigger : public Trigger<ESPNowPacket> {
|
||||||
public:
|
public:
|
||||||
explicit ESPNowNewPeerTrigger(ESPNowComponent *parent) {
|
explicit ESPNowNewPeerTrigger(ESPNowComponent *parent) {
|
||||||
parent->get_defaultProtocol_()->add_on_peer_callback([this](ESPNowPacket value) { this->trigger(value); });
|
parent->get_default_protocol()->add_on_peer_callback([this](ESPNowPacket value) { this->trigger(value); });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
static const char *const TAG = "espnow_packet";
|
static const char *const TAG = "espnow_packet";
|
||||||
|
|
||||||
ESPNowPacket::ESPNowPacket(const uint64_t mac64, const uint8_t *data, uint8_t size, uint32_t app_id)
|
ESPNowPacket::ESPNowPacket(uint64_t mac64, const uint8_t *data, uint8_t size, uint32_t app_id)
|
||||||
: mac64(mac64), size(size), app_id(app_id), retrys(0) {
|
: mac64(mac64), size(size), app_id(app_id), retrys(0) {
|
||||||
if (this->mac64 == 0)
|
if (this->mac64 == 0)
|
||||||
this->mac64 = ESPNOW_BROADCAST_ADDR;
|
this->mac64 = ESPNOW_BROADCAST_ADDR;
|
||||||
@ -29,13 +29,13 @@ inline void ESPNowPacket::info(std::string place) {
|
|||||||
bool ESPNowPacket::is_valid() {
|
bool ESPNowPacket::is_valid() {
|
||||||
uint16_t crc = this->crc16;
|
uint16_t crc = this->crc16;
|
||||||
recalc();
|
recalc();
|
||||||
bool valid = (std::memcmp(&header, &transport_header, 3) == 0);
|
bool valid = (std::memcmp(&header, &TRANSPORT_HEADER, 3) == 0);
|
||||||
valid &= (this->app_id != 0);
|
valid &= (this->app_id != 0);
|
||||||
valid &= (this->crc16 == crc);
|
valid &= (this->crc16 == crc);
|
||||||
if (!valid) {
|
if (!valid) {
|
||||||
ESP_LOGV("Packet", "Invalid H:%02x%02x%02x A:%06x R:%02x C:%04x ipv. %04x, %d&%d&%d=%d\n", this->header[0],
|
ESP_LOGV("Packet", "Invalid H:%02x%02x%02x A:%06x R:%02x C:%04x ipv. %04x, %d&%d&%d=%d\n", this->header[0],
|
||||||
this->header[1], this->header[2], this->app_id, this->ref_id, crc, this->crc16,
|
this->header[1], this->header[2], this->app_id, this->ref_id, crc, this->crc16,
|
||||||
std::memcmp(&header, &transport_header, 3) == 0, (this->app_id != 0), (this->crc16 == crc), valid);
|
std::memcmp(&header, &TRANSPORT_HEADER, 3) == 0, (this->app_id != 0), (this->crc16 == crc), valid);
|
||||||
}
|
}
|
||||||
|
|
||||||
this->crc16 = crc;
|
this->crc16 = crc;
|
||||||
|
@ -24,7 +24,7 @@ static const uint64_t ESPNOW_BROADCAST_ADDR = 0xFFFFFFFFFFFF;
|
|||||||
static espnow_addr_t ESPNOW_ADDR_SELF = {0};
|
static espnow_addr_t ESPNOW_ADDR_SELF = {0};
|
||||||
static const uint8_t MAX_ESPNOW_DATA_SIZE = 240;
|
static const uint8_t MAX_ESPNOW_DATA_SIZE = 240;
|
||||||
|
|
||||||
static const uint32_t transport_header = 0xC19983;
|
static const uint32_t TRANSPORT_HEADER = 0xC19983;
|
||||||
|
|
||||||
template<typename... Args> std::string string_format(const std::string &format, Args... args) {
|
template<typename... Args> std::string string_format(const std::string &format, Args... args) {
|
||||||
int size_s = std::snprintf(nullptr, 0, format.c_str(), args...) + 1; // Extra space for '\0'
|
int size_s = std::snprintf(nullptr, 0, format.c_str(), args...) + 1; // Extra space for '\0'
|
||||||
@ -61,7 +61,7 @@ struct ESPNowPacket {
|
|||||||
};
|
};
|
||||||
|
|
||||||
ESPNowPacket() ESPHOME_ALWAYS_INLINE : retrys(0) {}
|
ESPNowPacket() ESPHOME_ALWAYS_INLINE : retrys(0) {}
|
||||||
ESPNowPacket(const uint64_t mac64, const uint8_t *data, uint8_t size, uint32_t app_id);
|
ESPNowPacket(uint64_t mac64, const uint8_t *data, uint8_t size, uint32_t app_id);
|
||||||
|
|
||||||
inline void info(std::string place);
|
inline void info(std::string place);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user