Compare commits

..

1 Commits

Author SHA1 Message Date
Uri Tauber
389b119bfc
Merge 2e11b5723c into 8c1c80787a 2026-01-28 02:43:21 +11:00
31 changed files with 954 additions and 952 deletions

4
.gitignore vendored
View File

@ -6,6 +6,4 @@ lib/EpdFont/fontsrc
*.generated.h *.generated.h
.vs .vs
build build
**/__pycache__/ **/__pycache__/
/compile_commands.json
/.cache

View File

@ -368,46 +368,69 @@ void XMLCALL ChapterHtmlSlimParser::startElement(void* userData, const XML_Char*
} }
} }
if (matches(name, IMAGE_TAGS, NUM_IMAGE_TAGS)) {
// TODO: Start processing image tags
std::string alt;
if (atts != nullptr) {
for (int i = 0; atts[i]; i += 2) {
if (strcmp(atts[i], "alt") == 0) {
alt = "[Image: " + std::string(atts[i + 1]) + "]";
}
}
Serial.printf("[%lu] [EHP] Image alt: %s\n", millis(), alt.c_str());
self->startNewTextBlock(TextBlock::CENTER_ALIGN);
self->italicUntilDepth = min(self->italicUntilDepth, self->depth);
self->depth += 1;
self->characterData(userData, alt.c_str(), alt.length());
} else {
// Skip for now
self->skipUntilDepth = self->depth;
self->depth += 1;
return;
}
}
// Special handling for tables - show placeholder text instead of dropping silently // Special handling for tables - show placeholder text instead of dropping silently
if (strcmp(name, "table") == 0) { if (strcmp(name, "table") == 0) {
// Add placeholder text // Add placeholder text
self->startNewTextBlock(TextBlock::CENTER_ALIGN); self->startNewTextBlock(TextBlock::CENTER_ALIGN);
if (self->currentTextBlock) {
self->currentTextBlock->addWord("[Table omitted]", EpdFontFamily::ITALIC);
}
self->italicUntilDepth = min(self->italicUntilDepth, self->depth); // Skip table contents
// Advance depth before processing character data (like you would for a element with text) self->skipUntilDepth = self->depth;
self->depth += 1; self->depth += 1;
self->characterData(userData, "[Table omitted]", strlen("[Table omitted]"));
// Skip table contents (skip until parent as we pre-advanced depth above)
self->skipUntilDepth = self->depth - 1;
return; return;
} }
if (matches(name, IMAGE_TAGS, NUM_IMAGE_TAGS)) { if (matches(name, IMAGE_TAGS, NUM_IMAGE_TAGS)) {
// TODO: Start processing image tags // TODO: Start processing image tags
std::string alt = "[Image]"; std::string alt;
if (atts != nullptr) { if (atts != nullptr) {
for (int i = 0; atts[i]; i += 2) { for (int i = 0; atts[i]; i += 2) {
if (strcmp(atts[i], "alt") == 0) { if (strcmp(atts[i], "alt") == 0) {
if (strlen(atts[i + 1]) > 0) { // add " " (counts as whitespace) at the end of alt
alt = "[Image: " + std::string(atts[i + 1]) + "]"; // so the corresponding text block ends.
} // TODO: A zero-width breaking space would be more appropriate (once/if we support it)
break; alt = "[Image: " + std::string(atts[i + 1]) + "] ";
} }
} }
Serial.printf("[%lu] [EHP] Image alt: %s\n", millis(), alt.c_str());
self->startNewTextBlock(TextBlock::CENTER_ALIGN);
self->italicUntilDepth = min(self->italicUntilDepth, self->depth);
self->depth += 1;
self->characterData(userData, alt.c_str(), alt.length());
return;
} else {
// Skip for now
self->skipUntilDepth = self->depth;
self->depth += 1;
return;
} }
Serial.printf("[%lu] [EHP] Image alt: %s\n", millis(), alt.c_str());
self->startNewTextBlock(TextBlock::CENTER_ALIGN);
self->italicUntilDepth = min(self->italicUntilDepth, self->depth);
// Advance depth before processing character data (like you would for a element with text)
self->depth += 1;
self->characterData(userData, alt.c_str(), alt.length());
// Skip table contents (skip until parent as we pre-advanced depth above)
self->skipUntilDepth = self->depth - 1;
return;
} }
if (matches(name, SKIP_TAGS, NUM_SKIP_TAGS)) { if (matches(name, SKIP_TAGS, NUM_SKIP_TAGS)) {
@ -431,43 +454,25 @@ void XMLCALL ChapterHtmlSlimParser::startElement(void* userData, const XML_Char*
if (matches(name, HEADER_TAGS, NUM_HEADER_TAGS)) { if (matches(name, HEADER_TAGS, NUM_HEADER_TAGS)) {
self->startNewTextBlock(TextBlock::CENTER_ALIGN); self->startNewTextBlock(TextBlock::CENTER_ALIGN);
self->boldUntilDepth = std::min(self->boldUntilDepth, self->depth); self->boldUntilDepth = std::min(self->boldUntilDepth, self->depth);
self->depth += 1; } else if (matches(name, BLOCK_TAGS, NUM_BLOCK_TAGS)) {
return;
}
if (matches(name, BLOCK_TAGS, NUM_BLOCK_TAGS)) {
if (strcmp(name, "br") == 0) { if (strcmp(name, "br") == 0) {
if (self->partWordBufferIndex > 0) { if (self->partWordBufferIndex > 0) {
// flush word preceding <br/> to currentTextBlock before calling startNewTextBlock // flush word preceding <br/> to currentTextBlock before calling startNewTextBlock
self->flushPartWordBuffer(); self->flushPartWordBuffer();
} }
self->startNewTextBlock(self->currentTextBlock->getStyle()); self->startNewTextBlock(self->currentTextBlock->getStyle());
self->depth += 1; } else {
return; self->startNewTextBlock((TextBlock::Style)self->paragraphAlignment);
if (strcmp(name, "li") == 0) {
self->currentTextBlock->addWord("\xe2\x80\xa2", EpdFontFamily::REGULAR);
}
} }
} else if (matches(name, BOLD_TAGS, NUM_BOLD_TAGS)) {
self->startNewTextBlock(static_cast<TextBlock::Style>(self->paragraphAlignment));
if (strcmp(name, "li") == 0) {
self->currentTextBlock->addWord("\xe2\x80\xa2", EpdFontFamily::REGULAR);
}
self->depth += 1;
return;
}
if (matches(name, BOLD_TAGS, NUM_BOLD_TAGS)) {
self->boldUntilDepth = std::min(self->boldUntilDepth, self->depth); self->boldUntilDepth = std::min(self->boldUntilDepth, self->depth);
self->depth += 1; } else if (matches(name, ITALIC_TAGS, NUM_ITALIC_TAGS)) {
return;
}
if (matches(name, ITALIC_TAGS, NUM_ITALIC_TAGS)) {
self->italicUntilDepth = std::min(self->italicUntilDepth, self->depth); self->italicUntilDepth = std::min(self->italicUntilDepth, self->depth);
self->depth += 1;
return;
} }
// Unprocessed tag, just increasing depth and continue forward
self->depth += 1; self->depth += 1;
} }
@ -741,8 +746,7 @@ void XMLCALL ChapterHtmlSlimParser::endElement(void* userData, const XML_Char* n
if (self->partWordBufferIndex > 0) { if (self->partWordBufferIndex > 0) {
const bool shouldBreakText = const bool shouldBreakText =
matches(name, BLOCK_TAGS, NUM_BLOCK_TAGS) || matches(name, HEADER_TAGS, NUM_HEADER_TAGS) || matches(name, BLOCK_TAGS, NUM_BLOCK_TAGS) || matches(name, HEADER_TAGS, NUM_HEADER_TAGS) ||
matches(name, BOLD_TAGS, NUM_BOLD_TAGS) || matches(name, ITALIC_TAGS, NUM_ITALIC_TAGS) || matches(name, BOLD_TAGS, NUM_BOLD_TAGS) || matches(name, ITALIC_TAGS, NUM_ITALIC_TAGS) || self->depth == 1;
strcmp(name, "table") == 0 || matches(name, IMAGE_TAGS, NUM_IMAGE_TAGS) || self->depth == 1;
if (shouldBreakText) { if (shouldBreakText) {
self->flushPartWordBuffer(); self->flushPartWordBuffer();

View File

@ -10,19 +10,19 @@ void GfxRenderer::rotateCoordinates(const int x, const int y, int* rotatedX, int
// Logical portrait (480x800) → panel (800x480) // Logical portrait (480x800) → panel (800x480)
// Rotation: 90 degrees clockwise // Rotation: 90 degrees clockwise
*rotatedX = y; *rotatedX = y;
*rotatedY = HalDisplay::DISPLAY_HEIGHT - 1 - x; *rotatedY = EInkDisplay::DISPLAY_HEIGHT - 1 - x;
break; break;
} }
case LandscapeClockwise: { case LandscapeClockwise: {
// Logical landscape (800x480) rotated 180 degrees (swap top/bottom and left/right) // Logical landscape (800x480) rotated 180 degrees (swap top/bottom and left/right)
*rotatedX = HalDisplay::DISPLAY_WIDTH - 1 - x; *rotatedX = EInkDisplay::DISPLAY_WIDTH - 1 - x;
*rotatedY = HalDisplay::DISPLAY_HEIGHT - 1 - y; *rotatedY = EInkDisplay::DISPLAY_HEIGHT - 1 - y;
break; break;
} }
case PortraitInverted: { case PortraitInverted: {
// Logical portrait (480x800) → panel (800x480) // Logical portrait (480x800) → panel (800x480)
// Rotation: 90 degrees counter-clockwise // Rotation: 90 degrees counter-clockwise
*rotatedX = HalDisplay::DISPLAY_WIDTH - 1 - y; *rotatedX = EInkDisplay::DISPLAY_WIDTH - 1 - y;
*rotatedY = x; *rotatedY = x;
break; break;
} }
@ -36,7 +36,7 @@ void GfxRenderer::rotateCoordinates(const int x, const int y, int* rotatedX, int
} }
void GfxRenderer::drawPixel(const int x, const int y, const bool state) const { void GfxRenderer::drawPixel(const int x, const int y, const bool state) const {
uint8_t* frameBuffer = display.getFrameBuffer(); uint8_t* frameBuffer = einkDisplay.getFrameBuffer();
// Early return if no framebuffer is set // Early return if no framebuffer is set
if (!frameBuffer) { if (!frameBuffer) {
@ -49,13 +49,14 @@ void GfxRenderer::drawPixel(const int x, const int y, const bool state) const {
rotateCoordinates(x, y, &rotatedX, &rotatedY); rotateCoordinates(x, y, &rotatedX, &rotatedY);
// Bounds checking against physical panel dimensions // Bounds checking against physical panel dimensions
if (rotatedX < 0 || rotatedX >= HalDisplay::DISPLAY_WIDTH || rotatedY < 0 || rotatedY >= HalDisplay::DISPLAY_HEIGHT) { if (rotatedX < 0 || rotatedX >= EInkDisplay::DISPLAY_WIDTH || rotatedY < 0 ||
rotatedY >= EInkDisplay::DISPLAY_HEIGHT) {
Serial.printf("[%lu] [GFX] !! Outside range (%d, %d) -> (%d, %d)\n", millis(), x, y, rotatedX, rotatedY); Serial.printf("[%lu] [GFX] !! Outside range (%d, %d) -> (%d, %d)\n", millis(), x, y, rotatedX, rotatedY);
return; return;
} }
// Calculate byte position and bit position // Calculate byte position and bit position
const uint16_t byteIndex = rotatedY * HalDisplay::DISPLAY_WIDTH_BYTES + (rotatedX / 8); const uint16_t byteIndex = rotatedY * EInkDisplay::DISPLAY_WIDTH_BYTES + (rotatedX / 8);
const uint8_t bitPosition = 7 - (rotatedX % 8); // MSB first const uint8_t bitPosition = 7 - (rotatedX % 8); // MSB first
if (state) { if (state) {
@ -163,7 +164,7 @@ void GfxRenderer::drawImage(const uint8_t bitmap[], const int x, const int y, co
break; break;
} }
// TODO: Rotate bits // TODO: Rotate bits
display.drawImage(bitmap, rotatedX, rotatedY, width, height); einkDisplay.drawImage(bitmap, rotatedX, rotatedY, width, height);
} }
void GfxRenderer::drawBitmap(const Bitmap& bitmap, const int x, const int y, const int maxWidth, const int maxHeight, void GfxRenderer::drawBitmap(const Bitmap& bitmap, const int x, const int y, const int maxWidth, const int maxHeight,
@ -398,20 +399,22 @@ void GfxRenderer::fillPolygon(const int* xPoints, const int* yPoints, int numPoi
free(nodeX); free(nodeX);
} }
void GfxRenderer::clearScreen(const uint8_t color) const { display.clearScreen(color); } void GfxRenderer::clearScreen(const uint8_t color) const { einkDisplay.clearScreen(color); }
void GfxRenderer::invertScreen() const { void GfxRenderer::invertScreen() const {
uint8_t* buffer = display.getFrameBuffer(); uint8_t* buffer = einkDisplay.getFrameBuffer();
if (!buffer) { if (!buffer) {
Serial.printf("[%lu] [GFX] !! No framebuffer in invertScreen\n", millis()); Serial.printf("[%lu] [GFX] !! No framebuffer in invertScreen\n", millis());
return; return;
} }
for (int i = 0; i < HalDisplay::BUFFER_SIZE; i++) { for (int i = 0; i < EInkDisplay::BUFFER_SIZE; i++) {
buffer[i] = ~buffer[i]; buffer[i] = ~buffer[i];
} }
} }
void GfxRenderer::displayBuffer(const HalDisplay::RefreshMode refreshMode) const { display.displayBuffer(refreshMode); } void GfxRenderer::displayBuffer(const EInkDisplay::RefreshMode refreshMode) const {
einkDisplay.displayBuffer(refreshMode);
}
std::string GfxRenderer::truncatedText(const int fontId, const char* text, const int maxWidth, std::string GfxRenderer::truncatedText(const int fontId, const char* text, const int maxWidth,
const EpdFontFamily::Style style) const { const EpdFontFamily::Style style) const {
@ -430,13 +433,13 @@ int GfxRenderer::getScreenWidth() const {
case Portrait: case Portrait:
case PortraitInverted: case PortraitInverted:
// 480px wide in portrait logical coordinates // 480px wide in portrait logical coordinates
return HalDisplay::DISPLAY_HEIGHT; return EInkDisplay::DISPLAY_HEIGHT;
case LandscapeClockwise: case LandscapeClockwise:
case LandscapeCounterClockwise: case LandscapeCounterClockwise:
// 800px wide in landscape logical coordinates // 800px wide in landscape logical coordinates
return HalDisplay::DISPLAY_WIDTH; return EInkDisplay::DISPLAY_WIDTH;
} }
return HalDisplay::DISPLAY_HEIGHT; return EInkDisplay::DISPLAY_HEIGHT;
} }
int GfxRenderer::getScreenHeight() const { int GfxRenderer::getScreenHeight() const {
@ -444,13 +447,13 @@ int GfxRenderer::getScreenHeight() const {
case Portrait: case Portrait:
case PortraitInverted: case PortraitInverted:
// 800px tall in portrait logical coordinates // 800px tall in portrait logical coordinates
return HalDisplay::DISPLAY_WIDTH; return EInkDisplay::DISPLAY_WIDTH;
case LandscapeClockwise: case LandscapeClockwise:
case LandscapeCounterClockwise: case LandscapeCounterClockwise:
// 480px tall in landscape logical coordinates // 480px tall in landscape logical coordinates
return HalDisplay::DISPLAY_HEIGHT; return EInkDisplay::DISPLAY_HEIGHT;
} }
return HalDisplay::DISPLAY_WIDTH; return EInkDisplay::DISPLAY_WIDTH;
} }
int GfxRenderer::getSpaceWidth(const int fontId) const { int GfxRenderer::getSpaceWidth(const int fontId) const {
@ -650,18 +653,17 @@ void GfxRenderer::drawTextRotated90CW(const int fontId, const int x, const int y
} }
} }
uint8_t* GfxRenderer::getFrameBuffer() const { return display.getFrameBuffer(); } uint8_t* GfxRenderer::getFrameBuffer() const { return einkDisplay.getFrameBuffer(); }
size_t GfxRenderer::getBufferSize() { return HalDisplay::BUFFER_SIZE; } size_t GfxRenderer::getBufferSize() { return EInkDisplay::BUFFER_SIZE; }
// unused void GfxRenderer::grayscaleRevert() const { einkDisplay.grayscaleRevert(); }
// void GfxRenderer::grayscaleRevert() const { display.grayscaleRevert(); }
void GfxRenderer::copyGrayscaleLsbBuffers() const { display.copyGrayscaleLsbBuffers(display.getFrameBuffer()); } void GfxRenderer::copyGrayscaleLsbBuffers() const { einkDisplay.copyGrayscaleLsbBuffers(einkDisplay.getFrameBuffer()); }
void GfxRenderer::copyGrayscaleMsbBuffers() const { display.copyGrayscaleMsbBuffers(display.getFrameBuffer()); } void GfxRenderer::copyGrayscaleMsbBuffers() const { einkDisplay.copyGrayscaleMsbBuffers(einkDisplay.getFrameBuffer()); }
void GfxRenderer::displayGrayBuffer() const { display.displayGrayBuffer(); } void GfxRenderer::displayGrayBuffer() const { einkDisplay.displayGrayBuffer(); }
void GfxRenderer::freeBwBufferChunks() { void GfxRenderer::freeBwBufferChunks() {
for (auto& bwBufferChunk : bwBufferChunks) { for (auto& bwBufferChunk : bwBufferChunks) {
@ -679,7 +681,7 @@ void GfxRenderer::freeBwBufferChunks() {
* Returns true if buffer was stored successfully, false if allocation failed. * Returns true if buffer was stored successfully, false if allocation failed.
*/ */
bool GfxRenderer::storeBwBuffer() { bool GfxRenderer::storeBwBuffer() {
const uint8_t* frameBuffer = display.getFrameBuffer(); const uint8_t* frameBuffer = einkDisplay.getFrameBuffer();
if (!frameBuffer) { if (!frameBuffer) {
Serial.printf("[%lu] [GFX] !! No framebuffer in storeBwBuffer\n", millis()); Serial.printf("[%lu] [GFX] !! No framebuffer in storeBwBuffer\n", millis());
return false; return false;
@ -734,7 +736,7 @@ void GfxRenderer::restoreBwBuffer() {
return; return;
} }
uint8_t* frameBuffer = display.getFrameBuffer(); uint8_t* frameBuffer = einkDisplay.getFrameBuffer();
if (!frameBuffer) { if (!frameBuffer) {
Serial.printf("[%lu] [GFX] !! No framebuffer in restoreBwBuffer\n", millis()); Serial.printf("[%lu] [GFX] !! No framebuffer in restoreBwBuffer\n", millis());
freeBwBufferChunks(); freeBwBufferChunks();
@ -753,7 +755,7 @@ void GfxRenderer::restoreBwBuffer() {
memcpy(frameBuffer + offset, bwBufferChunks[i], BW_BUFFER_CHUNK_SIZE); memcpy(frameBuffer + offset, bwBufferChunks[i], BW_BUFFER_CHUNK_SIZE);
} }
display.cleanupGrayscaleBuffers(frameBuffer); einkDisplay.cleanupGrayscaleBuffers(frameBuffer);
freeBwBufferChunks(); freeBwBufferChunks();
Serial.printf("[%lu] [GFX] Restored and freed BW buffer chunks\n", millis()); Serial.printf("[%lu] [GFX] Restored and freed BW buffer chunks\n", millis());
@ -764,9 +766,9 @@ void GfxRenderer::restoreBwBuffer() {
* Use this when BW buffer was re-rendered instead of stored/restored. * Use this when BW buffer was re-rendered instead of stored/restored.
*/ */
void GfxRenderer::cleanupGrayscaleWithFrameBuffer() const { void GfxRenderer::cleanupGrayscaleWithFrameBuffer() const {
uint8_t* frameBuffer = display.getFrameBuffer(); uint8_t* frameBuffer = einkDisplay.getFrameBuffer();
if (frameBuffer) { if (frameBuffer) {
display.cleanupGrayscaleBuffers(frameBuffer); einkDisplay.cleanupGrayscaleBuffers(frameBuffer);
} }
} }

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include <EInkDisplay.h>
#include <EpdFontFamily.h> #include <EpdFontFamily.h>
#include <HalDisplay.h>
#include <map> #include <map>
@ -21,11 +21,11 @@ class GfxRenderer {
private: private:
static constexpr size_t BW_BUFFER_CHUNK_SIZE = 8000; // 8KB chunks to allow for non-contiguous memory static constexpr size_t BW_BUFFER_CHUNK_SIZE = 8000; // 8KB chunks to allow for non-contiguous memory
static constexpr size_t BW_BUFFER_NUM_CHUNKS = HalDisplay::BUFFER_SIZE / BW_BUFFER_CHUNK_SIZE; static constexpr size_t BW_BUFFER_NUM_CHUNKS = EInkDisplay::BUFFER_SIZE / BW_BUFFER_CHUNK_SIZE;
static_assert(BW_BUFFER_CHUNK_SIZE * BW_BUFFER_NUM_CHUNKS == HalDisplay::BUFFER_SIZE, static_assert(BW_BUFFER_CHUNK_SIZE * BW_BUFFER_NUM_CHUNKS == EInkDisplay::BUFFER_SIZE,
"BW buffer chunking does not line up with display buffer size"); "BW buffer chunking does not line up with display buffer size");
HalDisplay& display; EInkDisplay& einkDisplay;
RenderMode renderMode; RenderMode renderMode;
Orientation orientation; Orientation orientation;
uint8_t* bwBufferChunks[BW_BUFFER_NUM_CHUNKS] = {nullptr}; uint8_t* bwBufferChunks[BW_BUFFER_NUM_CHUNKS] = {nullptr};
@ -36,7 +36,7 @@ class GfxRenderer {
void rotateCoordinates(int x, int y, int* rotatedX, int* rotatedY) const; void rotateCoordinates(int x, int y, int* rotatedX, int* rotatedY) const;
public: public:
explicit GfxRenderer(HalDisplay& halDisplay) : display(halDisplay), renderMode(BW), orientation(Portrait) {} explicit GfxRenderer(EInkDisplay& einkDisplay) : einkDisplay(einkDisplay), renderMode(BW), orientation(Portrait) {}
~GfxRenderer() { freeBwBufferChunks(); } ~GfxRenderer() { freeBwBufferChunks(); }
static constexpr int VIEWABLE_MARGIN_TOP = 9; static constexpr int VIEWABLE_MARGIN_TOP = 9;
@ -54,7 +54,7 @@ class GfxRenderer {
// Screen ops // Screen ops
int getScreenWidth() const; int getScreenWidth() const;
int getScreenHeight() const; int getScreenHeight() const;
void displayBuffer(HalDisplay::RefreshMode refreshMode = HalDisplay::FAST_REFRESH) const; void displayBuffer(EInkDisplay::RefreshMode refreshMode = EInkDisplay::FAST_REFRESH) const;
// EXPERIMENTAL: Windowed update - display only a rectangular region // EXPERIMENTAL: Windowed update - display only a rectangular region
void displayWindow(int x, int y, int width, int height) const; void displayWindow(int x, int y, int width, int height) const;
void invertScreen() const; void invertScreen() const;

View File

@ -1,51 +0,0 @@
#include <HalDisplay.h>
#include <HalGPIO.h>
#define SD_SPI_MISO 7
HalDisplay::HalDisplay() : einkDisplay(EPD_SCLK, EPD_MOSI, EPD_CS, EPD_DC, EPD_RST, EPD_BUSY) {}
HalDisplay::~HalDisplay() {}
void HalDisplay::begin() { einkDisplay.begin(); }
void HalDisplay::clearScreen(uint8_t color) const { einkDisplay.clearScreen(color); }
void HalDisplay::drawImage(const uint8_t* imageData, uint16_t x, uint16_t y, uint16_t w, uint16_t h,
bool fromProgmem) const {
einkDisplay.drawImage(imageData, x, y, w, h, fromProgmem);
}
EInkDisplay::RefreshMode convertRefreshMode(HalDisplay::RefreshMode mode) {
switch (mode) {
case HalDisplay::FULL_REFRESH:
return EInkDisplay::FULL_REFRESH;
case HalDisplay::HALF_REFRESH:
return EInkDisplay::HALF_REFRESH;
case HalDisplay::FAST_REFRESH:
default:
return EInkDisplay::FAST_REFRESH;
}
}
void HalDisplay::displayBuffer(HalDisplay::RefreshMode mode) { einkDisplay.displayBuffer(convertRefreshMode(mode)); }
void HalDisplay::refreshDisplay(HalDisplay::RefreshMode mode, bool turnOffScreen) {
einkDisplay.refreshDisplay(convertRefreshMode(mode), turnOffScreen);
}
void HalDisplay::deepSleep() { einkDisplay.deepSleep(); }
uint8_t* HalDisplay::getFrameBuffer() const { return einkDisplay.getFrameBuffer(); }
void HalDisplay::copyGrayscaleBuffers(const uint8_t* lsbBuffer, const uint8_t* msbBuffer) {
einkDisplay.copyGrayscaleBuffers(lsbBuffer, msbBuffer);
}
void HalDisplay::copyGrayscaleLsbBuffers(const uint8_t* lsbBuffer) { einkDisplay.copyGrayscaleLsbBuffers(lsbBuffer); }
void HalDisplay::copyGrayscaleMsbBuffers(const uint8_t* msbBuffer) { einkDisplay.copyGrayscaleMsbBuffers(msbBuffer); }
void HalDisplay::cleanupGrayscaleBuffers(const uint8_t* bwBuffer) { einkDisplay.cleanupGrayscaleBuffers(bwBuffer); }
void HalDisplay::displayGrayBuffer() { einkDisplay.displayGrayBuffer(); }

View File

@ -1,52 +0,0 @@
#pragma once
#include <Arduino.h>
#include <EInkDisplay.h>
class HalDisplay {
public:
// Constructor with pin configuration
HalDisplay();
// Destructor
~HalDisplay();
// Refresh modes
enum RefreshMode {
FULL_REFRESH, // Full refresh with complete waveform
HALF_REFRESH, // Half refresh (1720ms) - balanced quality and speed
FAST_REFRESH // Fast refresh using custom LUT
};
// Initialize the display hardware and driver
void begin();
// Display dimensions
static constexpr uint16_t DISPLAY_WIDTH = EInkDisplay::DISPLAY_WIDTH;
static constexpr uint16_t DISPLAY_HEIGHT = EInkDisplay::DISPLAY_HEIGHT;
static constexpr uint16_t DISPLAY_WIDTH_BYTES = DISPLAY_WIDTH / 8;
static constexpr uint32_t BUFFER_SIZE = DISPLAY_WIDTH_BYTES * DISPLAY_HEIGHT;
// Frame buffer operations
void clearScreen(uint8_t color = 0xFF) const;
void drawImage(const uint8_t* imageData, uint16_t x, uint16_t y, uint16_t w, uint16_t h,
bool fromProgmem = false) const;
void displayBuffer(RefreshMode mode = RefreshMode::FAST_REFRESH);
void refreshDisplay(RefreshMode mode = RefreshMode::FAST_REFRESH, bool turnOffScreen = false);
// Power management
void deepSleep();
// Access to frame buffer
uint8_t* getFrameBuffer() const;
void copyGrayscaleBuffers(const uint8_t* lsbBuffer, const uint8_t* msbBuffer);
void copyGrayscaleLsbBuffers(const uint8_t* lsbBuffer);
void copyGrayscaleMsbBuffers(const uint8_t* msbBuffer);
void cleanupGrayscaleBuffers(const uint8_t* bwBuffer);
void displayGrayBuffer();
private:
EInkDisplay einkDisplay;
};

View File

@ -1,55 +0,0 @@
#include <HalGPIO.h>
#include <SPI.h>
#include <esp_sleep.h>
void HalGPIO::begin() {
inputMgr.begin();
SPI.begin(EPD_SCLK, SPI_MISO, EPD_MOSI, EPD_CS);
pinMode(BAT_GPIO0, INPUT);
pinMode(UART0_RXD, INPUT);
}
void HalGPIO::update() { inputMgr.update(); }
bool HalGPIO::isPressed(uint8_t buttonIndex) const { return inputMgr.isPressed(buttonIndex); }
bool HalGPIO::wasPressed(uint8_t buttonIndex) const { return inputMgr.wasPressed(buttonIndex); }
bool HalGPIO::wasAnyPressed() const { return inputMgr.wasAnyPressed(); }
bool HalGPIO::wasReleased(uint8_t buttonIndex) const { return inputMgr.wasReleased(buttonIndex); }
bool HalGPIO::wasAnyReleased() const { return inputMgr.wasAnyReleased(); }
unsigned long HalGPIO::getHeldTime() const { return inputMgr.getHeldTime(); }
void HalGPIO::startDeepSleep() {
esp_deep_sleep_enable_gpio_wakeup(1ULL << InputManager::POWER_BUTTON_PIN, ESP_GPIO_WAKEUP_GPIO_LOW);
// Ensure that the power button has been released to avoid immediately turning back on if you're holding it
while (inputMgr.isPressed(BTN_POWER)) {
delay(50);
inputMgr.update();
}
// Enter Deep Sleep
esp_deep_sleep_start();
}
int HalGPIO::getBatteryPercentage() const {
static const BatteryMonitor battery = BatteryMonitor(BAT_GPIO0);
return battery.readPercentage();
}
bool HalGPIO::isUsbConnected() const {
// U0RXD/GPIO20 reads HIGH when USB is connected
return digitalRead(UART0_RXD) == HIGH;
}
bool HalGPIO::isWakeupByPowerButton() const {
const auto wakeupCause = esp_sleep_get_wakeup_cause();
const auto resetReason = esp_reset_reason();
if (isUsbConnected()) {
return wakeupCause == ESP_SLEEP_WAKEUP_GPIO;
} else {
return (wakeupCause == ESP_SLEEP_WAKEUP_UNDEFINED) && (resetReason == ESP_RST_POWERON);
}
}

View File

@ -1,61 +0,0 @@
#pragma once
#include <Arduino.h>
#include <BatteryMonitor.h>
#include <InputManager.h>
// Display SPI pins (custom pins for XteinkX4, not hardware SPI defaults)
#define EPD_SCLK 8 // SPI Clock
#define EPD_MOSI 10 // SPI MOSI (Master Out Slave In)
#define EPD_CS 21 // Chip Select
#define EPD_DC 4 // Data/Command
#define EPD_RST 5 // Reset
#define EPD_BUSY 6 // Busy
#define SPI_MISO 7 // SPI MISO, shared between SD card and display (Master In Slave Out)
#define BAT_GPIO0 0 // Battery voltage
#define UART0_RXD 20 // Used for USB connection detection
class HalGPIO {
#if CROSSPOINT_EMULATED == 0
InputManager inputMgr;
#endif
public:
HalGPIO() = default;
// Start button GPIO and setup SPI for screen and SD card
void begin();
// Button input methods
void update();
bool isPressed(uint8_t buttonIndex) const;
bool wasPressed(uint8_t buttonIndex) const;
bool wasAnyPressed() const;
bool wasReleased(uint8_t buttonIndex) const;
bool wasAnyReleased() const;
unsigned long getHeldTime() const;
// Setup wake up GPIO and enter deep sleep
void startDeepSleep();
// Get battery percentage (range 0-100)
int getBatteryPercentage() const;
// Check if USB is connected
bool isUsbConnected() const;
// Check if wakeup was caused by power button press
bool isWakeupByPowerButton() const;
// Button indices
static constexpr uint8_t BTN_BACK = 0;
static constexpr uint8_t BTN_CONFIRM = 1;
static constexpr uint8_t BTN_LEFT = 2;
static constexpr uint8_t BTN_RIGHT = 3;
static constexpr uint8_t BTN_UP = 4;
static constexpr uint8_t BTN_DOWN = 5;
static constexpr uint8_t BTN_POWER = 6;
};

View File

@ -2,7 +2,7 @@
default_envs = default default_envs = default
[crosspoint] [crosspoint]
version = 0.16.0 version = 0.15.0
[base] [base]
platform = espressif32 @ 6.12.0 platform = espressif32 @ 6.12.0

View File

@ -19,20 +19,20 @@ struct SideLayoutMap {
// Order matches CrossPointSettings::FRONT_BUTTON_LAYOUT. // Order matches CrossPointSettings::FRONT_BUTTON_LAYOUT.
constexpr FrontLayoutMap kFrontLayouts[] = { constexpr FrontLayoutMap kFrontLayouts[] = {
{HalGPIO::BTN_BACK, HalGPIO::BTN_CONFIRM, HalGPIO::BTN_LEFT, HalGPIO::BTN_RIGHT}, {InputManager::BTN_BACK, InputManager::BTN_CONFIRM, InputManager::BTN_LEFT, InputManager::BTN_RIGHT},
{HalGPIO::BTN_LEFT, HalGPIO::BTN_RIGHT, HalGPIO::BTN_BACK, HalGPIO::BTN_CONFIRM}, {InputManager::BTN_LEFT, InputManager::BTN_RIGHT, InputManager::BTN_BACK, InputManager::BTN_CONFIRM},
{HalGPIO::BTN_CONFIRM, HalGPIO::BTN_LEFT, HalGPIO::BTN_BACK, HalGPIO::BTN_RIGHT}, {InputManager::BTN_CONFIRM, InputManager::BTN_LEFT, InputManager::BTN_BACK, InputManager::BTN_RIGHT},
{HalGPIO::BTN_BACK, HalGPIO::BTN_CONFIRM, HalGPIO::BTN_RIGHT, HalGPIO::BTN_LEFT}, {InputManager::BTN_BACK, InputManager::BTN_CONFIRM, InputManager::BTN_RIGHT, InputManager::BTN_LEFT},
}; };
// Order matches CrossPointSettings::SIDE_BUTTON_LAYOUT. // Order matches CrossPointSettings::SIDE_BUTTON_LAYOUT.
constexpr SideLayoutMap kSideLayouts[] = { constexpr SideLayoutMap kSideLayouts[] = {
{HalGPIO::BTN_UP, HalGPIO::BTN_DOWN}, {InputManager::BTN_UP, InputManager::BTN_DOWN},
{HalGPIO::BTN_DOWN, HalGPIO::BTN_UP}, {InputManager::BTN_DOWN, InputManager::BTN_UP},
}; };
} // namespace } // namespace
bool MappedInputManager::mapButton(const Button button, bool (HalGPIO::*fn)(uint8_t) const) const { bool MappedInputManager::mapButton(const Button button, bool (InputManager::*fn)(uint8_t) const) const {
const auto frontLayout = static_cast<CrossPointSettings::FRONT_BUTTON_LAYOUT>(SETTINGS.frontButtonLayout); const auto frontLayout = static_cast<CrossPointSettings::FRONT_BUTTON_LAYOUT>(SETTINGS.frontButtonLayout);
const auto sideLayout = static_cast<CrossPointSettings::SIDE_BUTTON_LAYOUT>(SETTINGS.sideButtonLayout); const auto sideLayout = static_cast<CrossPointSettings::SIDE_BUTTON_LAYOUT>(SETTINGS.sideButtonLayout);
const auto& front = kFrontLayouts[frontLayout]; const auto& front = kFrontLayouts[frontLayout];
@ -40,39 +40,41 @@ bool MappedInputManager::mapButton(const Button button, bool (HalGPIO::*fn)(uint
switch (button) { switch (button) {
case Button::Back: case Button::Back:
return (gpio.*fn)(front.back); return (inputManager.*fn)(front.back);
case Button::Confirm: case Button::Confirm:
return (gpio.*fn)(front.confirm); return (inputManager.*fn)(front.confirm);
case Button::Left: case Button::Left:
return (gpio.*fn)(front.left); return (inputManager.*fn)(front.left);
case Button::Right: case Button::Right:
return (gpio.*fn)(front.right); return (inputManager.*fn)(front.right);
case Button::Up: case Button::Up:
return (gpio.*fn)(HalGPIO::BTN_UP); return (inputManager.*fn)(InputManager::BTN_UP);
case Button::Down: case Button::Down:
return (gpio.*fn)(HalGPIO::BTN_DOWN); return (inputManager.*fn)(InputManager::BTN_DOWN);
case Button::Power: case Button::Power:
return (gpio.*fn)(HalGPIO::BTN_POWER); return (inputManager.*fn)(InputManager::BTN_POWER);
case Button::PageBack: case Button::PageBack:
return (gpio.*fn)(side.pageBack); return (inputManager.*fn)(side.pageBack);
case Button::PageForward: case Button::PageForward:
return (gpio.*fn)(side.pageForward); return (inputManager.*fn)(side.pageForward);
} }
return false; return false;
} }
bool MappedInputManager::wasPressed(const Button button) const { return mapButton(button, &HalGPIO::wasPressed); } bool MappedInputManager::wasPressed(const Button button) const { return mapButton(button, &InputManager::wasPressed); }
bool MappedInputManager::wasReleased(const Button button) const { return mapButton(button, &HalGPIO::wasReleased); } bool MappedInputManager::wasReleased(const Button button) const {
return mapButton(button, &InputManager::wasReleased);
}
bool MappedInputManager::isPressed(const Button button) const { return mapButton(button, &HalGPIO::isPressed); } bool MappedInputManager::isPressed(const Button button) const { return mapButton(button, &InputManager::isPressed); }
bool MappedInputManager::wasAnyPressed() const { return gpio.wasAnyPressed(); } bool MappedInputManager::wasAnyPressed() const { return inputManager.wasAnyPressed(); }
bool MappedInputManager::wasAnyReleased() const { return gpio.wasAnyReleased(); } bool MappedInputManager::wasAnyReleased() const { return inputManager.wasAnyReleased(); }
unsigned long MappedInputManager::getHeldTime() const { return gpio.getHeldTime(); } unsigned long MappedInputManager::getHeldTime() const { return inputManager.getHeldTime(); }
MappedInputManager::Labels MappedInputManager::mapLabels(const char* back, const char* confirm, const char* previous, MappedInputManager::Labels MappedInputManager::mapLabels(const char* back, const char* confirm, const char* previous,
const char* next) const { const char* next) const {
@ -89,4 +91,4 @@ MappedInputManager::Labels MappedInputManager::mapLabels(const char* back, const
default: default:
return {back, confirm, previous, next}; return {back, confirm, previous, next};
} }
} }

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include <HalGPIO.h> #include <InputManager.h>
class MappedInputManager { class MappedInputManager {
public: public:
@ -13,7 +13,7 @@ class MappedInputManager {
const char* btn4; const char* btn4;
}; };
explicit MappedInputManager(HalGPIO& gpio) : gpio(gpio) {} explicit MappedInputManager(InputManager& inputManager) : inputManager(inputManager) {}
bool wasPressed(Button button) const; bool wasPressed(Button button) const;
bool wasReleased(Button button) const; bool wasReleased(Button button) const;
@ -24,7 +24,7 @@ class MappedInputManager {
Labels mapLabels(const char* back, const char* confirm, const char* previous, const char* next) const; Labels mapLabels(const char* back, const char* confirm, const char* previous, const char* next) const;
private: private:
HalGPIO& gpio; InputManager& inputManager;
bool mapButton(Button button, bool (HalGPIO::*fn)(uint8_t) const) const; bool mapButton(Button button, bool (InputManager::*fn)(uint8_t) const) const;
}; };

View File

@ -7,23 +7,22 @@
#include <algorithm> #include <algorithm>
namespace { namespace {
constexpr uint8_t RECENT_BOOKS_FILE_VERSION = 2; constexpr uint8_t RECENT_BOOKS_FILE_VERSION = 1;
constexpr char RECENT_BOOKS_FILE[] = "/.crosspoint/recent.bin"; constexpr char RECENT_BOOKS_FILE[] = "/.crosspoint/recent.bin";
constexpr int MAX_RECENT_BOOKS = 10; constexpr int MAX_RECENT_BOOKS = 10;
} // namespace } // namespace
RecentBooksStore RecentBooksStore::instance; RecentBooksStore RecentBooksStore::instance;
void RecentBooksStore::addBook(const std::string& path, const std::string& title, const std::string& author) { void RecentBooksStore::addBook(const std::string& path) {
// Remove existing entry if present // Remove existing entry if present
auto it = auto it = std::find(recentBooks.begin(), recentBooks.end(), path);
std::find_if(recentBooks.begin(), recentBooks.end(), [&](const RecentBook& book) { return book.path == path; });
if (it != recentBooks.end()) { if (it != recentBooks.end()) {
recentBooks.erase(it); recentBooks.erase(it);
} }
// Add to front // Add to front
recentBooks.insert(recentBooks.begin(), {path, title, author}); recentBooks.insert(recentBooks.begin(), path);
// Trim to max size // Trim to max size
if (recentBooks.size() > MAX_RECENT_BOOKS) { if (recentBooks.size() > MAX_RECENT_BOOKS) {
@ -47,9 +46,7 @@ bool RecentBooksStore::saveToFile() const {
serialization::writePod(outputFile, count); serialization::writePod(outputFile, count);
for (const auto& book : recentBooks) { for (const auto& book : recentBooks) {
serialization::writeString(outputFile, book.path); serialization::writeString(outputFile, book);
serialization::writeString(outputFile, book.title);
serialization::writeString(outputFile, book.author);
} }
outputFile.close(); outputFile.close();
@ -66,41 +63,24 @@ bool RecentBooksStore::loadFromFile() {
uint8_t version; uint8_t version;
serialization::readPod(inputFile, version); serialization::readPod(inputFile, version);
if (version != RECENT_BOOKS_FILE_VERSION) { if (version != RECENT_BOOKS_FILE_VERSION) {
if (version == 1) { Serial.printf("[%lu] [RBS] Deserialization failed: Unknown version %u\n", millis(), version);
// Old version, just read paths inputFile.close();
uint8_t count; return false;
serialization::readPod(inputFile, count); }
recentBooks.clear();
recentBooks.reserve(count);
for (uint8_t i = 0; i < count; i++) {
std::string path;
serialization::readString(inputFile, path);
// Title and author will be empty, they will be filled when the book is
// opened again
recentBooks.push_back({path, "", ""});
}
} else {
Serial.printf("[%lu] [RBS] Deserialization failed: Unknown version %u\n", millis(), version);
inputFile.close();
return false;
}
} else {
uint8_t count;
serialization::readPod(inputFile, count);
recentBooks.clear(); uint8_t count;
recentBooks.reserve(count); serialization::readPod(inputFile, count);
for (uint8_t i = 0; i < count; i++) { recentBooks.clear();
std::string path, title, author; recentBooks.reserve(count);
serialization::readString(inputFile, path);
serialization::readString(inputFile, title); for (uint8_t i = 0; i < count; i++) {
serialization::readString(inputFile, author); std::string path;
recentBooks.push_back({path, title, author}); serialization::readString(inputFile, path);
} recentBooks.push_back(path);
} }
inputFile.close(); inputFile.close();
Serial.printf("[%lu] [RBS] Recent books loaded from file (%d entries)\n", millis(), recentBooks.size()); Serial.printf("[%lu] [RBS] Recent books loaded from file (%d entries)\n", millis(), count);
return true; return true;
} }

View File

@ -2,19 +2,11 @@
#include <string> #include <string>
#include <vector> #include <vector>
struct RecentBook {
std::string path;
std::string title;
std::string author;
bool operator==(const RecentBook& other) const { return path == other.path; }
};
class RecentBooksStore { class RecentBooksStore {
// Static instance // Static instance
static RecentBooksStore instance; static RecentBooksStore instance;
std::vector<RecentBook> recentBooks; std::vector<std::string> recentBooks;
public: public:
~RecentBooksStore() = default; ~RecentBooksStore() = default;
@ -22,11 +14,11 @@ class RecentBooksStore {
// Get singleton instance // Get singleton instance
static RecentBooksStore& getInstance() { return instance; } static RecentBooksStore& getInstance() { return instance; }
// Add a book to the recent list (moves to front if already exists) // Add a book path to the recent list (moves to front if already exists)
void addBook(const std::string& path, const std::string& title, const std::string& author); void addBook(const std::string& path);
// Get the list of recent books (most recent first) // Get the list of recent book paths (most recent first)
const std::vector<RecentBook>& getBooks() const { return recentBooks; } const std::vector<std::string>& getBooks() const { return recentBooks; }
// Get the count of recent books // Get the count of recent books
int getCount() const { return static_cast<int>(recentBooks.size()); } int getCount() const { return static_cast<int>(recentBooks.size()); }

View File

@ -133,7 +133,7 @@ void SleepActivity::renderDefaultSleepScreen() const {
renderer.invertScreen(); renderer.invertScreen();
} }
renderer.displayBuffer(HalDisplay::HALF_REFRESH); renderer.displayBuffer(EInkDisplay::HALF_REFRESH);
} }
void SleepActivity::renderBitmapSleepScreen(const Bitmap& bitmap) const { void SleepActivity::renderBitmapSleepScreen(const Bitmap& bitmap) const {
@ -189,7 +189,7 @@ void SleepActivity::renderBitmapSleepScreen(const Bitmap& bitmap) const {
renderer.invertScreen(); renderer.invertScreen();
} }
renderer.displayBuffer(HalDisplay::HALF_REFRESH); renderer.displayBuffer(EInkDisplay::HALF_REFRESH);
if (hasGreyscale) { if (hasGreyscale) {
bitmap.rewindToData(); bitmap.rewindToData();
@ -280,5 +280,5 @@ void SleepActivity::renderCoverSleepScreen() const {
void SleepActivity::renderBlankSleepScreen() const { void SleepActivity::renderBlankSleepScreen() const {
renderer.clearScreen(); renderer.clearScreen();
renderer.displayBuffer(HalDisplay::HALF_REFRESH); renderer.displayBuffer(EInkDisplay::HALF_REFRESH);
} }

View File

@ -16,7 +16,6 @@ namespace {
constexpr int TAB_BAR_Y = 15; constexpr int TAB_BAR_Y = 15;
constexpr int CONTENT_START_Y = 60; constexpr int CONTENT_START_Y = 60;
constexpr int LINE_HEIGHT = 30; constexpr int LINE_HEIGHT = 30;
constexpr int RECENTS_LINE_HEIGHT = 65; // Increased for two-line items
constexpr int LEFT_MARGIN = 20; constexpr int LEFT_MARGIN = 20;
constexpr int RIGHT_MARGIN = 40; // Extra space for scroll indicator constexpr int RIGHT_MARGIN = 40; // Extra space for scroll indicator
@ -48,7 +47,7 @@ int MyLibraryActivity::getPageItems() const {
int MyLibraryActivity::getCurrentItemCount() const { int MyLibraryActivity::getCurrentItemCount() const {
if (currentTab == Tab::Recent) { if (currentTab == Tab::Recent) {
return static_cast<int>(recentBooks.size()); return static_cast<int>(bookTitles.size());
} }
return static_cast<int>(files.size()); return static_cast<int>(files.size());
} }
@ -66,16 +65,34 @@ int MyLibraryActivity::getCurrentPage() const {
} }
void MyLibraryActivity::loadRecentBooks() { void MyLibraryActivity::loadRecentBooks() {
recentBooks.clear(); constexpr size_t MAX_RECENT_BOOKS = 20;
const auto& books = RECENT_BOOKS.getBooks();
recentBooks.reserve(books.size()); bookTitles.clear();
bookPaths.clear();
const auto& books = RECENT_BOOKS.getBooks();
bookTitles.reserve(std::min(books.size(), MAX_RECENT_BOOKS));
bookPaths.reserve(std::min(books.size(), MAX_RECENT_BOOKS));
for (const auto& path : books) {
// Limit to maximum number of recent books
if (bookTitles.size() >= MAX_RECENT_BOOKS) {
break;
}
for (const auto& book : books) {
// Skip if file no longer exists // Skip if file no longer exists
if (!SdMan.exists(book.path.c_str())) { if (!SdMan.exists(path.c_str())) {
continue; continue;
} }
recentBooks.push_back(book);
// Extract filename from path for display
std::string title = path;
const size_t lastSlash = title.find_last_of('/');
if (lastSlash != std::string::npos) {
title = title.substr(lastSlash + 1);
}
bookTitles.push_back(title);
bookPaths.push_back(path);
} }
} }
@ -159,6 +176,8 @@ void MyLibraryActivity::onExit() {
vSemaphoreDelete(renderingMutex); vSemaphoreDelete(renderingMutex);
renderingMutex = nullptr; renderingMutex = nullptr;
bookTitles.clear();
bookPaths.clear();
files.clear(); files.clear();
} }
@ -188,8 +207,8 @@ void MyLibraryActivity::loop() {
// Confirm button - open selected item // Confirm button - open selected item
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) { if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
if (currentTab == Tab::Recent) { if (currentTab == Tab::Recent) {
if (!recentBooks.empty() && selectorIndex < static_cast<int>(recentBooks.size())) { if (!bookPaths.empty() && selectorIndex < static_cast<int>(bookPaths.size())) {
onSelectBook(recentBooks[selectorIndex].path, currentTab); onSelectBook(bookPaths[selectorIndex], currentTab);
} }
} else { } else {
// Files tab // Files tab
@ -314,7 +333,7 @@ void MyLibraryActivity::render() const {
void MyLibraryActivity::renderRecentTab() const { void MyLibraryActivity::renderRecentTab() const {
const auto pageWidth = renderer.getScreenWidth(); const auto pageWidth = renderer.getScreenWidth();
const int pageItems = getPageItems(); const int pageItems = getPageItems();
const int bookCount = static_cast<int>(recentBooks.size()); const int bookCount = static_cast<int>(bookTitles.size());
if (bookCount == 0) { if (bookCount == 0) {
renderer.drawText(UI_10_FONT_ID, LEFT_MARGIN, CONTENT_START_Y, "No recent books"); renderer.drawText(UI_10_FONT_ID, LEFT_MARGIN, CONTENT_START_Y, "No recent books");
@ -324,37 +343,14 @@ void MyLibraryActivity::renderRecentTab() const {
const auto pageStartIndex = selectorIndex / pageItems * pageItems; const auto pageStartIndex = selectorIndex / pageItems * pageItems;
// Draw selection highlight // Draw selection highlight
renderer.fillRect(0, CONTENT_START_Y + (selectorIndex % pageItems) * RECENTS_LINE_HEIGHT - 2, renderer.fillRect(0, CONTENT_START_Y + (selectorIndex % pageItems) * LINE_HEIGHT - 2, pageWidth - RIGHT_MARGIN,
pageWidth - RIGHT_MARGIN, RECENTS_LINE_HEIGHT); LINE_HEIGHT);
// Draw items // Draw items
for (int i = pageStartIndex; i < bookCount && i < pageStartIndex + pageItems; i++) { for (int i = pageStartIndex; i < bookCount && i < pageStartIndex + pageItems; i++) {
const auto& book = recentBooks[i]; auto item = renderer.truncatedText(UI_10_FONT_ID, bookTitles[i].c_str(), pageWidth - LEFT_MARGIN - RIGHT_MARGIN);
const int y = CONTENT_START_Y + (i % pageItems) * RECENTS_LINE_HEIGHT; renderer.drawText(UI_10_FONT_ID, LEFT_MARGIN, CONTENT_START_Y + (i % pageItems) * LINE_HEIGHT, item.c_str(),
i != selectorIndex);
// Line 1: Title
std::string title = book.title;
if (title.empty()) {
// Fallback for older entries or files without metadata
title = book.path;
const size_t lastSlash = title.find_last_of('/');
if (lastSlash != std::string::npos) {
title = title.substr(lastSlash + 1);
}
const size_t dot = title.find_last_of('.');
if (dot != std::string::npos) {
title.resize(dot);
}
}
auto truncatedTitle = renderer.truncatedText(UI_12_FONT_ID, title.c_str(), pageWidth - LEFT_MARGIN - RIGHT_MARGIN);
renderer.drawText(UI_12_FONT_ID, LEFT_MARGIN, y + 2, truncatedTitle.c_str(), i != selectorIndex);
// Line 2: Author
if (!book.author.empty()) {
auto truncatedAuthor =
renderer.truncatedText(UI_10_FONT_ID, book.author.c_str(), pageWidth - LEFT_MARGIN - RIGHT_MARGIN);
renderer.drawText(UI_10_FONT_ID, LEFT_MARGIN, y + 32, truncatedAuthor.c_str(), i != selectorIndex);
}
} }
} }

View File

@ -8,7 +8,6 @@
#include <vector> #include <vector>
#include "../Activity.h" #include "../Activity.h"
#include "RecentBooksStore.h"
class MyLibraryActivity final : public Activity { class MyLibraryActivity final : public Activity {
public: public:
@ -23,7 +22,8 @@ class MyLibraryActivity final : public Activity {
bool updateRequired = false; bool updateRequired = false;
// Recent tab state // Recent tab state
std::vector<RecentBook> recentBooks; std::vector<std::string> bookTitles; // Display titles for each book
std::vector<std::string> bookPaths; // Paths for each visible book (excludes missing)
// Files tab state (from FileSelectionActivity) // Files tab state (from FileSelectionActivity)
std::string basepath = "/"; std::string basepath = "/";

View File

@ -7,7 +7,9 @@
#include "CrossPointSettings.h" #include "CrossPointSettings.h"
#include "CrossPointState.h" #include "CrossPointState.h"
#include "EpubReaderTocActivity.h" #include "EpubReaderChapterSelectionActivity.h"
#include "EpubReaderFootnotesActivity.h"
#include "EpubReaderMenuActivity.h"
#include "MappedInputManager.h" #include "MappedInputManager.h"
#include "RecentBooksStore.h" #include "RecentBooksStore.h"
#include "ScreenComponents.h" #include "ScreenComponents.h"
@ -84,7 +86,7 @@ void EpubReaderActivity::onEnter() {
// Save current epub as last opened epub and add to recent books // Save current epub as last opened epub and add to recent books
APP_STATE.openEpubPath = epub->getPath(); APP_STATE.openEpubPath = epub->getPath();
APP_STATE.saveToFile(); APP_STATE.saveToFile();
RECENT_BOOKS.addBook(epub->getPath(), epub->getTitle(), epub->getAuthor()); RECENT_BOOKS.addBook(epub->getPath());
// Trigger first update // Trigger first update
updateRequired = true; updateRequired = true;
@ -129,41 +131,65 @@ void EpubReaderActivity::loop() {
const int currentPage = section ? section->currentPage : 0; const int currentPage = section ? section->currentPage : 0;
const int totalPages = section ? section->pageCount : 0; const int totalPages = section ? section->pageCount : 0;
// Show consolidated TOC activity (Chapters and Footnotes) // Show menu instead of direct chapter selection, to allow access to footnotes
exitActivity(); exitActivity();
enterNewActivity(new EpubReaderTocActivity( enterNewActivity(new EpubReaderMenuActivity(
this->renderer, this->mappedInput, epub, epub->getPath(), currentSpineIndex, currentPage, totalPages, this->renderer, this->mappedInput,
currentPageFootnotes,
[this] { [this] {
// onGoBack // onGoBack from menu
exitActivity();
updateRequired = true; updateRequired = true;
// Re-enter reader activity logic if needed (handled by stack)
// Actually ActivityWithSubactivity handles subActivity exit naturally
exitActivity();
}, },
[this](int newSpineIndex) { [this, currentPage, totalPages](EpubReaderMenuActivity::MenuOption option) {
// onSelectSpineIndex // onSelectOption - handle menu choice
if (currentSpineIndex != newSpineIndex) { if (option == EpubReaderMenuActivity::CHAPTERS) {
currentSpineIndex = newSpineIndex; // Show chapter selection
nextPageNumber = 0; exitActivity();
section.reset(); enterNewActivity(new EpubReaderChapterSelectionActivity(
this->renderer, this->mappedInput, epub, epub->getPath(), currentSpineIndex, currentPage, totalPages,
[this] {
exitActivity();
updateRequired = true;
},
[this](int newSpineIndex) {
if (currentSpineIndex != newSpineIndex) {
currentSpineIndex = newSpineIndex;
nextPageNumber = 0;
section.reset();
}
exitActivity();
updateRequired = true;
},
[this](int newSpineIndex, int newPage) {
// Handle sync position
if (currentSpineIndex != newSpineIndex || (section && section->currentPage != newPage)) {
currentSpineIndex = newSpineIndex;
nextPageNumber = newPage;
section.reset();
}
exitActivity();
updateRequired = true;
}));
} else if (option == EpubReaderMenuActivity::FOOTNOTES) {
// Show footnotes page with current page notes
exitActivity();
enterNewActivity(new EpubReaderFootnotesActivity(
this->renderer, this->mappedInput,
currentPageFootnotes, // Pass collected footnotes (reference)
[this] {
// onGoBack from footnotes
exitActivity();
updateRequired = true;
},
[this](const char* href) {
// onSelectFootnote - navigate to the footnote location
navigateToHref(href, true); // true = save current position
exitActivity();
updateRequired = true;
}));
} }
exitActivity();
updateRequired = true;
},
[this](const char* href) {
// onSelectFootnote
navigateToHref(href, true);
exitActivity();
updateRequired = true;
},
[this](int newSpineIndex, int newPage) {
// onSyncPosition
if (currentSpineIndex != newSpineIndex || (section && section->currentPage != newPage)) {
currentSpineIndex = newSpineIndex;
nextPageNumber = newPage;
section.reset();
}
exitActivity();
updateRequired = true;
})); }));
xSemaphoreGive(renderingMutex); xSemaphoreGive(renderingMutex);
} }
@ -360,7 +386,7 @@ void EpubReaderActivity::renderScreen() {
auto progressCallback = [this, barX, barY, barWidth, barHeight](int progress) { auto progressCallback = [this, barX, barY, barWidth, barHeight](int progress) {
const int fillWidth = (barWidth - 2) * progress / 100; const int fillWidth = (barWidth - 2) * progress / 100;
renderer.fillRect(barX + 1, barY + 1, fillWidth, barHeight - 2, true); renderer.fillRect(barX + 1, barY + 1, fillWidth, barHeight - 2, true);
renderer.displayBuffer(HalDisplay::FAST_REFRESH); renderer.displayBuffer(EInkDisplay::FAST_REFRESH);
}; };
if (!section->createSectionFile(SETTINGS.getReaderFontId(), SETTINGS.getReaderLineCompression(), if (!section->createSectionFile(SETTINGS.getReaderFontId(), SETTINGS.getReaderLineCompression(),
@ -459,7 +485,7 @@ void EpubReaderActivity::renderContents(std::unique_ptr<Page> page, const int or
page->render(renderer, SETTINGS.getReaderFontId(), orientedMarginLeft, orientedMarginTop); page->render(renderer, SETTINGS.getReaderFontId(), orientedMarginLeft, orientedMarginTop);
renderStatusBar(orientedMarginRight, orientedMarginBottom, orientedMarginLeft); renderStatusBar(orientedMarginRight, orientedMarginBottom, orientedMarginLeft);
if (pagesUntilFullRefresh <= 1) { if (pagesUntilFullRefresh <= 1) {
renderer.displayBuffer(HalDisplay::HALF_REFRESH); renderer.displayBuffer(EInkDisplay::HALF_REFRESH);
pagesUntilFullRefresh = SETTINGS.getRefreshFrequency(); pagesUntilFullRefresh = SETTINGS.getRefreshFrequency();
} else { } else {
renderer.displayBuffer(); renderer.displayBuffer();

View File

@ -5,7 +5,7 @@
#include <freertos/semphr.h> #include <freertos/semphr.h>
#include <freertos/task.h> #include <freertos/task.h>
#include "FootnotesData.h" #include "EpubReaderFootnotesActivity.h"
#include "activities/ActivityWithSubactivity.h" #include "activities/ActivityWithSubactivity.h"
class EpubReaderActivity final : public ActivityWithSubactivity { class EpubReaderActivity final : public ActivityWithSubactivity {

View File

@ -0,0 +1,265 @@
#include "EpubReaderChapterSelectionActivity.h"
#include <GfxRenderer.h>
#include "KOReaderCredentialStore.h"
#include "KOReaderSyncActivity.h"
#include "MappedInputManager.h"
#include "fontIds.h"
namespace {
// Time threshold for treating a long press as a page-up/page-down
constexpr int SKIP_PAGE_MS = 700;
} // namespace
bool EpubReaderChapterSelectionActivity::hasSyncOption() const { return KOREADER_STORE.hasCredentials(); }
int EpubReaderChapterSelectionActivity::getTotalItems() const {
// Add 2 for sync options (top and bottom) if credentials are configured
const int syncCount = hasSyncOption() ? 2 : 0;
return epub->getTocItemsCount() + syncCount;
}
bool EpubReaderChapterSelectionActivity::isSyncItem(int index) const {
if (!hasSyncOption()) return false;
// First item and last item are sync options
return index == 0 || index == getTotalItems() - 1;
}
int EpubReaderChapterSelectionActivity::tocIndexFromItemIndex(int itemIndex) const {
// Account for the sync option at the top
const int offset = hasSyncOption() ? 1 : 0;
return itemIndex - offset;
}
int EpubReaderChapterSelectionActivity::getPageItems() const {
// Layout constants used in renderScreen
constexpr int startY = 60;
constexpr int lineHeight = 30;
const int screenHeight = renderer.getScreenHeight();
const int endY = screenHeight - lineHeight;
const int availableHeight = endY - startY;
int items = availableHeight / lineHeight;
// Ensure we always have at least one item per page to avoid division by zero
if (items < 1) {
items = 1;
}
return items;
}
void EpubReaderChapterSelectionActivity::taskTrampoline(void* param) {
auto* self = static_cast<EpubReaderChapterSelectionActivity*>(param);
self->displayTaskLoop();
}
void EpubReaderChapterSelectionActivity::buildFilteredChapterList() {
filteredSpineIndices.clear();
for (int i = 0; i < epub->getSpineItemsCount(); i++) {
// Skip footnote pages
if (epub->shouldHideFromToc(i)) {
Serial.printf("[%lu] [CHAP] Hiding footnote page at spine index: %d\n", millis(), i);
continue;
}
// Skip pages without TOC entry (unnamed pages)
int tocIndex = epub->getTocIndexForSpineIndex(i);
if (tocIndex == -1) {
Serial.printf("[%lu] [CHAP] Hiding unnamed page at spine index: %d\n", millis(), i);
continue;
}
filteredSpineIndices.push_back(i);
}
Serial.printf("[%lu] [CHAP] Filtered chapters: %d out of %d\n", millis(), filteredSpineIndices.size(),
epub->getSpineItemsCount());
}
void EpubReaderChapterSelectionActivity::onEnter() {
ActivityWithSubactivity::onEnter();
if (!epub) {
return;
}
renderingMutex = xSemaphoreCreateMutex();
// Build filtered chapter list (excluding footnote pages)
buildFilteredChapterList();
// Find the index in filtered list that corresponds to currentSpineIndex
selectorIndex = 0;
for (size_t i = 0; i < filteredSpineIndices.size(); i++) {
if (filteredSpineIndices[i] == currentSpineIndex) {
selectorIndex = i;
break;
}
}
// Account for sync option offset when finding current TOC index (if applicable)
// For simplicity, if we are using the filtered list, we might just put "Sync" at the top of THAT list?
// But wait, the filtered list is spine indices.
// The master logic used TOC indices directly.
// Let's adapt: We will display the filtered list.
// If sync is enabled, we prepend/append it to the selector range.
if (hasSyncOption()) {
selectorIndex += 1; // Offset for top sync option
}
// Trigger first update
updateRequired = true;
xTaskCreate(&EpubReaderChapterSelectionActivity::taskTrampoline, "EpubReaderChapterSelectionActivityTask",
4096, // Stack size
this, // Parameters
1, // Priority
&displayTaskHandle // Task handle
);
}
void EpubReaderChapterSelectionActivity::onExit() {
ActivityWithSubactivity::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;
}
void EpubReaderChapterSelectionActivity::launchSyncActivity() {
xSemaphoreTake(renderingMutex, portMAX_DELAY);
exitActivity();
enterNewActivity(new KOReaderSyncActivity(
renderer, mappedInput, epub, epubPath, currentSpineIndex, currentPage, totalPagesInSpine,
[this]() {
// On cancel
exitActivity();
updateRequired = true;
},
[this](int newSpineIndex, int newPage) {
// On sync complete
exitActivity();
onSyncPosition(newSpineIndex, newPage);
}));
xSemaphoreGive(renderingMutex);
}
void EpubReaderChapterSelectionActivity::loop() {
if (subActivity) {
subActivity->loop();
return;
}
const bool prevReleased = mappedInput.wasReleased(MappedInputManager::Button::Up) ||
mappedInput.wasReleased(MappedInputManager::Button::Left);
const bool nextReleased = mappedInput.wasReleased(MappedInputManager::Button::Down) ||
mappedInput.wasReleased(MappedInputManager::Button::Right);
const bool skipPage = mappedInput.getHeldTime() > SKIP_PAGE_MS;
const int pageItems = getPageItems();
// Total items = filtered chapters + sync options
const int syncCount = hasSyncOption() ? 2 : 0;
const int totalItems = filteredSpineIndices.size() + syncCount;
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
// Check if sync option is selected
if (hasSyncOption()) {
if (selectorIndex == 0 || selectorIndex == totalItems - 1) {
launchSyncActivity();
return;
}
}
// It's a chapter. Calculate index in filtered list.
int filteredIndex = selectorIndex;
if (hasSyncOption()) filteredIndex -= 1; // Remove top sync offset
if (filteredIndex >= 0 && filteredIndex < filteredSpineIndices.size()) {
onSelectSpineIndex(filteredSpineIndices[filteredIndex]);
}
} else if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
onGoBack();
} else if (prevReleased) {
if (skipPage) {
selectorIndex = ((selectorIndex / pageItems - 1) * pageItems + totalItems) % totalItems;
} else {
selectorIndex = (selectorIndex + totalItems - 1) % totalItems;
}
updateRequired = true;
} else if (nextReleased) {
if (skipPage) {
selectorIndex = ((selectorIndex / pageItems + 1) * pageItems) % totalItems;
} else {
selectorIndex = (selectorIndex + 1) % totalItems;
}
updateRequired = true;
}
}
void EpubReaderChapterSelectionActivity::displayTaskLoop() {
while (true) {
if (updateRequired && !subActivity) {
updateRequired = false;
xSemaphoreTake(renderingMutex, portMAX_DELAY);
renderScreen();
xSemaphoreGive(renderingMutex);
}
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
void EpubReaderChapterSelectionActivity::renderScreen() {
renderer.clearScreen();
const auto pageWidth = renderer.getScreenWidth();
const int pageItems = getPageItems();
const int totalItems = getTotalItems();
const std::string title =
renderer.truncatedText(UI_12_FONT_ID, epub->getTitle().c_str(), pageWidth - 40, EpdFontFamily::BOLD);
renderer.drawCenteredText(UI_12_FONT_ID, 15, title.c_str(), true, EpdFontFamily::BOLD);
const auto pageStartIndex = selectorIndex / pageItems * pageItems;
renderer.fillRect(0, 60 + (selectorIndex % pageItems) * 30 - 2, pageWidth - 1, 30);
for (int i = 0; i < pageItems; i++) {
int itemIndex = pageStartIndex + i;
if (itemIndex >= totalItems) break;
const int displayY = 60 + i * 30;
const bool isSelected = (itemIndex == selectorIndex);
if (isSyncItem(itemIndex)) {
renderer.drawText(UI_10_FONT_ID, 20, displayY, ">> Sync Progress", !isSelected);
} else {
const int tocIndex = tocIndexFromItemIndex(itemIndex);
if (tocIndex == -1) {
renderer.drawText(UI_10_FONT_ID, 20, displayY, "Unnamed", !isSelected);
} else {
// Master's rendering logic
auto item = epub->getTocItem(tocIndex);
const int indentSize = 20 + (item.level - 1) * 15;
const std::string chapterName =
renderer.truncatedText(UI_10_FONT_ID, item.title.c_str(), pageWidth - 40 - indentSize);
renderer.drawText(UI_10_FONT_ID, indentSize, displayY, chapterName.c_str(), !isSelected);
}
}
}
const auto labels = mappedInput.mapLabels("« Back", "Select", "Up", "Down");
renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
renderer.displayBuffer();
}

View File

@ -0,0 +1,70 @@
#pragma once
#include <Epub.h>
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
#include <freertos/task.h>
#include <memory>
#include <vector>
#include "../ActivityWithSubactivity.h"
class EpubReaderChapterSelectionActivity final : public ActivityWithSubactivity {
std::shared_ptr<Epub> epub;
std::string epubPath;
TaskHandle_t displayTaskHandle = nullptr;
SemaphoreHandle_t renderingMutex = nullptr;
int currentSpineIndex = 0;
int currentPage = 0;
int totalPagesInSpine = 0;
int selectorIndex = 0;
bool updateRequired = false;
const std::function<void()> onGoBack;
const std::function<void(int newSpineIndex)> onSelectSpineIndex;
const std::function<void(int newSpineIndex, int newPage)> onSyncPosition;
// Number of items that fit on a page, derived from logical screen height.
// This adapts automatically when switching between portrait and landscape.
int getPageItems() const;
// Total items including sync options (top and bottom)
int getTotalItems() const;
// Check if sync option is available (credentials configured)
bool hasSyncOption() const;
// Check if given item index is a sync option (first or last)
bool isSyncItem(int index) const;
// Convert item index to TOC index (accounting for top sync option offset)
int tocIndexFromItemIndex(int itemIndex) const;
// Filtered list of spine indices (excluding footnote pages)
std::vector<int> filteredSpineIndices;
static void taskTrampoline(void* param);
[[noreturn]] void displayTaskLoop();
void renderScreen();
void buildFilteredChapterList();
void launchSyncActivity();
public:
explicit EpubReaderChapterSelectionActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
const std::shared_ptr<Epub>& epub, const std::string& epubPath,
const int currentSpineIndex, const int currentPage,
const int totalPagesInSpine, const std::function<void()>& onGoBack,
const std::function<void(int newSpineIndex)>& onSelectSpineIndex,
const std::function<void(int newSpineIndex, int newPage)>& onSyncPosition)
: ActivityWithSubactivity("EpubReaderChapterSelection", renderer, mappedInput),
epub(epub),
epubPath(epubPath),
currentSpineIndex(currentSpineIndex),
currentPage(currentPage),
totalPagesInSpine(totalPagesInSpine),
onGoBack(onGoBack),
onSelectSpineIndex(onSelectSpineIndex),
onSyncPosition(onSyncPosition) {}
void onEnter() override;
void onExit() override;
void loop() override;
};

View File

@ -0,0 +1,91 @@
#include "EpubReaderFootnotesActivity.h"
#include <EpdFontFamily.h>
#include <GfxRenderer.h>
#include "MappedInputManager.h"
#include "fontIds.h"
void EpubReaderFootnotesActivity::onEnter() {
selectedIndex = 0;
render();
}
void EpubReaderFootnotesActivity::onExit() {
// Nothing to clean up
}
void EpubReaderFootnotesActivity::loop() {
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
onGoBack();
return;
}
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
const FootnoteEntry* entry = footnotes.getEntry(selectedIndex);
if (entry) {
Serial.printf("[%lu] [FNS] Selected footnote: %s -> %s\n", millis(), entry->number, entry->href);
onSelectFootnote(entry->href);
}
return;
}
bool needsRedraw = false;
if (mappedInput.wasPressed(MappedInputManager::Button::Up)) {
if (selectedIndex > 0) {
selectedIndex--;
needsRedraw = true;
}
}
if (mappedInput.wasPressed(MappedInputManager::Button::Down)) {
if (selectedIndex < footnotes.getCount() - 1) {
selectedIndex++;
needsRedraw = true;
}
}
if (needsRedraw) {
render();
}
}
void EpubReaderFootnotesActivity::render() {
renderer.clearScreen();
constexpr int startY = 50;
constexpr int lineHeight = 40;
constexpr int marginLeft = 20;
// Title
renderer.drawText(UI_12_FONT_ID, marginLeft, 20, "Footnotes", EpdFontFamily::BOLD);
if (footnotes.getCount() == 0) {
renderer.drawText(SMALL_FONT_ID, marginLeft, startY + 20, "No footnotes on this page");
renderer.displayBuffer();
return;
}
// Display footnotes
for (int i = 0; i < footnotes.getCount(); i++) {
const FootnoteEntry* entry = footnotes.getEntry(i);
if (!entry) continue;
const int y = startY + i * lineHeight;
// Draw selection indicator (arrow)
if (i == selectedIndex) {
renderer.drawText(UI_12_FONT_ID, marginLeft - 10, y, ">", EpdFontFamily::BOLD);
renderer.drawText(UI_12_FONT_ID, marginLeft + 10, y, entry->number, EpdFontFamily::BOLD);
} else {
renderer.drawText(UI_12_FONT_ID, marginLeft + 10, y, entry->number);
}
}
// Instructions at bottom
renderer.drawText(SMALL_FONT_ID, marginLeft, renderer.getScreenHeight() - 40,
"UP/DOWN: Select CONFIRM: Go to footnote BACK: Return");
renderer.displayBuffer();
}

View File

@ -0,0 +1,72 @@
#pragma once
#include <cstring>
#include <functional>
#include <memory>
#include "../../lib/Epub/Epub/FootnoteEntry.h"
#include "../Activity.h"
class FootnotesData {
private:
FootnoteEntry entries[16];
int count;
public:
FootnotesData() : count(0) {
for (int i = 0; i < 16; i++) {
entries[i].number[0] = '\0';
entries[i].href[0] = '\0';
}
}
void addFootnote(const char* number, const char* href) {
if (count < 16 && number && href) {
strncpy(entries[count].number, number, 2);
entries[count].number[2] = '\0';
strncpy(entries[count].href, href, 63);
entries[count].href[63] = '\0';
count++;
}
}
void clear() {
count = 0;
for (int i = 0; i < 16; i++) {
entries[i].number[0] = '\0';
entries[i].href[0] = '\0';
}
}
int getCount() const { return count; }
const FootnoteEntry* getEntry(int index) const {
if (index >= 0 && index < count) {
return &entries[index];
}
return nullptr;
}
};
class EpubReaderFootnotesActivity final : public Activity {
const FootnotesData& footnotes;
const std::function<void()> onGoBack;
const std::function<void(const char*)> onSelectFootnote;
int selectedIndex;
public:
EpubReaderFootnotesActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, const FootnotesData& footnotes,
const std::function<void()>& onGoBack,
const std::function<void(const char*)>& onSelectFootnote)
: Activity("EpubReaderFootnotes", renderer, mappedInput),
footnotes(footnotes),
onGoBack(onGoBack),
onSelectFootnote(onSelectFootnote),
selectedIndex(0) {}
void onEnter() override;
void onExit() override;
void loop() override;
private:
void render();
};

View File

@ -0,0 +1,96 @@
#include "EpubReaderMenuActivity.h"
#include <EpdFontFamily.h>
#include <GfxRenderer.h>
#include "MappedInputManager.h"
#include "fontIds.h"
constexpr int MENU_ITEMS_COUNT = 2;
void EpubReaderMenuActivity::taskTrampoline(void* param) {
auto* self = static_cast<EpubReaderMenuActivity*>(param);
self->displayTaskLoop();
}
void EpubReaderMenuActivity::onEnter() {
renderingMutex = xSemaphoreCreateMutex();
selectorIndex = 0;
// Trigger first update
updateRequired = true;
xTaskCreate(&EpubReaderMenuActivity::taskTrampoline, "EpubReaderMenuTask",
2048, // Stack size
this, // Parameters
1, // Priority
&displayTaskHandle // Task handle
);
}
void EpubReaderMenuActivity::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;
}
void EpubReaderMenuActivity::loop() {
const bool prevReleased = mappedInput.wasReleased(MappedInputManager::Button::Up) ||
mappedInput.wasReleased(MappedInputManager::Button::Left);
const bool nextReleased = mappedInput.wasReleased(MappedInputManager::Button::Down) ||
mappedInput.wasReleased(MappedInputManager::Button::Right);
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
onSelectOption(static_cast<MenuOption>(selectorIndex));
} else if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
onGoBack();
} else if (prevReleased) {
selectorIndex = (selectorIndex + MENU_ITEMS_COUNT - 1) % MENU_ITEMS_COUNT;
updateRequired = true;
} else if (nextReleased) {
selectorIndex = (selectorIndex + 1) % MENU_ITEMS_COUNT;
updateRequired = true;
}
}
void EpubReaderMenuActivity::displayTaskLoop() {
while (true) {
if (updateRequired) {
updateRequired = false;
xSemaphoreTake(renderingMutex, portMAX_DELAY);
renderScreen();
xSemaphoreGive(renderingMutex);
}
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
void EpubReaderMenuActivity::renderScreen() {
renderer.clearScreen();
const auto pageWidth = renderer.getScreenWidth();
renderer.drawCenteredText(UI_12_FONT_ID, 10, "Menu", true, EpdFontFamily::BOLD);
const char* menuItems[MENU_ITEMS_COUNT] = {"Go to chapter", "View footnotes"};
const int startY = 100;
const int itemHeight = 40;
for (int i = 0; i < MENU_ITEMS_COUNT; i++) {
const int y = startY + i * itemHeight;
// Draw selection indicator
if (i == selectorIndex) {
renderer.fillRect(10, y + 2, pageWidth - 20, itemHeight - 4);
renderer.drawText(UI_12_FONT_ID, 30, y, menuItems[i], false);
} else {
renderer.drawText(UI_12_FONT_ID, 30, y, menuItems[i], true);
}
}
renderer.displayBuffer();
}

View File

@ -0,0 +1,33 @@
#pragma once
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
#include <freertos/task.h>
#include "../Activity.h"
class EpubReaderMenuActivity final : public Activity {
public:
enum MenuOption { CHAPTERS, FOOTNOTES };
private:
TaskHandle_t displayTaskHandle = nullptr;
SemaphoreHandle_t renderingMutex = nullptr;
int selectorIndex = 0;
bool updateRequired = false;
const std::function<void()> onGoBack;
const std::function<void(MenuOption option)> onSelectOption;
static void taskTrampoline(void* param);
[[noreturn]] void displayTaskLoop();
void renderScreen();
public:
explicit EpubReaderMenuActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
const std::function<void()>& onGoBack,
const std::function<void(MenuOption option)>& onSelectOption)
: Activity("EpubReaderMenu", renderer, mappedInput), onGoBack(onGoBack), onSelectOption(onSelectOption) {}
void onEnter() override;
void onExit() override;
void loop() override;
};

View File

@ -1,312 +0,0 @@
#include "EpubReaderTocActivity.h"
#include <EpdFontFamily.h>
#include <GfxRenderer.h>
#include "KOReaderCredentialStore.h"
#include "KOReaderSyncActivity.h"
#include "MappedInputManager.h"
#include "ScreenComponents.h"
#include "fontIds.h"
namespace {
constexpr int TAB_BAR_Y = 15;
constexpr int CONTENT_START_Y = 60;
constexpr int CHAPTER_LINE_HEIGHT = 30;
constexpr int FOOTNOTE_LINE_HEIGHT = 40;
constexpr int SKIP_PAGE_MS = 700;
} // namespace
void EpubReaderTocActivity::taskTrampoline(void* param) {
auto* self = static_cast<EpubReaderTocActivity*>(param);
self->displayTaskLoop();
}
void EpubReaderTocActivity::onEnter() {
ActivityWithSubactivity::onEnter();
renderingMutex = xSemaphoreCreateMutex();
// Init chapters state
buildFilteredChapterList();
chaptersSelectorIndex = 0;
for (size_t i = 0; i < filteredSpineIndices.size(); i++) {
if (filteredSpineIndices[i] == currentSpineIndex) {
chaptersSelectorIndex = i;
break;
}
}
if (hasSyncOption()) {
chaptersSelectorIndex += 1;
}
// Init footnotes state
footnotesSelectedIndex = 0;
updateRequired = true;
xTaskCreate(&EpubReaderTocActivity::taskTrampoline, "EpubReaderTocTask", 4096, this, 1, &displayTaskHandle);
}
void EpubReaderTocActivity::onExit() {
ActivityWithSubactivity::onExit();
xSemaphoreTake(renderingMutex, portMAX_DELAY);
if (displayTaskHandle) {
vTaskDelete(displayTaskHandle);
displayTaskHandle = nullptr;
}
vSemaphoreDelete(renderingMutex);
renderingMutex = nullptr;
}
void EpubReaderTocActivity::launchSyncActivity() {
xSemaphoreTake(renderingMutex, portMAX_DELAY);
exitActivity();
enterNewActivity(new KOReaderSyncActivity(
renderer, mappedInput, this->epub, epubPath, currentSpineIndex, currentPage, totalPagesInSpine,
[this]() {
exitActivity();
this->updateRequired = true;
},
[this](int newSpineIndex, int newPage) {
exitActivity();
this->onSyncPosition(newSpineIndex, newPage);
}));
xSemaphoreGive(renderingMutex);
}
void EpubReaderTocActivity::loop() {
if (subActivity) {
subActivity->loop();
return;
}
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
onGoBack();
return;
}
const bool leftReleased = mappedInput.wasReleased(MappedInputManager::Button::Left);
const bool rightReleased = mappedInput.wasReleased(MappedInputManager::Button::Right);
if (leftReleased && currentTab == Tab::FOOTNOTES) {
currentTab = Tab::CHAPTERS;
updateRequired = true;
return;
}
if (rightReleased && currentTab == Tab::CHAPTERS) {
currentTab = Tab::FOOTNOTES;
updateRequired = true;
return;
}
if (currentTab == Tab::CHAPTERS) {
loopChapters();
} else {
loopFootnotes();
}
}
void EpubReaderTocActivity::loopChapters() {
const bool upReleased = mappedInput.wasReleased(MappedInputManager::Button::Up);
const bool downReleased = mappedInput.wasReleased(MappedInputManager::Button::Down);
const bool skipPage = mappedInput.getHeldTime() > SKIP_PAGE_MS;
const int totalItems = getChaptersTotalItems();
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
if (isSyncItem(chaptersSelectorIndex)) {
launchSyncActivity();
return;
}
int filteredIndex = chaptersSelectorIndex;
if (hasSyncOption() && chaptersSelectorIndex > 0) filteredIndex -= 1;
if (filteredIndex >= 0 && filteredIndex < static_cast<int>(filteredSpineIndices.size())) {
onSelectSpineIndex(filteredSpineIndices[filteredIndex]);
}
} else if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
onGoBack();
} else if (upReleased) {
if (totalItems > 0) {
if (skipPage) {
// TODO: implement page-skip navigation once page size is available
}
chaptersSelectorIndex = (chaptersSelectorIndex + totalItems - 1) % totalItems;
updateRequired = true;
}
} else if (downReleased) {
if (totalItems > 0) {
if (skipPage) {
// TODO: implement page-skip navigation once page size is available
}
chaptersSelectorIndex = (chaptersSelectorIndex + 1) % totalItems;
updateRequired = true;
}
}
}
void EpubReaderTocActivity::loopFootnotes() {
bool needsRedraw = false;
if (mappedInput.wasPressed(MappedInputManager::Button::Up)) {
if (footnotesSelectedIndex > 0) {
footnotesSelectedIndex--;
needsRedraw = true;
}
}
if (mappedInput.wasPressed(MappedInputManager::Button::Down)) {
if (footnotesSelectedIndex < footnotes.getCount() - 1) {
footnotesSelectedIndex++;
needsRedraw = true;
}
}
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
const FootnoteEntry* entry = footnotes.getEntry(footnotesSelectedIndex);
if (entry) {
onSelectFootnote(entry->href);
}
}
if (needsRedraw) {
updateRequired = true;
}
}
void EpubReaderTocActivity::displayTaskLoop() {
while (true) {
if (updateRequired && !subActivity) {
updateRequired = false;
xSemaphoreTake(renderingMutex, portMAX_DELAY);
renderScreen();
xSemaphoreGive(renderingMutex);
}
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
void EpubReaderTocActivity::renderScreen() {
renderer.clearScreen();
std::vector<TabInfo> tabs = {{"Chapters", currentTab == Tab::CHAPTERS}, {"Footnotes", currentTab == Tab::FOOTNOTES}};
ScreenComponents::drawTabBar(renderer, TAB_BAR_Y, tabs);
const int screenHeight = renderer.getScreenHeight();
const int contentHeight = screenHeight - CONTENT_START_Y - 60;
if (currentTab == Tab::CHAPTERS) {
renderChapters(CONTENT_START_Y, contentHeight);
} else {
renderFootnotes(CONTENT_START_Y, contentHeight);
}
ScreenComponents::drawScrollIndicator(renderer, getCurrentPage(), getTotalPages(), CONTENT_START_Y, contentHeight);
const auto labels = mappedInput.mapLabels("« Back", "Select", "< Tab", "Tab >");
renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
renderer.drawSideButtonHints(UI_10_FONT_ID, ">", "<");
renderer.displayBuffer();
}
void EpubReaderTocActivity::renderChapters(int contentTop, int contentHeight) {
const auto pageWidth = renderer.getScreenWidth();
const int pageItems = getChaptersPageItems(contentHeight);
const int totalItems = getChaptersTotalItems();
const auto pageStartIndex = chaptersSelectorIndex / pageItems * pageItems;
renderer.fillRect(0, contentTop + (chaptersSelectorIndex % pageItems) * CHAPTER_LINE_HEIGHT - 2, pageWidth - 1,
CHAPTER_LINE_HEIGHT);
for (int i = 0; i < pageItems; i++) {
int itemIndex = pageStartIndex + i;
if (itemIndex >= totalItems) break;
const int displayY = contentTop + i * CHAPTER_LINE_HEIGHT;
const bool isSelected = (itemIndex == chaptersSelectorIndex);
if (isSyncItem(itemIndex)) {
renderer.drawText(UI_10_FONT_ID, 20, displayY, ">> Sync Progress", !isSelected);
} else {
int filteredIndex = itemIndex;
if (hasSyncOption()) filteredIndex -= 1;
if (filteredIndex >= 0 && filteredIndex < static_cast<int>(filteredSpineIndices.size())) {
int spineIndex = filteredSpineIndices[filteredIndex];
int tocIndex = this->epub->getTocIndexForSpineIndex(spineIndex);
if (tocIndex == -1) {
renderer.drawText(UI_10_FONT_ID, 20, displayY, "Unnamed", !isSelected);
} else {
auto item = this->epub->getTocItem(tocIndex);
const int indentSize = 20 + (item.level - 1) * 15;
const std::string chapterName =
renderer.truncatedText(UI_10_FONT_ID, item.title.c_str(), pageWidth - 40 - indentSize);
renderer.drawText(UI_10_FONT_ID, indentSize, displayY, chapterName.c_str(), !isSelected);
}
}
}
}
}
void EpubReaderTocActivity::renderFootnotes(int contentTop, int contentHeight) {
const int marginLeft = 20;
if (footnotes.getCount() == 0) {
renderer.drawText(SMALL_FONT_ID, marginLeft, contentTop + 20, "No footnotes on this page");
return;
}
for (int i = 0; i < footnotes.getCount(); i++) {
const FootnoteEntry* entry = footnotes.getEntry(i);
if (!entry) continue;
const int y = contentTop + i * FOOTNOTE_LINE_HEIGHT;
if (i == footnotesSelectedIndex) {
renderer.drawText(UI_12_FONT_ID, marginLeft - 10, y, ">", EpdFontFamily::BOLD);
renderer.drawText(UI_12_FONT_ID, marginLeft + 10, y, entry->number, EpdFontFamily::BOLD);
} else {
renderer.drawText(UI_12_FONT_ID, marginLeft + 10, y, entry->number);
}
}
}
void EpubReaderTocActivity::buildFilteredChapterList() {
filteredSpineIndices.clear();
for (int i = 0; i < this->epub->getSpineItemsCount(); i++) {
if (this->epub->shouldHideFromToc(i)) continue;
int tocIndex = this->epub->getTocIndexForSpineIndex(i);
if (tocIndex == -1) continue;
filteredSpineIndices.push_back(i);
}
}
bool EpubReaderTocActivity::hasSyncOption() const { return KOREADER_STORE.hasCredentials(); }
bool EpubReaderTocActivity::isSyncItem(int index) const {
if (!hasSyncOption()) return false;
return index == 0 || index == getChaptersTotalItems() - 1;
}
int EpubReaderTocActivity::getChaptersTotalItems() const {
const int syncCount = hasSyncOption() ? 2 : 0;
return filteredSpineIndices.size() + syncCount;
}
int EpubReaderTocActivity::getChaptersPageItems(int contentHeight) const {
int items = contentHeight / CHAPTER_LINE_HEIGHT;
return (items < 1) ? 1 : items;
}
int EpubReaderTocActivity::getCurrentPage() const {
if (currentTab == Tab::CHAPTERS) {
const int availableHeight = renderer.getScreenHeight() - 120;
const int itemsPerPage = availableHeight / CHAPTER_LINE_HEIGHT;
return chaptersSelectorIndex / (itemsPerPage > 0 ? itemsPerPage : 1) + 1;
}
return 1;
}
int EpubReaderTocActivity::getTotalPages() const {
if (currentTab == Tab::CHAPTERS) {
const int availableHeight = renderer.getScreenHeight() - 120;
const int itemsPerPage = availableHeight / CHAPTER_LINE_HEIGHT;
const int totalItems = getChaptersTotalItems();
if (totalItems == 0) return 1;
return (totalItems + itemsPerPage - 1) / (itemsPerPage > 0 ? itemsPerPage : 1);
}
return 1;
}

View File

@ -1,90 +0,0 @@
#pragma once
#include <Epub.h>
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
#include <freertos/task.h>
#include <memory>
#include <vector>
#include "../ActivityWithSubactivity.h"
#include "FootnotesData.h"
class EpubReaderTocActivity final : public ActivityWithSubactivity {
public:
enum class Tab { CHAPTERS, FOOTNOTES };
private:
std::shared_ptr<Epub> epub;
std::string epubPath;
const FootnotesData& footnotes;
TaskHandle_t displayTaskHandle = nullptr;
SemaphoreHandle_t renderingMutex = nullptr;
int currentSpineIndex = 0;
int currentPage = 0;
int totalPagesInSpine = 0;
Tab currentTab = Tab::CHAPTERS;
bool updateRequired = false;
// Chapters tab state
int chaptersSelectorIndex = 0;
std::vector<int> filteredSpineIndices;
// Footnotes tab state
int footnotesSelectedIndex = 0;
// Callbacks
const std::function<void()> onGoBack;
const std::function<void(int newSpineIndex)> onSelectSpineIndex;
const std::function<void(const char* href)> onSelectFootnote;
const std::function<void(int newSpineIndex, int newPage)> onSyncPosition;
static void taskTrampoline(void* param);
[[noreturn]] void displayTaskLoop();
void renderScreen();
// Tab-specific methods
void loopChapters();
void loopFootnotes();
void renderChapters(int contentTop, int contentHeight);
void renderFootnotes(int contentTop, int contentHeight);
// Chapters helpers
void buildFilteredChapterList();
bool hasSyncOption() const;
bool isSyncItem(int index) const;
int getChaptersTotalItems() const;
int getChaptersPageItems(int contentHeight) const;
int tocIndexFromItemIndex(int itemIndex) const;
// Indicator helpers
int getCurrentPage() const;
int getTotalPages() const;
public:
EpubReaderTocActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, const std::shared_ptr<Epub>& epub_ptr,
const std::string& epubPath, int currentSpineIndex, int currentPage, int totalPagesInSpine,
const FootnotesData& footnotes, std::function<void()> onGoBack,
std::function<void(int)> onSelectSpineIndex, std::function<void(const char*)> onSelectFootnote,
std::function<void(int, int)> onSyncPosition)
: ActivityWithSubactivity("EpubReaderToc", renderer, mappedInput),
epub(epub_ptr),
epubPath(epubPath),
currentSpineIndex(currentSpineIndex),
currentPage(currentPage),
totalPagesInSpine(totalPagesInSpine),
footnotes(footnotes),
onGoBack(onGoBack),
onSelectSpineIndex(onSelectSpineIndex),
onSelectFootnote(onSelectFootnote),
onSyncPosition(onSyncPosition) {}
void onEnter() override;
void onExit() override;
void loop() override;
void launchSyncActivity();
};

View File

@ -1,45 +0,0 @@
#pragma once
#include <Epub/FootnoteEntry.h>
#include <cstring>
class FootnotesData {
private:
FootnoteEntry entries[16];
int count;
public:
FootnotesData() : count(0) {
for (int i = 0; i < 16; i++) {
entries[i].number[0] = '\0';
entries[i].href[0] = '\0';
}
}
void addFootnote(const char* number, const char* href) {
if (count < 16 && number && href) {
strncpy(entries[count].number, number, 2);
entries[count].number[2] = '\0';
strncpy(entries[count].href, href, 63);
entries[count].href[63] = '\0';
count++;
}
}
void clear() {
count = 0;
for (int i = 0; i < 16; i++) {
entries[i].number[0] = '\0';
entries[i].href[0] = '\0';
}
}
int getCount() const { return count; }
const FootnoteEntry* getEntry(int index) const {
if (index >= 0 && index < count) {
return &entries[index];
}
return nullptr;
}
};

View File

@ -60,7 +60,7 @@ void TxtReaderActivity::onEnter() {
// Save current txt as last opened file and add to recent books // Save current txt as last opened file and add to recent books
APP_STATE.openEpubPath = txt->getPath(); APP_STATE.openEpubPath = txt->getPath();
APP_STATE.saveToFile(); APP_STATE.saveToFile();
RECENT_BOOKS.addBook(txt->getPath(), "", ""); RECENT_BOOKS.addBook(txt->getPath());
// Trigger first update // Trigger first update
updateRequired = true; updateRequired = true;
@ -256,7 +256,7 @@ void TxtReaderActivity::buildPageIndex() {
// Fill progress bar // Fill progress bar
const int fillWidth = (barWidth - 2) * progressPercent / 100; const int fillWidth = (barWidth - 2) * progressPercent / 100;
renderer.fillRect(barX + 1, barY + 1, fillWidth, barHeight - 2, true); renderer.fillRect(barX + 1, barY + 1, fillWidth, barHeight - 2, true);
renderer.displayBuffer(HalDisplay::FAST_REFRESH); renderer.displayBuffer(EInkDisplay::FAST_REFRESH);
} }
// Yield to other tasks periodically // Yield to other tasks periodically
@ -484,7 +484,7 @@ void TxtReaderActivity::renderPage() {
renderStatusBar(orientedMarginRight, orientedMarginBottom, orientedMarginLeft); renderStatusBar(orientedMarginRight, orientedMarginBottom, orientedMarginLeft);
if (pagesUntilFullRefresh <= 1) { if (pagesUntilFullRefresh <= 1) {
renderer.displayBuffer(HalDisplay::HALF_REFRESH); renderer.displayBuffer(EInkDisplay::HALF_REFRESH);
pagesUntilFullRefresh = SETTINGS.getRefreshFrequency(); pagesUntilFullRefresh = SETTINGS.getRefreshFrequency();
} else { } else {
renderer.displayBuffer(); renderer.displayBuffer();

View File

@ -45,7 +45,7 @@ void XtcReaderActivity::onEnter() {
// Save current XTC as last opened book and add to recent books // Save current XTC as last opened book and add to recent books
APP_STATE.openEpubPath = xtc->getPath(); APP_STATE.openEpubPath = xtc->getPath();
APP_STATE.saveToFile(); APP_STATE.saveToFile();
RECENT_BOOKS.addBook(xtc->getPath(), xtc->getTitle(), xtc->getAuthor()); RECENT_BOOKS.addBook(xtc->getPath());
// Trigger first update // Trigger first update
updateRequired = true; updateRequired = true;
@ -276,7 +276,7 @@ void XtcReaderActivity::renderPage() {
// Display BW with conditional refresh based on pagesUntilFullRefresh // Display BW with conditional refresh based on pagesUntilFullRefresh
if (pagesUntilFullRefresh <= 1) { if (pagesUntilFullRefresh <= 1) {
renderer.displayBuffer(HalDisplay::HALF_REFRESH); renderer.displayBuffer(EInkDisplay::HALF_REFRESH);
pagesUntilFullRefresh = SETTINGS.getRefreshFrequency(); pagesUntilFullRefresh = SETTINGS.getRefreshFrequency();
} else { } else {
renderer.displayBuffer(); renderer.displayBuffer();
@ -356,7 +356,7 @@ void XtcReaderActivity::renderPage() {
// Display with appropriate refresh // Display with appropriate refresh
if (pagesUntilFullRefresh <= 1) { if (pagesUntilFullRefresh <= 1) {
renderer.displayBuffer(HalDisplay::HALF_REFRESH); renderer.displayBuffer(EInkDisplay::HALF_REFRESH);
pagesUntilFullRefresh = SETTINGS.getRefreshFrequency(); pagesUntilFullRefresh = SETTINGS.getRefreshFrequency();
} else { } else {
renderer.displayBuffer(); renderer.displayBuffer();

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include <EInkDisplay.h>
#include <EpdFontFamily.h> #include <EpdFontFamily.h>
#include <HalDisplay.h>
#include <string> #include <string>
#include <utility> #include <utility>
@ -10,12 +10,12 @@
class FullScreenMessageActivity final : public Activity { class FullScreenMessageActivity final : public Activity {
std::string text; std::string text;
EpdFontFamily::Style style; EpdFontFamily::Style style;
HalDisplay::RefreshMode refreshMode; EInkDisplay::RefreshMode refreshMode;
public: public:
explicit FullScreenMessageActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, std::string text, explicit FullScreenMessageActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, std::string text,
const EpdFontFamily::Style style = EpdFontFamily::REGULAR, const EpdFontFamily::Style style = EpdFontFamily::REGULAR,
const HalDisplay::RefreshMode refreshMode = HalDisplay::FAST_REFRESH) const EInkDisplay::RefreshMode refreshMode = EInkDisplay::FAST_REFRESH)
: Activity("FullScreenMessage", renderer, mappedInput), : Activity("FullScreenMessage", renderer, mappedInput),
text(std::move(text)), text(std::move(text)),
style(style), style(style),

View File

@ -1,8 +1,8 @@
#include <Arduino.h> #include <Arduino.h>
#include <EInkDisplay.h>
#include <Epub.h> #include <Epub.h>
#include <GfxRenderer.h> #include <GfxRenderer.h>
#include <HalDisplay.h> #include <InputManager.h>
#include <HalGPIO.h>
#include <SDCardManager.h> #include <SDCardManager.h>
#include <SPI.h> #include <SPI.h>
#include <builtinFonts/all.h> #include <builtinFonts/all.h>
@ -26,10 +26,23 @@
#include "activities/util/FullScreenMessageActivity.h" #include "activities/util/FullScreenMessageActivity.h"
#include "fontIds.h" #include "fontIds.h"
HalDisplay display; #define SPI_FQ 40000000
HalGPIO gpio; // Display SPI pins (custom pins for XteinkX4, not hardware SPI defaults)
MappedInputManager mappedInputManager(gpio); #define EPD_SCLK 8 // SPI Clock
GfxRenderer renderer(display); #define EPD_MOSI 10 // SPI MOSI (Master Out Slave In)
#define EPD_CS 21 // Chip Select
#define EPD_DC 4 // Data/Command
#define EPD_RST 5 // Reset
#define EPD_BUSY 6 // Busy
#define UART0_RXD 20 // Used for USB connection detection
#define SD_SPI_MISO 7
EInkDisplay einkDisplay(EPD_SCLK, EPD_MOSI, EPD_CS, EPD_DC, EPD_RST, EPD_BUSY);
InputManager inputManager;
MappedInputManager mappedInputManager(inputManager);
GfxRenderer renderer(einkDisplay);
Activity* currentActivity; Activity* currentActivity;
// Fonts // Fonts
@ -157,20 +170,21 @@ void verifyPowerButtonDuration() {
const uint16_t calibratedPressDuration = const uint16_t calibratedPressDuration =
(calibration < SETTINGS.getPowerButtonDuration()) ? SETTINGS.getPowerButtonDuration() - calibration : 1; (calibration < SETTINGS.getPowerButtonDuration()) ? SETTINGS.getPowerButtonDuration() - calibration : 1;
gpio.update(); inputManager.update();
// Verify the user has actually pressed
// Needed because inputManager.isPressed() may take up to ~500ms to return the correct state // Needed because inputManager.isPressed() may take up to ~500ms to return the correct state
while (!gpio.isPressed(HalGPIO::BTN_POWER) && millis() - start < 1000) { while (!inputManager.isPressed(InputManager::BTN_POWER) && millis() - start < 1000) {
delay(10); // only wait 10ms each iteration to not delay too much in case of short configured duration. delay(10); // only wait 10ms each iteration to not delay too much in case of short configured duration.
gpio.update(); inputManager.update();
} }
t2 = millis(); t2 = millis();
if (gpio.isPressed(HalGPIO::BTN_POWER)) { if (inputManager.isPressed(InputManager::BTN_POWER)) {
do { do {
delay(10); delay(10);
gpio.update(); inputManager.update();
} while (gpio.isPressed(HalGPIO::BTN_POWER) && gpio.getHeldTime() < calibratedPressDuration); } while (inputManager.isPressed(InputManager::BTN_POWER) && inputManager.getHeldTime() < calibratedPressDuration);
abort = gpio.getHeldTime() < calibratedPressDuration; abort = inputManager.getHeldTime() < calibratedPressDuration;
} else { } else {
abort = true; abort = true;
} }
@ -178,15 +192,16 @@ void verifyPowerButtonDuration() {
if (abort) { if (abort) {
// Button released too early. Returning to sleep. // Button released too early. Returning to sleep.
// IMPORTANT: Re-arm the wakeup trigger before sleeping again // IMPORTANT: Re-arm the wakeup trigger before sleeping again
gpio.startDeepSleep(); esp_deep_sleep_enable_gpio_wakeup(1ULL << InputManager::POWER_BUTTON_PIN, ESP_GPIO_WAKEUP_GPIO_LOW);
esp_deep_sleep_start();
} }
} }
void waitForPowerRelease() { void waitForPowerRelease() {
gpio.update(); inputManager.update();
while (gpio.isPressed(HalGPIO::BTN_POWER)) { while (inputManager.isPressed(InputManager::BTN_POWER)) {
delay(50); delay(50);
gpio.update(); inputManager.update();
} }
} }
@ -195,11 +210,14 @@ void enterDeepSleep() {
exitActivity(); exitActivity();
enterNewActivity(new SleepActivity(renderer, mappedInputManager)); enterNewActivity(new SleepActivity(renderer, mappedInputManager));
display.deepSleep(); einkDisplay.deepSleep();
Serial.printf("[%lu] [ ] Power button press calibration value: %lu ms\n", millis(), t2 - t1); Serial.printf("[%lu] [ ] Power button press calibration value: %lu ms\n", millis(), t2 - t1);
Serial.printf("[%lu] [ ] Entering deep sleep.\n", millis()); Serial.printf("[%lu] [ ] Entering deep sleep.\n", millis());
esp_deep_sleep_enable_gpio_wakeup(1ULL << InputManager::POWER_BUTTON_PIN, ESP_GPIO_WAKEUP_GPIO_LOW);
gpio.startDeepSleep(); // Ensure that the power button has been released to avoid immediately turning back on if you're holding it
waitForPowerRelease();
// Enter Deep Sleep
esp_deep_sleep_start();
} }
void onGoHome(); void onGoHome();
@ -243,7 +261,7 @@ void onGoHome() {
} }
void setupDisplayAndFonts() { void setupDisplayAndFonts() {
display.begin(); einkDisplay.begin();
Serial.printf("[%lu] [ ] Display initialized\n", millis()); Serial.printf("[%lu] [ ] Display initialized\n", millis());
renderer.insertFont(BOOKERLY_14_FONT_ID, bookerly14FontFamily); renderer.insertFont(BOOKERLY_14_FONT_ID, bookerly14FontFamily);
#ifndef OMIT_FONTS #ifndef OMIT_FONTS
@ -266,13 +284,27 @@ void setupDisplayAndFonts() {
Serial.printf("[%lu] [ ] Fonts setup\n", millis()); Serial.printf("[%lu] [ ] Fonts setup\n", millis());
} }
bool isUsbConnected() {
// U0RXD/GPIO20 reads HIGH when USB is connected
return digitalRead(UART0_RXD) == HIGH;
}
bool isWakeupByPowerButton() {
const auto wakeupCause = esp_sleep_get_wakeup_cause();
const auto resetReason = esp_reset_reason();
if (isUsbConnected()) {
return wakeupCause == ESP_SLEEP_WAKEUP_GPIO;
} else {
return (wakeupCause == ESP_SLEEP_WAKEUP_UNDEFINED) && (resetReason == ESP_RST_POWERON);
}
}
void setup() { void setup() {
t1 = millis(); t1 = millis();
gpio.begin();
// Only start serial if USB connected // Only start serial if USB connected
if (gpio.isUsbConnected()) { pinMode(UART0_RXD, INPUT);
if (isUsbConnected()) {
Serial.begin(115200); Serial.begin(115200);
// Wait up to 3 seconds for Serial to be ready to catch early logs // Wait up to 3 seconds for Serial to be ready to catch early logs
unsigned long start = millis(); unsigned long start = millis();
@ -281,6 +313,13 @@ void setup() {
} }
} }
inputManager.begin();
// Initialize pins
pinMode(BAT_GPIO0, INPUT);
// Initialize SPI with custom pins
SPI.begin(EPD_SCLK, SD_SPI_MISO, EPD_MOSI, EPD_CS);
// SD Card Initialization // SD Card Initialization
// We need 6 open files concurrently when parsing a new chapter // We need 6 open files concurrently when parsing a new chapter
if (!SdMan.begin()) { if (!SdMan.begin()) {
@ -294,7 +333,7 @@ void setup() {
SETTINGS.loadFromFile(); SETTINGS.loadFromFile();
KOREADER_STORE.loadFromFile(); KOREADER_STORE.loadFromFile();
if (gpio.isWakeupByPowerButton()) { if (isWakeupByPowerButton()) {
// For normal wakeups, verify power button press duration // For normal wakeups, verify power button press duration
Serial.printf("[%lu] [ ] Verifying power button press duration\n", millis()); Serial.printf("[%lu] [ ] Verifying power button press duration\n", millis());
verifyPowerButtonDuration(); verifyPowerButtonDuration();
@ -331,7 +370,7 @@ void loop() {
const unsigned long loopStartTime = millis(); const unsigned long loopStartTime = millis();
static unsigned long lastMemPrint = 0; static unsigned long lastMemPrint = 0;
gpio.update(); inputManager.update();
if (Serial && millis() - lastMemPrint >= 10000) { if (Serial && millis() - lastMemPrint >= 10000) {
Serial.printf("[%lu] [MEM] Free: %d bytes, Total: %d bytes, Min Free: %d bytes\n", millis(), ESP.getFreeHeap(), Serial.printf("[%lu] [MEM] Free: %d bytes, Total: %d bytes, Min Free: %d bytes\n", millis(), ESP.getFreeHeap(),
@ -341,7 +380,8 @@ void loop() {
// Check for any user activity (button press or release) or active background work // Check for any user activity (button press or release) or active background work
static unsigned long lastActivityTime = millis(); static unsigned long lastActivityTime = millis();
if (gpio.wasAnyPressed() || gpio.wasAnyReleased() || (currentActivity && currentActivity->preventAutoSleep())) { if (inputManager.wasAnyPressed() || inputManager.wasAnyReleased() ||
(currentActivity && currentActivity->preventAutoSleep())) {
lastActivityTime = millis(); // Reset inactivity timer lastActivityTime = millis(); // Reset inactivity timer
} }
@ -353,7 +393,8 @@ void loop() {
return; return;
} }
if (gpio.isPressed(HalGPIO::BTN_POWER) && gpio.getHeldTime() > SETTINGS.getPowerButtonDuration()) { if (inputManager.isPressed(InputManager::BTN_POWER) &&
inputManager.getHeldTime() > SETTINGS.getPowerButtonDuration()) {
enterDeepSleep(); enterDeepSleep();
// This should never be hit as `enterDeepSleep` calls esp_deep_sleep_start // This should never be hit as `enterDeepSleep` calls esp_deep_sleep_start
return; return;