5 Commits
0.1.1 ... 0.2.1

Author SHA1 Message Date
Dave Allie
ddec7f78dd Fix hold to wake logic
esp_sleep_get_wakeup_cause does not seem to be set when not connected to USB power
2025-12-04 00:57:32 +11:00
Dave Allie
2f9f86b3dd Update README.md features 2025-12-04 00:14:47 +11:00
Dave Allie
47eb1157ef Support left and right buttons in reader and file picker 2025-12-04 00:11:51 +11:00
Dave Allie
aee239a931 Adjust input button thresholds to support more devices 2025-12-04 00:11:51 +11:00
Dave Allie
1ee8b728f9 Add file selection screen 2025-12-04 00:11:51 +11:00
11 changed files with 232 additions and 49 deletions

View File

@@ -31,7 +31,9 @@ This project is **not affiliated with Xteink**; it's built as a community projec
- [x] EPUB parsing and rendering - [x] EPUB parsing and rendering
- [x] Saved reading position - [x] Saved reading position
- [ ] File explorer with file picker - [ ] File explorer with file picker
- Currently CrossPoint will just open the first EPUB it finds at the root of the SD card - [x] Basic EPUB picker from root directory
- [ ] Support nested folders
- [ ] EPUB picker with cover art
- [ ] Image support within EPUB - [ ] Image support within EPUB
- [ ] Configurable font, layout, and display options - [ ] Configurable font, layout, and display options
- [ ] WiFi connectivity - [ ] WiFi connectivity

View File

@@ -57,10 +57,10 @@ void EpdRenderer::drawText(const int x, const int y, const char* text, const boo
getFontRenderer(bold, italic)->renderString(text, &xpos, &ypos, color > 0 ? GxEPD_BLACK : GxEPD_WHITE); getFontRenderer(bold, italic)->renderString(text, &xpos, &ypos, color > 0 ? GxEPD_BLACK : GxEPD_WHITE);
} }
void EpdRenderer::drawSmallText(const int x, const int y, const char* text) const { void EpdRenderer::drawSmallText(const int x, const int y, const char* text, const uint16_t color) const {
int ypos = y + smallFont->font->data->advanceY + marginTop; int ypos = y + smallFont->font->data->advanceY + marginTop;
int xpos = x + marginLeft; int xpos = x + marginLeft;
smallFont->renderString(text, &xpos, &ypos, GxEPD_BLACK); smallFont->renderString(text, &xpos, &ypos, color > 0 ? GxEPD_BLACK : GxEPD_WHITE);
} }
void EpdRenderer::drawTextBox(const int x, const int y, const std::string& text, const int width, const int height, void EpdRenderer::drawTextBox(const int x, const int y, const std::string& text, const int width, const int height,

View File

@@ -26,7 +26,7 @@ class EpdRenderer {
int getTextWidth(const char* text, bool bold = false, bool italic = false) const; int getTextWidth(const char* text, bool bold = false, bool italic = false) const;
int getSmallTextWidth(const char* text) const; int getSmallTextWidth(const char* text) const;
void drawText(int x, int y, const char* text, bool bold = false, bool italic = false, uint16_t color = 1) const; void drawText(int x, int y, const char* text, bool bold = false, bool italic = false, uint16_t color = 1) const;
void drawSmallText(int x, int y, const char* text) const; void drawSmallText(int x, int y, const char* text, uint16_t color = 1) const;
void drawTextBox(int x, int y, const std::string& text, int width, int height, bool bold = false, void drawTextBox(int x, int y, const std::string& text, int width, int height, bool bold = false,
bool italic = false) const; bool italic = false) const;
void drawLine(int x1, int y1, int x2, int y2, uint16_t color) const; void drawLine(int x1, int y1, int x2, int y2, uint16_t color) const;

46
src/CrossPointState.cpp Normal file
View File

@@ -0,0 +1,46 @@
#include "CrossPointState.h"
#include <HardwareSerial.h>
#include <SD.h>
#include <Serialization.h>
#include <fstream>
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);
}
CrossPointState* CrossPointState::deserialize(std::istream& is) {
const auto state = new CrossPointState();
uint8_t version;
serialization::readPod(is, version);
if (version != STATE_VERSION) {
Serial.printf("CrossPointState: Unknown version %u\n", version);
return state;
}
serialization::readString(is, state->openEpubPath);
return state;
}
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;
}

14
src/CrossPointState.h Normal file
View File

@@ -0,0 +1,14 @@
#pragma once
#include <iosfwd>
#include <string>
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();
};

View File

