229 lines
6.9 KiB
C++
Raw Normal View History

2025-12-03 22:00:29 +11:00
#include <Arduino.h>
#include <EInkDisplay.h>
2025-12-03 22:00:29 +11:00
#include <Epub.h>
#include <GfxRenderer.h>
2025-12-06 12:35:41 +11:00
#include <InputManager.h>
2025-12-03 22:00:29 +11:00
#include <SD.h>
#include <SPI.h>
#include <builtinFonts/bookerly_2b.h>
#include <builtinFonts/bookerly_bold_2b.h>
#include <builtinFonts/bookerly_bold_italic_2b.h>
#include <builtinFonts/bookerly_italic_2b.h>
#include <builtinFonts/pixelarial14.h>
#include <builtinFonts/ubuntu_10.h>
#include <builtinFonts/ubuntu_bold_10.h>
2025-12-03 22:00:29 +11:00
#include "Battery.h"
#include "CrossPointSettings.h"
2025-12-04 00:07:25 +11:00
#include "CrossPointState.h"
#include "activities/boot_sleep/BootActivity.h"
#include "activities/boot_sleep/SleepActivity.h"
#include "activities/home/HomeActivity.h"
#include "activities/reader/ReaderActivity.h"
#include "activities/settings/SettingsActivity.h"
#include "activities/util/FullScreenMessageActivity.h"
#include "config.h"
2025-12-03 22:00:29 +11:00
#define SPI_FQ 40000000
// Display SPI pins (custom pins for XteinkX4, not hardware SPI defaults)
#define EPD_SCLK 8 // SPI Clock
#define EPD_MOSI 10 // SPI MOSI (Master Out Slave In)
#define EPD_CS 21 // Chip Select
#define EPD_DC 4 // Data/Command
#define EPD_RST 5 // Reset
#define EPD_BUSY 6 // Busy
#define UART0_RXD 20 // Used for USB connection detection
#define SD_SPI_CS 12
#define SD_SPI_MISO 7
EInkDisplay einkDisplay(EPD_SCLK, EPD_MOSI, EPD_CS, EPD_DC, EPD_RST, EPD_BUSY);
2025-12-06 12:35:41 +11:00
InputManager inputManager;
GfxRenderer renderer(einkDisplay);
Activity* currentActivity;
2025-12-03 22:00:29 +11:00
// Fonts
EpdFont bookerlyFont(&bookerly_2b);
EpdFont bookerlyBoldFont(&bookerly_bold_2b);
EpdFont bookerlyItalicFont(&bookerly_italic_2b);
EpdFont bookerlyBoldItalicFont(&bookerly_bold_italic_2b);
EpdFontFamily bookerlyFontFamily(&bookerlyFont, &bookerlyBoldFont, &bookerlyItalicFont, &bookerlyBoldItalicFont);
EpdFont smallFont(&pixelarial14);
EpdFontFamily smallFontFamily(&smallFont);
EpdFont ubuntu10Font(&ubuntu_10);
EpdFont ubuntuBold10Font(&ubuntu_bold_10);
EpdFontFamily ubuntuFontFamily(&ubuntu10Font, &ubuntuBold10Font);
2025-12-03 22:00:29 +11:00
// Power button timing
// Time required to confirm boot from sleep
constexpr unsigned long POWER_BUTTON_WAKEUP_MS = 500;
2025-12-03 22:00:29 +11:00
// 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;
2025-12-03 22:00:29 +11:00
void exitActivity() {
if (currentActivity) {
currentActivity->onExit();
delete currentActivity;
2025-12-03 22:00:29 +11:00
}
2025-12-06 01:37:20 +11:00
}
void enterNewActivity(Activity* activity) {
currentActivity = activity;
currentActivity->onEnter();
2025-12-03 22:00:29 +11:00
}
// 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
const auto start = millis();
2025-12-06 12:35:41 +11:00
bool abort = false;
2025-12-08 22:39:23 +11:00
Serial.printf("[%lu] [ ] Verifying power button press\n", millis());
2025-12-06 12:35:41 +11:00
inputManager.update();
while (!inputManager.isPressed(InputManager::BTN_POWER) && millis() - start < 1000) {
delay(50);
2025-12-06 12:35:41 +11:00
inputManager.update();
}
2025-12-03 22:00:29 +11:00
2025-12-06 12:35:41 +11:00
if (inputManager.isPressed(InputManager::BTN_POWER)) {
do {
delay(50);
inputManager.update();
} while (inputManager.isPressed(InputManager::BTN_POWER) && inputManager.getHeldTime() < POWER_BUTTON_WAKEUP_MS);
abort = inputManager.getHeldTime() < POWER_BUTTON_WAKEUP_MS;
} else {
abort = true;
}
if (abort) {
2025-12-03 22:00:29 +11:00
// Button released too early. Returning to sleep.
// IMPORTANT: Re-arm the wakeup trigger before sleeping again
2025-12-06 12:35:41 +11:00
esp_deep_sleep_enable_gpio_wakeup(1ULL << InputManager::POWER_BUTTON_PIN, ESP_GPIO_WAKEUP_GPIO_LOW);
2025-12-03 22:00:29 +11:00
esp_deep_sleep_start();
}
}
2025-12-06 12:35:41 +11:00
void waitForPowerRelease() {
inputManager.update();
while (inputManager.isPressed(InputManager::BTN_POWER)) {
2025-12-05 17:47:23 +11:00
delay(50);
2025-12-06 12:35:41 +11:00
inputManager.update();
2025-12-05 17:47:23 +11:00
}
}
2025-12-03 22:00:29 +11:00
// Enter deep sleep mode
void enterDeepSleep() {
exitActivity();
enterNewActivity(new SleepActivity(renderer, inputManager));
2025-12-03 22:00:29 +11:00
2025-12-08 22:39:23 +11:00
Serial.printf("[%lu] [ ] Power button released after a long press. Entering deep sleep.\n", millis());
delay(1000); // Allow Serial buffer to empty and display to update
2025-12-03 22:00:29 +11:00
// Enable Wakeup on LOW (button press)
2025-12-06 12:35:41 +11:00
esp_deep_sleep_enable_gpio_wakeup(1ULL << InputManager::POWER_BUTTON_PIN, ESP_GPIO_WAKEUP_GPIO_LOW);
2025-12-03 22:00:29 +11:00
einkDisplay.deepSleep();
2025-12-03 22:00:29 +11:00
// Enter Deep Sleep
esp_deep_sleep_start();
}
2025-12-04 00:07:25 +11:00
void onGoHome();
void onGoToReader(const std::string& initialEpubPath) {
exitActivity();
enterNewActivity(new ReaderActivity(renderer, inputManager, initialEpubPath, onGoHome));
}
void onGoToReaderHome() { onGoToReader(std::string()); }
void onGoToSettings() {
exitActivity();
enterNewActivity(new SettingsActivity(renderer, inputManager, onGoHome));
}
2025-12-06 01:37:20 +11:00
void onGoHome() {
exitActivity();
enterNewActivity(new HomeActivity(renderer, inputManager, onGoToReaderHome, onGoToSettings));
2025-12-06 01:37:20 +11:00
}
2025-12-04 00:07:25 +11:00
2025-12-03 22:00:29 +11:00
void setup() {
2025-12-13 00:42:17 +11:00
Serial.begin(115200);
2025-12-03 22:00:29 +11:00
2025-12-08 23:13:33 +11:00
Serial.printf("[%lu] [ ] Starting CrossPoint version " CROSSPOINT_VERSION "\n", millis());
inputManager.begin();
verifyWakeupLongPress();
2025-12-03 22:00:29 +11:00
// Initialize pins
pinMode(BAT_GPIO0, INPUT);
// Initialize SPI with custom pins
SPI.begin(EPD_SCLK, SD_SPI_MISO, EPD_MOSI, EPD_CS);
// Initialize display
einkDisplay.begin();
2025-12-08 22:39:23 +11:00
Serial.printf("[%lu] [ ] Display initialized\n", millis());
2025-12-03 22:00:29 +11:00
renderer.insertFont(READER_FONT_ID, bookerlyFontFamily);
renderer.insertFont(UI_FONT_ID, ubuntuFontFamily);
renderer.insertFont(SMALL_FONT_ID, smallFontFamily);
2025-12-08 22:39:23 +11:00
Serial.printf("[%lu] [ ] Fonts setup\n", millis());
exitActivity();
enterNewActivity(new BootActivity(renderer, inputManager));
2025-12-03 22:00:29 +11:00
// SD Card Initialization
SD.begin(SD_SPI_CS, SPI, SPI_FQ);
SETTINGS.loadFromFile();
APP_STATE.loadFromFile();
if (APP_STATE.openEpubPath.empty()) {
onGoHome();
} else {
onGoToReader(APP_STATE.openEpubPath);
2025-12-03 22:00:29 +11:00
}
2025-12-05 17:47:23 +11:00
// Ensure we're not still holding the power button before leaving setup
2025-12-06 12:35:41 +11:00
waitForPowerRelease();
2025-12-03 22:00:29 +11:00
}
void loop() {
2025-12-06 12:35:41 +11:00
delay(10);
2025-12-03 22:00:29 +11:00
2025-12-06 12:56:39 +11:00
static unsigned long lastMemPrint = 0;
2025-12-08 22:39:23 +11:00
if (Serial && millis() - lastMemPrint >= 10000) {
Serial.printf("[%lu] [MEM] Free: %d bytes, Total: %d bytes, Min Free: %d bytes\n", millis(), ESP.getFreeHeap(),
2025-12-06 12:56:39 +11:00
ESP.getHeapSize(), ESP.getMinFreeHeap());
lastMemPrint = millis();
}
2025-12-06 12:35:41 +11:00
inputManager.update();
// Check for any user activity (button press or release)
static unsigned long lastActivityTime = millis();
if (inputManager.wasAnyPressed() || inputManager.wasAnyReleased()) {
lastActivityTime = millis(); // Reset inactivity timer
}
if (millis() - lastActivityTime >= AUTO_SLEEP_TIMEOUT_MS) {
Serial.printf("[%lu] [SLP] Auto-sleep triggered after %lu ms of inactivity\n", millis(), AUTO_SLEEP_TIMEOUT_MS);
enterDeepSleep();
// This should never be hit as `enterDeepSleep` calls esp_deep_sleep_start
return;
}
if (inputManager.wasReleased(InputManager::BTN_POWER) && inputManager.getHeldTime() > POWER_BUTTON_SLEEP_MS) {
2025-12-03 22:00:29 +11:00
enterDeepSleep();
// This should never be hit as `enterDeepSleep` calls esp_deep_sleep_start
return;
}
if (currentActivity) {
currentActivity->loop();
2025-12-03 22:00:29 +11:00
}
}