2025-12-17 23:32:18 +11:00
|
|
|
#include "HomeActivity.h"
|
2025-12-17 20:47:43 +11:00
|
|
|
|
|
|
|
|
#include <GfxRenderer.h>
|
2025-12-22 00:31:25 +11:00
|
|
|
#include <InputManager.h>
|
2025-12-17 20:47:43 +11:00
|
|
|
#include <SD.h>
|
|
|
|
|
|
2025-12-26 09:55:23 +09:00
|
|
|
#include "CrossPointState.h"
|
2025-12-17 20:47:43 +11:00
|
|
|
#include "config.h"
|
|
|
|
|
|
2025-12-17 23:32:18 +11:00
|
|
|
void HomeActivity::taskTrampoline(void* param) {
|
|
|
|
|
auto* self = static_cast<HomeActivity*>(param);
|
2025-12-17 20:47:43 +11:00
|
|
|
self->displayTaskLoop();
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-26 09:55:23 +09:00
|
|
|
int HomeActivity::getMenuItemCount() const { return hasContinueReading ? 4 : 3; }
|
|
|
|
|
|
2025-12-17 23:32:18 +11:00
|
|
|
void HomeActivity::onEnter() {
|
2025-12-21 21:17:00 +11:00
|
|
|
Activity::onEnter();
|
|
|
|
|
|
2025-12-17 20:47:43 +11:00
|
|
|
renderingMutex = xSemaphoreCreateMutex();
|
|
|
|
|
|
2025-12-26 09:55:23 +09:00
|
|
|
// Check if we have a book to continue reading
|
|
|
|
|
hasContinueReading = !APP_STATE.openEpubPath.empty() && SD.exists(APP_STATE.openEpubPath.c_str());
|
|
|
|
|
|
2025-12-17 20:47:43 +11:00
|
|
|
selectorIndex = 0;
|
|
|
|
|
|
|
|
|
|
// Trigger first update
|
|
|
|
|
updateRequired = true;
|
|
|
|
|
|
2025-12-17 23:32:18 +11:00
|
|
|
xTaskCreate(&HomeActivity::taskTrampoline, "HomeActivityTask",
|
2025-12-17 20:47:43 +11:00
|
|
|
2048, // Stack size
|
|
|
|
|
this, // Parameters
|
|
|
|
|
1, // Priority
|
|
|
|
|
&displayTaskHandle // Task handle
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-17 23:32:18 +11:00
|
|
|
void HomeActivity::onExit() {
|
2025-12-21 21:17:00 +11:00
|
|
|
Activity::onExit();
|
|
|
|
|
|
2025-12-17 20:47:43 +11:00
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-17 23:32:18 +11:00
|
|
|
void HomeActivity::loop() {
|
2025-12-28 21:59:14 -06:00
|
|
|
const bool prevPressed = mappedInput.wasPressed(MappedInputManager::Button::Up) ||
|
|
|
|
|
mappedInput.wasPressed(MappedInputManager::Button::Left);
|
|
|
|
|
const bool nextPressed = mappedInput.wasPressed(MappedInputManager::Button::Down) ||
|
|
|
|
|
mappedInput.wasPressed(MappedInputManager::Button::Right);
|
2025-12-17 20:47:43 +11:00
|
|
|
|
2025-12-26 09:55:23 +09:00
|
|
|
const int menuCount = getMenuItemCount();
|
|
|
|
|
|
2025-12-28 21:59:14 -06:00
|
|
|
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
|
2025-12-26 09:55:23 +09:00
|
|
|
if (hasContinueReading) {
|
|
|
|
|
// Menu: Continue Reading, Browse, File transfer, Settings
|
|
|
|
|
if (selectorIndex == 0) {
|
|
|
|
|
onContinueReading();
|
|
|
|
|
} else if (selectorIndex == 1) {
|
|
|
|
|
onReaderOpen();
|
|
|
|
|
} else if (selectorIndex == 2) {
|
|
|
|
|
onFileTransferOpen();
|
|
|
|
|
} else if (selectorIndex == 3) {
|
|
|
|
|
onSettingsOpen();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Menu: Browse, File transfer, Settings
|
|
|
|
|
if (selectorIndex == 0) {
|
|
|
|
|
onReaderOpen();
|
|
|
|
|
} else if (selectorIndex == 1) {
|
|
|
|
|
onFileTransferOpen();
|
|
|
|
|
} else if (selectorIndex == 2) {
|
|
|
|
|
onSettingsOpen();
|
|
|
|
|
}
|
2025-12-17 20:47:43 +11:00
|
|
|
}
|
|
|
|
|
} else if (prevPressed) {
|
2025-12-26 09:55:23 +09:00
|
|
|
selectorIndex = (selectorIndex + menuCount - 1) % menuCount;
|
2025-12-17 20:47:43 +11:00
|
|
|
updateRequired = true;
|
|
|
|
|
} else if (nextPressed) {
|
2025-12-26 09:55:23 +09:00
|
|
|
selectorIndex = (selectorIndex + 1) % menuCount;
|
2025-12-17 20:47:43 +11:00
|
|
|
updateRequired = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-17 23:32:18 +11:00
|
|
|
void HomeActivity::displayTaskLoop() {
|
2025-12-17 20:47:43 +11:00
|
|
|
while (true) {
|
|
|
|
|
if (updateRequired) {
|
|
|
|
|
updateRequired = false;
|
|
|
|
|
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
|
|
|
|
render();
|
|
|
|
|
xSemaphoreGive(renderingMutex);
|
|
|
|
|
}
|
|
|
|
|
vTaskDelay(10 / portTICK_PERIOD_MS);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-17 23:32:18 +11:00
|
|
|
void HomeActivity::render() const {
|
2025-12-17 20:47:43 +11:00
|
|
|
renderer.clearScreen();
|
|
|
|
|
|
2025-12-22 00:31:25 +11:00
|
|
|
const auto pageWidth = renderer.getScreenWidth();
|
2025-12-17 20:47:43 +11:00
|
|
|
renderer.drawCenteredText(READER_FONT_ID, 10, "CrossPoint Reader", true, BOLD);
|
|
|
|
|
|
|
|
|
|
// Draw selection
|
2025-12-28 21:30:01 +10:00
|
|
|
renderer.fillRect(0, 60 + selectorIndex * 30 - 2, pageWidth - 1, 30);
|
2025-12-26 09:55:23 +09:00
|
|
|
|
|
|
|
|
int menuY = 60;
|
|
|
|
|
int menuIndex = 0;
|
|
|
|
|
|
|
|
|
|
if (hasContinueReading) {
|
|
|
|
|
// Extract filename from path for display
|
|
|
|
|
std::string bookName = APP_STATE.openEpubPath;
|
|
|
|
|
const size_t lastSlash = bookName.find_last_of('/');
|
|
|
|
|
if (lastSlash != std::string::npos) {
|
|
|
|
|
bookName = bookName.substr(lastSlash + 1);
|
|
|
|
|
}
|
|
|
|
|
// Remove .epub extension
|
|
|
|
|
if (bookName.length() > 5 && bookName.substr(bookName.length() - 5) == ".epub") {
|
|
|
|
|
bookName.resize(bookName.length() - 5);
|
|
|
|
|
}
|
2025-12-29 13:18:23 +01:00
|
|
|
|
2025-12-26 09:55:23 +09:00
|
|
|
// Truncate if too long
|
|
|
|
|
std::string continueLabel = "Continue: " + bookName;
|
2025-12-29 13:18:23 +01:00
|
|
|
int itemWidth = renderer.getTextWidth(UI_FONT_ID, continueLabel.c_str());
|
|
|
|
|
while (itemWidth > renderer.getScreenWidth() - 40 && continueLabel.length() > 8) {
|
|
|
|
|
continueLabel.replace(continueLabel.length() - 5, 5, "...");
|
|
|
|
|
itemWidth = renderer.getTextWidth(UI_FONT_ID, continueLabel.c_str());
|
|
|
|
|
Serial.printf("[%lu] [HOM] width: %lu, pageWidth: %lu\n", millis(), itemWidth, pageWidth);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-26 09:55:23 +09:00
|
|
|
renderer.drawText(UI_FONT_ID, 20, menuY, continueLabel.c_str(), selectorIndex != menuIndex);
|
|
|
|
|
menuY += 30;
|
|
|
|
|
menuIndex++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderer.drawText(UI_FONT_ID, 20, menuY, "Browse", selectorIndex != menuIndex);
|
|
|
|
|
menuY += 30;
|
|
|
|
|
menuIndex++;
|
|
|
|
|
|
|
|
|
|
renderer.drawText(UI_FONT_ID, 20, menuY, "File transfer", selectorIndex != menuIndex);
|
|
|
|
|
menuY += 30;
|
|
|
|
|
menuIndex++;
|
|
|
|
|
|
|
|
|
|
renderer.drawText(UI_FONT_ID, 20, menuY, "Settings", selectorIndex != menuIndex);
|
2025-12-17 20:47:43 +11:00
|
|
|
|
2025-12-28 21:59:14 -06:00
|
|
|
const auto labels = mappedInput.mapLabels("Back", "Confirm", "Left", "Right");
|
|
|
|
|
renderer.drawButtonHints(UI_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
|
2025-12-17 20:47:43 +11:00
|
|
|
|
|
|
|
|
renderer.displayBuffer();
|
|
|
|
|
}
|