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-21 15:43:53 +11:00
|
|
|
#include <SD.h>
|
2025-12-06 04:20:03 +11:00
|
|
|
|
2025-12-19 14:17:26 +01:00
|
|
|
#include <vector>
|
|
|
|
|
|
2025-12-15 23:17:23 +11:00
|
|
|
#include "CrossPointSettings.h"
|
2025-12-21 18:42:06 +11:00
|
|
|
#include "CrossPointState.h"
|
2025-12-08 22:06:09 +11:00
|
|
|
#include "config.h"
|
2025-12-08 19:48:49 +11:00
|
|
|
#include "images/CrossLarge.h"
|
2025-12-06 04:20:03 +11:00
|
|
|
|
2025-12-17 23:32:18 +11:00
|
|
|
void SleepActivity::onEnter() {
|
2025-12-21 21:02:36 +11:00
|
|
|
Activity::onEnter();
|
2025-12-19 14:17:26 +01:00
|
|
|
renderPopup("Entering Sleep...");
|
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 {
|
|
|
|
|
const int textWidth = renderer.getTextWidth(READER_FONT_ID, message);
|
|
|
|
|
constexpr int margin = 20;
|
|
|
|
|
const int x = (renderer.getScreenWidth() - textWidth - margin * 2) / 2;
|
|
|
|
|
constexpr int y = 117;
|
|
|
|
|
const int w = textWidth + margin * 2;
|
|
|
|
|
const int h = renderer.getLineHeight(READER_FONT_ID) + margin * 2;
|
|
|
|
|
// renderer.clearScreen();
|
|
|
|
|
renderer.fillRect(x + 5, y + 5, w - 10, h - 10, false);
|
|
|
|
|
renderer.drawText(READER_FONT_ID, x + margin, y + margin, message);
|
|
|
|
|
renderer.drawRect(x + 5, y + 5, w - 10, h - 10);
|
|
|
|
|
renderer.displayBuffer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SleepActivity::renderCustomSleepScreen() const {
|
2025-12-19 14:17:26 +01:00
|
|
|
// Check if we have a /sleep directory
|
|
|
|
|
auto dir = SD.open("/sleep");
|
|
|
|
|
if (dir && dir.isDirectory()) {
|
|
|
|
|
std::vector<std::string> files;
|
|
|
|
|
// collect all valid BMP files
|
|
|
|
|
for (File file = dir.openNextFile(); file; file = dir.openNextFile()) {
|
|
|
|
|
if (file.isDirectory()) {
|
|
|
|
|
file.close();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
auto filename = std::string(file.name());
|
|
|
|
|
if (filename[0] == '.') {
|
|
|
|
|
file.close();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (filename.substr(filename.length() - 4) != ".bmp") {
|
2025-12-21 18:42:06 +11:00
|
|
|
Serial.printf("[%lu] [SLP] Skipping non-.bmp file name: %s\n", millis(), file.name());
|
2025-12-19 14:17:26 +01:00
|
|
|
file.close();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
Bitmap bitmap(file);
|
|
|
|
|
if (bitmap.parseHeaders() != BmpReaderError::Ok) {
|
2025-12-21 18:42:06 +11:00
|
|
|
Serial.printf("[%lu] [SLP] Skipping invalid BMP file: %s\n", millis(), file.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
|
2025-12-21 18:42:06 +11:00
|
|
|
const auto randomFileIndex = random(numFiles);
|
|
|
|
|
const auto filename = "/sleep/" + files[randomFileIndex];
|
2025-12-19 14:17:26 +01:00
|
|
|
auto file = SD.open(filename.c_str());
|
|
|
|
|
if (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);
|
|
|
|
|
Bitmap bitmap(file);
|
|
|
|
|
if (bitmap.parseHeaders() == BmpReaderError::Ok) {
|
2025-12-21 18:42:06 +11:00
|
|
|
renderBitmapSleepScreen(bitmap);
|
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.
|
|
|
|
|
auto file = SD.open("/sleep.bmp");
|
|
|
|
|
if (file) {
|
|
|
|
|
Bitmap bitmap(file);
|
|
|
|
|
if (bitmap.parseHeaders() == BmpReaderError::Ok) {
|
2025-12-21 18:42:06 +11:00
|
|
|
Serial.printf("[%lu] [SLP] Loading: /sleep.bmp\n", millis());
|
|
|
|
|
renderBitmapSleepScreen(bitmap);
|
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();
|
2025-12-08 22:52:19 +11:00
|
|
|
renderer.drawImage(CrossLarge, (pageWidth - 128) / 2, (pageHeight - 128) / 2, 128, 128);
|
|
|
|
|
renderer.drawCenteredText(UI_FONT_ID, pageHeight / 2 + 70, "CrossPoint", true, BOLD);
|
|
|
|
|
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
|
|
|
|
2025-12-21 18:42:06 +11:00
|
|
|
void SleepActivity::renderBitmapSleepScreen(const Bitmap& bitmap) 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();
|
2025-12-19 08:45:14 +11:00
|
|
|
|
|
|
|
|
if (bitmap.getWidth() > pageWidth || bitmap.getHeight() > pageHeight) {
|
|
|
|
|
// image will scale, make sure placement is right
|
|
|
|
|
const float ratio = static_cast<float>(bitmap.getWidth()) / static_cast<float>(bitmap.getHeight());
|
|
|
|
|
const float screenRatio = static_cast<float>(pageWidth) / static_cast<float>(pageHeight);
|
|
|
|
|
|
|
|
|
|
if (ratio > screenRatio) {
|
|
|
|
|
// image wider than viewport ratio, scaled down image needs to be centered vertically
|
|
|
|
|
x = 0;
|
|
|
|
|
y = (pageHeight - pageWidth / ratio) / 2;
|
|
|
|
|
} else {
|
|
|
|
|
// image taller than viewport ratio, scaled down image needs to be centered horizontally
|
|
|
|
|
x = (pageWidth - pageHeight * ratio) / 2;
|
|
|
|
|
y = 0;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// center the image
|
|
|
|
|
x = (pageWidth - bitmap.getWidth()) / 2;
|
|
|
|
|
y = (pageHeight - bitmap.getHeight()) / 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderer.clearScreen();
|
|
|
|
|
renderer.drawBitmap(bitmap, x, y, pageWidth, pageHeight);
|
|
|
|
|
renderer.displayBuffer(EInkDisplay::HALF_REFRESH);
|
|
|
|
|
|
|
|
|
|
if (bitmap.hasGreyscale()) {
|
|
|
|
|
bitmap.rewindToData();
|
|
|
|
|
renderer.clearScreen(0x00);
|
|
|
|
|
renderer.setRenderMode(GfxRenderer::GRAYSCALE_LSB);
|
|
|
|
|
renderer.drawBitmap(bitmap, x, y, pageWidth, pageHeight);
|
|
|
|
|
renderer.copyGrayscaleLsbBuffers();
|
|
|
|
|
|
|
|
|
|
bitmap.rewindToData();
|
|
|
|
|
renderer.clearScreen(0x00);
|
|
|
|
|
renderer.setRenderMode(GfxRenderer::GRAYSCALE_MSB);
|
|
|
|
|
renderer.drawBitmap(bitmap, x, y, pageWidth, pageHeight);
|
|
|
|
|
renderer.copyGrayscaleMsbBuffers();
|
|
|
|
|
|
|
|
|
|
renderer.displayGrayBuffer();
|
|
|
|
|
renderer.setRenderMode(GfxRenderer::BW);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-12-21 18:42:06 +11:00
|
|
|
|
|
|
|
|
void SleepActivity::renderCoverSleepScreen() const {
|
2025-12-21 21:02:36 +11:00
|
|
|
if (APP_STATE.openEpubPath.empty()) {
|
|
|
|
|
return renderDefaultSleepScreen();
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-21 18:42:06 +11:00
|
|
|
Epub lastEpub(APP_STATE.openEpubPath, "/.crosspoint");
|
|
|
|
|
if (!lastEpub.load()) {
|
|
|
|
|
Serial.println("[SLP] Failed to load last epub");
|
|
|
|
|
return renderDefaultSleepScreen();
|
|
|
|
|
}
|
2025-12-21 21:02:36 +11:00
|
|
|
|
2025-12-21 18:42:06 +11:00
|
|
|
if (!lastEpub.generateCoverBmp()) {
|
|
|
|
|
Serial.println("[SLP] Failed to generate cover bmp");
|
|
|
|
|
return renderDefaultSleepScreen();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto file = SD.open(lastEpub.getCoverBmpPath().c_str(), FILE_READ);
|
|
|
|
|
if (file) {
|
|
|
|
|
Bitmap bitmap(file);
|
|
|
|
|
if (bitmap.parseHeaders() == BmpReaderError::Ok) {
|
|
|
|
|
renderBitmapSleepScreen(bitmap);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderDefaultSleepScreen();
|
|
|
|
|
}
|