mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2026-02-04 14:47:37 +03:00
## Summary * **What is the goal of this PR?** Add support for XTC (XTeink X4 native) ebook format, which contains pre-rendered 480x800 1-bit bitmap pages optimized for e-ink displays. * **What changes are included?** - New `lib/Xtc/` library with XtcParser for reading XTC files - XtcReaderActivity for displaying XTC pages on e-ink display - XTC file detection in FileSelectionActivity - Cover BMP generation from first XTC page - Correct XTG page header structure (22 bytes) and bit polarity handling ## Additional Context - XTC files contain pre-rendered bitmap pages with embedded status bar (page numbers, progress %) - XTG page header: 22 bytes (magic + dimensions + reserved fields + bitmap size) - Bit polarity: 0 = black, 1 = white - No runtime text rendering needed - pages display directly on e-ink - Faster page display compared to EPUB since no parsing/rendering required - Memory efficient: loads one page at a time (48KB per page) - Tested with XTC files generated from https://x4converter.rho.sh/ - Verified correct page alignment and color rendering - Please report any issues if you test with XTC files from other sources. --------- Co-authored-by: Dave Allie <dave@daveallie.com>
191 lines
5.8 KiB
C++
191 lines
5.8 KiB
C++
#include "FileSelectionActivity.h"
|
|
|
|
#include <GfxRenderer.h>
|
|
#include <InputManager.h>
|
|
#include <SD.h>
|
|
|
|
#include "config.h"
|
|
|
|
namespace {
|
|
constexpr int PAGE_ITEMS = 23;
|
|
constexpr int SKIP_PAGE_MS = 700;
|
|
constexpr unsigned long GO_HOME_MS = 1000;
|
|
} // namespace
|
|
|
|
void sortFileList(std::vector<std::string>& strs) {
|
|
std::sort(begin(strs), end(strs), [](const std::string& str1, const std::string& str2) {
|
|
if (str1.back() == '/' && str2.back() != '/') return true;
|
|
if (str1.back() != '/' && str2.back() == '/') return false;
|
|
return lexicographical_compare(
|
|
begin(str1), end(str1), begin(str2), end(str2),
|
|
[](const char& char1, const char& char2) { return tolower(char1) < tolower(char2); });
|
|
});
|
|
}
|
|
|
|
void FileSelectionActivity::taskTrampoline(void* param) {
|
|
auto* self = static_cast<FileSelectionActivity*>(param);
|
|
self->displayTaskLoop();
|
|
}
|
|
|
|
void FileSelectionActivity::loadFiles() {
|
|
files.clear();
|
|
selectorIndex = 0;
|
|
auto root = SD.open(basepath.c_str());
|
|
for (File file = root.openNextFile(); file; file = root.openNextFile()) {
|
|
auto filename = std::string(file.name());
|
|
if (filename[0] == '.') {
|
|
file.close();
|
|
continue;
|
|
}
|
|
|
|
if (file.isDirectory()) {
|
|
files.emplace_back(filename + "/");
|
|
} else {
|
|
std::string ext4 = filename.length() >= 4 ? filename.substr(filename.length() - 4) : "";
|
|
std::string ext5 = filename.length() >= 5 ? filename.substr(filename.length() - 5) : "";
|
|
if (ext5 == ".epub" || ext5 == ".xtch" || ext4 == ".xtc") {
|
|
files.emplace_back(filename);
|
|
}
|
|
}
|
|
file.close();
|
|
}
|
|
root.close();
|
|
sortFileList(files);
|
|
}
|
|
|
|
void FileSelectionActivity::onEnter() {
|
|
Activity::onEnter();
|
|
|
|
renderingMutex = xSemaphoreCreateMutex();
|
|
|
|
// basepath is set via constructor parameter (defaults to "/" if not specified)
|
|
loadFiles();
|
|
selectorIndex = 0;
|
|
|
|
// Trigger first update
|
|
updateRequired = true;
|
|
|
|
xTaskCreate(&FileSelectionActivity::taskTrampoline, "FileSelectionActivityTask",
|
|
2048, // Stack size
|
|
this, // Parameters
|
|
1, // Priority
|
|
&displayTaskHandle // Task handle
|
|
);
|
|
}
|
|
|
|
void FileSelectionActivity::onExit() {
|
|
Activity::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();
|
|
}
|
|
|
|
void FileSelectionActivity::loop() {
|
|
// Long press BACK (1s+) goes to root folder
|
|
if (inputManager.isPressed(InputManager::BTN_BACK) && inputManager.getHeldTime() >= GO_HOME_MS) {
|
|
if (basepath != "/") {
|
|
basepath = "/";
|
|
loadFiles();
|
|
updateRequired = true;
|
|
}
|
|
return;
|
|
}
|
|
|
|
const bool prevReleased =
|
|
inputManager.wasReleased(InputManager::BTN_UP) || inputManager.wasReleased(InputManager::BTN_LEFT);
|
|
const bool nextReleased =
|
|
inputManager.wasReleased(InputManager::BTN_DOWN) || inputManager.wasReleased(InputManager::BTN_RIGHT);
|
|
|
|
const bool skipPage = inputManager.getHeldTime() > SKIP_PAGE_MS;
|
|
|
|
if (inputManager.wasReleased(InputManager::BTN_CONFIRM)) {
|
|
if (files.empty()) {
|
|
return;
|
|
}
|
|
|
|
if (basepath.back() != '/') basepath += "/";
|
|
if (files[selectorIndex].back() == '/') {
|
|
basepath += files[selectorIndex].substr(0, files[selectorIndex].length() - 1);
|
|
loadFiles();
|
|
updateRequired = true;
|
|
} else {
|
|
onSelect(basepath + files[selectorIndex]);
|
|
}
|
|
} else if (inputManager.wasReleased(InputManager::BTN_BACK)) {
|
|
// Short press: go up one directory, or go home if at root
|
|
if (inputManager.getHeldTime() < GO_HOME_MS) {
|
|
if (basepath != "/") {
|
|
basepath.replace(basepath.find_last_of('/'), std::string::npos, "");
|
|
if (basepath.empty()) basepath = "/";
|
|
loadFiles();
|
|
updateRequired = true;
|
|
} else {
|
|
onGoHome();
|
|
}
|
|
}
|
|
} else if (prevReleased) {
|
|
if (skipPage) {
|
|
selectorIndex = ((selectorIndex / PAGE_ITEMS - 1) * PAGE_ITEMS + files.size()) % files.size();
|
|
} else {
|
|
selectorIndex = (selectorIndex + files.size() - 1) % files.size();
|
|
}
|
|
updateRequired = true;
|
|
} else if (nextReleased) {
|
|
if (skipPage) {
|
|
selectorIndex = ((selectorIndex / PAGE_ITEMS + 1) * PAGE_ITEMS) % files.size();
|
|
} else {
|
|
selectorIndex = (selectorIndex + 1) % files.size();
|
|
}
|
|
updateRequired = true;
|
|
}
|
|
}
|
|
|
|
void FileSelectionActivity::displayTaskLoop() {
|
|
while (true) {
|
|
if (updateRequired) {
|
|
updateRequired = false;
|
|
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
|
render();
|
|
xSemaphoreGive(renderingMutex);
|
|
}
|
|
vTaskDelay(10 / portTICK_PERIOD_MS);
|
|
}
|
|
}
|
|
|
|
void FileSelectionActivity::render() const {
|
|
renderer.clearScreen();
|
|
|
|
const auto pageWidth = renderer.getScreenWidth();
|
|
renderer.drawCenteredText(READER_FONT_ID, 10, "Books", true, BOLD);
|
|
|
|
// Help text
|
|
renderer.drawButtonHints(UI_FONT_ID, "« Home", "Open", "", "");
|
|
|
|
if (files.empty()) {
|
|
renderer.drawText(UI_FONT_ID, 20, 60, "No books found");
|
|
renderer.displayBuffer();
|
|
return;
|
|
}
|
|
|
|
const auto pageStartIndex = selectorIndex / PAGE_ITEMS * PAGE_ITEMS;
|
|
renderer.fillRect(0, 60 + (selectorIndex % PAGE_ITEMS) * 30 - 2, pageWidth - 1, 30);
|
|
for (int i = pageStartIndex; i < files.size() && i < pageStartIndex + PAGE_ITEMS; i++) {
|
|
auto item = files[i];
|
|
int itemWidth = renderer.getTextWidth(UI_FONT_ID, item.c_str());
|
|
while (itemWidth > renderer.getScreenWidth() - 40 && item.length() > 8) {
|
|
item.replace(item.length() - 5, 5, "...");
|
|
itemWidth = renderer.getTextWidth(UI_FONT_ID, item.c_str());
|
|
}
|
|
renderer.drawText(UI_FONT_ID, 20, 60 + (i % PAGE_ITEMS) * 30, item.c_str(), i != selectorIndex);
|
|
}
|
|
|
|
renderer.displayBuffer();
|
|
}
|