mirror of
https://github.com/ByteWelder/Tactility.git
synced 2026-02-18 10:53:17 +00:00
Lots of things "ported" over from the "enhanced" fork. With some adjustments here and there. KeyboardBacklight driver (for T-Deck only currently) Trackball driver (for T-Deck only currently) Keyboard backlight sleep/wake (for T-Deck only currently...also requires keyboard firmware update) Display sleep/wake Files - create file/folder Keyboard settings (for T-Deck only currently) Time & Date settings tweaks Locale settings tweaks Systeminfo additions Espnow wifi coexist initI2cDevices - moved to T-deck init.cpp / initBoot KeyboardInitService - removed, moved to T-deck init.cpp / initBoot Adjusted TIMER_UPDATE_INTERVAL to 2 seconds. Added lock to ActionCreateFolder Maybe missed some things in the list. Display wake could do with some kind of block on wake first touch to prevent UI elements being hit when waking device with touch. Same with encoder/trackball/keyboard press i guess. The original code was written by @cscott0108 at https://github.com/cscott0108/tactility-enhanced-t-deck
119 lines
3.7 KiB
C++
119 lines
3.7 KiB
C++
#ifdef ESP_PLATFORM
|
|
#include <sdkconfig.h>
|
|
#endif
|
|
|
|
#ifdef CONFIG_ESP_WIFI_ENABLED
|
|
|
|
#include "Tactility/service/espnow/EspNow.h"
|
|
#include "Tactility/service/wifi/Wifi.h"
|
|
#include <Tactility/Log.h>
|
|
|
|
#include <esp_now.h>
|
|
#include <esp_wifi.h>
|
|
|
|
namespace tt::service::espnow {
|
|
|
|
constexpr const char* TAG = "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;
|
|
}
|
|
}
|
|
|
|
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()) {
|
|
TT_LOG_E(TAG, "Failed to disable wifi");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
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) {
|
|
TT_LOG_E(TAG, "esp_wifi_init() failed");
|
|
return false;
|
|
}
|
|
|
|
if (esp_wifi_set_storage(WIFI_STORAGE_RAM) != ESP_OK) {
|
|
TT_LOG_E(TAG, "esp_wifi_set_storage() failed");
|
|
return false;
|
|
}
|
|
|
|
if (esp_wifi_set_mode(mode) != ESP_OK) {
|
|
TT_LOG_E(TAG, "esp_wifi_set_mode() failed");
|
|
return false;
|
|
}
|
|
|
|
if (esp_wifi_start() != ESP_OK) {
|
|
TT_LOG_E(TAG, "esp_wifi_start() failed");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (esp_wifi_set_channel(config.channel, WIFI_SECOND_CHAN_NONE) != ESP_OK) {
|
|
TT_LOG_E(TAG, "esp_wifi_set_channel() failed");
|
|
return false;
|
|
}
|
|
|
|
if (config.longRange) {
|
|
wifi_interface_t wifi_interface;
|
|
if (config.mode == Mode::Station) {
|
|
wifi_interface = WIFI_IF_STA;
|
|
} else {
|
|
wifi_interface = WIFI_IF_AP;
|
|
}
|
|
|
|
if (esp_wifi_set_protocol(wifi_interface, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N | WIFI_PROTOCOL_LR) != ESP_OK) {
|
|
TT_LOG_W(TAG, "esp_wifi_set_protocol() for long range failed");
|
|
}
|
|
}
|
|
|
|
TT_LOG_I(TAG, "WiFi initialized for ESP-NOW (preserved existing connection: %s)", wifi_was_connected ? "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.
|
|
|
|
TT_LOG_I(TAG, "ESP-NOW WiFi deinitialized (WiFi service continues independently)");
|
|
return true;
|
|
}
|
|
|
|
} // namespace tt::service::espnow
|
|
|
|
#endif // ESP_PLATFORM
|