From 9a33030623c101497f77f905e55a8788ab41938f Mon Sep 17 00:00:00 2001 From: Dave Allie Date: Sat, 6 Dec 2025 12:56:39 +1100 Subject: [PATCH] Use reference passing for EpdRenderer --- lib/Epub/Epub/EpubHtmlParser.cpp | 4 +- lib/Epub/Epub/EpubHtmlParser.h | 4 +- lib/Epub/Epub/Page.cpp | 4 +- lib/Epub/Epub/Page.h | 6 +-- lib/Epub/Epub/Section.cpp | 8 ++-- lib/Epub/Epub/Section.h | 4 +- lib/Epub/Epub/blocks/Block.h | 2 +- lib/Epub/Epub/blocks/TextBlock.cpp | 12 +++--- lib/Epub/Epub/blocks/TextBlock.h | 6 +-- src/CrossPointState.cpp | 37 +++++++----------- src/CrossPointState.h | 9 ++--- src/main.cpp | 21 ++++++---- src/screens/BootLogoScreen.cpp | 8 ++-- src/screens/BootLogoScreen.h | 2 +- src/screens/EpubReaderScreen.cpp | 52 ++++++++++++------------- src/screens/EpubReaderScreen.h | 2 +- src/screens/FileSelectionScreen.cpp | 16 ++++---- src/screens/FileSelectionScreen.h | 2 +- src/screens/FullScreenMessageScreen.cpp | 14 +++---- src/screens/FullScreenMessageScreen.h | 2 +- src/screens/Screen.h | 4 +- src/screens/SleepScreen.cpp | 2 +- src/screens/SleepScreen.h | 2 +- 23 files changed, 109 insertions(+), 114 deletions(-) diff --git a/lib/Epub/Epub/EpubHtmlParser.cpp b/lib/Epub/Epub/EpubHtmlParser.cpp index 8ba55ae..83e4a11 100644 --- a/lib/Epub/Epub/EpubHtmlParser.cpp +++ b/lib/Epub/Epub/EpubHtmlParser.cpp @@ -146,8 +146,8 @@ void EpubHtmlParser::makePages() { currentPage = new Page(); } - const int lineHeight = renderer->getLineHeight(); - const int pageHeight = renderer->getPageHeight(); + const int lineHeight = renderer.getLineHeight(); + const int pageHeight = renderer.getPageHeight(); // Long running task, make sure to let other things happen vTaskDelay(1); diff --git a/lib/Epub/Epub/EpubHtmlParser.h b/lib/Epub/Epub/EpubHtmlParser.h index 36eccbd..59ada4f 100644 --- a/lib/Epub/Epub/EpubHtmlParser.h +++ b/lib/Epub/Epub/EpubHtmlParser.h @@ -10,7 +10,7 @@ class EpdRenderer; class EpubHtmlParser final : public tinyxml2::XMLVisitor { const char* filepath; - EpdRenderer* renderer; + EpdRenderer& renderer; std::function completePageFn; bool insideBoldTag = false; @@ -27,7 +27,7 @@ class EpubHtmlParser final : public tinyxml2::XMLVisitor { bool VisitExit(const tinyxml2::XMLElement& element) override; // xml parser callbacks public: - explicit EpubHtmlParser(const char* filepath, EpdRenderer* renderer, const std::function& completePageFn) + explicit EpubHtmlParser(const char* filepath, EpdRenderer& renderer, const std::function& completePageFn) : filepath(filepath), renderer(renderer), completePageFn(completePageFn) {} ~EpubHtmlParser() override = default; bool parseAndBuildPages(); diff --git a/lib/Epub/Epub/Page.cpp b/lib/Epub/Epub/Page.cpp index 8ad2878..49fe4f8 100644 --- a/lib/Epub/Epub/Page.cpp +++ b/lib/Epub/Epub/Page.cpp @@ -3,7 +3,7 @@ #include #include -void PageLine::render(EpdRenderer* renderer) { block->render(renderer, 0, yPos); } +void PageLine::render(EpdRenderer& renderer) { block->render(renderer, 0, yPos); } void PageLine::serialize(std::ostream& os) { serialization::writePod(os, yPos); @@ -20,7 +20,7 @@ PageLine* PageLine::deserialize(std::istream& is) { return new PageLine(tb, yPos); } -void Page::render(EpdRenderer* renderer) const { +void Page::render(EpdRenderer& renderer) const { const auto start = millis(); for (const auto element : elements) { element->render(renderer); diff --git a/lib/Epub/Epub/Page.h b/lib/Epub/Epub/Page.h index 9d843b1..5671090 100644 --- a/lib/Epub/Epub/Page.h +++ b/lib/Epub/Epub/Page.h @@ -11,7 +11,7 @@ class PageElement { int yPos; explicit PageElement(const int yPos) : yPos(yPos) {} virtual ~PageElement() = default; - virtual void render(EpdRenderer* renderer) = 0; + virtual void render(EpdRenderer& renderer) = 0; virtual void serialize(std::ostream& os) = 0; }; @@ -22,7 +22,7 @@ class PageLine final : public PageElement { public: PageLine(const TextBlock* block, const int yPos) : PageElement(yPos), block(block) {} ~PageLine() override { delete block; } - void render(EpdRenderer* renderer) override; + void render(EpdRenderer& renderer) override; void serialize(std::ostream& os) override; static PageLine* deserialize(std::istream& is); }; @@ -32,7 +32,7 @@ class Page { int nextY = 0; // the list of block index and line numbers on this page std::vector elements; - void render(EpdRenderer* renderer) const; + void render(EpdRenderer& renderer) const; ~Page() { for (const auto element : elements) { delete element; diff --git a/lib/Epub/Epub/Section.cpp b/lib/Epub/Epub/Section.cpp index 06f3a5d..1f23f37 100644 --- a/lib/Epub/Epub/Section.cpp +++ b/lib/Epub/Epub/Section.cpp @@ -107,11 +107,11 @@ void Section::renderPage() { delete p; } else if (pageCount == 0) { Serial.println("No pages to render"); - const int width = renderer->getTextWidth("Empty chapter", BOLD); - renderer->drawText((renderer->getPageWidth() - width) / 2, 300, "Empty chapter", 1, BOLD); + const int width = renderer.getTextWidth("Empty chapter", BOLD); + renderer.drawText((renderer.getPageWidth() - width) / 2, 300, "Empty chapter", 1, BOLD); } else { Serial.printf("Page out of bounds: %d (max %d)\n", currentPage, pageCount); - const int width = renderer->getTextWidth("Out of bounds", BOLD); - renderer->drawText((renderer->getPageWidth() - width) / 2, 300, "Out of bounds", 1, BOLD); + const int width = renderer.getTextWidth("Out of bounds", BOLD); + renderer.drawText((renderer.getPageWidth() - width) / 2, 300, "Out of bounds", 1, BOLD); } } diff --git a/lib/Epub/Epub/Section.h b/lib/Epub/Epub/Section.h index 52db796..4a8d2e1 100644 --- a/lib/Epub/Epub/Section.h +++ b/lib/Epub/Epub/Section.h @@ -7,7 +7,7 @@ class EpdRenderer; class Section { Epub* epub; const int spineIndex; - EpdRenderer* renderer; + EpdRenderer& renderer; std::string cachePath; void onPageComplete(const Page* page); @@ -16,7 +16,7 @@ class Section { int pageCount = 0; int currentPage = 0; - explicit Section(Epub* epub, const int spineIndex, EpdRenderer* renderer) + explicit Section(Epub* epub, const int spineIndex, EpdRenderer& renderer) : epub(epub), spineIndex(spineIndex), renderer(renderer) { cachePath = epub->getCachePath() + "/" + std::to_string(spineIndex); } diff --git a/lib/Epub/Epub/blocks/Block.h b/lib/Epub/Epub/blocks/Block.h index 11406c0..3f58e54 100644 --- a/lib/Epub/Epub/blocks/Block.h +++ b/lib/Epub/Epub/blocks/Block.h @@ -8,7 +8,7 @@ typedef enum { TEXT_BLOCK, IMAGE_BLOCK } BlockType; class Block { public: virtual ~Block() = default; - virtual void layout(EpdRenderer* renderer) = 0; + virtual void layout(EpdRenderer& renderer) = 0; virtual BlockType getType() = 0; virtual bool isEmpty() = 0; virtual void finish() {} diff --git a/lib/Epub/Epub/blocks/TextBlock.cpp b/lib/Epub/Epub/blocks/TextBlock.cpp index 1178a92..a3e2c73 100644 --- a/lib/Epub/Epub/blocks/TextBlock.cpp +++ b/lib/Epub/Epub/blocks/TextBlock.cpp @@ -43,10 +43,10 @@ void TextBlock::addSpan(const std::string& span, const bool is_bold, const bool } } -std::list TextBlock::splitIntoLines(const EpdRenderer* renderer) { +std::list TextBlock::splitIntoLines(const EpdRenderer& renderer) { const int totalWordCount = words.size(); - const int pageWidth = renderer->getPageWidth(); - const int spaceWidth = renderer->getSpaceWidth(); + const int pageWidth = renderer.getPageWidth(); + const int spaceWidth = renderer.getSpaceWidth(); words.shrink_to_fit(); wordStyles.shrink_to_fit(); @@ -66,7 +66,7 @@ std::list TextBlock::splitIntoLines(const EpdRenderer* renderer) { } else if (wordStyles[i] & ITALIC_SPAN) { fontStyle = ITALIC; } - const int width = renderer->getTextWidth(words[i].c_str(), fontStyle); + const int width = renderer.getTextWidth(words[i].c_str(), fontStyle); wordWidths[i] = width; } @@ -187,7 +187,7 @@ std::list TextBlock::splitIntoLines(const EpdRenderer* renderer) { return lines; } -void TextBlock::render(const EpdRenderer* renderer, const int x, const int y) const { +void TextBlock::render(const EpdRenderer& renderer, const int x, const int y) const { for (int i = 0; i < words.size(); i++) { // get the style const uint8_t wordStyle = wordStyles[i]; @@ -203,7 +203,7 @@ void TextBlock::render(const EpdRenderer* renderer, const int x, const int y) co } else if (wordStyles[i] & ITALIC_SPAN) { fontStyle = ITALIC; } - renderer->drawText(x + wordXpos[i], y, words[i].c_str(), 1, fontStyle); + renderer.drawText(x + wordXpos[i], y, words[i].c_str(), 1, fontStyle); } } diff --git a/lib/Epub/Epub/blocks/TextBlock.h b/lib/Epub/Epub/blocks/TextBlock.h index 6866f38..4f77e06 100644 --- a/lib/Epub/Epub/blocks/TextBlock.h +++ b/lib/Epub/Epub/blocks/TextBlock.h @@ -40,10 +40,10 @@ class TextBlock final : public Block { void set_style(const BLOCK_STYLE style) { this->style = style; } BLOCK_STYLE get_style() const { return style; } bool isEmpty() override { return words.empty(); } - void layout(EpdRenderer* renderer) override {}; + void layout(EpdRenderer& renderer) override {}; // given a renderer works out where to break the words into lines - std::list splitIntoLines(const EpdRenderer* renderer); - void render(const EpdRenderer* renderer, int x, int y) const; + std::list splitIntoLines(const EpdRenderer& renderer); + void render(const EpdRenderer& renderer, int x, int y) const; BlockType getType() override { return TEXT_BLOCK; } void serialize(std::ostream& os) const; static TextBlock* deserialize(std::istream& is); diff --git a/src/CrossPointState.cpp b/src/CrossPointState.cpp index 6f523dc..7564aac 100644 --- a/src/CrossPointState.cpp +++ b/src/CrossPointState.cpp @@ -9,38 +9,27 @@ constexpr uint8_t STATE_VERSION = 1; constexpr char STATE_FILE[] = "/sd/.crosspoint/state.bin"; -void CrossPointState::serialize(std::ostream& os) const { - serialization::writePod(os, STATE_VERSION); - serialization::writeString(os, openEpubPath); +bool CrossPointState::saveToFile() const { + std::ofstream outputFile(STATE_FILE); + serialization::writePod(outputFile, STATE_VERSION); + serialization::writeString(outputFile, openEpubPath); + outputFile.close(); + return true; } -CrossPointState* CrossPointState::deserialize(std::istream& is) { - const auto state = new CrossPointState(); +bool CrossPointState::loadFromFile() { + std::ifstream inputFile(STATE_FILE); uint8_t version; - serialization::readPod(is, version); + serialization::readPod(inputFile, version); if (version != STATE_VERSION) { Serial.printf("CrossPointState: Unknown version %u\n", version); - return state; + inputFile.close(); + return false; } - serialization::readString(is, state->openEpubPath); - return state; -} + serialization::readString(inputFile, openEpubPath); -void CrossPointState::saveToFile() const { - std::ofstream outputFile(STATE_FILE); - serialize(outputFile); - outputFile.close(); -} - -CrossPointState* CrossPointState::loadFromFile() { - if (!SD.exists(&STATE_FILE[3])) { - return new CrossPointState(); - } - - std::ifstream inputFile(STATE_FILE); - CrossPointState* state = deserialize(inputFile); inputFile.close(); - return state; + return true; } diff --git a/src/CrossPointState.h b/src/CrossPointState.h index dadb3f2..35b928d 100644 --- a/src/CrossPointState.h +++ b/src/CrossPointState.h @@ -3,12 +3,11 @@ #include class CrossPointState { - void serialize(std::ostream& os) const; - static CrossPointState* deserialize(std::istream& is); - public: std::string openEpubPath; ~CrossPointState() = default; - void saveToFile() const; - static CrossPointState* loadFromFile(); + + bool saveToFile() const; + + bool loadFromFile(); }; diff --git a/src/main.cpp b/src/main.cpp index 647bb6f..ba04a99 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,9 +31,9 @@ GxEPD2_BW display(GxEPD2_426_GDEQ0426T82(EPD_CS, EPD_DC, EPD_RST, EPD_BUSY)); InputManager inputManager; -auto renderer = new EpdRenderer(&display); +EpdRenderer renderer(&display); Screen* currentScreen; -CrossPointState* appState; +CrossPointState appState; // Power button timing // Time required to confirm boot from sleep @@ -136,8 +136,8 @@ void onSelectEpubFile(const std::string& path) { Epub* epub = loadEpub(path); if (epub) { - appState->openEpubPath = path; - appState->saveToFile(); + appState.openEpubPath = path; + appState.saveToFile(); exitScreen(); enterNewScreen(new EpubReaderScreen(renderer, inputManager, epub, onGoHome)); } else { @@ -182,9 +182,9 @@ void setup() { // SD Card Initialization SD.begin(SD_SPI_CS, SPI, SPI_FQ); - appState = CrossPointState::loadFromFile(); - if (!appState->openEpubPath.empty()) { - Epub* epub = loadEpub(appState->openEpubPath); + appState.loadFromFile(); + if (!appState.openEpubPath.empty()) { + Epub* epub = loadEpub(appState.openEpubPath); if (epub) { exitScreen(); enterNewScreen(new EpubReaderScreen(renderer, inputManager, epub, onGoHome)); @@ -204,6 +204,13 @@ void setup() { void loop() { delay(10); + static unsigned long lastMemPrint = 0; + if (Serial && millis() - lastMemPrint >= 2000) { + Serial.printf("[%lu] Memory - Free: %d bytes, Total: %d bytes, Min Free: %d bytes\n", millis(), ESP.getFreeHeap(), + ESP.getHeapSize(), ESP.getMinFreeHeap()); + lastMemPrint = millis(); + } + inputManager.update(); if (inputManager.wasReleased(InputManager::BTN_POWER) && inputManager.getHeldTime() > POWER_BUTTON_WAKEUP_MS) { enterDeepSleep(); diff --git a/src/screens/BootLogoScreen.cpp b/src/screens/BootLogoScreen.cpp index a346e2a..8206e81 100644 --- a/src/screens/BootLogoScreen.cpp +++ b/src/screens/BootLogoScreen.cpp @@ -5,10 +5,10 @@ #include "images/CrossLarge.h" void BootLogoScreen::onEnter() { - const auto pageWidth = renderer->getPageWidth(); - const auto pageHeight = renderer->getPageHeight(); + const auto pageWidth = renderer.getPageWidth(); + const auto pageHeight = renderer.getPageHeight(); - renderer->clearScreen(); + renderer.clearScreen(); // Location for images is from top right in landscape orientation - renderer->drawImage(CrossLarge, (pageHeight - 128) / 2, (pageWidth - 128) / 2, 128, 128); + renderer.drawImage(CrossLarge, (pageHeight - 128) / 2, (pageWidth - 128) / 2, 128, 128); } diff --git a/src/screens/BootLogoScreen.h b/src/screens/BootLogoScreen.h index 153b1ab..58d99f0 100644 --- a/src/screens/BootLogoScreen.h +++ b/src/screens/BootLogoScreen.h @@ -3,6 +3,6 @@ class BootLogoScreen final : public Screen { public: - explicit BootLogoScreen(EpdRenderer* renderer, InputManager& inputManager) : Screen(renderer, inputManager) {} + explicit BootLogoScreen(EpdRenderer& renderer, InputManager& inputManager) : Screen(renderer, inputManager) {} void onEnter() override; }; diff --git a/src/screens/EpubReaderScreen.cpp b/src/screens/EpubReaderScreen.cpp index 5ec94ba..357f8fa 100644 --- a/src/screens/EpubReaderScreen.cpp +++ b/src/screens/EpubReaderScreen.cpp @@ -154,16 +154,16 @@ void EpubReaderScreen::renderPage() { Serial.println("Cache not found, building..."); { - const int textWidth = renderer->getTextWidth("Indexing..."); + const int textWidth = renderer.getTextWidth("Indexing..."); constexpr int margin = 20; - const int x = (renderer->getPageWidth() - textWidth - margin * 2) / 2; + const int x = (renderer.getPageWidth() - textWidth - margin * 2) / 2; constexpr int y = 50; const int w = textWidth + margin * 2; - const int h = renderer->getLineHeight() + margin * 2; - renderer->fillRect(x, y, w, h, 0); - renderer->drawText(x + margin, y + margin, "Indexing..."); - renderer->drawRect(x + 5, y + 5, w - 10, h - 10); - renderer->flushArea(x, y, w, h); + const int h = renderer.getLineHeight() + margin * 2; + renderer.fillRect(x, y, w, h, 0); + renderer.drawText(x + margin, y + margin, "Indexing..."); + renderer.drawRect(x + 5, y + 5, w - 10, h - 10); + renderer.flushArea(x, y, w, h); } section->setupCacheDir(); @@ -184,14 +184,14 @@ void EpubReaderScreen::renderPage() { } } - renderer->clearScreen(); + renderer.clearScreen(); section->renderPage(); renderStatusBar(); if (pagesUntilFullRefresh <= 1) { - renderer->flushDisplay(false); + renderer.flushDisplay(false); pagesUntilFullRefresh = PAGES_PER_REFRESH; } else { - renderer->flushDisplay(); + renderer.flushDisplay(); pagesUntilFullRefresh--; } @@ -206,16 +206,16 @@ void EpubReaderScreen::renderPage() { } void EpubReaderScreen::renderStatusBar() const { - const auto pageWidth = renderer->getPageWidth(); + const auto pageWidth = renderer.getPageWidth(); const std::string progress = std::to_string(section->currentPage + 1) + " / " + std::to_string(section->pageCount); - const auto progressTextWidth = renderer->getSmallTextWidth(progress.c_str()); - renderer->drawSmallText(pageWidth - progressTextWidth, 765, progress.c_str()); + const auto progressTextWidth = renderer.getSmallTextWidth(progress.c_str()); + renderer.drawSmallText(pageWidth - progressTextWidth, 765, progress.c_str()); const uint16_t percentage = battery.readPercentage(); const auto percentageText = std::to_string(percentage) + "%"; - const auto percentageTextWidth = renderer->getSmallTextWidth(percentageText.c_str()); - renderer->drawSmallText(20, 765, percentageText.c_str()); + const auto percentageTextWidth = renderer.getSmallTextWidth(percentageText.c_str()); + renderer.drawSmallText(20, 765, percentageText.c_str()); // 1 column on left, 2 columns on right, 5 columns of battery body constexpr int batteryWidth = 15; @@ -224,23 +224,23 @@ void EpubReaderScreen::renderStatusBar() const { constexpr int y = 772; // Top line - renderer->drawLine(x, y, x + batteryWidth - 4, y); + renderer.drawLine(x, y, x + batteryWidth - 4, y); // Bottom line - renderer->drawLine(x, y + batteryHeight - 1, x + batteryWidth - 4, y + batteryHeight - 1); + renderer.drawLine(x, y + batteryHeight - 1, x + batteryWidth - 4, y + batteryHeight - 1); // Left line - renderer->drawLine(x, y, x, y + batteryHeight - 1); + renderer.drawLine(x, y, x, y + batteryHeight - 1); // Battery end - renderer->drawLine(x + batteryWidth - 4, y, x + batteryWidth - 4, y + batteryHeight - 1); - renderer->drawLine(x + batteryWidth - 3, y + 2, x + batteryWidth - 3, y + batteryHeight - 3); - renderer->drawLine(x + batteryWidth - 2, y + 2, x + batteryWidth - 2, y + batteryHeight - 3); - renderer->drawLine(x + batteryWidth - 1, y + 2, x + batteryWidth - 1, y + batteryHeight - 3); + renderer.drawLine(x + batteryWidth - 4, y, x + batteryWidth - 4, y + batteryHeight - 1); + renderer.drawLine(x + batteryWidth - 3, y + 2, x + batteryWidth - 3, y + batteryHeight - 3); + renderer.drawLine(x + batteryWidth - 2, y + 2, x + batteryWidth - 2, y + batteryHeight - 3); + renderer.drawLine(x + batteryWidth - 1, y + 2, x + batteryWidth - 1, y + batteryHeight - 3); // The +1 is to round up, so that we always fill at least one pixel int filledWidth = percentage * (batteryWidth - 5) / 100 + 1; if (filledWidth > batteryWidth - 5) { filledWidth = batteryWidth - 5; // Ensure we don't overflow } - renderer->fillRect(x + 1, y + 1, filledWidth, batteryHeight - 2); + renderer.fillRect(x + 1, y + 1, filledWidth, batteryHeight - 2); // Page width minus existing content with 30px padding on each side const int leftMargin = 20 + percentageTextWidth + 30; @@ -248,11 +248,11 @@ void EpubReaderScreen::renderStatusBar() const { const int availableTextWidth = pageWidth - leftMargin - rightMargin; const auto tocItem = epub->getTocItem(epub->getTocIndexForSpineIndex(currentSpineIndex)); auto title = tocItem.title; - int titleWidth = renderer->getSmallTextWidth(title.c_str()); + int titleWidth = renderer.getSmallTextWidth(title.c_str()); while (titleWidth > availableTextWidth) { title = title.substr(0, title.length() - 8) + "..."; - titleWidth = renderer->getSmallTextWidth(title.c_str()); + titleWidth = renderer.getSmallTextWidth(title.c_str()); } - renderer->drawSmallText(leftMargin + (availableTextWidth - titleWidth) / 2, 765, title.c_str()); + renderer.drawSmallText(leftMargin + (availableTextWidth - titleWidth) / 2, 765, title.c_str()); } diff --git a/src/screens/EpubReaderScreen.h b/src/screens/EpubReaderScreen.h index 1fe44ed..9aa3293 100644 --- a/src/screens/EpubReaderScreen.h +++ b/src/screens/EpubReaderScreen.h @@ -24,7 +24,7 @@ class EpubReaderScreen final : public Screen { void renderStatusBar() const; public: - explicit EpubReaderScreen(EpdRenderer* renderer, InputManager& inputManager, Epub* epub, + explicit EpubReaderScreen(EpdRenderer& renderer, InputManager& inputManager, Epub* epub, const std::function& onGoHome) : Screen(renderer, inputManager), epub(epub), onGoHome(onGoHome) {} void onEnter() override; diff --git a/src/screens/FileSelectionScreen.cpp b/src/screens/FileSelectionScreen.cpp index be800e2..1e5637a 100644 --- a/src/screens/FileSelectionScreen.cpp +++ b/src/screens/FileSelectionScreen.cpp @@ -105,23 +105,23 @@ void FileSelectionScreen::displayTaskLoop() { } void FileSelectionScreen::render() const { - renderer->clearScreen(); + renderer.clearScreen(); - const auto pageWidth = renderer->getPageWidth(); - const auto titleWidth = renderer->getTextWidth("CrossPoint Reader", BOLD); - renderer->drawText((pageWidth - titleWidth) / 2, 0, "CrossPoint Reader", 1, BOLD); + const auto pageWidth = renderer.getPageWidth(); + const auto titleWidth = renderer.getTextWidth("CrossPoint Reader", BOLD); + renderer.drawText((pageWidth - titleWidth) / 2, 0, "CrossPoint Reader", 1, BOLD); if (files.empty()) { - renderer->drawUiText(10, 50, "No EPUBs found"); + renderer.drawUiText(10, 50, "No EPUBs found"); } else { // Draw selection - renderer->fillRect(0, 50 + selectorIndex * 30 + 2, pageWidth - 1, 30); + renderer.fillRect(0, 50 + selectorIndex * 30 + 2, pageWidth - 1, 30); for (size_t i = 0; i < files.size(); i++) { const auto file = files[i]; - renderer->drawUiText(10, 50 + i * 30, file.c_str(), i == selectorIndex ? 0 : 1); + renderer.drawUiText(10, 50 + i * 30, file.c_str(), i == selectorIndex ? 0 : 1); } } - renderer->flushDisplay(); + renderer.flushDisplay(); } diff --git a/src/screens/FileSelectionScreen.h b/src/screens/FileSelectionScreen.h index 25d8402..66f3b01 100644 --- a/src/screens/FileSelectionScreen.h +++ b/src/screens/FileSelectionScreen.h @@ -24,7 +24,7 @@ class FileSelectionScreen final : public Screen { void loadFiles(); public: - explicit FileSelectionScreen(EpdRenderer* renderer, InputManager& inputManager, + explicit FileSelectionScreen(EpdRenderer& renderer, InputManager& inputManager, const std::function& onSelect) : Screen(renderer, inputManager), onSelect(onSelect) {} void onEnter() override; diff --git a/src/screens/FullScreenMessageScreen.cpp b/src/screens/FullScreenMessageScreen.cpp index 3535f78..bba4a09 100644 --- a/src/screens/FullScreenMessageScreen.cpp +++ b/src/screens/FullScreenMessageScreen.cpp @@ -3,12 +3,12 @@ #include void FullScreenMessageScreen::onEnter() { - const auto width = renderer->getUiTextWidth(text.c_str(), style); - const auto height = renderer->getLineHeight(); - const auto left = (renderer->getPageWidth() - width) / 2; - const auto top = (renderer->getPageHeight() - height) / 2; + const auto width = renderer.getUiTextWidth(text.c_str(), style); + const auto height = renderer.getLineHeight(); + const auto left = (renderer.getPageWidth() - width) / 2; + const auto top = (renderer.getPageHeight() - height) / 2; - renderer->clearScreen(invert); - renderer->drawUiText(left, top, text.c_str(), invert ? 0 : 1, style); - renderer->flushDisplay(partialUpdate); + renderer.clearScreen(invert); + renderer.drawUiText(left, top, text.c_str(), invert ? 0 : 1, style); + renderer.flushDisplay(partialUpdate); } diff --git a/src/screens/FullScreenMessageScreen.h b/src/screens/FullScreenMessageScreen.h index 68c971b..6d5fe9b 100644 --- a/src/screens/FullScreenMessageScreen.h +++ b/src/screens/FullScreenMessageScreen.h @@ -12,7 +12,7 @@ class FullScreenMessageScreen final : public Screen { bool partialUpdate; public: - explicit FullScreenMessageScreen(EpdRenderer* renderer, InputManager& inputManager, std::string text, + explicit FullScreenMessageScreen(EpdRenderer& renderer, InputManager& inputManager, std::string text, const EpdFontStyle style = REGULAR, const bool invert = false, const bool partialUpdate = true) : Screen(renderer, inputManager), diff --git a/src/screens/Screen.h b/src/screens/Screen.h index 373525e..6888c55 100644 --- a/src/screens/Screen.h +++ b/src/screens/Screen.h @@ -5,11 +5,11 @@ class EpdRenderer; class Screen { protected: - EpdRenderer* renderer; + EpdRenderer& renderer; InputManager& inputManager; public: - explicit Screen(EpdRenderer* renderer, InputManager& inputManager) : renderer(renderer), inputManager(inputManager) {} + explicit Screen(EpdRenderer& renderer, InputManager& inputManager) : renderer(renderer), inputManager(inputManager) {} virtual ~Screen() = default; virtual void onEnter() {} virtual void onExit() {} diff --git a/src/screens/SleepScreen.cpp b/src/screens/SleepScreen.cpp index b2793e8..489fcf5 100644 --- a/src/screens/SleepScreen.cpp +++ b/src/screens/SleepScreen.cpp @@ -4,4 +4,4 @@ #include "images/SleepScreenImg.h" -void SleepScreen::onEnter() { renderer->drawImageNoMargin(SleepScreenImg, 0, 0, 800, 480, false, true); } +void SleepScreen::onEnter() { renderer.drawImageNoMargin(SleepScreenImg, 0, 0, 800, 480, false, true); } diff --git a/src/screens/SleepScreen.h b/src/screens/SleepScreen.h index 02bd15f..c56bada 100644 --- a/src/screens/SleepScreen.h +++ b/src/screens/SleepScreen.h @@ -3,6 +3,6 @@ class SleepScreen final : public Screen { public: - explicit SleepScreen(EpdRenderer* renderer, InputManager& inputManager) : Screen(renderer, inputManager) {} + explicit SleepScreen(EpdRenderer& renderer, InputManager& inputManager) : Screen(renderer, inputManager) {} void onEnter() override; };