ESP-NOW chat & P4 fixes (#454)
* Espnow chat fix Fixes chat app not appearing and coexistence with wifi STA mode. Wifi OFF and obviously not connected, espnow starts. Wifi ON and connected, espnow starts. Wifi ON and NOT connected, espnow starts. * Fix P4 build
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
#include <sdkconfig.h>
|
||||
#endif
|
||||
|
||||
#if defined(CONFIG_TT_WIFI_ENABLED) && !defined(CONFIG_ESP_WIFI_REMOTE_ENABLED)
|
||||
#if defined(CONFIG_SOC_WIFI_SUPPORTED) && !defined(CONFIG_SLAVE_SOC_WIFI_SUPPORTED)
|
||||
|
||||
#include <Tactility/service/espnow/EspNow.h>
|
||||
#include <Tactility/service/espnow/EspNowService.h>
|
||||
@@ -81,4 +81,4 @@ void unsubscribeReceiver(ReceiverSubscription subscription) {
|
||||
|
||||
}
|
||||
|
||||
#endif // ESP_PLATFORM
|
||||
#endif // CONFIG_SOC_WIFI_SUPPORTED && !CONFIG_SLAVE_SOC_WIFI_SUPPORTED
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <sdkconfig.h>
|
||||
#endif
|
||||
|
||||
#if defined(CONFIG_TT_WIFI_ENABLED) && !defined(CONFIG_ESP_WIFI_REMOTE_ENABLED)
|
||||
#if defined(CONFIG_SOC_WIFI_SUPPORTED) && !defined(CONFIG_SLAVE_SOC_WIFI_SUPPORTED)
|
||||
|
||||
#include <Tactility/Logger.h>
|
||||
#include <Tactility/Tactility.h>
|
||||
@@ -208,4 +208,4 @@ extern const ServiceManifest manifest = {
|
||||
|
||||
}
|
||||
|
||||
#endif // ESP_PLATFORM
|
||||
#endif // CONFIG_SOC_WIFI_SUPPORTED && !CONFIG_SLAVE_SOC_WIFI_SUPPORTED
|
||||
|
||||
@@ -2,9 +2,8 @@
|
||||
#include <sdkconfig.h>
|
||||
#endif
|
||||
|
||||
#if defined(CONFIG_TT_WIFI_ENABLED) && !defined(CONFIG_ESP_WIFI_REMOTE_ENABLED)
|
||||
#if defined(CONFIG_SOC_WIFI_SUPPORTED) && !defined(CONFIG_SLAVE_SOC_WIFI_SUPPORTED)
|
||||
|
||||
#include <Tactility/kernel/Kernel.h>
|
||||
#include <Tactility/Logger.h>
|
||||
#include <Tactility/service/espnow/EspNow.h>
|
||||
#include <Tactility/service/wifi/Wifi.h>
|
||||
@@ -15,74 +14,47 @@
|
||||
namespace tt::service::espnow {
|
||||
|
||||
static const auto LOGGER = Logger("EspNowService");
|
||||
|
||||
static bool disableWifiService() {
|
||||
auto wifi_state = wifi::getRadioState();
|
||||
if (wifi_state != wifi::RadioState::Off && wifi_state != wifi::RadioState::OffPending) {
|
||||
wifi::setEnabled(false);
|
||||
}
|
||||
|
||||
if (wifi::getRadioState() == wifi::RadioState::Off) {
|
||||
return true;
|
||||
} else {
|
||||
TickType_t timeout_time = kernel::getTicks() + kernel::millisToTicks(2000);
|
||||
while (kernel::getTicks() < timeout_time && wifi::getRadioState() != wifi::RadioState::Off) {
|
||||
kernel::delayTicks(50);
|
||||
}
|
||||
|
||||
return wifi::getRadioState() == wifi::RadioState::Off;
|
||||
}
|
||||
}
|
||||
static bool wifiStartedByEspNow = false;
|
||||
|
||||
bool initWifi(const EspNowConfig& config) {
|
||||
// ESP-NOW can coexist with WiFi STA mode; only preserve WiFi state if already connected
|
||||
auto wifi_state = wifi::getRadioState();
|
||||
bool wifi_was_connected = (wifi_state == wifi::RadioState::ConnectionActive);
|
||||
|
||||
// If WiFi is off or in other states, temporarily disable it to initialize ESP-NOW
|
||||
// If WiFi is already connected, keep it running and just add ESP-NOW on top
|
||||
if (!wifi_was_connected && wifi_state != wifi::RadioState::Off && wifi_state != wifi::RadioState::OffPending) {
|
||||
if (!disableWifiService()) {
|
||||
LOGGER.error("Failed to disable wifi");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
bool wifi_already_running = (wifi_state != wifi::RadioState::Off && wifi_state != wifi::RadioState::OffPending);
|
||||
|
||||
wifi_mode_t mode;
|
||||
if (config.mode == Mode::Station) {
|
||||
// Use STA mode to allow coexistence with normal WiFi connection
|
||||
mode = wifi_mode_t::WIFI_MODE_STA;
|
||||
} else {
|
||||
mode = wifi_mode_t::WIFI_MODE_AP;
|
||||
}
|
||||
|
||||
// Only reinitialize WiFi if it's not already running
|
||||
if (wifi::getRadioState() == wifi::RadioState::Off) {
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
if (esp_wifi_init(&cfg) != ESP_OK) {
|
||||
LOGGER.error("esp_wifi_init() failed");
|
||||
return false;
|
||||
}
|
||||
// Only initialize WiFi if it's not already running; ESP-NOW coexists with STA mode
|
||||
wifiStartedByEspNow = !wifi_already_running;
|
||||
if (wifiStartedByEspNow) {
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
if (esp_wifi_init(&cfg) != ESP_OK) {
|
||||
LOGGER.error("esp_wifi_init() failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (esp_wifi_set_storage(WIFI_STORAGE_RAM) != ESP_OK) {
|
||||
LOGGER.error("esp_wifi_set_storage() failed");
|
||||
return false;
|
||||
}
|
||||
if (esp_wifi_set_storage(WIFI_STORAGE_RAM) != ESP_OK) {
|
||||
LOGGER.error("esp_wifi_set_storage() failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (esp_wifi_set_mode(mode) != ESP_OK) {
|
||||
LOGGER.error("esp_wifi_set_mode() failed");
|
||||
return false;
|
||||
}
|
||||
if (esp_wifi_set_mode(mode) != ESP_OK) {
|
||||
LOGGER.error("esp_wifi_set_mode() failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (esp_wifi_start() != ESP_OK) {
|
||||
LOGGER.error("esp_wifi_start() failed");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (esp_wifi_start() != ESP_OK) {
|
||||
LOGGER.error("esp_wifi_start() failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (esp_wifi_set_channel(config.channel, WIFI_SECOND_CHAN_NONE) != ESP_OK) {
|
||||
LOGGER.error("esp_wifi_set_channel() failed");
|
||||
return false;
|
||||
if (esp_wifi_set_channel(config.channel, WIFI_SECOND_CHAN_NONE) != ESP_OK) {
|
||||
LOGGER.error("esp_wifi_set_channel() failed");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (config.longRange) {
|
||||
@@ -98,22 +70,22 @@ bool initWifi(const EspNowConfig& config) {
|
||||
}
|
||||
}
|
||||
|
||||
LOGGER.info("WiFi initialized for ESP-NOW (preserved existing connection: {})", wifi_was_connected ? "yes" : "no");
|
||||
LOGGER.info("WiFi initialized for ESP-NOW (wifi already running: {})", wifi_already_running ? "yes" : "no");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool deinitWifi() {
|
||||
// Don't deinitialize WiFi completely - just disable ESP-NOW
|
||||
// This allows normal WiFi connection to continue
|
||||
// Only stop/deinit if WiFi was originally off
|
||||
|
||||
// Since we're only using WiFi for ESP-NOW, we can safely keep it in a minimal state
|
||||
// or shut it down. For now, keep it running to support STA + ESP-NOW coexistence.
|
||||
|
||||
LOGGER.info("ESP-NOW WiFi deinitialized (WiFi service continues independently)");
|
||||
if (wifiStartedByEspNow) {
|
||||
esp_wifi_stop();
|
||||
esp_wifi_deinit();
|
||||
wifiStartedByEspNow = false;
|
||||
LOGGER.info("WiFi stopped (was started by ESP-NOW)");
|
||||
} else {
|
||||
LOGGER.info("WiFi left running (managed by WiFi service)");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace tt::service::espnow
|
||||
|
||||
#endif // ESP_PLATFORM
|
||||
#endif // CONFIG_SOC_WIFI_SUPPORTED && !CONFIG_SLAVE_SOC_WIFI_SUPPORTED
|
||||
|
||||
Reference in New Issue
Block a user