2025-12-17 23:32:18 +11:00
|
|
|
#include "SleepActivity.h"
|
2025-12-06 04:20:03 +11:00
|
|
|
|
2025-12-21 18:42:06 +11:00
|
|
|
#include <Epub.h>
|
2025-12-08 22:06:09 +11:00
|
|
|
#include <GfxRenderer.h>
|
2025-12-30 15:09:30 +10:00
|
|
|
#include <SDCardManager.h>
|
2026-01-14 19:36:40 +09:00
|
|
|
#include <Txt.h>
|
2025-12-28 23:56:05 +09:00
|
|
|
#include <Xtc.h>
|
2025-12-06 04:20:03 +11:00
|
|
|
|
2025-12-15 23:17:23 +11:00
|
|
|
#include "CrossPointSettings.h"
|
2025-12-21 18:42:06 +11:00
|
|
|
#include "CrossPointState.h"
|
Aleo, Noto Sans, Open Dyslexic fonts (#163)
## Summary
* Swap out Bookerly font due to licensing issues, replace default font
with Aleo
* I did a bunch of searching around for a nice replacement font, and
this trumped several other like Literata, Merriwether, Vollkorn, etc
* Add Noto Sans, and Open Dyslexic as font options
* They can be selected in the settings screen
* Add font size options (Small, Medium, Large, Extra Large)
* Adjustable in settings
* Swap out uses of reader font in headings and replaced with slightly
larger Ubuntu font
* Replaced PixelArial14 font as it was difficult to track down, replace
with Space Grotesk
* Remove auto formatting on generated font files
* Massively speeds up formatting step now that there is a lot more CPP
font source
* Include fonts with their licenses in the repo
## Additional Context
Line compression setting will follow
| Font | Small | Medium | Large | X Large |
| --- | --- | --- | --- | --- |
| Aleo |

|

|

|

|
| Noto Sans |

|

|

|

|
| Open Dyslexic |

|

|

|

|
2025-12-30 18:21:47 +10:00
|
|
|
#include "fontIds.h"
|
2025-12-08 19:48:49 +11:00
|
|
|
#include "images/CrossLarge.h"
|
2026-01-07 20:07:23 +10:00
|
|
|
#include "util/StringUtils.h"
|
2025-12-28 23:56:05 +09:00
|
|
|
|
2026-01-24 03:29:34 -05:00
|
|
|
namespace {
|
|
|
|
|
// Perimeter cache file format:
|
|
|
|
|
// - 4 bytes: uint32_t file size (for cache invalidation)
|
|
|
|
|
// - 1 byte: result (0 = white perimeter, 1 = black perimeter)
|
|
|
|
|
constexpr size_t PERIM_CACHE_SIZE = 5;
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2025-12-17 23:32:18 +11:00
|
|
|
void SleepActivity::onEnter() {
|
2025-12-21 21:17:00 +11:00
|
|
|
Activity::onEnter();
|
2025-12-19 14:17:26 +01:00
|
|
|
renderPopup("Entering Sleep...");
|
2025-12-21 18:42:06 +11:00
|
|
|
|
2026-01-05 10:08:39 +01:00
|
|
|
if (SETTINGS.sleepScreen == CrossPointSettings::SLEEP_SCREEN_MODE::BLANK) {
|
|
|
|
|
return renderBlankSleepScreen();
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-21 18:42:06 +11:00
|
|
|
if (SETTINGS.sleepScreen == CrossPointSettings::SLEEP_SCREEN_MODE::CUSTOM) {
|
|
|
|
|
return renderCustomSleepScreen();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (SETTINGS.sleepScreen == CrossPointSettings::SLEEP_SCREEN_MODE::COVER) {
|
|
|
|
|
return renderCoverSleepScreen();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderDefaultSleepScreen();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SleepActivity::renderPopup(const char* message) const {
|
2026-01-02 07:55:21 +01:00
|
|
|
const int textWidth = renderer.getTextWidth(UI_12_FONT_ID, message, EpdFontFamily::BOLD);
|
2025-12-21 18:42:06 +11:00
|
|
|
constexpr int margin = 20;
|
|
|
|
|
const int x = (renderer.getScreenWidth() - textWidth - margin * 2) / 2;
|
|
|
|
|
constexpr int y = 117;
|
|
|
|
|
const int w = textWidth + margin * 2;
|
Aleo, Noto Sans, Open Dyslexic fonts (#163)
## Summary
* Swap out Bookerly font due to licensing issues, replace default font
with Aleo
* I did a bunch of searching around for a nice replacement font, and
this trumped several other like Literata, Merriwether, Vollkorn, etc
* Add Noto Sans, and Open Dyslexic as font options
* They can be selected in the settings screen
* Add font size options (Small, Medium, Large, Extra Large)
* Adjustable in settings
* Swap out uses of reader font in headings and replaced with slightly
larger Ubuntu font
* Replaced PixelArial14 font as it was difficult to track down, replace
with Space Grotesk
* Remove auto formatting on generated font files
* Massively speeds up formatting step now that there is a lot more CPP
font source
* Include fonts with their licenses in the repo
## Additional Context
Line compression setting will follow
| Font | Small | Medium | Large | X Large |
| --- | --- | --- | --- | --- |
| Aleo |

|

|

|

|
| Noto Sans |

|

|

|

|
| Open Dyslexic |

|

|

|

|
2025-12-30 18:21:47 +10:00
|
|
|
const int h = renderer.getLineHeight(UI_12_FONT_ID) + margin * 2;
|
2025-12-21 18:42:06 +11:00
|
|
|
// renderer.clearScreen();
|
2026-01-02 07:55:21 +01:00
|
|
|
renderer.fillRect(x - 5, y - 5, w + 10, h + 10, true);
|
2025-12-21 18:42:06 +11:00
|
|
|
renderer.fillRect(x + 5, y + 5, w - 10, h - 10, false);
|
2026-01-02 07:55:21 +01:00
|
|
|
renderer.drawText(UI_12_FONT_ID, x + margin, y + margin, message, true, EpdFontFamily::BOLD);
|
2025-12-21 18:42:06 +11:00
|
|
|
renderer.displayBuffer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SleepActivity::renderCustomSleepScreen() const {
|
2025-12-19 14:17:26 +01:00
|
|
|
// Check if we have a /sleep directory
|
2025-12-30 15:09:30 +10:00
|
|
|
auto dir = SdMan.open("/sleep");
|
2025-12-19 14:17:26 +01:00
|
|
|
if (dir && dir.isDirectory()) {
|
|
|
|
|
std::vector<std::string> files;
|
2026-01-07 22:43:19 +10:00
|
|
|
char name[500];
|
2025-12-19 14:17:26 +01:00
|
|
|
// collect all valid BMP files
|
2025-12-30 15:09:30 +10:00
|
|
|
for (auto file = dir.openNextFile(); file; file = dir.openNextFile()) {
|
2025-12-19 14:17:26 +01:00
|
|
|
if (file.isDirectory()) {
|
|
|
|
|
file.close();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2025-12-30 15:09:30 +10:00
|
|
|
file.getName(name, sizeof(name));
|
|
|
|
|
auto filename = std::string(name);
|
2025-12-19 14:17:26 +01:00
|
|
|
if (filename[0] == '.') {
|
|
|
|
|
file.close();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (filename.substr(filename.length() - 4) != ".bmp") {
|
2025-12-30 15:09:30 +10:00
|
|
|
Serial.printf("[%lu] [SLP] Skipping non-.bmp file name: %s\n", millis(), name);
|
2025-12-19 14:17:26 +01:00
|
|
|
file.close();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
Bitmap bitmap(file);
|
|
|
|
|
if (bitmap.parseHeaders() != BmpReaderError::Ok) {
|
2025-12-30 15:09:30 +10:00
|
|
|
Serial.printf("[%lu] [SLP] Skipping invalid BMP file: %s\n", millis(), name);
|
2025-12-19 14:17:26 +01:00
|
|
|
file.close();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
files.emplace_back(filename);
|
|
|
|
|
file.close();
|
|
|
|
|
}
|
2025-12-21 18:42:06 +11:00
|
|
|
const auto numFiles = files.size();
|
2025-12-19 14:17:26 +01:00
|
|
|
if (numFiles > 0) {
|
|
|
|
|
// Generate a random number between 1 and numFiles
|
2026-01-14 05:05:08 -05:00
|
|
|
auto randomFileIndex = random(numFiles);
|
|
|
|
|
// If we picked the same image as last time, reroll
|
|
|
|
|
while (numFiles > 1 && randomFileIndex == APP_STATE.lastSleepImage) {
|
|
|
|
|
randomFileIndex = random(numFiles);
|
|
|
|
|
}
|
|
|
|
|
APP_STATE.lastSleepImage = randomFileIndex;
|
|
|
|
|
APP_STATE.saveToFile();
|
2026-01-24 03:29:34 -05:00
|
|
|
const auto bmpPath = "/sleep/" + files[randomFileIndex];
|
2025-12-30 15:09:30 +10:00
|
|
|
FsFile file;
|
2026-01-24 03:29:34 -05:00
|
|
|
if (SdMan.openFileForRead("SLP", bmpPath, file)) {
|
2025-12-21 18:42:06 +11:00
|
|
|
Serial.printf("[%lu] [SLP] Randomly loading: /sleep/%s\n", millis(), files[randomFileIndex].c_str());
|
2025-12-19 14:17:26 +01:00
|
|
|
delay(100);
|
2026-01-12 12:36:19 +01:00
|
|
|
Bitmap bitmap(file, true);
|
2025-12-19 14:17:26 +01:00
|
|
|
if (bitmap.parseHeaders() == BmpReaderError::Ok) {
|
2026-01-24 03:29:34 -05:00
|
|
|
renderBitmapSleepScreen(bitmap, bmpPath);
|
2025-12-19 14:17:26 +01:00
|
|
|
dir.close();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (dir) dir.close();
|
|
|
|
|
|
2025-12-19 08:45:14 +11:00
|
|
|
// Look for sleep.bmp on the root of the sd card to determine if we should
|
|
|
|
|
// render a custom sleep screen instead of the default.
|
2025-12-30 15:09:30 +10:00
|
|
|
FsFile file;
|
2026-01-24 03:29:34 -05:00
|
|
|
const std::string rootSleepPath = "/sleep.bmp";
|
|
|
|
|
if (SdMan.openFileForRead("SLP", rootSleepPath, file)) {
|
2026-01-12 12:36:19 +01:00
|
|
|
Bitmap bitmap(file, true);
|
2025-12-19 08:45:14 +11:00
|
|
|
if (bitmap.parseHeaders() == BmpReaderError::Ok) {
|
2025-12-21 18:42:06 +11:00
|
|
|
Serial.printf("[%lu] [SLP] Loading: /sleep.bmp\n", millis());
|
2026-01-24 03:29:34 -05:00
|
|
|
renderBitmapSleepScreen(bitmap, rootSleepPath);
|
2025-12-19 08:45:14 +11:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderDefaultSleepScreen();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SleepActivity::renderDefaultSleepScreen() const {
|
2025-12-21 18:42:06 +11:00
|
|
|
const auto pageWidth = renderer.getScreenWidth();
|
|
|
|
|
const auto pageHeight = renderer.getScreenHeight();
|
2025-12-08 19:48:49 +11:00
|
|
|
|
|
|
|
|
renderer.clearScreen();
|
Rotation Support (#77)
• What is the goal of this PR?
Implement a horizontal EPUB reading mode so books can be read in
landscape orientation (both 90° and 270°), while keeping the rest of the
UI in portrait.
• What changes are included?
◦ Rendering / Display
▪ Added an orientation model to GfxRenderer (Portrait, LandscapeNormal,
LandscapeFlipped) and made:
▪ drawPixel, drawImage, displayWindow map logical coordinates
differently depending on orientation.
▪ getScreenWidth() / getScreenHeight() return orientation‑aware logical
dimensions (480×800 in portrait, 800×480 in landscape).
◦ Settings / Configuration
▪ Extended CrossPointSettings with:
▪ landscapeReading (toggle for portrait vs. landscape EPUB reading).
▪ landscapeFlipped (toggle to flip landscape 180° so both horizontal
holding directions are supported).
▪ Updated settings serialization/deserialization to persist these fields
while remaining backward‑compatible with existing settings files.
▪ Updated SettingsActivity to expose two new toggles:
▪ “Landscape Reading”
▪ “Flip Landscape (swap top/bottom)”
◦ EPUB Reader
▪ In EpubReaderActivity:
▪ On onEnter, set GfxRenderer orientation based on the new settings
(Portrait, LandscapeNormal, or LandscapeFlipped).
▪ On onExit, reset orientation back to Portrait so Home, WiFi, Settings,
etc. continue to render as before.
▪ Adjusted renderStatusBar to position the status bar and battery
indicator relative to GfxRenderer::getScreenHeight() instead of
hard‑coded Y coordinates, so it stays correctly at the bottom in both
portrait and landscape.
◦ EPUB Caching / Layout
▪ Extended Section cache metadata (section.bin) to include the logical
screenWidth and screenHeight used when pages were generated; bumped
SECTION_FILE_VERSION.
▪ Updated loadCacheMetadata to compare:
▪ font/margins/line compression/extraParagraphSpacing and screen
dimensions; mismatches now invalidate and clear the cache.
▪ Updated persistPageDataToSD and all call sites in EpubReaderActivity
to pass the current GfxRenderer::getScreenWidth() / getScreenHeight() so
portrait and landscape caches are kept separate and correctly sized.
Additional Context
• Cache behavior / migration
◦ Existing section.bin files (old SECTION_FILE_VERSION) will be detected
as incompatible and their caches cleared and rebuilt once per chapter
when first opened after this change.
◦ Within a given orientation, caches will be reused as before. Switching
orientation (portrait ↔ landscape) will cause a one‑time re‑index of
each chapter in the new orientation.
• Scope and risks
◦ Orientation changes are scoped to the EPUB reader; the Home screen,
Settings, WiFi selection, sleep screens, and web server UI continue to
assume portrait orientation.
◦ The renderer’s orientation is a static/global setting; if future code
uses GfxRenderer outside the reader while a reader instance is active,
it should be aware that orientation is no longer implicitly fixed.
◦ All drawing primitives now go through orientation‑aware coordinate
transforms; any code that previously relied on edge‑case behavior or
out‑of‑bounds writes might surface as logged “Outside range” warnings
instead.
• Testing suggestions / areas to focus on
◦ Verify in hardware:
▪ Portrait mode still renders correctly (boot, home, settings, WiFi,
reader).
▪ Landscape reading in both directions:
▪ Landscape Reading = ON, Flip Landscape = OFF.
▪ Landscape Reading = ON, Flip Landscape = ON.
▪ Status bar (page X/Y, % progress, battery icon) is fully visible and
aligned at the bottom in all three combinations.
◦ Open the same book:
▪ In portrait first, then switch to landscape and reopen it.
▪ Confirm that:
▪ Old portrait caches are rebuilt once for landscape (you should see the
“Indexing…” page).
▪ Progress save/restore still works (resume opens to the correct page in
the current orientation).
◦ Ensure grayscale rendering (the secondary pass in
EpubReaderActivity::renderContents) still looks correct in both
orientations.
---------
Co-authored-by: Dave Allie <dave@daveallie.com>
2025-12-28 05:33:20 -05:00
|
|
|
renderer.drawImage(CrossLarge, (pageWidth + 128) / 2, (pageHeight - 128) / 2, 128, 128);
|
2025-12-31 12:11:36 +10:00
|
|
|
renderer.drawCenteredText(UI_10_FONT_ID, pageHeight / 2 + 70, "CrossPoint", true, EpdFontFamily::BOLD);
|
2025-12-08 22:52:19 +11:00
|
|
|
renderer.drawCenteredText(SMALL_FONT_ID, pageHeight / 2 + 95, "SLEEPING");
|
2025-12-15 13:16:46 +01:00
|
|
|
|
2025-12-21 18:42:06 +11:00
|
|
|
// Make sleep screen dark unless light is selected in settings
|
|
|
|
|
if (SETTINGS.sleepScreen != CrossPointSettings::SLEEP_SCREEN_MODE::LIGHT) {
|
2025-12-15 13:16:46 +01:00
|
|
|
renderer.invertScreen();
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-08 22:06:09 +11:00
|
|
|
renderer.displayBuffer(EInkDisplay::HALF_REFRESH);
|
2025-12-08 19:48:49 +11:00
|
|
|
}
|
2025-12-19 08:45:14 +11:00
|
|
|
|
2026-01-24 03:29:34 -05:00
|
|
|
void SleepActivity::renderBitmapSleepScreen(const Bitmap& bitmap, const std::string& bmpPath) const {
|
2025-12-19 08:45:14 +11:00
|
|
|
int x, y;
|
2025-12-21 18:42:06 +11:00
|
|
|
const auto pageWidth = renderer.getScreenWidth();
|
|
|
|
|
const auto pageHeight = renderer.getScreenHeight();
|
2026-01-05 11:07:27 +01:00
|
|
|
float cropX = 0, cropY = 0;
|
2026-01-22 13:13:12 -05:00
|
|
|
int drawWidth = pageWidth;
|
|
|
|
|
int drawHeight = pageHeight;
|
2026-01-24 03:29:34 -05:00
|
|
|
int fillWidth = pageWidth; // Actual area the image will occupy
|
|
|
|
|
int fillHeight = pageHeight;
|
2025-12-19 08:45:14 +11:00
|
|
|
|
2026-01-05 11:07:27 +01:00
|
|
|
Serial.printf("[%lu] [SLP] bitmap %d x %d, screen %d x %d\n", millis(), bitmap.getWidth(), bitmap.getHeight(),
|
|
|
|
|
pageWidth, pageHeight);
|
2026-01-22 13:13:12 -05:00
|
|
|
|
|
|
|
|
const float bitmapRatio = static_cast<float>(bitmap.getWidth()) / static_cast<float>(bitmap.getHeight());
|
|
|
|
|
const float screenRatio = static_cast<float>(pageWidth) / static_cast<float>(pageHeight);
|
|
|
|
|
Serial.printf("[%lu] [SLP] bitmap ratio: %f, screen ratio: %f\n", millis(), bitmapRatio, screenRatio);
|
|
|
|
|
|
|
|
|
|
const auto coverMode = SETTINGS.sleepScreenCoverMode;
|
|
|
|
|
|
|
|
|
|
if (coverMode == CrossPointSettings::SLEEP_SCREEN_COVER_MODE::ACTUAL) {
|
|
|
|
|
// ACTUAL mode: Show image at actual size, centered (no scaling)
|
2025-12-19 08:45:14 +11:00
|
|
|
x = (pageWidth - bitmap.getWidth()) / 2;
|
|
|
|
|
y = (pageHeight - bitmap.getHeight()) / 2;
|
2026-01-22 13:13:12 -05:00
|
|
|
// Don't constrain to screen dimensions - drawBitmap will clip
|
|
|
|
|
drawWidth = 0;
|
|
|
|
|
drawHeight = 0;
|
2026-01-24 03:29:34 -05:00
|
|
|
fillWidth = bitmap.getWidth();
|
|
|
|
|
fillHeight = bitmap.getHeight();
|
2026-01-22 13:13:12 -05:00
|
|
|
Serial.printf("[%lu] [SLP] ACTUAL mode: centering at %d, %d\n", millis(), x, y);
|
|
|
|
|
} else if (coverMode == CrossPointSettings::SLEEP_SCREEN_COVER_MODE::CROP) {
|
|
|
|
|
// CROP mode: Scale to fill screen completely (may crop edges)
|
|
|
|
|
// Calculate crop values to fill the screen while maintaining aspect ratio
|
|
|
|
|
if (bitmapRatio > screenRatio) {
|
|
|
|
|
// Image is wider than screen ratio - crop horizontally
|
|
|
|
|
cropX = 1.0f - (screenRatio / bitmapRatio);
|
|
|
|
|
Serial.printf("[%lu] [SLP] CROP mode: cropping x by %f\n", millis(), cropX);
|
|
|
|
|
} else if (bitmapRatio < screenRatio) {
|
|
|
|
|
// Image is taller than screen ratio - crop vertically
|
|
|
|
|
cropY = 1.0f - (bitmapRatio / screenRatio);
|
|
|
|
|
Serial.printf("[%lu] [SLP] CROP mode: cropping y by %f\n", millis(), cropY);
|
|
|
|
|
}
|
|
|
|
|
// After cropping, the image should fill the screen exactly
|
|
|
|
|
x = 0;
|
|
|
|
|
y = 0;
|
2026-01-24 03:29:34 -05:00
|
|
|
fillWidth = pageWidth;
|
|
|
|
|
fillHeight = pageHeight;
|
2026-01-22 13:13:12 -05:00
|
|
|
Serial.printf("[%lu] [SLP] CROP mode: drawing at 0, 0 with crop %f, %f\n", millis(), cropX, cropY);
|
|
|
|
|
} else {
|
|
|
|
|
// FIT mode (default): Scale to fit entire image within screen (may have letterboxing)
|
|
|
|
|
// Calculate the scaled dimensions
|
|
|
|
|
float scale;
|
|
|
|
|
if (bitmapRatio > screenRatio) {
|
|
|
|
|
// Image is wider than screen ratio - fit to width
|
|
|
|
|
scale = static_cast<float>(pageWidth) / static_cast<float>(bitmap.getWidth());
|
|
|
|
|
} else {
|
|
|
|
|
// Image is taller than screen ratio - fit to height
|
|
|
|
|
scale = static_cast<float>(pageHeight) / static_cast<float>(bitmap.getHeight());
|
|
|
|
|
}
|
2026-01-24 03:29:34 -05:00
|
|
|
fillWidth = static_cast<int>(bitmap.getWidth() * scale);
|
|
|
|
|
fillHeight = static_cast<int>(bitmap.getHeight() * scale);
|
2026-01-22 13:13:12 -05:00
|
|
|
|
|
|
|
|
// Center the scaled image
|
2026-01-24 03:29:34 -05:00
|
|
|
x = (pageWidth - fillWidth) / 2;
|
|
|
|
|
y = (pageHeight - fillHeight) / 2;
|
2026-01-22 13:13:12 -05:00
|
|
|
Serial.printf("[%lu] [SLP] FIT mode: scale %f, scaled size %d x %d, position %d, %d\n", millis(), scale,
|
2026-01-24 03:29:34 -05:00
|
|
|
fillWidth, fillHeight, x, y);
|
2025-12-19 08:45:14 +11:00
|
|
|
}
|
|
|
|
|
|
2026-01-24 03:29:34 -05:00
|
|
|
// Detect perimeter color and clear to matching background
|
|
|
|
|
const bool isBlackPerimeter = getPerimeterIsBlack(bitmap, bmpPath);
|
|
|
|
|
const uint8_t clearColor = isBlackPerimeter ? 0x00 : 0xFF;
|
|
|
|
|
|
2026-01-05 11:07:27 +01:00
|
|
|
Serial.printf("[%lu] [SLP] drawing to %d x %d\n", millis(), x, y);
|
2026-01-24 03:29:34 -05:00
|
|
|
renderer.clearScreen(clearColor);
|
|
|
|
|
|
|
|
|
|
// If background is black, fill the image area with white first so white pixels render correctly
|
|
|
|
|
if (isBlackPerimeter) {
|
|
|
|
|
renderer.fillRect(x, y, fillWidth, fillHeight, false); // false = white
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-22 13:13:12 -05:00
|
|
|
renderer.drawBitmap(bitmap, x, y, drawWidth, drawHeight, cropX, cropY);
|
2025-12-19 08:45:14 +11:00
|
|
|
renderer.displayBuffer(EInkDisplay::HALF_REFRESH);
|
|
|
|
|
|
|
|
|
|
if (bitmap.hasGreyscale()) {
|
|
|
|
|
bitmap.rewindToData();
|
|
|
|
|
renderer.clearScreen(0x00);
|
|
|
|
|
renderer.setRenderMode(GfxRenderer::GRAYSCALE_LSB);
|
2026-01-22 13:13:12 -05:00
|
|
|
renderer.drawBitmap(bitmap, x, y, drawWidth, drawHeight, cropX, cropY);
|
2025-12-19 08:45:14 +11:00
|
|
|
renderer.copyGrayscaleLsbBuffers();
|
|
|
|
|
|
|
|
|
|
bitmap.rewindToData();
|
|
|
|
|
renderer.clearScreen(0x00);
|
|
|
|
|
renderer.setRenderMode(GfxRenderer::GRAYSCALE_MSB);
|
2026-01-22 13:13:12 -05:00
|
|
|
renderer.drawBitmap(bitmap, x, y, drawWidth, drawHeight, cropX, cropY);
|
2025-12-19 08:45:14 +11:00
|
|
|
renderer.copyGrayscaleMsbBuffers();
|
|
|
|
|
|
|
|
|
|
renderer.displayGrayBuffer();
|
|
|
|
|
renderer.setRenderMode(GfxRenderer::BW);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-12-21 18:42:06 +11:00
|
|
|
|
|
|
|
|
void SleepActivity::renderCoverSleepScreen() const {
|
2025-12-21 21:17:00 +11:00
|
|
|
if (APP_STATE.openEpubPath.empty()) {
|
|
|
|
|
return renderDefaultSleepScreen();
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-28 23:56:05 +09:00
|
|
|
std::string coverBmpPath;
|
2026-01-12 10:55:47 +01:00
|
|
|
bool cropped = SETTINGS.sleepScreenCoverMode == CrossPointSettings::SLEEP_SCREEN_COVER_MODE::CROP;
|
2025-12-21 21:17:00 +11:00
|
|
|
|
2026-01-14 19:36:40 +09:00
|
|
|
// Check if the current book is XTC, TXT, or EPUB
|
2026-01-07 20:07:23 +10:00
|
|
|
if (StringUtils::checkFileExtension(APP_STATE.openEpubPath, ".xtc") ||
|
|
|
|
|
StringUtils::checkFileExtension(APP_STATE.openEpubPath, ".xtch")) {
|
2025-12-28 23:56:05 +09:00
|
|
|
// Handle XTC file
|
|
|
|
|
Xtc lastXtc(APP_STATE.openEpubPath, "/.crosspoint");
|
|
|
|
|
if (!lastXtc.load()) {
|
|
|
|
|
Serial.println("[SLP] Failed to load last XTC");
|
|
|
|
|
return renderDefaultSleepScreen();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!lastXtc.generateCoverBmp()) {
|
|
|
|
|
Serial.println("[SLP] Failed to generate XTC cover bmp");
|
|
|
|
|
return renderDefaultSleepScreen();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
coverBmpPath = lastXtc.getCoverBmpPath();
|
2026-01-14 19:36:40 +09:00
|
|
|
} else if (StringUtils::checkFileExtension(APP_STATE.openEpubPath, ".txt")) {
|
|
|
|
|
// Handle TXT file - looks for cover image in the same folder
|
|
|
|
|
Txt lastTxt(APP_STATE.openEpubPath, "/.crosspoint");
|
|
|
|
|
if (!lastTxt.load()) {
|
|
|
|
|
Serial.println("[SLP] Failed to load last TXT");
|
|
|
|
|
return renderDefaultSleepScreen();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!lastTxt.generateCoverBmp()) {
|
|
|
|
|
Serial.println("[SLP] No cover image found for TXT file");
|
|
|
|
|
return renderDefaultSleepScreen();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
coverBmpPath = lastTxt.getCoverBmpPath();
|
2026-01-07 20:07:23 +10:00
|
|
|
} else if (StringUtils::checkFileExtension(APP_STATE.openEpubPath, ".epub")) {
|
2025-12-28 23:56:05 +09:00
|
|
|
// Handle EPUB file
|
|
|
|
|
Epub lastEpub(APP_STATE.openEpubPath, "/.crosspoint");
|
|
|
|
|
if (!lastEpub.load()) {
|
|
|
|
|
Serial.println("[SLP] Failed to load last epub");
|
|
|
|
|
return renderDefaultSleepScreen();
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-12 10:55:47 +01:00
|
|
|
if (!lastEpub.generateCoverBmp(cropped)) {
|
2025-12-28 23:56:05 +09:00
|
|
|
Serial.println("[SLP] Failed to generate cover bmp");
|
|
|
|
|
return renderDefaultSleepScreen();
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-12 10:55:47 +01:00
|
|
|
coverBmpPath = lastEpub.getCoverBmpPath(cropped);
|
2026-01-07 20:07:23 +10:00
|
|
|
} else {
|
|
|
|
|
return renderDefaultSleepScreen();
|
2025-12-21 18:42:06 +11:00
|
|
|
}
|
|
|
|
|
|
2025-12-30 15:09:30 +10:00
|
|
|
FsFile file;
|
|
|
|
|
if (SdMan.openFileForRead("SLP", coverBmpPath, file)) {
|
2025-12-21 18:42:06 +11:00
|
|
|
Bitmap bitmap(file);
|
|
|
|
|
if (bitmap.parseHeaders() == BmpReaderError::Ok) {
|
2026-01-24 03:29:34 -05:00
|
|
|
renderBitmapSleepScreen(bitmap, coverBmpPath);
|
2025-12-21 18:42:06 +11:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderDefaultSleepScreen();
|
|
|
|
|
}
|
2026-01-05 10:08:39 +01:00
|
|
|
|
|
|
|
|
void SleepActivity::renderBlankSleepScreen() const {
|
|
|
|
|
renderer.clearScreen();
|
|
|
|
|
renderer.displayBuffer(EInkDisplay::HALF_REFRESH);
|
|
|
|
|
}
|
2026-01-24 03:29:34 -05:00
|
|
|
|
|
|
|
|
std::string SleepActivity::getPerimeterCachePath(const std::string& bmpPath) {
|
|
|
|
|
// Convert "/dir/file.bmp" to "/dir/.file.bmp.perim"
|
|
|
|
|
const size_t lastSlash = bmpPath.find_last_of('/');
|
|
|
|
|
if (lastSlash == std::string::npos) {
|
|
|
|
|
// No directory, just prepend dot
|
|
|
|
|
return "." + bmpPath + ".perim";
|
|
|
|
|
}
|
|
|
|
|
const std::string dir = bmpPath.substr(0, lastSlash + 1);
|
|
|
|
|
const std::string filename = bmpPath.substr(lastSlash + 1);
|
|
|
|
|
return dir + "." + filename + ".perim";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SleepActivity::getPerimeterIsBlack(const Bitmap& bitmap, const std::string& bmpPath) const {
|
|
|
|
|
const std::string cachePath = getPerimeterCachePath(bmpPath);
|
|
|
|
|
|
|
|
|
|
// Try to read from cache
|
|
|
|
|
FsFile cacheFile;
|
|
|
|
|
if (SdMan.openFileForRead("SLP", cachePath, cacheFile)) {
|
|
|
|
|
uint8_t cacheData[PERIM_CACHE_SIZE];
|
|
|
|
|
if (cacheFile.read(cacheData, PERIM_CACHE_SIZE) == PERIM_CACHE_SIZE) {
|
|
|
|
|
// Extract cached file size
|
|
|
|
|
const uint32_t cachedSize = static_cast<uint32_t>(cacheData[0]) |
|
|
|
|
|
(static_cast<uint32_t>(cacheData[1]) << 8) |
|
|
|
|
|
(static_cast<uint32_t>(cacheData[2]) << 16) |
|
|
|
|
|
(static_cast<uint32_t>(cacheData[3]) << 24);
|
|
|
|
|
|
|
|
|
|
// Get current BMP file size
|
|
|
|
|
FsFile bmpFile;
|
|
|
|
|
uint32_t currentSize = 0;
|
|
|
|
|
if (SdMan.openFileForRead("SLP", bmpPath, bmpFile)) {
|
|
|
|
|
currentSize = bmpFile.size();
|
|
|
|
|
bmpFile.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Validate cache
|
|
|
|
|
if (cachedSize == currentSize && currentSize > 0) {
|
|
|
|
|
const bool result = cacheData[4] != 0;
|
|
|
|
|
Serial.printf("[%lu] [SLP] Perimeter cache hit for %s: %s\n", millis(), bmpPath.c_str(),
|
|
|
|
|
result ? "black" : "white");
|
|
|
|
|
cacheFile.close();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
Serial.printf("[%lu] [SLP] Perimeter cache invalid (size mismatch: %lu vs %lu)\n", millis(),
|
|
|
|
|
static_cast<unsigned long>(cachedSize), static_cast<unsigned long>(currentSize));
|
|
|
|
|
}
|
|
|
|
|
cacheFile.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Cache miss - calculate perimeter
|
|
|
|
|
Serial.printf("[%lu] [SLP] Calculating perimeter for %s\n", millis(), bmpPath.c_str());
|
|
|
|
|
const bool isBlack = bitmap.detectPerimeterIsBlack();
|
|
|
|
|
Serial.printf("[%lu] [SLP] Perimeter detected: %s\n", millis(), isBlack ? "black" : "white");
|
|
|
|
|
|
|
|
|
|
// Get BMP file size for cache
|
|
|
|
|
FsFile bmpFile;
|
|
|
|
|
uint32_t fileSize = 0;
|
|
|
|
|
if (SdMan.openFileForRead("SLP", bmpPath, bmpFile)) {
|
|
|
|
|
fileSize = bmpFile.size();
|
|
|
|
|
bmpFile.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Save to cache
|
|
|
|
|
if (fileSize > 0 && SdMan.openFileForWrite("SLP", cachePath, cacheFile)) {
|
|
|
|
|
uint8_t cacheData[PERIM_CACHE_SIZE];
|
|
|
|
|
cacheData[0] = fileSize & 0xFF;
|
|
|
|
|
cacheData[1] = (fileSize >> 8) & 0xFF;
|
|
|
|
|
cacheData[2] = (fileSize >> 16) & 0xFF;
|
|
|
|
|
cacheData[3] = (fileSize >> 24) & 0xFF;
|
|
|
|
|
cacheData[4] = isBlack ? 1 : 0;
|
|
|
|
|
cacheFile.write(cacheData, PERIM_CACHE_SIZE);
|
|
|
|
|
cacheFile.close();
|
|
|
|
|
Serial.printf("[%lu] [SLP] Saved perimeter cache to %s\n", millis(), cachePath.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return isBlack;
|
|
|
|
|
}
|