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:
Shadowtrance 2026-01-24 21:42:49 +10:00 committed by GitHub
parent b8214fd378
commit 0d16eb606f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 49 additions and 84 deletions

View File

@ -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

View File

@ -4,7 +4,7 @@
#include <sdkconfig.h>
#endif
#ifdef CONFIG_ESP_WIFI_ENABLED
#if defined(CONFIG_SOC_WIFI_SUPPORTED) && !defined(CONFIG_SLAVE_SOC_WIFI_SUPPORTED)
#include <cstdint>
#include <cstring>
@ -56,4 +56,4 @@ void unsubscribeReceiver(ReceiverSubscription subscription);
}
#endif // ESP_PLATFORM
#endif // CONFIG_SOC_WIFI_SUPPORTED && !CONFIG_SLAVE_SOC_WIFI_SUPPORTED

View File

@ -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

View File

@ -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/app/AppManifest.h>
#include <Tactility/lvgl/Toolbar.h>
@ -149,4 +149,4 @@ extern const AppManifest manifest = {
}
#endif
#endif // CONFIG_SOC_WIFI_SUPPORTED && !CONFIG_SLAVE_SOC_WIFI_SUPPORTED

View File

@ -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

View File

@ -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

View File

@ -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,49 +14,22 @@
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) {
// 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");
@ -78,12 +50,12 @@ bool initWifi(const EspNowConfig& config) {
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 (config.longRange) {
wifi_interface_t wifi_interface;
@ -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