mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2026-02-05 07:07:38 +03:00
Compare commits
7 Commits
d86b3fe134
...
7b5a63d220
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7b5a63d220 | ||
|
|
c1d5f5d562 | ||
|
|
adfeee063f | ||
|
|
2d3928ed81 | ||
|
|
48249fbd1e | ||
|
|
1a53dccebd | ||
|
|
3e28724b62 |
@ -322,6 +322,11 @@ int Epub::getTocItemsCount() const { return toc.size(); }
|
||||
|
||||
// work out the section index for a toc index
|
||||
int Epub::getSpineIndexForTocIndex(const int tocIndex) const {
|
||||
if (tocIndex < 0 || tocIndex >= toc.size()) {
|
||||
Serial.printf("[%lu] [EBP] getSpineIndexForTocIndex: tocIndex %d out of range\n", millis(), tocIndex);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// the toc entry should have an href that matches the spine item
|
||||
// so we can find the spine index by looking for the href
|
||||
for (int i = 0; i < spine.size(); i++) {
|
||||
@ -336,6 +341,11 @@ int Epub::getSpineIndexForTocIndex(const int tocIndex) const {
|
||||
}
|
||||
|
||||
int Epub::getTocIndexForSpineIndex(const int spineIndex) const {
|
||||
if (spineIndex < 0 || spineIndex >= spine.size()) {
|
||||
Serial.printf("[%lu] [EBP] getTocIndexForSpineIndex: spineIndex %d out of range\n", millis(), spineIndex);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// the toc entry should have an href that matches the spine item
|
||||
// so we can find the toc index by looking for the href
|
||||
for (int i = 0; i < toc.size(); i++) {
|
||||
@ -348,13 +358,21 @@ int Epub::getTocIndexForSpineIndex(const int spineIndex) const {
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t Epub::getBookSize() const { return getCumulativeSpineItemSize(getSpineItemsCount() - 1); }
|
||||
size_t Epub::getBookSize() const {
|
||||
if (spine.empty()) {
|
||||
return 0;
|
||||
}
|
||||
return getCumulativeSpineItemSize(getSpineItemsCount() - 1);
|
||||
}
|
||||
|
||||
// Calculate progress in book
|
||||
uint8_t Epub::calculateProgress(const int currentSpineIndex, const float currentSpineRead) {
|
||||
size_t bookSize = getBookSize();
|
||||
if (bookSize == 0) {
|
||||
return 0;
|
||||
}
|
||||
size_t prevChapterSize = (currentSpineIndex >= 1) ? getCumulativeSpineItemSize(currentSpineIndex - 1) : 0;
|
||||
size_t curChapterSize = getCumulativeSpineItemSize(currentSpineIndex) - prevChapterSize;
|
||||
size_t bookSize = getBookSize();
|
||||
size_t sectionProgSize = currentSpineRead * curChapterSize;
|
||||
return round(static_cast<float>(prevChapterSize + sectionProgSize) / bookSize * 100.0);
|
||||
}
|
||||
|
||||
@ -62,6 +62,10 @@ long ZipFile::getDataOffset(const mz_zip_archive_file_stat& fileStat) const {
|
||||
const uint64_t fileOffset = fileStat.m_local_header_ofs;
|
||||
|
||||
FILE* file = fopen(filePath.c_str(), "r");
|
||||
if (!file) {
|
||||
Serial.printf("[%lu] [ZIP] Failed to open file for reading local header\n", millis());
|
||||
return -1;
|
||||
}
|
||||
fseek(file, fileOffset, SEEK_SET);
|
||||
const size_t read = fread(pLocalHeader, 1, localHeaderSize, file);
|
||||
fclose(file);
|
||||
@ -104,6 +108,10 @@ uint8_t* ZipFile::readFileToMemory(const char* filename, size_t* size, const boo
|
||||
}
|
||||
|
||||
FILE* file = fopen(filePath.c_str(), "rb");
|
||||
if (!file) {
|
||||
Serial.printf("[%lu] [ZIP] Failed to open file for reading\n", millis());
|
||||
return nullptr;
|
||||
}
|
||||
fseek(file, fileOffset, SEEK_SET);
|
||||
|
||||
const auto deflatedDataSize = static_cast<size_t>(fileStat.m_comp_size);
|
||||
@ -175,6 +183,10 @@ bool ZipFile::readFileToStream(const char* filename, Print& out, const size_t ch
|
||||
}
|
||||
|
||||
FILE* file = fopen(filePath.c_str(), "rb");
|
||||
if (!file) {
|
||||
Serial.printf("[%lu] [ZIP] Failed to open file for streaming\n", millis());
|
||||
return false;
|
||||
}
|
||||
fseek(file, fileOffset, SEEK_SET);
|
||||
|
||||
const auto deflatedDataSize = static_cast<size_t>(fileStat.m_comp_size);
|
||||
|
||||
@ -12,7 +12,7 @@ CrossPointSettings CrossPointSettings::instance;
|
||||
|
||||
namespace {
|
||||
constexpr uint8_t SETTINGS_FILE_VERSION = 1;
|
||||
constexpr uint8_t SETTINGS_COUNT = 2;
|
||||
constexpr uint8_t SETTINGS_COUNT = 3;
|
||||
constexpr char SETTINGS_FILE[] = "/sd/.crosspoint/settings.bin";
|
||||
} // namespace
|
||||
|
||||
@ -25,6 +25,7 @@ bool CrossPointSettings::saveToFile() const {
|
||||
serialization::writePod(outputFile, SETTINGS_COUNT);
|
||||
serialization::writePod(outputFile, whiteSleepScreen);
|
||||
serialization::writePod(outputFile, extraParagraphSpacing);
|
||||
serialization::writePod(outputFile, shortPwrBtn);
|
||||
outputFile.close();
|
||||
|
||||
Serial.printf("[%lu] [CPS] Settings saved to file\n", millis());
|
||||
@ -51,15 +52,15 @@ bool CrossPointSettings::loadFromFile() {
|
||||
serialization::readPod(inputFile, fileSettingsCount);
|
||||
|
||||
// load settings that exist
|
||||
switch (fileSettingsCount) {
|
||||
case 1:
|
||||
serialization::readPod(inputFile, whiteSleepScreen);
|
||||
break;
|
||||
case 2:
|
||||
serialization::readPod(inputFile, whiteSleepScreen);
|
||||
serialization::readPod(inputFile, extraParagraphSpacing);
|
||||
break;
|
||||
}
|
||||
uint8_t settingsRead = 0;
|
||||
do {
|
||||
serialization::readPod(inputFile, whiteSleepScreen);
|
||||
if (++settingsRead >= fileSettingsCount) break;
|
||||
serialization::readPod(inputFile, extraParagraphSpacing);
|
||||
if (++settingsRead >= fileSettingsCount) break;
|
||||
serialization::readPod(inputFile, shortPwrBtn);
|
||||
if (++settingsRead >= fileSettingsCount) break;
|
||||
} while (false);
|
||||
|
||||
inputFile.close();
|
||||
Serial.printf("[%lu] [CPS] Settings loaded from file\n", millis());
|
||||
|
||||
@ -17,15 +17,18 @@ class CrossPointSettings {
|
||||
|
||||
// Sleep screen settings
|
||||
uint8_t whiteSleepScreen = 0;
|
||||
|
||||
// Text rendering settings
|
||||
uint8_t extraParagraphSpacing = 1;
|
||||
// Duration of the power button press
|
||||
uint8_t shortPwrBtn = 0;
|
||||
|
||||
~CrossPointSettings() = default;
|
||||
|
||||
// Get singleton instance
|
||||
static CrossPointSettings& getInstance() { return instance; }
|
||||
|
||||
uint16_t getPowerButtonDuration() const { return shortPwrBtn ? 10 : 500; }
|
||||
|
||||
bool saveToFile() const;
|
||||
bool loadFromFile();
|
||||
};
|
||||
|
||||
@ -33,13 +33,14 @@ void EpubReaderActivity::onEnter() {
|
||||
|
||||
epub->setupCacheDir();
|
||||
|
||||
if (SD.exists((epub->getCachePath() + "/progress.bin").c_str())) {
|
||||
File f = SD.open((epub->getCachePath() + "/progress.bin").c_str());
|
||||
File f = SD.open((epub->getCachePath() + "/progress.bin").c_str());
|
||||
if (f) {
|
||||
uint8_t data[4];
|
||||
f.read(data, 4);
|
||||
currentSpineIndex = data[0] + (data[1] << 8);
|
||||
nextPageNumber = data[2] + (data[3] << 8);
|
||||
Serial.printf("[%lu] [ERS] Loaded cache: %d, %d\n", millis(), currentSpineIndex, nextPageNumber);
|
||||
if (f.read(data, 4) == 4) {
|
||||
currentSpineIndex = data[0] + (data[1] << 8);
|
||||
nextPageNumber = data[2] + (data[3] << 8);
|
||||
Serial.printf("[%lu] [ERS] Loaded cache: %d, %d\n", millis(), currentSpineIndex, nextPageNumber);
|
||||
}
|
||||
f.close();
|
||||
}
|
||||
|
||||
@ -382,7 +383,7 @@ void EpubReaderActivity::renderStatusBar() const {
|
||||
const auto tocItem = epub->getTocItem(tocIndex);
|
||||
title = tocItem.title;
|
||||
titleWidth = renderer.getTextWidth(SMALL_FONT_ID, title.c_str());
|
||||
while (titleWidth > availableTextWidth) {
|
||||
while (titleWidth > availableTextWidth && title.length() > 11) {
|
||||
title = title.substr(0, title.length() - 8) + "...";
|
||||
titleWidth = renderer.getTextWidth(SMALL_FONT_ID, title.c_str());
|
||||
}
|
||||
|
||||
@ -9,7 +9,8 @@
|
||||
|
||||
const SettingInfo SettingsActivity::settingsList[settingsCount] = {
|
||||
{"White Sleep Screen", &CrossPointSettings::whiteSleepScreen},
|
||||
{"Extra Paragraph Spacing", &CrossPointSettings::extraParagraphSpacing}};
|
||||
{"Extra Paragraph Spacing", &CrossPointSettings::extraParagraphSpacing},
|
||||
{"Short Power Button Click", &CrossPointSettings::shortPwrBtn}};
|
||||
|
||||
void SettingsActivity::taskTrampoline(void* param) {
|
||||
auto* self = static_cast<SettingsActivity*>(param);
|
||||
|
||||
@ -25,7 +25,7 @@ class SettingsActivity final : public Activity {
|
||||
const std::function<void()> onGoHome;
|
||||
|
||||
// Static settings list
|
||||
static constexpr int settingsCount = 2; // Number of settings
|
||||
static constexpr int settingsCount = 3; // Number of settings
|
||||
static const SettingInfo settingsList[settingsCount];
|
||||
|
||||
static void taskTrampoline(void* param);
|
||||
|
||||
42
src/main.cpp
42
src/main.cpp
@ -57,11 +57,6 @@ EpdFont ubuntu10Font(&ubuntu_10);
|
||||
EpdFont ubuntuBold10Font(&ubuntu_bold_10);
|
||||
EpdFontFamily ubuntuFontFamily(&ubuntu10Font, &ubuntuBold10Font);
|
||||
|
||||
// Power button timing
|
||||
// Time required to confirm boot from sleep
|
||||
constexpr unsigned long POWER_BUTTON_WAKEUP_MS = 500;
|
||||
// Time required to enter sleep mode
|
||||
constexpr unsigned long POWER_BUTTON_SLEEP_MS = 500;
|
||||
// Auto-sleep timeout (10 minutes of inactivity)
|
||||
constexpr unsigned long AUTO_SLEEP_TIMEOUT_MS = 10 * 60 * 1000;
|
||||
|
||||
@ -79,23 +74,24 @@ void enterNewActivity(Activity* activity) {
|
||||
|
||||
// Verify long press on wake-up from deep sleep
|
||||
void verifyWakeupLongPress() {
|
||||
// Give the user up to 1000ms to start holding the power button, and must hold for POWER_BUTTON_WAKEUP_MS
|
||||
// Give the user up to 1000ms to start holding the power button, and must hold for SETTINGS.getPowerButtonDuration()
|
||||
const auto start = millis();
|
||||
bool abort = false;
|
||||
|
||||
Serial.printf("[%lu] [ ] Verifying power button press\n", millis());
|
||||
inputManager.update();
|
||||
// Verify the user has actually pressed
|
||||
while (!inputManager.isPressed(InputManager::BTN_POWER) && millis() - start < 1000) {
|
||||
delay(50);
|
||||
delay(10); // only wait 10ms each iteration to not delay too much in case of short configured duration.
|
||||
inputManager.update();
|
||||
}
|
||||
|
||||
if (inputManager.isPressed(InputManager::BTN_POWER)) {
|
||||
do {
|
||||
delay(50);
|
||||
delay(10);
|
||||
inputManager.update();
|
||||
} while (inputManager.isPressed(InputManager::BTN_POWER) && inputManager.getHeldTime() < POWER_BUTTON_WAKEUP_MS);
|
||||
abort = inputManager.getHeldTime() < POWER_BUTTON_WAKEUP_MS;
|
||||
} while (inputManager.isPressed(InputManager::BTN_POWER) &&
|
||||
inputManager.getHeldTime() < SETTINGS.getPowerButtonDuration());
|
||||
abort = inputManager.getHeldTime() < SETTINGS.getPowerButtonDuration();
|
||||
} else {
|
||||
abort = true;
|
||||
}
|
||||
@ -121,7 +117,7 @@ void enterDeepSleep() {
|
||||
exitActivity();
|
||||
enterNewActivity(new SleepActivity(renderer, inputManager));
|
||||
|
||||
Serial.printf("[%lu] [ ] Power button released after a long press. Entering deep sleep.\n", millis());
|
||||
Serial.printf("[%lu] [ ] Entering deep sleep.\n", millis());
|
||||
delay(1000); // Allow Serial buffer to empty and display to update
|
||||
|
||||
// Enable Wakeup on LOW (button press)
|
||||
@ -156,14 +152,25 @@ void setup() {
|
||||
Serial.printf("[%lu] [ ] Starting CrossPoint version " CROSSPOINT_VERSION "\n", millis());
|
||||
|
||||
inputManager.begin();
|
||||
verifyWakeupLongPress();
|
||||
|
||||
// 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
|
||||
if (!SD.begin(SD_SPI_CS, SPI, SPI_FQ)) {
|
||||
Serial.printf("[%lu] [ ] SD card initialization failed\n", millis());
|
||||
exitActivity();
|
||||
enterNewActivity(new FullScreenMessageActivity(renderer, inputManager, "SD card error", BOLD));
|
||||
return;
|
||||
}
|
||||
|
||||
SETTINGS.loadFromFile();
|
||||
|
||||
// verify power button press duration after we've read settings.
|
||||
verifyWakeupLongPress();
|
||||
|
||||
// Initialize display
|
||||
einkDisplay.begin();
|
||||
Serial.printf("[%lu] [ ] Display initialized\n", millis());
|
||||
@ -176,10 +183,6 @@ void setup() {
|
||||
exitActivity();
|
||||
enterNewActivity(new BootActivity(renderer, inputManager));
|
||||
|
||||
// SD Card Initialization
|
||||
SD.begin(SD_SPI_CS, SPI, SPI_FQ);
|
||||
|
||||
SETTINGS.loadFromFile();
|
||||
APP_STATE.loadFromFile();
|
||||
if (APP_STATE.openEpubPath.empty()) {
|
||||
onGoHome();
|
||||
@ -216,7 +219,8 @@ void loop() {
|
||||
return;
|
||||
}
|
||||
|
||||
if (inputManager.wasReleased(InputManager::BTN_POWER) && inputManager.getHeldTime() > POWER_BUTTON_SLEEP_MS) {
|
||||
if (inputManager.wasReleased(InputManager::BTN_POWER) &&
|
||||
inputManager.getHeldTime() > SETTINGS.getPowerButtonDuration()) {
|
||||
enterDeepSleep();
|
||||
// This should never be hit as `enterDeepSleep` calls esp_deep_sleep_start
|
||||
return;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user