refactor: move render() to Activity super class, use freeRTOS notification (#774)

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.

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%)
```

---

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

* **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:
Xuan-Son Nguyen
2026-02-16 11:11:15 +01:00
committed by cottongin
parent 12cc7de49e
commit ed8a0feac1
53 changed files with 511 additions and 1462 deletions

View File

@@ -40,11 +40,6 @@ void syncTimeWithNTP() {
}
} // namespace
void KOReaderSyncActivity::taskTrampoline(void* param) {
auto* self = static_cast<KOReaderSyncActivity*>(param);
self->displayTaskLoop();
}
void KOReaderSyncActivity::onWifiSelectionComplete(const bool success) {
exitActivity();
@@ -60,7 +55,7 @@ void KOReaderSyncActivity::onWifiSelectionComplete(const bool success) {
state = SYNCING;
statusMessage = "Syncing time...";
xSemaphoreGive(renderingMutex);
updateRequired = true;
requestUpdate();
// Sync time with NTP before making API requests
syncTimeWithNTP();
@@ -68,7 +63,7 @@ void KOReaderSyncActivity::onWifiSelectionComplete(const bool success) {
xSemaphoreTake(renderingMutex, portMAX_DELAY);
statusMessage = "Calculating document hash...";
xSemaphoreGive(renderingMutex);
updateRequired = true;
requestUpdate();
performSync();
}
@@ -85,7 +80,7 @@ void KOReaderSyncActivity::performSync() {
state = SYNC_FAILED;
statusMessage = "Failed to calculate document hash";
xSemaphoreGive(renderingMutex);
updateRequired = true;
requestUpdate();
return;
}
@@ -94,8 +89,7 @@ void KOReaderSyncActivity::performSync() {
xSemaphoreTake(renderingMutex, portMAX_DELAY);
statusMessage = "Fetching remote progress...";
xSemaphoreGive(renderingMutex);
updateRequired = true;
vTaskDelay(10 / portTICK_PERIOD_MS);
requestUpdateAndWait();
// Fetch remote progress
const auto result = KOReaderSyncClient::getProgress(documentHash, remoteProgress);
@@ -106,7 +100,7 @@ void KOReaderSyncActivity::performSync() {
state = NO_REMOTE_PROGRESS;
hasRemoteProgress = false;
xSemaphoreGive(renderingMutex);
updateRequired = true;
requestUpdate();
return;
}
@@ -115,7 +109,7 @@ void KOReaderSyncActivity::performSync() {
state = SYNC_FAILED;
statusMessage = KOReaderSyncClient::errorString(result);
xSemaphoreGive(renderingMutex);
updateRequired = true;
requestUpdate();
return;
}
@@ -132,7 +126,7 @@ void KOReaderSyncActivity::performSync() {
state = SHOWING_RESULT;
selectedOption = 0; // Default to "Apply"
xSemaphoreGive(renderingMutex);
updateRequired = true;
requestUpdate();
}
void KOReaderSyncActivity::performUpload() {
@@ -140,8 +134,8 @@ void KOReaderSyncActivity::performUpload() {
state = UPLOADING;
statusMessage = "Uploading progress...";
xSemaphoreGive(renderingMutex);
updateRequired = true;
vTaskDelay(10 / portTICK_PERIOD_MS);
requestUpdate();
requestUpdateAndWait();
// Convert current position to KOReader format
CrossPointPosition localPos = {currentSpineIndex, currentPage, totalPagesInSpine};
@@ -159,32 +153,23 @@ void KOReaderSyncActivity::performUpload() {
state = SYNC_FAILED;
statusMessage = KOReaderSyncClient::errorString(result);
xSemaphoreGive(renderingMutex);
updateRequired = true;
requestUpdate();
return;
}
xSemaphoreTake(renderingMutex, portMAX_DELAY);
state = UPLOAD_COMPLETE;
xSemaphoreGive(renderingMutex);
updateRequired = true;
requestUpdate();
}
void KOReaderSyncActivity::onEnter() {
ActivityWithSubactivity::onEnter();
renderingMutex = xSemaphoreCreateMutex();
xTaskCreate(&KOReaderSyncActivity::taskTrampoline, "KOSyncTask",
4096, // Stack size (larger for network operations)
this, // Parameters
1, // Priority
&displayTaskHandle // Task handle
);
// Check for credentials first
if (!KOREADER_STORE.hasCredentials()) {
state = NO_CREDENTIALS;
updateRequired = true;
requestUpdate();
return;
}
@@ -197,7 +182,7 @@ void KOReaderSyncActivity::onEnter() {
LOG_DBG("KOSync", "Already connected to WiFi");
state = SYNCING;
statusMessage = "Syncing time...";
updateRequired = true;
requestUpdate();
// Perform sync directly (will be handled in loop)
xTaskCreate(
@@ -208,7 +193,7 @@ void KOReaderSyncActivity::onEnter() {
xSemaphoreTake(self->renderingMutex, portMAX_DELAY);
self->statusMessage = "Calculating document hash...";
xSemaphoreGive(self->renderingMutex);
self->updateRequired = true;
self->requestUpdate();
self->performSync();
vTaskDelete(nullptr);
},
@@ -230,30 +215,9 @@ void KOReaderSyncActivity::onExit() {
delay(100);
WiFi.mode(WIFI_OFF);
delay(100);
// Wait until not rendering to delete task
xSemaphoreTake(renderingMutex, portMAX_DELAY);
if (displayTaskHandle) {
vTaskDelete(displayTaskHandle);
displayTaskHandle = nullptr;
}
vSemaphoreDelete(renderingMutex);
renderingMutex = nullptr;
}
void KOReaderSyncActivity::displayTaskLoop() {
while (true) {
if (updateRequired) {
updateRequired = false;
xSemaphoreTake(renderingMutex, portMAX_DELAY);
render();
xSemaphoreGive(renderingMutex);
}
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
void KOReaderSyncActivity::render() {
void KOReaderSyncActivity::render(Activity::RenderLock&&) {
if (subActivity) {
return;
}
@@ -388,11 +352,11 @@ void KOReaderSyncActivity::loop() {
if (mappedInput.wasPressed(MappedInputManager::Button::Up) ||
mappedInput.wasPressed(MappedInputManager::Button::Left)) {
selectedOption = (selectedOption + 1) % 2; // Wrap around among 2 options
updateRequired = true;
requestUpdate();
} else if (mappedInput.wasPressed(MappedInputManager::Button::Down) ||
mappedInput.wasPressed(MappedInputManager::Button::Right)) {
selectedOption = (selectedOption + 1) % 2; // Wrap around among 2 options
updateRequired = true;
requestUpdate();
}
if (mappedInput.wasPressed(MappedInputManager::Button::Confirm)) {