crosspoint-reader/src/screens/FileSelectionScreen.cpp

128 lines
3.6 KiB
C++
Raw Normal View History

2025-12-04 00:07:25 +11:00
#include "FileSelectionScreen.h"
#include <EpdRenderer.h>
#include <SD.h>
void FileSelectionScreen::taskTrampoline(void* param) {
auto* self = static_cast<FileSelectionScreen*>(param);
self->displayTaskLoop();
}
2025-12-05 22:12:28 +11:00
void FileSelectionScreen::loadFiles() {
2025-12-04 00:07:25 +11:00
files.clear();
2025-12-05 22:12:28 +11:00
selectorIndex = 0;
auto root = SD.open(basepath.c_str());
for (File file = root.openNextFile(); file; file = root.openNextFile()) {
2025-12-04 00:07:25 +11:00
auto filename = std::string(file.name());
2025-12-05 22:12:28 +11:00
if (filename[0] == '.') {
2025-12-04 00:07:25 +11:00
file.close();
continue;
}
2025-12-05 22:12:28 +11:00
if (file.isDirectory()) {
files.emplace_back(filename + "/");
} else if (filename.substr(filename.length() - 5) == ".epub") {
files.emplace_back(filename);
}
2025-12-04 00:07:25 +11:00
file.close();
}
root.close();
2025-12-05 22:12:28 +11:00
}
void FileSelectionScreen::onEnter() {
renderingMutex = xSemaphoreCreateMutex();
2025-12-05 22:12:28 +11:00
basepath = "/";
loadFiles();
selectorIndex = 0;
2025-12-04 00:07:25 +11:00
// Trigger first update
updateRequired = true;
xTaskCreate(&FileSelectionScreen::taskTrampoline, "FileSelectionScreenTask",
1024, // Stack size
this, // Parameters
1, // Priority
&displayTaskHandle // Task handle
);
}
void FileSelectionScreen::onExit() {
// Wait until not rendering to delete task to avoid killing mid-instruction to EPD
xSemaphoreTake(renderingMutex, portMAX_DELAY);
if (displayTaskHandle) {
vTaskDelete(displayTaskHandle);
displayTaskHandle = nullptr;
}
vSemaphoreDelete(renderingMutex);
renderingMutex = nullptr;
files.clear();
2025-12-04 00:07:25 +11:00
}
2025-12-06 12:35:41 +11:00
void FileSelectionScreen::handleInput() {
const bool prevPressed =
inputManager.wasPressed(InputManager::BTN_UP) || inputManager.wasPressed(InputManager::BTN_LEFT);
const bool nextPressed =
inputManager.wasPressed(InputManager::BTN_DOWN) || inputManager.wasPressed(InputManager::BTN_RIGHT);
if (inputManager.wasPressed(InputManager::BTN_CONFIRM)) {
2025-12-05 22:12:28 +11:00
if (files.empty()) {
return;
}
if (files[selectorIndex].back() == '/') {
if (basepath.back() != '/') basepath += "/";
basepath += files[selectorIndex].substr(0, files[selectorIndex].length() - 1);
loadFiles();
updateRequired = true;
} else {
onSelect(basepath + files[selectorIndex]);
}
2025-12-06 12:35:41 +11:00
} else if (inputManager.wasPressed(InputManager::BTN_BACK) && basepath != "/") {
2025-12-05 22:12:28 +11:00
basepath = basepath.substr(0, basepath.rfind('/'));
if (basepath.empty()) basepath = "/";
loadFiles();
updateRequired = true;
2025-12-06 12:35:41 +11:00
} else if (prevPressed) {
selectorIndex = (selectorIndex + files.size() - 1) % files.size();
updateRequired = true;
} else if (nextPressed) {
selectorIndex = (selectorIndex + 1) % files.size();
updateRequired = true;
2025-12-04 00:07:25 +11:00
}
}
void FileSelectionScreen::displayTaskLoop() {
while (true) {
if (updateRequired) {
updateRequired = false;
xSemaphoreTake(renderingMutex, portMAX_DELAY);
2025-12-04 00:07:25 +11:00
render();
xSemaphoreGive(renderingMutex);
2025-12-04 00:07:25 +11:00
}
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
void FileSelectionScreen::render() const {
2025-12-06 12:56:39 +11:00
renderer.clearScreen();
2025-12-04 00:07:25 +11:00
2025-12-06 12:56:39 +11:00
const auto pageWidth = renderer.getPageWidth();
const auto titleWidth = renderer.getTextWidth("CrossPoint Reader", BOLD);
renderer.drawText((pageWidth - titleWidth) / 2, 0, "CrossPoint Reader", 1, BOLD);
2025-12-04 00:07:25 +11:00
2025-12-05 22:12:28 +11:00
if (files.empty()) {
2025-12-06 12:56:39 +11:00
renderer.drawUiText(10, 50, "No EPUBs found");
2025-12-05 22:12:28 +11:00
} else {
// Draw selection
2025-12-06 12:56:39 +11:00
renderer.fillRect(0, 50 + selectorIndex * 30 + 2, pageWidth - 1, 30);
2025-12-04 00:07:25 +11:00
2025-12-05 22:12:28 +11:00
for (size_t i = 0; i < files.size(); i++) {
const auto file = files[i];
2025-12-06 12:56:39 +11:00
renderer.drawUiText(10, 50 + i * 30, file.c_str(), i == selectorIndex ? 0 : 1);
2025-12-05 22:12:28 +11:00
}
2025-12-04 00:07:25 +11:00
}
2025-12-06 12:56:39 +11:00
renderer.flushDisplay();
2025-12-04 00:07:25 +11:00
}