refactor: move render() to Activity super class, use freeRTOS notification (#774)
## Summary Currently, each activity has to manage their own `displayTaskLoop` which adds redundant boilerplate code. The loop is a wait loop which is also not the best practice, as the `updateRequested` boolean is not protected by a mutex. In this PR: - Move `displayTaskLoop` to the super `Activity` class - Replace `updateRequested` with freeRTOS's [direct to task notification](https://www.freertos.org/Documentation/02-Kernel/02-Kernel-features/03-Direct-to-task-notifications/01-Task-notifications) - For `ActivityWithSubactivity`, whenever a sub-activity is present, the parent's `render()` automatically goes inactive With this change, activities now only need to expose `render()` function, and anywhere in the code base can call `requestUpdate()` to request a new rendering pass. ## Additional Context In theory, this change may also make the battery life a bit better, since one wait loop is removed. Although the equipment in my home lab wasn't been able to verify it (the electric current is too noisy and small). Would appreciate if anyone has any insights on this subject. Update: I managed to hack [a small piece of code](https://github.com/ngxson/crosspoint-reader/tree/xsn/measure_cpu_usage) that allow tracking CPU idle time. The CPU load does decrease a bit (1.47% down to 1.39%), which make sense, because the display task is now sleeping most of the time unless notified. This should translate to a slightly increase in battery life in the long run. ``` PR: [40012] [MEM] Free: 185856 bytes, Total: 231004 bytes, Min Free: 123316 bytes [40012] [IDLE] Idle time: 98.61% (CPU load: 1.39%) [50017] [MEM] Free: 185856 bytes, Total: 231004 bytes, Min Free: 123316 bytes [50017] [IDLE] Idle time: 98.61% (CPU load: 1.39%) [60022] [MEM] Free: 185856 bytes, Total: 231004 bytes, Min Free: 123316 bytes [60022] [IDLE] Idle time: 98.61% (CPU load: 1.39%) master: [20012] [MEM] Free: 195016 bytes, Total: 231532 bytes, Min Free: 132460 bytes [20012] [IDLE] Idle time: 98.53% (CPU load: 1.47%) [30017] [MEM] Free: 195016 bytes, Total: 231532 bytes, Min Free: 132460 bytes [30017] [IDLE] Idle time: 98.53% (CPU load: 1.47%) [40022] [MEM] Free: 195016 bytes, Total: 231532 bytes, Min Free: 132460 bytes [40022] [IDLE] Idle time: 98.53% (CPU load: 1.47%) ``` --- ### AI Usage While CrossPoint doesn't have restrictions on AI tools in contributing, please be transparent about their usage as it helps set the right context for reviewers. Did you use AI tools to help write this code? **NO** <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Refactor** * Streamlined rendering architecture by consolidating update mechanisms across all activities, improving efficiency and consistency. * Modernized synchronization patterns for display updates to ensure reliable, conflict-free rendering. * **Bug Fixes** * Enhanced rendering stability through improved locking mechanisms and explicit update requests. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: znelson <znelson@users.noreply.github.com>
This commit is contained in:
@@ -14,16 +14,10 @@ namespace {
|
||||
constexpr const char* HOSTNAME = "crosspoint";
|
||||
} // namespace
|
||||
|
||||
void CalibreConnectActivity::taskTrampoline(void* param) {
|
||||
auto* self = static_cast<CalibreConnectActivity*>(param);
|
||||
self->displayTaskLoop();
|
||||
}
|
||||
|
||||
void CalibreConnectActivity::onEnter() {
|
||||
ActivityWithSubactivity::onEnter();
|
||||
|
||||
renderingMutex = xSemaphoreCreateMutex();
|
||||
updateRequired = true;
|
||||
requestUpdate();
|
||||
state = CalibreConnectState::WIFI_SELECTION;
|
||||
connectedIP.clear();
|
||||
connectedSSID.clear();
|
||||
@@ -35,13 +29,6 @@ void CalibreConnectActivity::onEnter() {
|
||||
lastCompleteAt = 0;
|
||||
exitRequested = false;
|
||||
|
||||
xTaskCreate(&CalibreConnectActivity::taskTrampoline, "CalibreConnectTask",
|
||||
2048, // Stack size
|
||||
this, // Parameters
|
||||
1, // Priority
|
||||
&displayTaskHandle // Task handle
|
||||
);
|
||||
|
||||
if (WiFi.status() != WL_CONNECTED) {
|
||||
enterNewActivity(new WifiSelectionActivity(renderer, mappedInput,
|
||||
[this](const bool connected) { onWifiSelectionComplete(connected); }));
|
||||
@@ -63,14 +50,6 @@ void CalibreConnectActivity::onExit() {
|
||||
delay(30);
|
||||
WiFi.mode(WIFI_OFF);
|
||||
delay(30);
|
||||
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
if (displayTaskHandle) {
|
||||
vTaskDelete(displayTaskHandle);
|
||||
displayTaskHandle = nullptr;
|
||||
}
|
||||
vSemaphoreDelete(renderingMutex);
|
||||
renderingMutex = nullptr;
|
||||
}
|
||||
|
||||
void CalibreConnectActivity::onWifiSelectionComplete(const bool connected) {
|
||||
@@ -92,7 +71,7 @@ void CalibreConnectActivity::onWifiSelectionComplete(const bool connected) {
|
||||
|
||||
void CalibreConnectActivity::startWebServer() {
|
||||
state = CalibreConnectState::SERVER_STARTING;
|
||||
updateRequired = true;
|
||||
requestUpdate();
|
||||
|
||||
if (MDNS.begin(HOSTNAME)) {
|
||||
// mDNS is optional for the Calibre plugin but still helpful for users.
|
||||
@@ -104,10 +83,10 @@ void CalibreConnectActivity::startWebServer() {
|
||||
|
||||
if (webServer->isRunning()) {
|
||||
state = CalibreConnectState::SERVER_RUNNING;
|
||||
updateRequired = true;
|
||||
requestUpdate();
|
||||
} else {
|
||||
state = CalibreConnectState::ERROR;
|
||||
updateRequired = true;
|
||||
requestUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -178,7 +157,7 @@ void CalibreConnectActivity::loop() {
|
||||
changed = true;
|
||||
}
|
||||
if (changed) {
|
||||
updateRequired = true;
|
||||
requestUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -188,19 +167,7 @@ void CalibreConnectActivity::loop() {
|
||||
}
|
||||
}
|
||||
|
||||
void CalibreConnectActivity::displayTaskLoop() {
|
||||
while (true) {
|
||||
if (updateRequired) {
|
||||
updateRequired = false;
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
render();
|
||||
xSemaphoreGive(renderingMutex);
|
||||
}
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
void CalibreConnectActivity::render() const {
|
||||
void CalibreConnectActivity::render(Activity::RenderLock&&) {
|
||||
if (state == CalibreConnectState::SERVER_RUNNING) {
|
||||
renderer.clearScreen();
|
||||
renderServerRunning();
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
#pragma once
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
@@ -17,9 +14,6 @@ enum class CalibreConnectState { WIFI_SELECTION, SERVER_STARTING, SERVER_RUNNING
|
||||
* but renders Calibre-specific instructions instead of the web transfer UI.
|
||||
*/
|
||||
class CalibreConnectActivity final : public ActivityWithSubactivity {
|
||||
TaskHandle_t displayTaskHandle = nullptr;
|
||||
SemaphoreHandle_t renderingMutex = nullptr;
|
||||
bool updateRequired = false;
|
||||
CalibreConnectState state = CalibreConnectState::WIFI_SELECTION;
|
||||
const std::function<void()> onComplete;
|
||||
|
||||
@@ -34,9 +28,6 @@ class CalibreConnectActivity final : public ActivityWithSubactivity {
|
||||
unsigned long lastCompleteAt = 0;
|
||||
bool exitRequested = false;
|
||||
|
||||
static void taskTrampoline(void* param);
|
||||
[[noreturn]] void displayTaskLoop();
|
||||
void render() const;
|
||||
void renderServerRunning() const;
|
||||
|
||||
void onWifiSelectionComplete(bool connected);
|
||||
@@ -50,6 +41,7 @@ class CalibreConnectActivity final : public ActivityWithSubactivity {
|
||||
void onEnter() override;
|
||||
void onExit() override;
|
||||
void loop() override;
|
||||
void render(Activity::RenderLock&&) override;
|
||||
bool skipLoopDelay() override { return webServer && webServer->isRunning(); }
|
||||
bool preventAutoSleep() override { return webServer && webServer->isRunning(); }
|
||||
};
|
||||
|
||||
@@ -29,17 +29,10 @@ DNSServer* dnsServer = nullptr;
|
||||
constexpr uint16_t DNS_PORT = 53;
|
||||
} // namespace
|
||||
|
||||
void CrossPointWebServerActivity::taskTrampoline(void* param) {
|
||||
auto* self = static_cast<CrossPointWebServerActivity*>(param);
|
||||
self->displayTaskLoop();
|
||||
}
|
||||
|
||||
void CrossPointWebServerActivity::onEnter() {
|
||||
ActivityWithSubactivity::onEnter();
|
||||
|
||||
LOG_DBG("WEBACT] [MEM", "Free heap at onEnter: %d bytes", ESP.getFreeHeap());
|
||||
|
||||
renderingMutex = xSemaphoreCreateMutex();
|
||||
LOG_DBG("WEBACT", "Free heap at onEnter: %d bytes", ESP.getFreeHeap());
|
||||
|
||||
// Reset state
|
||||
state = WebServerActivityState::MODE_SELECTION;
|
||||
@@ -48,14 +41,7 @@ void CrossPointWebServerActivity::onEnter() {
|
||||
connectedIP.clear();
|
||||
connectedSSID.clear();
|
||||
lastHandleClientTime = 0;
|
||||
updateRequired = true;
|
||||
|
||||
xTaskCreate(&CrossPointWebServerActivity::taskTrampoline, "WebServerActivityTask",
|
||||
2048, // Stack size
|
||||
this, // Parameters
|
||||
1, // Priority
|
||||
&displayTaskHandle // Task handle
|
||||
);
|
||||
requestUpdate();
|
||||
|
||||
// Launch network mode selection subactivity
|
||||
LOG_DBG("WEBACT", "Launching NetworkModeSelectionActivity...");
|
||||
@@ -68,7 +54,7 @@ void CrossPointWebServerActivity::onEnter() {
|
||||
void CrossPointWebServerActivity::onExit() {
|
||||
ActivityWithSubactivity::onExit();
|
||||
|
||||
LOG_DBG("WEBACT] [MEM", "Free heap at onExit start: %d bytes", ESP.getFreeHeap());
|
||||
LOG_DBG("WEBACT", "Free heap at onExit start: %d bytes", ESP.getFreeHeap());
|
||||
|
||||
state = WebServerActivityState::SHUTTING_DOWN;
|
||||
|
||||
@@ -103,27 +89,7 @@ void CrossPointWebServerActivity::onExit() {
|
||||
WiFi.mode(WIFI_OFF);
|
||||
delay(30); // Allow WiFi hardware to power down
|
||||
|
||||
LOG_DBG("WEBACT] [MEM", "Free heap after WiFi disconnect: %d bytes", ESP.getFreeHeap());
|
||||
|
||||
// Acquire mutex before deleting task
|
||||
LOG_DBG("WEBACT", "Acquiring rendering mutex before task deletion...");
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
|
||||
// Delete the display task
|
||||
LOG_DBG("WEBACT", "Deleting display task...");
|
||||
if (displayTaskHandle) {
|
||||
vTaskDelete(displayTaskHandle);
|
||||
displayTaskHandle = nullptr;
|
||||
LOG_DBG("WEBACT", "Display task deleted");
|
||||
}
|
||||
|
||||
// Delete the mutex
|
||||
LOG_DBG("WEBACT", "Deleting mutex...");
|
||||
vSemaphoreDelete(renderingMutex);
|
||||
renderingMutex = nullptr;
|
||||
LOG_DBG("WEBACT", "Mutex deleted");
|
||||
|
||||
LOG_DBG("WEBACT] [MEM", "Free heap at onExit end: %d bytes", ESP.getFreeHeap());
|
||||
LOG_DBG("WEBACT", "Free heap at onExit end: %d bytes", ESP.getFreeHeap());
|
||||
}
|
||||
|
||||
void CrossPointWebServerActivity::onNetworkModeSelected(const NetworkMode mode) {
|
||||
@@ -165,7 +131,7 @@ void CrossPointWebServerActivity::onNetworkModeSelected(const NetworkMode mode)
|
||||
} else {
|
||||
// AP mode - start access point
|
||||
state = WebServerActivityState::AP_STARTING;
|
||||
updateRequired = true;
|
||||
requestUpdate();
|
||||
startAccessPoint();
|
||||
}
|
||||
}
|
||||
@@ -200,7 +166,7 @@ void CrossPointWebServerActivity::onWifiSelectionComplete(const bool connected)
|
||||
|
||||
void CrossPointWebServerActivity::startAccessPoint() {
|
||||
LOG_DBG("WEBACT", "Starting Access Point mode...");
|
||||
LOG_DBG("WEBACT] [MEM", "Free heap before AP start: %d bytes", ESP.getFreeHeap());
|
||||
LOG_DBG("WEBACT", "Free heap before AP start: %d bytes", ESP.getFreeHeap());
|
||||
|
||||
// Configure and start the AP
|
||||
WiFi.mode(WIFI_AP);
|
||||
@@ -248,7 +214,7 @@ void CrossPointWebServerActivity::startAccessPoint() {
|
||||
dnsServer->start(DNS_PORT, "*", apIP);
|
||||
LOG_DBG("WEBACT", "DNS server started for captive portal");
|
||||
|
||||
LOG_DBG("WEBACT] [MEM", "Free heap after AP start: %d bytes", ESP.getFreeHeap());
|
||||
LOG_DBG("WEBACT", "Free heap after AP start: %d bytes", ESP.getFreeHeap());
|
||||
|
||||
// Start the web server
|
||||
startWebServer();
|
||||
@@ -267,9 +233,10 @@ void CrossPointWebServerActivity::startWebServer() {
|
||||
|
||||
// Force an immediate render since we're transitioning from a subactivity
|
||||
// that had its own rendering task. We need to make sure our display is shown.
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
render();
|
||||
xSemaphoreGive(renderingMutex);
|
||||
{
|
||||
RenderLock lock(*this);
|
||||
render(std::move(lock));
|
||||
}
|
||||
LOG_DBG("WEBACT", "Rendered File Transfer screen");
|
||||
} else {
|
||||
LOG_ERR("WEBACT", "ERROR: Failed to start web server!");
|
||||
@@ -312,7 +279,7 @@ void CrossPointWebServerActivity::loop() {
|
||||
LOG_DBG("WEBACT", "WiFi disconnected! Status: %d", wifiStatus);
|
||||
// Show error and exit gracefully
|
||||
state = WebServerActivityState::SHUTTING_DOWN;
|
||||
updateRequired = true;
|
||||
requestUpdate();
|
||||
return;
|
||||
}
|
||||
// Log weak signal warnings
|
||||
@@ -368,19 +335,7 @@ void CrossPointWebServerActivity::loop() {
|
||||
}
|
||||
}
|
||||
|
||||
void CrossPointWebServerActivity::displayTaskLoop() {
|
||||
while (true) {
|
||||
if (updateRequired) {
|
||||
updateRequired = false;
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
render();
|
||||
xSemaphoreGive(renderingMutex);
|
||||
}
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
void CrossPointWebServerActivity::render() const {
|
||||
void CrossPointWebServerActivity::render(Activity::RenderLock&&) {
|
||||
// Only render our own UI when server is running
|
||||
// Subactivities handle their own rendering
|
||||
if (state == WebServerActivityState::SERVER_RUNNING) {
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
#pragma once
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
@@ -31,9 +28,6 @@ enum class WebServerActivityState {
|
||||
* - Cleans up the server and shuts down WiFi on exit
|
||||
*/
|
||||
class CrossPointWebServerActivity final : public ActivityWithSubactivity {
|
||||
TaskHandle_t displayTaskHandle = nullptr;
|
||||
SemaphoreHandle_t renderingMutex = nullptr;
|
||||
bool updateRequired = false;
|
||||
WebServerActivityState state = WebServerActivityState::MODE_SELECTION;
|
||||
const std::function<void()> onGoBack;
|
||||
|
||||
@@ -51,9 +45,6 @@ class CrossPointWebServerActivity final : public ActivityWithSubactivity {
|
||||
// Performance monitoring
|
||||
unsigned long lastHandleClientTime = 0;
|
||||
|
||||
static void taskTrampoline(void* param);
|
||||
[[noreturn]] void displayTaskLoop();
|
||||
void render() const;
|
||||
void renderServerRunning() const;
|
||||
|
||||
void onNetworkModeSelected(NetworkMode mode);
|
||||
@@ -69,6 +60,7 @@ class CrossPointWebServerActivity final : public ActivityWithSubactivity {
|
||||
void onEnter() override;
|
||||
void onExit() override;
|
||||
void loop() override;
|
||||
void render(Activity::RenderLock&&) override;
|
||||
bool skipLoopDelay() override { return webServer && webServer->isRunning(); }
|
||||
bool preventAutoSleep() override { return webServer && webServer->isRunning(); }
|
||||
};
|
||||
|
||||
@@ -16,42 +16,17 @@ const char* MENU_DESCRIPTIONS[MENU_ITEM_COUNT] = {
|
||||
};
|
||||
} // namespace
|
||||
|
||||
void NetworkModeSelectionActivity::taskTrampoline(void* param) {
|
||||
auto* self = static_cast<NetworkModeSelectionActivity*>(param);
|
||||
self->displayTaskLoop();
|
||||
}
|
||||
|
||||
void NetworkModeSelectionActivity::onEnter() {
|
||||
Activity::onEnter();
|
||||
|
||||
renderingMutex = xSemaphoreCreateMutex();
|
||||
|
||||
// Reset selection
|
||||
selectedIndex = 0;
|
||||
|
||||
// Trigger first update
|
||||
updateRequired = true;
|
||||
|
||||
xTaskCreate(&NetworkModeSelectionActivity::taskTrampoline, "NetworkModeTask",
|
||||
2048, // Stack size
|
||||
this, // Parameters
|
||||
1, // Priority
|
||||
&displayTaskHandle // Task handle
|
||||
);
|
||||
requestUpdate();
|
||||
}
|
||||
|
||||
void NetworkModeSelectionActivity::onExit() {
|
||||
Activity::onExit();
|
||||
|
||||
// Wait until not rendering to delete task
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
if (displayTaskHandle) {
|
||||
vTaskDelete(displayTaskHandle);
|
||||
displayTaskHandle = nullptr;
|
||||
}
|
||||
vSemaphoreDelete(renderingMutex);
|
||||
renderingMutex = nullptr;
|
||||
}
|
||||
void NetworkModeSelectionActivity::onExit() { Activity::onExit(); }
|
||||
|
||||
void NetworkModeSelectionActivity::loop() {
|
||||
// Handle back button - cancel
|
||||
@@ -75,28 +50,16 @@ void NetworkModeSelectionActivity::loop() {
|
||||
// Handle navigation
|
||||
buttonNavigator.onNext([this] {
|
||||
selectedIndex = ButtonNavigator::nextIndex(selectedIndex, MENU_ITEM_COUNT);
|
||||
updateRequired = true;
|
||||
requestUpdate();
|
||||
});
|
||||
|
||||
buttonNavigator.onPrevious([this] {
|
||||
selectedIndex = ButtonNavigator::previousIndex(selectedIndex, MENU_ITEM_COUNT);
|
||||
updateRequired = true;
|
||||
requestUpdate();
|
||||
});
|
||||
}
|
||||
|
||||
void NetworkModeSelectionActivity::displayTaskLoop() {
|
||||
while (true) {
|
||||
if (updateRequired) {
|
||||
updateRequired = false;
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
render();
|
||||
xSemaphoreGive(renderingMutex);
|
||||
}
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkModeSelectionActivity::render() const {
|
||||
void NetworkModeSelectionActivity::render(Activity::RenderLock&&) {
|
||||
renderer.clearScreen();
|
||||
|
||||
const auto pageWidth = renderer.getScreenWidth();
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
#pragma once
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
#include <functional>
|
||||
|
||||
@@ -21,19 +18,13 @@ enum class NetworkMode { JOIN_NETWORK, CONNECT_CALIBRE, CREATE_HOTSPOT };
|
||||
* The onCancel callback is called if the user presses back.
|
||||
*/
|
||||
class NetworkModeSelectionActivity final : public Activity {
|
||||
TaskHandle_t displayTaskHandle = nullptr;
|
||||
SemaphoreHandle_t renderingMutex = nullptr;
|
||||
ButtonNavigator buttonNavigator;
|
||||
|
||||
int selectedIndex = 0;
|
||||
bool updateRequired = false;
|
||||
|
||||
const std::function<void(NetworkMode)> onModeSelected;
|
||||
const std::function<void()> onCancel;
|
||||
|
||||
static void taskTrampoline(void* param);
|
||||
[[noreturn]] void displayTaskLoop();
|
||||
void render() const;
|
||||
|
||||
public:
|
||||
explicit NetworkModeSelectionActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
||||
const std::function<void(NetworkMode)>& onModeSelected,
|
||||
@@ -42,4 +33,5 @@ class NetworkModeSelectionActivity final : public Activity {
|
||||
void onEnter() override;
|
||||
void onExit() override;
|
||||
void loop() override;
|
||||
void render(Activity::RenderLock&&) override;
|
||||
};
|
||||
|
||||
@@ -12,21 +12,15 @@
|
||||
#include "components/UITheme.h"
|
||||
#include "fontIds.h"
|
||||
|
||||
void WifiSelectionActivity::taskTrampoline(void* param) {
|
||||
auto* self = static_cast<WifiSelectionActivity*>(param);
|
||||
self->displayTaskLoop();
|
||||
}
|
||||
|
||||
void WifiSelectionActivity::onEnter() {
|
||||
Activity::onEnter();
|
||||
|
||||
renderingMutex = xSemaphoreCreateMutex();
|
||||
|
||||
// Load saved WiFi credentials - SD card operations need lock as we use SPI
|
||||
// for both
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
WIFI_STORE.loadFromFile();
|
||||
xSemaphoreGive(renderingMutex);
|
||||
{
|
||||
RenderLock lock(*this);
|
||||
WIFI_STORE.loadFromFile();
|
||||
}
|
||||
|
||||
// Reset state
|
||||
selectedNetworkIndex = 0;
|
||||
@@ -49,13 +43,8 @@ void WifiSelectionActivity::onEnter() {
|
||||
mac[5]);
|
||||
cachedMacAddress = std::string(macStr);
|
||||
|
||||
// Task creation
|
||||
xTaskCreate(&WifiSelectionActivity::taskTrampoline, "WifiSelectionTask",
|
||||
4096, // Stack size (larger for WiFi operations)
|
||||
this, // Parameters
|
||||
1, // Priority
|
||||
&displayTaskHandle // Task handle
|
||||
);
|
||||
// Trigger first update to show scanning message
|
||||
requestUpdate();
|
||||
|
||||
// Attempt to auto-connect to the last network
|
||||
if (allowAutoConnect) {
|
||||
@@ -70,7 +59,7 @@ void WifiSelectionActivity::onEnter() {
|
||||
usedSavedPassword = true;
|
||||
autoConnecting = true;
|
||||
attemptConnection();
|
||||
updateRequired = true;
|
||||
requestUpdate();
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -83,45 +72,25 @@ void WifiSelectionActivity::onEnter() {
|
||||
void WifiSelectionActivity::onExit() {
|
||||
Activity::onExit();
|
||||
|
||||
LOG_DBG("WIFI] [MEM", "Free heap at onExit start: %d bytes", ESP.getFreeHeap());
|
||||
LOG_DBG("WIFI", "Free heap at onExit start: %d bytes", ESP.getFreeHeap());
|
||||
|
||||
// Stop any ongoing WiFi scan
|
||||
LOG_DBG("WIFI", "Deleting WiFi scan...");
|
||||
WiFi.scanDelete();
|
||||
LOG_DBG("WIFI] [MEM", "Free heap after scanDelete: %d bytes", ESP.getFreeHeap());
|
||||
LOG_DBG("WIFI", "Free heap after scanDelete: %d bytes", ESP.getFreeHeap());
|
||||
|
||||
// Note: We do NOT disconnect WiFi here - the parent activity
|
||||
// (CrossPointWebServerActivity) manages WiFi connection state. We just clean
|
||||
// up the scan and task.
|
||||
|
||||
// Acquire mutex before deleting task to ensure task isn't using it
|
||||
// This prevents hangs/crashes if the task holds the mutex when deleted
|
||||
LOG_DBG("WIFI", "Acquiring rendering mutex before task deletion...");
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
|
||||
// Delete the display task (we now hold the mutex, so task is blocked if it
|
||||
// needs it)
|
||||
LOG_DBG("WIFI", "Deleting display task...");
|
||||
if (displayTaskHandle) {
|
||||
vTaskDelete(displayTaskHandle);
|
||||
displayTaskHandle = nullptr;
|
||||
LOG_DBG("WIFI", "Display task deleted");
|
||||
}
|
||||
|
||||
// Now safe to delete the mutex since we own it
|
||||
LOG_DBG("WIFI", "Deleting mutex...");
|
||||
vSemaphoreDelete(renderingMutex);
|
||||
renderingMutex = nullptr;
|
||||
LOG_DBG("WIFI", "Mutex deleted");
|
||||
|
||||
LOG_DBG("WIFI] [MEM", "Free heap at onExit end: %d bytes", ESP.getFreeHeap());
|
||||
LOG_DBG("WIFI", "Free heap at onExit end: %d bytes", ESP.getFreeHeap());
|
||||
}
|
||||
|
||||
void WifiSelectionActivity::startWifiScan() {
|
||||
autoConnecting = false;
|
||||
state = WifiSelectionState::SCANNING;
|
||||
networks.clear();
|
||||
updateRequired = true;
|
||||
requestUpdate();
|
||||
|
||||
// Set WiFi mode to station
|
||||
WiFi.mode(WIFI_STA);
|
||||
@@ -142,7 +111,7 @@ void WifiSelectionActivity::processWifiScanResults() {
|
||||
|
||||
if (scanResult == WIFI_SCAN_FAILED) {
|
||||
state = WifiSelectionState::NETWORK_LIST;
|
||||
updateRequired = true;
|
||||
requestUpdate();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -191,7 +160,7 @@ void WifiSelectionActivity::processWifiScanResults() {
|
||||
WiFi.scanDelete();
|
||||
state = WifiSelectionState::NETWORK_LIST;
|
||||
selectedNetworkIndex = 0;
|
||||
updateRequired = true;
|
||||
requestUpdate();
|
||||
}
|
||||
|
||||
void WifiSelectionActivity::selectNetwork(const int index) {
|
||||
@@ -221,7 +190,6 @@ void WifiSelectionActivity::selectNetwork(const int index) {
|
||||
// Show password entry
|
||||
state = WifiSelectionState::PASSWORD_ENTRY;
|
||||
// Don't allow screen updates while changing activity
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
enterNewActivity(new KeyboardEntryActivity(
|
||||
renderer, mappedInput, "Enter WiFi Password",
|
||||
"", // No initial text
|
||||
@@ -234,11 +202,9 @@ void WifiSelectionActivity::selectNetwork(const int index) {
|
||||
},
|
||||
[this] {
|
||||
state = WifiSelectionState::NETWORK_LIST;
|
||||
updateRequired = true;
|
||||
exitActivity();
|
||||
requestUpdate();
|
||||
}));
|
||||
updateRequired = true;
|
||||
xSemaphoreGive(renderingMutex);
|
||||
} else {
|
||||
// Connect directly for open networks
|
||||
attemptConnection();
|
||||
@@ -250,7 +216,7 @@ void WifiSelectionActivity::attemptConnection() {
|
||||
connectionStartTime = millis();
|
||||
connectedIP.clear();
|
||||
connectionError.clear();
|
||||
updateRequired = true;
|
||||
requestUpdate();
|
||||
|
||||
WiFi.mode(WIFI_STA);
|
||||
|
||||
@@ -287,7 +253,7 @@ void WifiSelectionActivity::checkConnectionStatus() {
|
||||
if (!usedSavedPassword && !enteredPassword.empty()) {
|
||||
state = WifiSelectionState::SAVE_PROMPT;
|
||||
savePromptSelection = 0; // Default to "Yes"
|
||||
updateRequired = true;
|
||||
requestUpdate();
|
||||
} else {
|
||||
// Using saved password or open network - complete immediately
|
||||
LOG_DBG("WIFI",
|
||||
@@ -304,7 +270,7 @@ void WifiSelectionActivity::checkConnectionStatus() {
|
||||
connectionError = "Error: Network not found";
|
||||
}
|
||||
state = WifiSelectionState::CONNECTION_FAILED;
|
||||
updateRequired = true;
|
||||
requestUpdate();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -313,7 +279,7 @@ void WifiSelectionActivity::checkConnectionStatus() {
|
||||
WiFi.disconnect();
|
||||
connectionError = "Error: Connection timeout";
|
||||
state = WifiSelectionState::CONNECTION_FAILED;
|
||||
updateRequired = true;
|
||||
requestUpdate();
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -348,20 +314,19 @@ void WifiSelectionActivity::loop() {
|
||||
mappedInput.wasPressed(MappedInputManager::Button::Left)) {
|
||||
if (savePromptSelection > 0) {
|
||||
savePromptSelection--;
|
||||
updateRequired = true;
|
||||
requestUpdate();
|
||||
}
|
||||
} else if (mappedInput.wasPressed(MappedInputManager::Button::Down) ||
|
||||
mappedInput.wasPressed(MappedInputManager::Button::Right)) {
|
||||
if (savePromptSelection < 1) {
|
||||
savePromptSelection++;
|
||||
updateRequired = true;
|
||||
requestUpdate();
|
||||
}
|
||||
} else if (mappedInput.wasPressed(MappedInputManager::Button::Confirm)) {
|
||||
if (savePromptSelection == 0) {
|
||||
// User chose "Yes" - save the password
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
RenderLock lock(*this);
|
||||
WIFI_STORE.addCredential(selectedSSID, enteredPassword);
|
||||
xSemaphoreGive(renderingMutex);
|
||||
}
|
||||
// Complete - parent will start web server
|
||||
onComplete(true);
|
||||
@@ -378,20 +343,19 @@ void WifiSelectionActivity::loop() {
|
||||
mappedInput.wasPressed(MappedInputManager::Button::Left)) {
|
||||
if (forgetPromptSelection > 0) {
|
||||
forgetPromptSelection--;
|
||||
updateRequired = true;
|
||||
requestUpdate();
|
||||
}
|
||||
} else if (mappedInput.wasPressed(MappedInputManager::Button::Down) ||
|
||||
mappedInput.wasPressed(MappedInputManager::Button::Right)) {
|
||||
if (forgetPromptSelection < 1) {
|
||||
forgetPromptSelection++;
|
||||
updateRequired = true;
|
||||
requestUpdate();
|
||||
}
|
||||
} else if (mappedInput.wasPressed(MappedInputManager::Button::Confirm)) {
|
||||
if (forgetPromptSelection == 1) {
|
||||
RenderLock lock(*this);
|
||||
// User chose "Forget network" - forget the network
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
WIFI_STORE.removeCredential(selectedSSID);
|
||||
xSemaphoreGive(renderingMutex);
|
||||
// Update the network list to reflect the change
|
||||
const auto network = find_if(networks.begin(), networks.end(),
|
||||
[this](const WifiNetworkInfo& net) { return net.ssid == selectedSSID; });
|
||||
@@ -430,7 +394,7 @@ void WifiSelectionActivity::loop() {
|
||||
// Go back to network list on failure for non-saved credentials
|
||||
state = WifiSelectionState::NETWORK_LIST;
|
||||
}
|
||||
updateRequired = true;
|
||||
requestUpdate();
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -465,7 +429,7 @@ void WifiSelectionActivity::loop() {
|
||||
selectedSSID = networks[selectedNetworkIndex].ssid;
|
||||
state = WifiSelectionState::FORGET_PROMPT;
|
||||
forgetPromptSelection = 0; // Default to "Cancel"
|
||||
updateRequired = true;
|
||||
requestUpdate();
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -473,12 +437,12 @@ void WifiSelectionActivity::loop() {
|
||||
// Handle navigation
|
||||
buttonNavigator.onNext([this] {
|
||||
selectedNetworkIndex = ButtonNavigator::nextIndex(selectedNetworkIndex, networks.size());
|
||||
updateRequired = true;
|
||||
requestUpdate();
|
||||
});
|
||||
|
||||
buttonNavigator.onPrevious([this] {
|
||||
selectedNetworkIndex = ButtonNavigator::previousIndex(selectedNetworkIndex, networks.size());
|
||||
updateRequired = true;
|
||||
requestUpdate();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -500,32 +464,14 @@ std::string WifiSelectionActivity::getSignalStrengthIndicator(const int32_t rssi
|
||||
return " "; // Very weak
|
||||
}
|
||||
|
||||
void WifiSelectionActivity::displayTaskLoop() {
|
||||
while (true) {
|
||||
// If a subactivity is active, yield CPU time but don't render
|
||||
if (subActivity) {
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Don't render if we're in PASSWORD_ENTRY state - we're just transitioning
|
||||
// from the keyboard subactivity back to the main activity
|
||||
if (state == WifiSelectionState::PASSWORD_ENTRY) {
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (updateRequired) {
|
||||
updateRequired = false;
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
render();
|
||||
xSemaphoreGive(renderingMutex);
|
||||
}
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||
void WifiSelectionActivity::render(Activity::RenderLock&&) {
|
||||
// Don't render if we're in PASSWORD_ENTRY state - we're just transitioning
|
||||
// from the keyboard subactivity back to the main activity
|
||||
if (state == WifiSelectionState::PASSWORD_ENTRY) {
|
||||
requestUpdateAndWait();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void WifiSelectionActivity::render() const {
|
||||
renderer.clearScreen();
|
||||
|
||||
switch (state) {
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
#pragma once
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
@@ -45,10 +42,8 @@ enum class WifiSelectionState {
|
||||
* The onComplete callback receives true if connected successfully, false if cancelled.
|
||||
*/
|
||||
class WifiSelectionActivity final : public ActivityWithSubactivity {
|
||||
TaskHandle_t displayTaskHandle = nullptr;
|
||||
SemaphoreHandle_t renderingMutex = nullptr;
|
||||
ButtonNavigator buttonNavigator;
|
||||
bool updateRequired = false;
|
||||
|
||||
WifiSelectionState state = WifiSelectionState::SCANNING;
|
||||
int selectedNetworkIndex = 0;
|
||||
std::vector<WifiNetworkInfo> networks;
|
||||
@@ -85,9 +80,6 @@ class WifiSelectionActivity final : public ActivityWithSubactivity {
|
||||
static constexpr unsigned long CONNECTION_TIMEOUT_MS = 15000;
|
||||
unsigned long connectionStartTime = 0;
|
||||
|
||||
static void taskTrampoline(void* param);
|
||||
[[noreturn]] void displayTaskLoop();
|
||||
void render() const;
|
||||
void renderNetworkList() const;
|
||||
void renderPasswordEntry() const;
|
||||
void renderConnecting() const;
|
||||
@@ -112,6 +104,7 @@ class WifiSelectionActivity final : public ActivityWithSubactivity {
|
||||
void onEnter() override;
|
||||
void onExit() override;
|
||||
void loop() override;
|
||||
void render(Activity::RenderLock&&) override;
|
||||
|
||||
// Get the IP address after successful connection
|
||||
const std::string& getConnectedIP() const { return connectedIP; }
|
||||
|
||||
Reference in New Issue
Block a user