System (pocketmage_sys, pocketmage_globals)

System API

The system module owns the board bring-up, the CPU clock, deep sleep, the power button, and the shared globals (battState, allLines, CurrentAppState, the SPI buses, and NVS prefs). Apps use it for power management and boot-time configuration; the OS uses it for full bring-up.

Access

#include <pocketmage.h>

Shared globals

pocketmage_globals defines the state both the OS and apps link against.

SPI and filesystem

extern SPIClass* vspi;    // the global SPI on SPI_SCK/SPI_MOSI
extern SPIClass* hspi;
extern fs::FS*   global_fs;  // active filesystem (SD_MMC or SD)

PocketMage_INIT wires vspi = &SPI and pins the pins documented in configuration.

NVS

extern Preferences prefs;   // "PocketMage" namespace

The shared Preferences handle. pocketmage::loadSettings() reads the persistent toggles into the config.h globals (TIMEOUT, SAVE_POWER, FAST_REFRESH, OLED_BRIGHTNESS, the UI language, …); an app that reads preferences uses prefs directly with the same namespace.

App state

enum AppState { HOME, TXT, FILEWIZ, USB_APP, COMM, SETTINGS, TASKS,
                CALENDAR, JOURNAL, LEXICON, APPLOADER, TERMINAL,
                ONBOARDING, ELFAPP };
extern AppState CurrentAppState;

The OS’s app dispatch enum. ELFAPP is the state set while a loaded ELF app is running; the OS restores HOME when it exits.

Keyboard state

enum KBState { NORMAL, SHIFT, FUNC, FN_SHIFT };

The keyboard modifier state, mirrored by KB().setKeyboardState().

pocketmage:: namespace

namespace pocketmage {
  void loadSettings();          // load NVS settings into the config.h globals
  void recoverFromCrash();      // reset app state after a panic/watchdog
  void checkRTCPowerLoss();     // baseline the RTC when it has lost power
}
  • loadSettings() reads every persistent toggle from prefs and sets the UI language. Apps call it during APP_INIT (the OS reaches it via PocketMage_INIT on the host path).
  • recoverFromCrash() checks the ESP reset reason and, on panic or watchdog reset, resets CurrentAppState to HOME and clears the editing-file pointer so the device boots clean.
  • checkRTCPowerLoss() re-baselines the RTC when its power was lost.

Battery helper

inline float getBatteryVoltage() {
  return (analogRead(BAT_SENS) * (3.3 / 4095.0) * 2) + 0.2;
}

battState (0 through 5) indexes the battery bitmaps used by the OLED status line (oled, assets).

CPU speed

void pocketmage::setCpuSpeed(int newFreq);
void pocketmage::maintainDebugConsoleCpuSpeed();

Clamps to the valid S3 frequencies {240, 160, 80, 40, 20, 10} and returns early if the CPU is already there. Requests that would drop the clock below 240 MHz are ignored while WiFi is not Off or USB Serial/JTAG is connected to a host, since both share the PLL-derived clocks the displays need. POWER_SAVE_FREQ (40) is the OS’s idle speed; sysMessage and the WiFi service bump back to 240 when they need it.

maintainDebugConsoleCpuSpeed() restores the 240 MHz floor if USB Serial/JTAG was attached after the CPU had already dropped. The OS calls it from checkIdleSleep(), so plugging in a data cable recovers the display speed without a reboot. It does nothing when no host is attached or the CPU is already fast enough.

Scope guard

class pocketmage::ScopedCpuBoost {
public:
  ScopedCpuBoost();            // raises to 240 MHz if not already there
  ~ScopedCpuBoost();           // restores the previous frequency
};

Non-copyable RAII. Usable as a stack local around a latency-critical section.

Deep sleep

void pocketmage::deepSleep(bool alternateScreenSaver = false);

Full power-down sequence:

