запущен тест между есп с перемычкой
This commit is contained in:
@@ -23,16 +23,22 @@ bool Radio::begin() {
|
||||
if (esp_wifi_set_channel(ESPNOW_WIFI_CHANNEL, WIFI_SECOND_CHAN_NONE) != ESP_OK) {
|
||||
Log::event("ESP-NOW", "Wi-Fi channel setup FAILED"); return false;
|
||||
}
|
||||
queue_ = xQueueCreate(8, sizeof(ReceivedPacket));
|
||||
if (!queue_) { Log::event("ESP-NOW", "receive queue creation FAILED"); return false; }
|
||||
if (!queue_) queue_ = xQueueCreate(8, sizeof(ReceivedPacket));
|
||||
if (!heartbeatQueue_) heartbeatQueue_ = xQueueCreate(4, sizeof(ReceivedPacket));
|
||||
if (!heartbeatTask_ && xTaskCreate(heartbeatTaskEntry, "radio-hb", 2048, this, 5, &heartbeatTask_) != pdPASS)
|
||||
heartbeatTask_ = nullptr;
|
||||
if (!queue_ || !heartbeatQueue_ || !heartbeatTask_) {
|
||||
Log::event("ESP-NOW", "receive/heartbeat service creation FAILED"); return false;
|
||||
}
|
||||
if (esp_now_init() != ESP_OK) {
|
||||
vQueueDelete(queue_); queue_ = nullptr;
|
||||
Log::event("ESP-NOW", "initialization FAILED"); return false;
|
||||
}
|
||||
const esp_err_t rateResult = esp_wifi_config_espnow_rate(WIFI_IF_STA, WIFI_PHY_RATE_1M_L);
|
||||
instance_ = this;
|
||||
if (esp_now_register_recv_cb(onReceive) != ESP_OK) { end(); return false; }
|
||||
active_ = true;
|
||||
if (esp_now_register_recv_cb(onReceive) != ESP_OK) {
|
||||
instance_ = nullptr; esp_now_deinit(); return false;
|
||||
}
|
||||
active_ = true; __atomic_store_n(&lastValidRxMs_, millis(), __ATOMIC_RELAXED);
|
||||
const bool ok = ensurePeer(BROADCAST_MAC);
|
||||
uint8_t primaryChannel = 0;
|
||||
wifi_second_chan_t secondaryChannel = WIFI_SECOND_CHAN_NONE;
|
||||
@@ -44,9 +50,11 @@ bool Radio::begin() {
|
||||
}
|
||||
|
||||
void Radio::end() {
|
||||
if (active_) { esp_now_unregister_recv_cb(); esp_now_deinit(); }
|
||||
if (queue_) { vQueueDelete(queue_); queue_ = nullptr; }
|
||||
active_ = false; if (instance_ == this) instance_ = nullptr;
|
||||
const bool wasActive = active_; active_ = false;
|
||||
if (wasActive) { esp_now_unregister_recv_cb(); esp_now_deinit(); }
|
||||
if (queue_) xQueueReset(queue_);
|
||||
if (heartbeatQueue_) xQueueReset(heartbeatQueue_);
|
||||
if (instance_ == this) instance_ = nullptr;
|
||||
Log::event("ESP-NOW", "stopped");
|
||||
}
|
||||
|
||||
@@ -72,7 +80,7 @@ bool Radio::sendTo(const uint8_t mac[6], ProtocolPacket p) {
|
||||
finalizePacket(p);
|
||||
const bool ok = esp_now_send(mac, reinterpret_cast<const uint8_t *>(&p), sizeof(p)) == ESP_OK;
|
||||
const MessageType type = static_cast<MessageType>(p.type);
|
||||
if (type != MessageType::HEARTBEAT && type != MessageType::HEARTBEAT_ACK)
|
||||
if (type != MessageType::HEARTBEAT && type != MessageType::HEARTBEAT_ACK && type != MessageType::PROGRESS)
|
||||
Log::printf("ESP-NOW", "TX %s to %s session=%08lX stage=%u seq=%u %s",
|
||||
messageName(type), peer, p.session, p.stage, p.sequence, ok ? "QUEUED" : "FAILED");
|
||||
return ok;
|
||||
@@ -97,16 +105,37 @@ void Radio::maintainChannel() {
|
||||
restored ? "RESTORED" : "FAILED");
|
||||
}
|
||||
|
||||
void Radio::flush() { if (queue_) xQueueReset(queue_); }
|
||||
void Radio::flush() {
|
||||
if (queue_) xQueueReset(queue_);
|
||||
if (heartbeatQueue_) xQueueReset(heartbeatQueue_);
|
||||
}
|
||||
|
||||
void Radio::onReceive(const esp_now_recv_info_t *info, const uint8_t *data, int length) {
|
||||
if (!instance_ || !instance_->queue_ || !info || length != sizeof(ProtocolPacket)) return;
|
||||
ReceivedPacket item;
|
||||
memcpy(item.mac, info->src_addr, 6); memcpy(&item.packet, data, sizeof(item.packet));
|
||||
if (!validPacket(item.packet)) return;
|
||||
__atomic_store_n(&instance_->lastValidRxMs_, millis(), __ATOMIC_RELAXED);
|
||||
if (static_cast<MessageType>(item.packet.type) == MessageType::HEARTBEAT && instance_->heartbeatQueue_)
|
||||
xQueueSend(instance_->heartbeatQueue_, &item, 0);
|
||||
xQueueSend(instance_->queue_, &item, 0); // Wi-Fi task callback: copy only, never block
|
||||
}
|
||||
|
||||
void Radio::heartbeatTaskEntry(void *context) {
|
||||
static_cast<Radio *>(context)->heartbeatTaskLoop();
|
||||
}
|
||||
|
||||
void Radio::heartbeatTaskLoop() {
|
||||
ReceivedPacket item;
|
||||
for (;;) {
|
||||
if (xQueueReceive(heartbeatQueue_, &item, portMAX_DELAY) != pdTRUE || !active_) continue;
|
||||
ProtocolPacket ack = item.packet;
|
||||
ack.type = static_cast<uint8_t>(MessageType::HEARTBEAT_ACK);
|
||||
finalizePacket(ack);
|
||||
esp_now_send(item.mac, reinterpret_cast<const uint8_t *>(&ack), sizeof(ack));
|
||||
}
|
||||
}
|
||||
|
||||
void Radio::macText(const uint8_t mac[6], char *out, size_t n) {
|
||||
snprintf(out, n, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user