docs/ARCHITECTURE

Nebble Architecture

This document describes the internal architecture of the Nebble wrapper and how it enables developing Pebble smartwatch applications using Nim.

1. Architectural Overview

Nebble is built on a two-layer design that bridges the gap between the low-level Pebble C SDK and idiomatic Nim.

Layer 1: Low-Level FFI (nebble/ffi)

This layer provides a 1:1 mapping of the Pebble C API.

Layer 2: High-Level Idiomatic API (nebble/*)

This layer wraps the FFI in Nim-friendly abstractions.

---

2. Memory Management (The Managed System)

The most significant technical challenge in wrapping the Pebble SDK is its manual memory management (_create and _destroy pairs) in a 24KB-256KB RAM environment.

ARC-Based Handles

Nebble uses Nim's ARC (Automatic Reference Counting) memory management. We implement "Managed Handles" using an ownership-aware object model:

  1. Internal Structure: Handles (e.g., TextLayerHandle) are defined as DefineUniqueHandle types containing:
    • pRaw: The underlying C pointer to the SDK resource.
    • ownership: A HandleOwnership enum (hoOwned, hoParented, hoUnowned).
  2. Destructors: We implement the =destroy hook. Nim calls this automatically when a handle goes out of scope or is reassigned. The destructor only calls the C _destroy function if ownership == hoOwned.
  3. Move Semantics: Copying is disabled via {.error.} on =copy, ensuring unique ownership of UI resources. Reassignment performs a =sink which transfers ownership and invalidates the source.

Hierarchy & Ownership Lifecycle

Pebble's UI system is hierarchical: destroying a parent Layer automatically destroys its children. To prevent double-free errors, Nebble handles automatically transition through ownership states:

When a layer is removed from its parent, the handle automatically transitions back to hoOwned, ensuring it isn't leaked.

---

3. The Build Pipeline

Nebble doesn't just provide a library; it provides an intelligent compilation pipeline via the nebble CLI.

  1. Nim Phase:
    • Watch: nim c --os:any --cpu:arm --mm:arc --compileOnly ... compiles Nim to C source files.
    • Phone: nim js -d:release ... compiles src/pkjs.nim to JavaScript.
    • Cross-compilation flags are used to target the ARM Cortex-M architecture without a standard OS.
  2. Bridge Phase:
    • The CLI generates a unified package.json (replacing the legacy appinfo.json) that lists all target platforms.
    • Nim-generated C files are segregated into platform-specific subdirectories (e.g., src/c/aplite/, src/c/basalt/) to allow for distinct platform-specific optimizations and defines.
  3. Pebble Phase: pebble build
    • The Pebble SDK's waf build system takes over.
    • Because all target platforms are defined in one manifest and sources are segregated, a single pebble build command produces a unified .pbw bundle.
    • This bundle contains the binaries and resources for all requested hardware platforms.

---

4. Metaprogramming & Scaffolding

The nebbleApp Macro

To remove C boilerplate (entry point, window creation, event loop), Nebble uses a powerful macro that generates the entire app skeleton. Key features include:

Heap-Free Strings (FixedString)

Since TextLayer doesn't copy the string it displays, and heap allocations are expensive/dangerous on Pebble, Nebble provides a FixedString[N] type and an f macro for safe, stack-based formatting:

  1. Zero Allocation: All string operations happen within a pre-allocated stack buffer.
  2. Safety: The f macro provides fmt-like convenience while ensuring bounds-checking and null-termination.
  3. C-string Access: Use the .cstr template to safely convert FixedString to cstring. The template operates at the call site (no temporary copy), ensuring the pointer remains valid.
  4. Storage Integration: The persistent storage API (storage.read) natively supports FixedString for safe data loading.

Important: Pebble's text_layer_set_text stores the pointer (doesn't copy the string), so the FixedString must remain alive as long as the text is displayed. Always use global or static FixedString variables for persistent text.

---

5. Platform Abstraction

Pebble hardware varies significantly (B&W vs. Color, Rectangular vs. Round). Nebble handles this using Nim's when statement:

# Type-safe platform detection
when declared(GColorRed):
  # This block is only compiled for Basalt, Chalk, Emery
  textLayer.textColor = GColorRed

Because when is evaluated at compile-time, features not supported on a platform (like the Microphone or Heart Rate Monitor) result in zero code weight on platforms that don't have them (like Aplite).

---

6. Technical Limitations