@@ -10,9 +10,9 @@
// Button ADC thresholds // Button ADC thresholds
#define BTN_THRESHOLD 100 // Threshold tolerance #define BTN_THRESHOLD 100 // Threshold tolerance
#define BTN_RIGHT_VAL 3 #define BTN_RIGHT_VAL 3
#define BTN_LEFT_VAL 1470 #define BTN_LEFT_VAL 1500
#define BTN_CONFIRM_VAL 2655 #define BTN_CONFIRM_VAL 2700
#define BTN_BACK_VAL 3470 #define BTN_BACK_VAL 3550
#define BTN_VOLUME_DOWN_VAL 3 #define BTN_VOLUME_DOWN_VAL 3
#define BTN_VOLUME_UP_VAL 2305 #define BTN_VOLUME_UP_VAL 2305

View File

@@ -6,8 +6,10 @@
#include <SPI.h> #include <SPI.h>
#include "Battery.h" #include "Battery.h"
#include "CrossPointState.h"
#include "Input.h" #include "Input.h"
#include "screens/EpubReaderScreen.h" #include "screens/EpubReaderScreen.h"
#include "screens/FileSelectionScreen.h"
#include "screens/FullScreenMessageScreen.h" #include "screens/FullScreenMessageScreen.h"
#define SPI_FQ 40000000 #define SPI_FQ 40000000
@@ -28,10 +30,11 @@ GxEPD2_BW<GxEPD2_426_GDEQ0426T82, GxEPD2_426_GDEQ0426T82::HEIGHT> display(GxEPD2
EPD_RST, EPD_BUSY)); EPD_RST, EPD_BUSY));
auto renderer = new EpdRenderer(&display); auto renderer = new EpdRenderer(&display);
Screen* currentScreen; Screen* currentScreen;
CrossPointState* appState;
// Power button timing // Power button timing
// Time required to confirm boot from sleep // Time required to confirm boot from sleep
constexpr unsigned long POWER_BUTTON_WAKEUP_MS = 1500; constexpr unsigned long POWER_BUTTON_WAKEUP_MS = 1000;
// Time required to enter sleep mode // Time required to enter sleep mode
constexpr unsigned long POWER_BUTTON_SLEEP_MS = 1000; constexpr unsigned long POWER_BUTTON_SLEEP_MS = 1000;
@@ -62,9 +65,15 @@ void enterNewScreen(Screen* screen) {
// Verify long press on wake-up from deep sleep // Verify long press on wake-up from deep sleep
void verifyWakeupLongPress() { void verifyWakeupLongPress() {
const auto input = getInput(); // Give the user up to 1000ms to start holding the power button, and must hold for POWER_BUTTON_WAKEUP_MS
const auto start = millis();
auto input = getInput();
while (input.button != POWER && millis() - start < 1000) {
delay(50);
input = getInput();
}
if (input.button == POWER && input.pressTime > POWER_BUTTON_WAKEUP_MS) { if (input.button != POWER || input.pressTime < POWER_BUTTON_WAKEUP_MS) {
// 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
esp_deep_sleep_enable_gpio_wakeup(1ULL << BTN_GPIO3, ESP_GPIO_WAKEUP_GPIO_LOW); esp_deep_sleep_enable_gpio_wakeup(1ULL << BTN_GPIO3, ESP_GPIO_WAKEUP_GPIO_LOW);
@@ -77,7 +86,7 @@ void enterDeepSleep() {
enterNewScreen(new FullScreenMessageScreen(renderer, "Sleeping", true, false, true)); enterNewScreen(new FullScreenMessageScreen(renderer, "Sleeping", true, false, true));
Serial.println("Power button released after a long press. Entering deep sleep."); Serial.println("Power button released after a long press. Entering deep sleep.");
delay(2000); // Allow Serial buffer to empty and display to update delay(1000); // Allow Serial buffer to empty and display to update
// Enable Wakeup on LOW (button press) // Enable Wakeup on LOW (button press)
esp_deep_sleep_enable_gpio_wakeup(1ULL << BTN_GPIO3, ESP_GPIO_WAKEUP_GPIO_LOW); esp_deep_sleep_enable_gpio_wakeup(1ULL << BTN_GPIO3, ESP_GPIO_WAKEUP_GPIO_LOW);
@@ -102,16 +111,24 @@ void setupSerial() {
} }
} }
void onGoHome();
void onSelectEpubFile(const std::string& path) {
enterNewScreen(new FullScreenMessageScreen(renderer, "Loading..."));
Epub* epub = loadEpub(path);
if (epub) {
appState->openEpubPath = path;
appState->saveToFile();
enterNewScreen(new EpubReaderScreen(renderer, epub, onGoHome));
} else {
enterNewScreen(new FullScreenMessageScreen(renderer, "Failed to load epub"));
}
}
void onGoHome() { enterNewScreen(new FileSelectionScreen(renderer, onSelectEpubFile)); }
void setup() { void setup() {
setupInputPinModes(); setupInputPinModes();
verifyWakeupLongPress();
// Check if boot was triggered by the Power Button (Deep Sleep Wakeup)
// If triggered by RST pin or Battery insertion, this will be false, allowing
// normal boot.
if (esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_GPIO) {
verifyWakeupLongPress();
}
setupSerial(); setupSerial();
// Initialize pins // Initialize pins
@@ -127,37 +144,21 @@ void setup() {
display.setTextColor(GxEPD_BLACK); display.setTextColor(GxEPD_BLACK);
Serial.println("Display initialized"); Serial.println("Display initialized");
enterNewScreen(new FullScreenMessageScreen(renderer, "Loading...", true)); enterNewScreen(new FullScreenMessageScreen(renderer, "Booting...", true));
// SD Card Initialization // SD Card Initialization
SD.begin(SD_SPI_CS, SPI, SPI_FQ); SD.begin(SD_SPI_CS, SPI, SPI_FQ);
// TODO: Add a file selection screen, for now just load the first file appState = CrossPointState::loadFromFile();
File root = SD.open("/"); if (!appState->openEpubPath.empty()) {
String filename; Epub* epub = loadEpub(appState->openEpubPath);
while (true) { if (epub) {
filename = root.getNextFileName(); enterNewScreen(new EpubReaderScreen(renderer, epub, onGoHome));
if (!filename) { return;
break;
}
if (filename.substring(filename.length() - 5) == ".epub") {
Serial.printf("Found epub: %s\n", filename.c_str());
break;
} }
} }
if (!filename) { enterNewScreen(new FileSelectionScreen(renderer, onSelectEpubFile));
enterNewScreen(new FullScreenMessageScreen(renderer, "Could not find epub"));
return;
}
Epub* epub = loadEpub(std::string(filename.c_str()));
if (epub) {
enterNewScreen(new EpubReaderScreen(renderer, epub));
} else {
enterNewScreen(new FullScreenMessageScreen(renderer, "Failed to load epub"));
}
} }
void loop() { void loop() {

View File

@@ -41,13 +41,14 @@ void EpubReaderScreen::onEnter() {
void EpubReaderScreen::onExit() { void EpubReaderScreen::onExit() {
vTaskDelete(displayTaskHandle); vTaskDelete(displayTaskHandle);
displayTaskHandle = nullptr;
xSemaphoreTake(sectionMutex, portMAX_DELAY); xSemaphoreTake(sectionMutex, portMAX_DELAY);
vSemaphoreDelete(sectionMutex); vSemaphoreDelete(sectionMutex);
sectionMutex = nullptr; sectionMutex = nullptr;
} }
void EpubReaderScreen::handleInput(const Input input) { void EpubReaderScreen::handleInput(const Input input) {
if (input.button == VOLUME_UP || input.button == VOLUME_DOWN) { if (input.button == VOLUME_UP || input.button == VOLUME_DOWN || input.button == LEFT || input.button == RIGHT) {
const bool skipChapter = input.pressTime > SKIP_CHAPTER_MS; const bool skipChapter = input.pressTime > SKIP_CHAPTER_MS;
// No current section, attempt to rerender the book // No current section, attempt to rerender the book
@@ -56,17 +57,17 @@ void EpubReaderScreen::handleInput(const Input input) {
return; return;
} }
if (input.button == VOLUME_UP && skipChapter) { if ((input.button == VOLUME_UP || input.button == LEFT) && skipChapter) {
nextPageNumber = 0; nextPageNumber = 0;
currentSpineIndex--; currentSpineIndex--;
delete section; delete section;
section = nullptr; section = nullptr;
} else if (input.button == VOLUME_DOWN && skipChapter) { } else if ((input.button == VOLUME_DOWN || input.button == RIGHT) && skipChapter) {
nextPageNumber = 0; nextPageNumber = 0;
currentSpineIndex++; currentSpineIndex++;
delete section; delete section;
section = nullptr; section = nullptr;
} else if (input.button == VOLUME_UP) { } else if (input.button == VOLUME_UP || input.button == LEFT) {
if (section->currentPage > 0) { if (section->currentPage > 0) {
section->currentPage--; section->currentPage--;
} else { } else {
@@ -77,7 +78,7 @@ void EpubReaderScreen::handleInput(const Input input) {
section = nullptr; section = nullptr;
xSemaphoreGive(sectionMutex); xSemaphoreGive(sectionMutex);
} }
} else if (input.button == VOLUME_DOWN) { } else if (input.button == VOLUME_DOWN || input.button == RIGHT) {
if (section->currentPage < section->pageCount - 1) { if (section->currentPage < section->pageCount - 1) {
section->currentPage++; section->currentPage++;
} else { } else {
@@ -91,6 +92,8 @@ void EpubReaderScreen::handleInput(const Input input) {
} }
updateRequired = true; updateRequired = true;
} else if (input.button == BACK) {
onGoHome();
} }
} }

View File

@@ -15,6 +15,7 @@ class EpubReaderScreen final : public Screen {
int currentSpineIndex = 0; int currentSpineIndex = 0;
int nextPageNumber = 0; int nextPageNumber = 0;
bool updateRequired = false; bool updateRequired = false;
const std::function<void()> onGoHome;
static void taskTrampoline(void* param); static void taskTrampoline(void* param);
[[noreturn]] void displayTaskLoop(); [[noreturn]] void displayTaskLoop();
@@ -22,7 +23,8 @@ class EpubReaderScreen final : public Screen {
void renderStatusBar() const; void renderStatusBar() const;
public: public:
explicit EpubReaderScreen(EpdRenderer* renderer, Epub* epub) : Screen(renderer), epub(epub) {} explicit EpubReaderScreen(EpdRenderer* renderer, Epub* epub, const std::function<void()>& onGoHome)
: Screen(renderer), epub(epub), onGoHome(onGoHome) {}
~EpubReaderScreen() override { free(section); } ~EpubReaderScreen() override { free(section); }
void onEnter() override; void onEnter() override;
void onExit() override; void onExit() override;

View File

@@ -0,0 +1,87 @@
#include "FileSelectionScreen.h"
#include <EpdRenderer.h>
#include <SD.h>
void FileSelectionScreen::taskTrampoline(void* param) {
auto* self = static_cast<FileSelectionScreen*>(param);
self->displayTaskLoop();
}
void FileSelectionScreen::onEnter() {
files.clear();
auto root = SD.open("/");
File file;
while ((file = root.openNextFile())) {
if (file.isDirectory()) {
file.close();
continue;
}
auto filename = std::string(file.name());
if (filename.substr(filename.length() - 5) != ".epub" || filename[0] == '.') {
file.close();
continue;
}
files.emplace_back(filename);
file.close();
}
root.close();
// Trigger first update
updateRequired = true;
xTaskCreate(&FileSelectionScreen::taskTrampoline, "FileSelectionScreenTask",
1024, // Stack size
this, // Parameters
1, // Priority
&displayTaskHandle // Task handle
);
}
void FileSelectionScreen::onExit() {
vTaskDelete(displayTaskHandle);
displayTaskHandle = nullptr;
}
void FileSelectionScreen::handleInput(const Input input) {
if (input.button == VOLUME_DOWN || input.button == RIGHT) {
selectorIndex = (selectorIndex + 1) % files.size();
updateRequired = true;
} else if (input.button == VOLUME_UP || input.button == LEFT) {
selectorIndex = (selectorIndex + files.size() - 1) % files.size();
updateRequired = true;
} else if (input.button == CONFIRM) {
Serial.printf("Selected file: %s\n", files[selectorIndex].c_str());
onSelect("/" + files[selectorIndex]);
}
}
void FileSelectionScreen::displayTaskLoop() {
while (true) {
if (updateRequired) {
updateRequired = false;
render();
}
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
void FileSelectionScreen::render() const {
renderer->clearScreen();
const auto pageWidth = renderer->getPageWidth();
const auto titleWidth = renderer->getTextWidth("CrossPoint Reader", true);
renderer->drawText((pageWidth - titleWidth) / 2, 0, "CrossPoint Reader", true);
// Draw selection
renderer->fillRect(0, 50 + selectorIndex * 20 + 2, pageWidth - 1, 20, 1);
for (size_t i = 0; i < files.size(); i++) {
const auto file = files[i];
renderer->drawSmallText(50, 50 + i * 20, file.c_str(), i == selectorIndex ? 0 : 1);
}
renderer->flushDisplay();
}

View File

@@ -0,0 +1,28 @@
#pragma once
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <functional>
#include <string>
#include <vector>
#include "Screen.h"
class FileSelectionScreen final : public Screen {
TaskHandle_t displayTaskHandle = nullptr;
std::vector<std::string> files;
int selectorIndex = 0;
bool updateRequired = false;
const std::function<void(const std::string&)> onSelect;
static void taskTrampoline(void* param);
[[noreturn]] void displayTaskLoop();
void render() const;
public:
explicit FileSelectionScreen(EpdRenderer* renderer, const std::function<void(const std::string&)>& onSelect)
: Screen(renderer), onSelect(onSelect) {}
void onEnter() override;
void onExit() override;
void handleInput(Input input) override;
};