From 0d16eb606f7ab2af0b6bb91bff88c96572b6a861 Mon Sep 17 00:00:00 2001 From: Shadowtrance Date: Sat, 24 Jan 2026 21:42:49 +1000 Subject: [PATCH] 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 --- Firmware/Kconfig | 7 -- .../Include/Tactility/service/espnow/EspNow.h | 4 +- Tactility/Source/Tactility.cpp | 8 +- Tactility/Source/app/chat/ChatApp.cpp | 4 +- Tactility/Source/service/espnow/EspNow.cpp | 4 +- .../Source/service/espnow/EspNowService.cpp | 4 +- .../Source/service/espnow/EspNowWifi.cpp | 102 +++++++----------- 7 files changed, 49 insertions(+), 84 deletions(-) diff --git a/Firmware/Kconfig b/Firmware/Kconfig index 1aded79f..649bde91 100644 --- a/Firmware/Kconfig +++ b/Firmware/Kconfig @@ -96,11 +96,4 @@ menu "Tactility App" help The minimum time to show the splash screen in milliseconds. When set to 0, startup will continue to desktop as soon as boot operations are finished. - - config TT_WIFI_ENABLED - bool "Enable WiFi Support" - default n - help - Enable WiFi support for Tactility. - Uses native WiFi on ESP32/ESP32-S3 or ESP-Hosted on ESP32-P4. endmenu diff --git a/Tactility/Include/Tactility/service/espnow/EspNow.h b/Tactility/Include/Tactility/service/espnow/EspNow.h index 80cbd76d..429a5664 100644 --- a/Tactility/Include/Tactility/service/espnow/EspNow.h +++ b/Tactility/Include/Tactility/service/espnow/EspNow.h @@ -4,7 +4,7 @@ #include #endif -#ifdef CONFIG_ESP_WIFI_ENABLED +#if defined(CONFIG_SOC_WIFI_SUPPORTED) && !defined(CONFIG_SLAVE_SOC_WIFI_SUPPORTED) #include #include @@ -56,4 +56,4 @@ void unsubscribeReceiver(ReceiverSubscription subscription); } -#endif // ESP_PLATFORM \ No newline at end of file +#endif // CONFIG_SOC_WIFI_SUPPORTED && !CONFIG_SLAVE_SOC_WIFI_SUPPORTED \ No newline at end of file diff --git a/Tactility/Source/Tactility.cpp b/Tactility/Source/Tactility.cpp index 6f30c6eb..97b28cb0 100644 --- a/Tactility/Source/Tactility.cpp +++ b/Tactility/Source/Tactility.cpp @@ -45,7 +45,7 @@ namespace service { #ifdef ESP_PLATFORM namespace development { extern const ServiceManifest manifest; } #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) namespace espnow { extern const ServiceManifest manifest; } #endif // Secondary (UI) @@ -79,7 +79,7 @@ namespace app { namespace applist { extern const AppManifest manifest; } namespace appsettings { extern const AppManifest manifest; } namespace boot { extern const AppManifest manifest; } -#if defined(CONFIG_TT_WIFI_ENABLED) && !defined(CONFIG_ESP_WIFI_REMOTE_ENABLED) +#if defined(CONFIG_SOC_WIFI_SUPPORTED) && !defined(CONFIG_SLAVE_SOC_WIFI_SUPPORTED) namespace chat { extern const AppManifest manifest; } #endif namespace development { extern const AppManifest manifest; } @@ -167,7 +167,7 @@ static void registerInternalApps() { addAppManifest(app::screenshot::manifest); #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) addAppManifest(app::chat::manifest); #endif @@ -272,7 +272,7 @@ static void registerAndStartPrimaryServices() { addService(service::development::manifest); #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) addService(service::espnow::manifest); #endif #ifdef ESP_PLATFORM diff --git a/Tactility/Source/app/chat/ChatApp.cpp b/Tactility/Source/app/chat/ChatApp.cpp index f678cc1e..aa349748 100644 --- a/Tactility/Source/app/chat/ChatApp.cpp +++ b/Tactility/Source/app/chat/ChatApp.cpp @@ -2,7 +2,7 @@ #include #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 #include @@ -149,4 +149,4 @@ extern const AppManifest manifest = { } -#endif +#endif // CONFIG_SOC_WIFI_SUPPORTED && !CONFIG_SLAVE_SOC_WIFI_SUPPORTED diff --git a/Tactility/Source/service/espnow/EspNow.cpp b/Tactility/Source/service/espnow/EspNow.cpp index 015c7a56..42c934e3 100644 --- a/Tactility/Source/service/espnow/EspNow.cpp +++ b/Tactility/Source/service/espnow/EspNow.cpp @@ -2,7 +2,7 @@ #include #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 #include @@ -81,4 +81,4 @@ void unsubscribeReceiver(ReceiverSubscription subscription) { } -#endif // ESP_PLATFORM +#endif // CONFIG_SOC_WIFI_SUPPORTED && !CONFIG_SLAVE_SOC_WIFI_SUPPORTED diff --git a/Tactility/Source/service/espnow/EspNowService.cpp b/Tactility/Source/service/espnow/EspNowService.cpp index 86c6416c..476b1b7c 100644 --- a/Tactility/Source/service/espnow/EspNowService.cpp +++ b/Tactility/Source/service/espnow/EspNowService.cpp @@ -2,7 +2,7 @@ #include #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 #include @@ -208,4 +208,4 @@ extern const ServiceManifest manifest = { } -#endif // ESP_PLATFORM +#endif // CONFIG_SOC_WIFI_SUPPORTED && !CONFIG_SLAVE_SOC_WIFI_SUPPORTED diff --git a/Tactility/Source/service/espnow/EspNowWifi.cpp b/Tactility/Source/service/espnow/EspNowWifi.cpp index 81b5af7d..806e3d89 100644 --- a/Tactility/Source/service/espnow/EspNowWifi.cpp +++ b/Tactility/Source/service/espnow/EspNowWifi.cpp @@ -2,9 +2,8 @@ #include #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 #include #include #include @@ -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