## 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>
258 lines
9.1 KiB
C++
258 lines
9.1 KiB
C++
#include "SettingsActivity.h"
|
|
|
|
#include <GfxRenderer.h>
|
|
#include <Logging.h>
|
|
|
|
#include "ButtonRemapActivity.h"
|
|
#include "CalibreSettingsActivity.h"
|
|
#include "ClearCacheActivity.h"
|
|
#include "CrossPointSettings.h"
|
|
#include "KOReaderSettingsActivity.h"
|
|
#include "MappedInputManager.h"
|
|
#include "OtaUpdateActivity.h"
|
|
#include "SettingsList.h"
|
|
#include "activities/network/WifiSelectionActivity.h"
|
|
#include "components/UITheme.h"
|
|
#include "fontIds.h"
|
|
|
|
const char* SettingsActivity::categoryNames[categoryCount] = {"Display", "Reader", "Controls", "System"};
|
|
|
|
void SettingsActivity::onEnter() {
|
|
Activity::onEnter();
|
|
|
|
// Build per-category vectors from the shared settings list
|
|
displaySettings.clear();
|
|
readerSettings.clear();
|
|
controlsSettings.clear();
|
|
systemSettings.clear();
|
|
|
|
for (auto& setting : getSettingsList()) {
|
|
if (!setting.category) continue;
|
|
if (strcmp(setting.category, "Display") == 0) {
|
|
displaySettings.push_back(std::move(setting));
|
|
} else if (strcmp(setting.category, "Reader") == 0) {
|
|
readerSettings.push_back(std::move(setting));
|
|
} else if (strcmp(setting.category, "Controls") == 0) {
|
|
controlsSettings.push_back(std::move(setting));
|
|
} else if (strcmp(setting.category, "System") == 0) {
|
|
systemSettings.push_back(std::move(setting));
|
|
}
|
|
// Web-only categories (KOReader Sync, OPDS Browser) are skipped for device UI
|
|
}
|
|
|
|
// Append device-only ACTION items
|
|
controlsSettings.insert(controlsSettings.begin(),
|
|
SettingInfo::Action("Remap Front Buttons", SettingAction::RemapFrontButtons));
|
|
systemSettings.push_back(SettingInfo::Action("Network", SettingAction::Network));
|
|
systemSettings.push_back(SettingInfo::Action("KOReader Sync", SettingAction::KOReaderSync));
|
|
systemSettings.push_back(SettingInfo::Action("OPDS Browser", SettingAction::OPDSBrowser));
|
|
systemSettings.push_back(SettingInfo::Action("Clear Cache", SettingAction::ClearCache));
|
|
systemSettings.push_back(SettingInfo::Action("Check for updates", SettingAction::CheckForUpdates));
|
|
|
|
// Reset selection to first category
|
|
selectedCategoryIndex = 0;
|
|
selectedSettingIndex = 0;
|
|
|
|
// Initialize with first category (Display)
|
|
currentSettings = &displaySettings;
|
|
settingsCount = static_cast<int>(displaySettings.size());
|
|
|
|
// Trigger first update
|
|
requestUpdate();
|
|
}
|
|
|
|
void SettingsActivity::onExit() {
|
|
ActivityWithSubactivity::onExit();
|
|
|
|
UITheme::getInstance().reload(); // Re-apply theme in case it was changed
|
|
}
|
|
|
|
void SettingsActivity::loop() {
|
|
if (subActivity) {
|
|
subActivity->loop();
|
|
return;
|
|
}
|
|
bool hasChangedCategory = false;
|
|
|
|
// Handle actions with early return
|
|
if (mappedInput.wasPressed(MappedInputManager::Button::Confirm)) {
|
|
if (selectedSettingIndex == 0) {
|
|
selectedCategoryIndex = (selectedCategoryIndex < categoryCount - 1) ? (selectedCategoryIndex + 1) : 0;
|
|
hasChangedCategory = true;
|
|
requestUpdate();
|
|
} else {
|
|
toggleCurrentSetting();
|
|
requestUpdate();
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
|
SETTINGS.saveToFile();
|
|
onGoHome();
|
|
return;
|
|
}
|
|
|
|
// Handle navigation
|
|
buttonNavigator.onNextRelease([this] {
|
|
selectedSettingIndex = ButtonNavigator::nextIndex(selectedSettingIndex, settingsCount + 1);
|
|
requestUpdate();
|
|
});
|
|
|
|
buttonNavigator.onPreviousRelease([this] {
|
|
selectedSettingIndex = ButtonNavigator::previousIndex(selectedSettingIndex, settingsCount + 1);
|
|
requestUpdate();
|
|
});
|
|
|
|
buttonNavigator.onNextContinuous([this, &hasChangedCategory] {
|
|
hasChangedCategory = true;
|
|
selectedCategoryIndex = ButtonNavigator::nextIndex(selectedCategoryIndex, categoryCount);
|
|
requestUpdate();
|
|
});
|
|
|
|
buttonNavigator.onPreviousContinuous([this, &hasChangedCategory] {
|
|
hasChangedCategory = true;
|
|
selectedCategoryIndex = ButtonNavigator::previousIndex(selectedCategoryIndex, categoryCount);
|
|
requestUpdate();
|
|
});
|
|
|
|
if (hasChangedCategory) {
|
|
selectedSettingIndex = (selectedSettingIndex == 0) ? 0 : 1;
|
|
switch (selectedCategoryIndex) {
|
|
case 0:
|
|
currentSettings = &displaySettings;
|
|
break;
|
|
case 1:
|
|
currentSettings = &readerSettings;
|
|
break;
|
|
case 2:
|
|
currentSettings = &controlsSettings;
|
|
break;
|
|
case 3:
|
|
currentSettings = &systemSettings;
|
|
break;
|
|
}
|
|
settingsCount = static_cast<int>(currentSettings->size());
|
|
}
|
|
}
|
|
|
|
void SettingsActivity::toggleCurrentSetting() {
|
|
int selectedSetting = selectedSettingIndex - 1;
|
|
if (selectedSetting < 0 || selectedSetting >= settingsCount) {
|
|
return;
|
|
}
|
|
|
|
const auto& setting = (*currentSettings)[selectedSetting];
|
|
|
|
if (setting.type == SettingType::TOGGLE && setting.valuePtr != nullptr) {
|
|
// Toggle the boolean value using the member pointer
|
|
const bool currentValue = SETTINGS.*(setting.valuePtr);
|
|
SETTINGS.*(setting.valuePtr) = !currentValue;
|
|
} else if (setting.type == SettingType::ENUM && setting.valuePtr != nullptr) {
|
|
const uint8_t currentValue = SETTINGS.*(setting.valuePtr);
|
|
SETTINGS.*(setting.valuePtr) = (currentValue + 1) % static_cast<uint8_t>(setting.enumValues.size());
|
|
} else if (setting.type == SettingType::VALUE && setting.valuePtr != nullptr) {
|
|
const int8_t currentValue = SETTINGS.*(setting.valuePtr);
|
|
if (currentValue + setting.valueRange.step > setting.valueRange.max) {
|
|
SETTINGS.*(setting.valuePtr) = setting.valueRange.min;
|
|
} else {
|
|
SETTINGS.*(setting.valuePtr) = currentValue + setting.valueRange.step;
|
|
}
|
|
} else if (setting.type == SettingType::ACTION) {
|
|
auto enterSubActivity = [this](Activity* activity) {
|
|
exitActivity();
|
|
enterNewActivity(activity);
|
|
};
|
|
|
|
auto onComplete = [this] {
|
|
exitActivity();
|
|
requestUpdate();
|
|
};
|
|
|
|
auto onCompleteBool = [this](bool) {
|
|
exitActivity();
|
|
requestUpdate();
|
|
};
|
|
|
|
switch (setting.action) {
|
|
case SettingAction::RemapFrontButtons:
|
|
enterSubActivity(new ButtonRemapActivity(renderer, mappedInput, onComplete));
|
|
break;
|
|
case SettingAction::KOReaderSync:
|
|
enterSubActivity(new KOReaderSettingsActivity(renderer, mappedInput, onComplete));
|
|
break;
|
|
case SettingAction::OPDSBrowser:
|
|
enterSubActivity(new CalibreSettingsActivity(renderer, mappedInput, onComplete));
|
|
break;
|
|
case SettingAction::Network:
|
|
enterSubActivity(new WifiSelectionActivity(renderer, mappedInput, onCompleteBool, false));
|
|
break;
|
|
case SettingAction::ClearCache:
|
|
enterSubActivity(new ClearCacheActivity(renderer, mappedInput, onComplete));
|
|
break;
|
|
case SettingAction::CheckForUpdates:
|
|
enterSubActivity(new OtaUpdateActivity(renderer, mappedInput, onComplete));
|
|
break;
|
|
case SettingAction::None:
|
|
// Do nothing
|
|
break;
|
|
}
|
|
} else {
|
|
return;
|
|
}
|
|
|
|
SETTINGS.saveToFile();
|
|
}
|
|
|
|
void SettingsActivity::render(Activity::RenderLock&&) {
|
|
renderer.clearScreen();
|
|
|
|
const auto pageWidth = renderer.getScreenWidth();
|
|
const auto pageHeight = renderer.getScreenHeight();
|
|
|
|
auto metrics = UITheme::getInstance().getMetrics();
|
|
|
|
GUI.drawHeader(renderer, Rect{0, metrics.topPadding, pageWidth, metrics.headerHeight}, "Settings");
|
|
|
|
std::vector<TabInfo> tabs;
|
|
tabs.reserve(categoryCount);
|
|
for (int i = 0; i < categoryCount; i++) {
|
|
tabs.push_back({categoryNames[i], selectedCategoryIndex == i});
|
|
}
|
|
GUI.drawTabBar(renderer, Rect{0, metrics.topPadding + metrics.headerHeight, pageWidth, metrics.tabBarHeight}, tabs,
|
|
selectedSettingIndex == 0);
|
|
|
|
const auto& settings = *currentSettings;
|
|
GUI.drawList(
|
|
renderer,
|
|
Rect{0, metrics.topPadding + metrics.headerHeight + metrics.tabBarHeight + metrics.verticalSpacing, pageWidth,
|
|
pageHeight - (metrics.topPadding + metrics.headerHeight + metrics.tabBarHeight + metrics.buttonHintsHeight +
|
|
metrics.verticalSpacing * 2)},
|
|
settingsCount, selectedSettingIndex - 1, [&settings](int index) { return std::string(settings[index].name); },
|
|
nullptr, nullptr,
|
|
[&settings](int i) {
|
|
std::string valueText = "";
|
|
if (settings[i].type == SettingType::TOGGLE && settings[i].valuePtr != nullptr) {
|
|
const bool value = SETTINGS.*(settings[i].valuePtr);
|
|
valueText = value ? "ON" : "OFF";
|
|
} else if (settings[i].type == SettingType::ENUM && settings[i].valuePtr != nullptr) {
|
|
const uint8_t value = SETTINGS.*(settings[i].valuePtr);
|
|
valueText = settings[i].enumValues[value];
|
|
} else if (settings[i].type == SettingType::VALUE && settings[i].valuePtr != nullptr) {
|
|
valueText = std::to_string(SETTINGS.*(settings[i].valuePtr));
|
|
}
|
|
return valueText;
|
|
});
|
|
|
|
// Draw version text
|
|
renderer.drawText(SMALL_FONT_ID,
|
|
pageWidth - metrics.versionTextRightX - renderer.getTextWidth(SMALL_FONT_ID, CROSSPOINT_VERSION),
|
|
metrics.versionTextY, CROSSPOINT_VERSION);
|
|
|
|
// Draw help text
|
|
const auto labels = mappedInput.mapLabels("« Back", "Toggle", "Up", "Down");
|
|
GUI.drawButtonHints(renderer, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
|
|
|
|
// Always use standard refresh for settings screen
|
|
renderer.displayBuffer();
|
|
} |