mirror of
https://github.com/esphome/esphome.git
synced 2024-12-28 16:31:44 +01:00
wip
This commit is contained in:
parent
a271adad3b
commit
9e4b7f954d
2 changed files with 72 additions and 43 deletions
|
@ -55,7 +55,7 @@ struct {
|
||||||
} __attribute__((packed)) espnow_frame_format_t;
|
} __attribute__((packed)) espnow_frame_format_t;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
ESPNowPacket::ESPNowPacket(uint64_t mac64, const uint8_t *data, uint8_t size, uint32_t app_id)
|
ESPNowPacket::ESPNowPacket(uint64_t peer, 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;
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#include "esphome/core/automation.h"
|
#include "esphome/core/automation.h"
|
||||||
#include "esphome/core/component.h"
|
#include "esphome/core/component.h"
|
||||||
#include "esphome/core/helpers.h"
|
#include "esphome/core/helpers.h"
|
||||||
|
#include "esphome/core/bytebuffer.h"
|
||||||
|
|
||||||
#include <esp_now.h>
|
#include <esp_now.h>
|
||||||
#include <esp_crc.h>
|
#include <esp_crc.h>
|
||||||
|
@ -23,6 +24,15 @@ typedef uint8_t espnow_addr_t[6];
|
||||||
|
|
||||||
static const uint64_t ESPNOW_BROADCAST_ADDR = 0xFFFFFFFFFFFF;
|
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 ESPNOW_DATA_HEADER = 0x00;
|
||||||
|
static const uint8_t ESPNOW_DATA_PROTOCOL = 0x03;
|
||||||
|
static const uint8_t ESPNOW_DATA_PACKET = 0x07;
|
||||||
|
static const uint8_t ESPNOW_DATA_CRC = 0x08;
|
||||||
|
static const uint8_t ESPNOW_DATA_CONTENT = 0x0A;
|
||||||
|
|
||||||
|
static const uint8_t MAX_ESPNOW_DATA_SIZE = 240;
|
||||||
|
|
||||||
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;
|
||||||
|
@ -40,63 +50,82 @@ template<typename... Args> std::string string_format(const std::string &format,
|
||||||
|
|
||||||
static uint8_t last_ref_id = 0;
|
static uint8_t last_ref_id = 0;
|
||||||
|
|
||||||
struct ESPNowPacket {
|
class ESPNowPacket {
|
||||||
uint64_t mac64 = 0;
|
private:
|
||||||
uint8_t size = 0;
|
espnow_addr_t peer_{0};
|
||||||
uint8_t rssi = 0;
|
uint8_t rssi_{0};
|
||||||
uint8_t retrys : 4;
|
uint8_t retrys_{0};
|
||||||
uint8_t is_broadcast : 1;
|
bool is_broadcast_{false};
|
||||||
uint8_t dummy : 3;
|
uint32_t timestamp_{0};
|
||||||
uint32_t timestamp = 0;
|
ByteBuffer content_(251);
|
||||||
|
|
||||||
union {
|
public:
|
||||||
uint8_t content[MAX_ESPNOW_DATA_SIZE + 11];
|
ESPNowPacket() ESPHOME_ALWAYS_INLINE { this->content_.put_uint24(TRANSPORT_HEADER); }
|
||||||
struct {
|
// Create packet to be send.
|
||||||
uint8_t header[3] = {0xC1, 0x99, 0x83};
|
ESPNowPacket(espnow_addr_t mac64, const uint8_t *data, uint8_t size, uint32_t app_id);
|
||||||
uint32_t app_id = 0xFFFFFF;
|
|
||||||
uint8_t ref_id = 0x99;
|
|
||||||
uint16_t crc16 = 0x1234;
|
|
||||||
uint8_t data[MAX_ESPNOW_DATA_SIZE];
|
|
||||||
uint8_t space = 0;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
inline ESPNowPacket() ESPHOME_ALWAYS_INLINE : retrys(0) {}
|
// Load received packet's.
|
||||||
inline ESPNowPacket(uint64_t mac64, const uint8_t *data, uint8_t size, uint32_t app_id);
|
ESPNowPacket(espnow_addr_t mac64, const uint8_t *data, uint8_t size);
|
||||||
|
|
||||||
inline void info(std::string place);
|
void info(std::string place);
|
||||||
|
|
||||||
inline void get_mac(espnow_addr_t *mac_addres) { std::memcpy(mac_addres, &mac64, 6); }
|
uint8_T *peer() { return this->peer_; }
|
||||||
inline void set_mac(espnow_addr_t *mac_addres) { this->mac64 = this->to_mac64(mac_addres); }
|
void peer(espnow_addr_t mac_addres) { std::memcpy(&this->peer_, &mac_addres, 6); }
|
||||||
|
|
||||||
uint64_t to_mac64(espnow_addr_t *mac_addres) {
|
uint8_T size() { return this->content_; }
|
||||||
uint64_t result;
|
|
||||||
std::memcpy(&result, mac_addres, 6);
|
bool broadcast(){return this->is_broadcast_};
|
||||||
return result;
|
void broadcast(bool state) { this->is_broadcast_ = state; }
|
||||||
|
|
||||||
|
uint32_t timestamp() { return this->timestamp_; };
|
||||||
|
void timestamp(uint32_t timestamp) { this->timestamp_ = timestamp; };
|
||||||
|
|
||||||
|
uint32_t protocol() {
|
||||||
|
this->content_->set_position(ESPNOW_DATA_PROTOCOL);
|
||||||
|
return this->content_->get_uint24();
|
||||||
|
}
|
||||||
|
void protocol(uint32_t protocol) {
|
||||||
|
this->content_->set_position(ESPNOW_DATA_PROTOCOL);
|
||||||
|
this->content_->put_uint24(protocol);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t packet_id() {
|
||||||
|
this->content_->set_position(ESPNOW_DATA_PACKET);
|
||||||
|
return this->content_->get_uint8();
|
||||||
|
}
|
||||||
|
void packet_id(uint8_t packet_id) {
|
||||||
|
this->content_->set_position(ESPNOW_DATA_PACKET);
|
||||||
|
this->content_->put_uint8(packet_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t crc() {
|
||||||
|
this->content_->set_position(ESPNOW_DATA_CRC);
|
||||||
|
return this->content_->get_uint16();
|
||||||
|
}
|
||||||
|
void crc(uint16_t crc) {
|
||||||
|
this->content_->set_position(ESPNOW_DATA_CRC);
|
||||||
|
this->content_->put_uint16(crc);
|
||||||
|
}
|
||||||
|
|
||||||
|
ByteBuffer *content() {
|
||||||
|
this->content_.set_position(ESPNOW_DATA_CONTENT + this->size_);
|
||||||
|
return &this->content_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void retry() {
|
void retry() {
|
||||||
if (this->retrys < 7) {
|
if (this->retrys_ < 7) {
|
||||||
retrys = retrys + 1;
|
this->retrys_ = this->retrys_ + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void recalc() {
|
void calc_crc() {
|
||||||
crc16 = 0;
|
this->crc(0);
|
||||||
crc16 = esp_crc16_le(ref_id, (uint8_t *) &content, 10 + size);
|
this->crc(esp_crc16_le(this->packet(), this->dataptr(), 10 + size()));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool is_valid();
|
bool is_valid();
|
||||||
|
|
||||||
inline std::string to_str(uint64_t mac64 = 0) {
|
uint8_t *dataptr() { return this->content_->get_data()->data(); }
|
||||||
espnow_addr_t mac;
|
|
||||||
if (mac64 == 0)
|
|
||||||
mac64 = this->mac64;
|
|
||||||
memcpy((void *) &mac, &mac64, 6);
|
|
||||||
return string_format("{\"%02x:%02x:%02x:%02x:%02x:%02x\"}", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline uint8_t *dataptr() { return (uint8_t *) &content; }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class ESPNowComponent;
|
class ESPNowComponent;
|
||||||
|
|
Loading…
Reference in a new issue