# denk0403/algedraw-calculator architecture

> Architecture diagram of the GitHub repository denk0403/algedraw-calculator, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/denk0403/algedraw-calculator
- Repository: https://github.com/denk0403/algedraw-calculator

## Overview

##### Project Type and Purpose
- This is a Fitbit OS application (“AlgeDraw Calculator”) that runs on the watch (app/) and leverages a companion phone app (companion/) for advanced AI-based digit recognition. There’s also a settings page (settings/) and UI definitions (resources/) for the watch.

##### Main Components
a. Watch App (app/)
- index.js: orchestrates UI, user input, calculation engine, local session stack.
- resources/: GUI definitions (.gui), styling (.css), widget layouts.
b. Companion App (companion/)
- index.js: receives raw drawing data via Fitbit messaging, runs the custom AI model to recognize handwritten digits, sends results back to the watch.
c. Settings Page (settings/)
- index.jsx: React-based settings interface on the companion side or Fitbit settings screen, used to configure constants, units (radians/degrees), etc.
d. External Dependencies & Services
- Fitbit OS Messaging API (watch ↔ phone over Bluetooth)
- Custom AI inference library (e.g., TensorFlow.js or a lightweight JS model on the companion)
- Fitbit SDK for UI, storage, device APIs

##### Component Interactions & Data Flow
1. User draws a digit on the watch UI canvas (resources/widgets.gui → app/index.js listens).
2. The watch app packages the stroke data and sends it to the companion via the Messaging API.
3. The companion receives strokes, runs the AI model on the phone, obtains a digit prediction.
4. The companion sends the recognized digit back to the watch.
5. The watch app’s calculation engine (app/index.js) integrates the new digit into the current expression or stack.
6. The UI updates to reflect the new input or computed result.
7. All inputs/results are stored in the app’s session stack (in-memory or simple key-value storage) until the session ends.

##### Architectural Patterns & Design Principles
- Separation of Concerns: UI (resources + app/index.js), logic & storage (app), AI inference (companion).
- Message-Driven RPC: the watch and companion communicate via asynchronous messages.
- MVC-like layering on the watch:
- View: GUI definitions in resources/
- Controller: app/index.js handles events & updates
- Model: in-memory stack + constants
- Settings as a distinct module: React/JSX-based page (settings/index.jsx) isolates configuration from runtime logic.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Watch App Subgraph
    subgraph "Watch App"
        direction TB
        UICanvas["UI Canvas\n(resources/*.gui)"]:::watch
        Controller["Controller\n(app/index.js)"]:::watch
        CalcEngine["Calculation Engine\n(app/index.js)"]:::watch
        SessionStack[(Session Stack)]:::watch
    end

    %% Companion App Subgraph
    subgraph "Companion App"
        direction TB
        MessagingHandler["Messaging Handler\n(companion/index.js)"]:::comp
        AIInference["AI Inference\n(TensorFlow.js)"]:::comp
        CompanionNotes["Companion Docs\n(companion/note.txt)"]:::comp
    end

    %% Settings Interface Subgraph
    subgraph "Settings Interface"
        direction TB
        SettingsPage["Settings Interface\n(React)\n(settings/index.jsx)"]:::comp
    end

    %% External Services
    MessagingAPI{{"Fitbit Messaging API\n(Bluetooth)"}}:::external
    FitbitSDK["Fitbit SDK"]:::external
    TensorFlowJS["TensorFlow.js Library"]:::external
    PackageJSON["package.json"]:::external
    README["README.md"]:::external
    LICENSE["LICENSE"]:::external

    %% Data Flow
    UICanvas -->|strokeData| MessagingAPI
    MessagingAPI -->|strokeData| MessagingHandler
    MessagingHandler --> AIInference
    AIInference -->|digitPrediction| MessagingAPI
    MessagingAPI -->|digitPrediction| Controller
    Controller --> CalcEngine
    CalcEngine --> SessionStack
    Controller --> UICanvas

    %% Settings Flow
    SettingsPage -->|configures| Controller
    SettingsPage -->|configures| SessionStack

    %% Dependencies
    Controller --> FitbitSDK
    MessagingHandler --> TensorFlowJS

    %% Project Files
    PackageJSON
    README
    LICENSE

    %% Legend
    subgraph "Legend"
        direction TB
        W["Watch-side Components"]:::watch
        C["Companion-side Components"]:::comp
        E["External Services/Libraries"]:::external
        cyl[(Cylinder = Storage)] 
        cloud{{Cloud = Messaging API}}
    end

    %% Click Events
    click Controller "https://github.com/denk0403/algedraw-calculator/blob/master/app/index.js"
    click UICanvas "https://github.com/denk0403/algedraw-calculator/blob/master/resources/index.gui"
    click UICanvas "https://github.com/denk0403/algedraw-calculator/blob/master/resources/widgets.gui"
    click SessionStack "https://github.com/denk0403/algedraw-calculator/blob/master/app/index.js"
    click MessagingHandler "https://github.com/denk0403/algedraw-calculator/blob/master/companion/index.js"
    click AIInference "https://github.com/denk0403/algedraw-calculator/blob/master/companion/index.js"
    click CompanionNotes "https://github.com/denk0403/algedraw-calculator/blob/master/companion/note.txt"
    click SettingsPage "https://github.com/denk0403/algedraw-calculator/blob/master/settings/index.jsx"
    click PackageJSON "https://github.com/denk0403/algedraw-calculator/blob/master/package.json"
    click README "https://github.com/denk0403/algedraw-calculator/blob/master/README.md"
    click LICENSE "https://github.com/denk0403/algedraw-calculator/tree/master/LICENSE"

    %% Styles
    classDef watch fill:#D0E8FF,stroke:#333,stroke-width:1px
    classDef comp fill:#D0FFD0,stroke:#333,stroke-width:1px
    classDef external fill:#FFE0B2,stroke:#333,stroke-width:1px
```
