mirror of
https://github.com/ByteWelder/Tactility.git
synced 2026-02-18 19:03:16 +00:00
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:
parent
b8214fd378
commit
0d16eb606f
@ -96,11 +96,4 @@ menu "Tactility App"
|
|||||||
help
|
help
|
||||||
The minimum time to show the splash screen in milliseconds.
|
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.
|
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
|
endmenu
|
||||||
|
|||||||
@ -4,7 +4,7 @@
|
|||||||
#include <sdkconfig.h>
|
#include <sdkconfig.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CONFIG_ESP_WIFI_ENABLED
|
#if defined(CONFIG_SOC_WIFI_SUPPORTED) && !defined(CONFIG_SLAVE_SOC_WIFI_SUPPORTED)
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
@ -56,4 +56,4 @@ void unsubscribeReceiver(ReceiverSubscription subscription);
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // ESP_PLATFORM
|
#endif // CONFIG_SOC_WIFI_SUPPORTED && !CONFIG_SLAVE_SOC_WIFI_SUPPORTED
|
||||||
@ -45,7 +45,7 @@ namespace service {
|
|||||||
#ifdef ESP_PLATFORM
|
#ifdef ESP_PLATFORM
|
||||||
namespace development { extern const ServiceManifest manifest; }
|
namespace development { extern const ServiceManifest manifest; }
|
||||||
#endif
|
#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; }
|
namespace espnow { extern const ServiceManifest manifest; }
|
||||||
#endif
|
#endif
|
||||||
// Secondary (UI)
|
// Secondary (UI)
|
||||||
@ -79,7 +79,7 @@ namespace app {
|
|||||||
namespace applist { extern const AppManifest manifest; }
|
namespace applist { extern const AppManifest manifest; }
|
||||||
namespace appsettings { extern const AppManifest manifest; }
|
namespace appsettings { extern const AppManifest manifest; }
|
||||||
namespace boot { 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; }
|
namespace chat { extern const AppManifest manifest; }
|
||||||
#endif
|
#endif
|
||||||
namespace development { extern const AppManifest manifest; }
|
namespace development { extern const AppManifest manifest; }
|
||||||
@ -167,7 +167,7 @@ static void registerInternalApps() {
|
|||||||
addAppManifest(app::screenshot::manifest);
|
addAppManifest(app::screenshot::manifest);
|
||||||
#endif
|
#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);
|
addAppManifest(app::chat::manifest);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -272,7 +272,7 @@ static void registerAndStartPrimaryServices() {
|
|||||||
addService(service::development::manifest);
|
addService(service::development::manifest);
|
||||||
#endif
|
#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);
|
addService(service::espnow::manifest);
|
||||||
#endif
|
#endif
|
||||||
#ifdef ESP_PLATFORM
|
#ifdef ESP_PLATFORM
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
#include <sdkconfig.h>
|
#include <sdkconfig.h>
|
||||||
#endif
|
#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/app/AppManifest.h>
|
||||||
#include <Tactility/lvgl/Toolbar.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
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
#include <sdkconfig.h>
|
#include <sdkconfig.h>
|
||||||
#endif
|
#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/EspNow.h>
|
||||||
#include <Tactility/service/espnow/EspNowService.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>
|
#include <sdkconfig.h>
|
||||||
#endif
|
#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/Logger.h>
|
||||||
#include <Tactility/Tactility.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>
|
#include <sdkconfig.h>
|
||||||
#endif
|
#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/Logger.h>
|
||||||
#include <Tactility/service/espnow/EspNow.h>
|
#include <Tactility/service/espnow/EspNow.h>
|
||||||
#include <Tactility/service/wifi/Wifi.h>
|
#include <Tactility/service/wifi/Wifi.h>
|
||||||
@ -15,74 +14,47 @@
|
|||||||
namespace tt::service::espnow {
|
namespace tt::service::espnow {
|
||||||
|
|
||||||
static const auto LOGGER = Logger("EspNowService");
|
static const auto LOGGER = Logger("EspNowService");
|
||||||
|
static bool wifiStartedByEspNow = false;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool initWifi(const EspNowConfig& config) {
|
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();
|
auto wifi_state = wifi::getRadioState();
|
||||||
bool wifi_was_connected = (wifi_state == wifi::RadioState::ConnectionActive);
|
bool wifi_already_running = (wifi_state != wifi::RadioState::Off && wifi_state != wifi::RadioState::OffPending);
|
||||||
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
wifi_mode_t mode;
|
wifi_mode_t mode;
|
||||||
if (config.mode == Mode::Station) {
|
if (config.mode == Mode::Station) {
|
||||||
// Use STA mode to allow coexistence with normal WiFi connection
|
|
||||||
mode = wifi_mode_t::WIFI_MODE_STA;
|
mode = wifi_mode_t::WIFI_MODE_STA;
|
||||||
} else {
|
} else {
|
||||||
mode = wifi_mode_t::WIFI_MODE_AP;
|
mode = wifi_mode_t::WIFI_MODE_AP;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only reinitialize WiFi if it's not already running
|
// Only initialize WiFi if it's not already running; ESP-NOW coexists with STA mode
|
||||||
if (wifi::getRadioState() == wifi::RadioState::Off) {
|
wifiStartedByEspNow = !wifi_already_running;
|
||||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
if (wifiStartedByEspNow) {
|
||||||
if (esp_wifi_init(&cfg) != ESP_OK) {
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||||
LOGGER.error("esp_wifi_init() failed");
|
if (esp_wifi_init(&cfg) != ESP_OK) {
|
||||||
return false;
|
LOGGER.error("esp_wifi_init() failed");
|
||||||
}
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (esp_wifi_set_storage(WIFI_STORAGE_RAM) != ESP_OK) {
|
if (esp_wifi_set_storage(WIFI_STORAGE_RAM) != ESP_OK) {
|
||||||
LOGGER.error("esp_wifi_set_storage() failed");
|
LOGGER.error("esp_wifi_set_storage() failed");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (esp_wifi_set_mode(mode) != ESP_OK) {
|
if (esp_wifi_set_mode(mode) != ESP_OK) {
|
||||||
LOGGER.error("esp_wifi_set_mode() failed");
|
LOGGER.error("esp_wifi_set_mode() failed");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (esp_wifi_start() != ESP_OK) {
|
if (esp_wifi_start() != ESP_OK) {
|
||||||
LOGGER.error("esp_wifi_start() failed");
|
LOGGER.error("esp_wifi_start() failed");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (esp_wifi_set_channel(config.channel, WIFI_SECOND_CHAN_NONE) != ESP_OK) {
|
if (esp_wifi_set_channel(config.channel, WIFI_SECOND_CHAN_NONE) != ESP_OK) {
|
||||||
LOGGER.error("esp_wifi_set_channel() failed");
|
LOGGER.error("esp_wifi_set_channel() failed");
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.longRange) {
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool deinitWifi() {
|
bool deinitWifi() {
|
||||||
// Don't deinitialize WiFi completely - just disable ESP-NOW
|
if (wifiStartedByEspNow) {
|
||||||
// This allows normal WiFi connection to continue
|
esp_wifi_stop();
|
||||||
// Only stop/deinit if WiFi was originally off
|
esp_wifi_deinit();
|
||||||
|
wifiStartedByEspNow = false;
|
||||||
// Since we're only using WiFi for ESP-NOW, we can safely keep it in a minimal state
|
LOGGER.info("WiFi stopped (was started by ESP-NOW)");
|
||||||
// or shut it down. For now, keep it running to support STA + ESP-NOW coexistence.
|
} else {
|
||||||
|
LOGGER.info("WiFi left running (managed by WiFi service)");
|
||||||
LOGGER.info("ESP-NOW WiFi deinitialized (WiFi service continues independently)");
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace tt::service::espnow
|
} // namespace tt::service::espnow
|
||||||
|
|
||||||
#endif // ESP_PLATFORM
|
#endif // CONFIG_SOC_WIFI_SUPPORTED && !CONFIG_SLAVE_SOC_WIFI_SUPPORTED
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user