фиксы до работы с перемычкой вместо оптики

This commit is contained in:
2026-08-05 18:28:17 +03:00
parent e5c8c45dcc
commit c4c9167df1
17 changed files with 263 additions and 85 deletions

View File

@@ -1,5 +1,6 @@
#include "Radio.h"
#include "Config.h"
#include "Log.h"
#include <WiFi.h>
#include <esp_wifi.h>
#include <string.h>
@@ -8,21 +9,26 @@ Radio *Radio::instance_ = nullptr;
static const uint8_t BROADCAST_MAC[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
bool Radio::begin() {
if (active_) return true;
if (active_) { Log::event("ESP-NOW", "already active"); return true; }
WiFi.mode(WIFI_STA); WiFi.disconnect();
if (esp_wifi_set_channel(ESPNOW_WIFI_CHANNEL, WIFI_SECOND_CHAN_NONE) != ESP_OK) return false;
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_ || esp_now_init() != ESP_OK) return false;
if (!queue_ || esp_now_init() != ESP_OK) { Log::event("ESP-NOW", "initialization FAILED"); return false; }
instance_ = this;
if (esp_now_register_recv_cb(onReceive) != ESP_OK) { end(); return false; }
active_ = true;
return ensurePeer(BROADCAST_MAC);
const bool ok = ensurePeer(BROADCAST_MAC);
Log::printf("ESP-NOW", "started channel=%u broadcast-peer=%s", ESPNOW_WIFI_CHANNEL, ok ? "OK" : "FAILED");
return ok;
}
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;
Log::event("ESP-NOW", "stopped");
}
bool Radio::ensurePeer(const uint8_t mac[6]) {
@@ -35,9 +41,16 @@ bool Radio::ensurePeer(const uint8_t mac[6]) {
bool Radio::sendBroadcast(ProtocolPacket p) { return sendTo(BROADCAST_MAC, p); }
bool Radio::sendTo(const uint8_t mac[6], ProtocolPacket p) {
if (!active_ || !ensurePeer(mac)) return false;
char peer[20]; macText(mac, peer, sizeof(peer));
if (!active_ || !ensurePeer(mac)) {
Log::printf("ESP-NOW", "TX %s to %s FAILED: inactive/peer", messageName(static_cast<MessageType>(p.type)), peer);
return false;
}
finalizePacket(p);
return esp_now_send(mac, reinterpret_cast<const uint8_t *>(&p), sizeof(p)) == ESP_OK;
const bool ok = esp_now_send(mac, reinterpret_cast<const uint8_t *>(&p), sizeof(p)) == ESP_OK;
Log::printf("ESP-NOW", "TX %s to %s session=%08lX stage=%u seq=%u %s",
messageName(static_cast<MessageType>(p.type)), peer, p.session, p.stage, p.sequence, ok ? "QUEUED" : "FAILED");
return ok;
}
bool Radio::receive(ReceivedPacket &r) {