Before powering down either display, the OS calls checked audio and USB keyboard shutdown. If either cannot finish, it retains live resources, shows the existing localized USB failure message, and returns without entering sleep.

  1. Power-saves the OLED.
  2. Stops the einkHandlerTask under the panel lock, so an in-flight refresh completes.
  3. Plays Jingles::Shutdown.
  4. Draws a screensaver (custom /assets/backgrounds/*.bin, else a random built-in) at rotation 3, or the alternate screensaver when asked. SAVE_POWER drops the CPU to POWER_SAVE_FREQ for the draw.
  5. Clears the buffer, hibernates the panel, unlocks it.
  6. Persists CurrentAppState, the editing file, and clears the seamless reboot flag.
  7. On production hardware, isolates the peripheral pins with setLoadSwitch(false) (GPIO hold + gpio_deep_sleep_hold_en).
  8. esp_deep_sleep_start().

Wake is on the KB_IRQ falling edge (configured in PocketMage_INIT). Resume re-runs PocketMage_INIT, which clears the pin holds and re-inits peripherals.

Power button

void IRAM_ATTR pocketmage::PWR_BTN_irq();
extern volatile bool PWR_BTN_event;

The FALLING-edge interrupt service routine sets PWR_BTN_event. The OS consumes it for wake/shutdown handling; holding PWR_BTN for 3 seconds while awake raises the hard-reset-to-home path (resetRequested, see the hardReset task). SDActive, mscEnabled, and sinkEnabled are the status-line indicators apps toggle for long I/O.

Bring-up

void PocketMage_INIT();

The one-call bring-up: release GPIO holds, enable the load switch (production), read the seamless reboot flag, FontEngine::init(), Serial, Wire (plus Wire1 on production), the global SPI, the keyboard wake pin, setupOled(), setupKB(), setupEink(), setupSD(), power-button interrupt + MP2722 power init, the hard-reset task, the power-save clock, setupTouch(), setupClock(), the random seed, settings load, crash recovery, setupBZ() + startup jingle, the WiFi service (host only), key flush, then the app hook APP_INIT() (app builds). Apps do not call it; it runs as part of the OS or the app entry shim.

Text line buffer

extern volatile bool resetRequested;
extern volatile bool newLineAdded;
extern std::vector<String> allLines;   // the text editor's line list
extern bool noTimeout;
extern void setLoadSwitch(bool state);

allLines is the shared text buffer the OS’s TXT flow and vectorToString / stringToVector operate on; noTimeout suppresses the auto-sleep timeout. setLoadSwitch(false) is the production deep-sleep pin isolation path described above.

Example

#include <pocketmage.h>

void onUserExit() {
  {
    pocketmage::ScopedCpuBoost boost;   // 240 MHz for the save
    // write the document...
  }  // back to the previous speed
  pocketmage::deepSleep();              // and go to sleep
}

C ABI (pm_*)

External ELF apps call these C wrappers and host primitives rather than the C++ API above; the pm_* wrappers are generated by gen_app_api.py and declared in pm_sdk_app.h (pulled in via pm_app_api.h). See the function index and the projection model for the general rules.

uint32_t pm_app_abi(void);
const char* pm_host_sdk_version(void);
extern const char pocketmage_sdk_version[];
void delay(unsigned long ms);
  • pm_app_abi() returns the PM_APP_API_ABI value the running host was built with. An app compares it against its own PM_APP_API_ABI at startup and refuses to run on a mismatch.
  • pm_host_sdk_version() returns the SDK version string the host was built with. The return points into the rotating string pool; copy it if it must outlive four further string-returning calls.
  • pocketmage_sdk_version is the SDK version string exported as a bare const char[] symbol (not a function), declared directly rather than through pocketmage_globals.h so the export table can take its address. pm_host_sdk_version() returns this value.
  • delay(ms) is the host delay primitive exported from the firmware core, not a generated pm_* wrapper. It blocks the calling task for ms milliseconds.