# ironmanfpv/fitness-odometer-for-monocle architecture

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

- Interactive diagram: https://gitdiagram.com/ironmanfpv/fitness-odometer-for-monocle
- Repository: https://github.com/ironmanfpv/fitness-odometer-for-monocle

## Overview

##### Project type and purpose
- This is a single-page, front-end proof-of-concept web application (“Fitness Odometer for Monocle”) that runs entirely in the browser.
- It uses the Web Geolocation API to collect latitude/longitude points, computes distance traveled, renders a route overlay in the UI, and (optionally) streams distance updates over Bluetooth Low Energy (BLE) to a Monocle device.

##### File structure
- Top-level:
- index.html – entry point and skeleton of the UI
- css/ – style sheets (button.css, overlay.css, tracker.css)
- js/ – JavaScript modules:
– config.js (BLE service/characteristic UUIDs, config constants)
– connect.js (BLE device discovery & connection logic)
– distance.js (haversine or similar distance‐calculation functions)
– overlay.js (canvas or SVG route‐drawing code)
– tracker.js (geolocation polling, start/stop controls, event orchestration)
– senddist.js (packaging & transmitting distance over BLE)
- No backend code or server-side components. All logic executes locally in the browser.

##### Key insights from README
- Relies on Mozilla Web Geolocation API and Web Bluetooth API.
- UI consists of a “Slide to start/stop” button, a map or route overlay, and a “Connect”/“Transmit” button for Monocle.
- No data is sent to any web server; all location and distance processing is client-side.

##### Main components
a. Browser UI Layer
- Presentation controls: Start/Stop slider, Connect!, Transmit button
- Overlay canvas or map container
b. Geolocation Service
- Browser’s navigator.geolocation API
- Managed by tracker.js
c. Data Processing Layer
- distance.js calculates incremental and cumulative distance
- overlay.js renders path segments onto the map/canvas
d. BLE Service Layer
- .js handles device scan, connect, and characteristic discovery
- senddist.js formats distance data and writes to BLE characteristic
e. External Device
- Monocle BLE device (with custom UUIDs defined in config.js)

##### Relationships and interactions
1. User clicks “Slide to start” → tracker.js subscribes to geolocation.watchPosition
2. Geolocation Service returns lat/lon → distance.js computes delta distance
3. distance.js emits new distance → overlay.js draws the new segment on UI
4. User clicks “Connect!” → connect.js invokes navigator.bluetooth.requestDevice
5. After BLE connection, user clicks “Transmit” → senddist.js writes current distance to Monocle over BLE
6. User clicks “Slide to stop” → tracker.js clears watchPosition and stops updates

##### Architectural patterns and design principles
- Modular JavaScript with Single Responsibility modules
- Event-driven flow: modules emit and consume custom events (e.g., “positionUpdate”, “distanceComputed”)
- Separation of concerns: UI controls vs. data services vs. external device integration

## Diagram (Mermaid)

```mermaid
flowchart TB
    title["Fitness Odometer for Monocle – Client-Side Architecture"]

    subgraph "Browser UI Layer"
        direction TB
        I["index.html"]:::ui
        Slider["Start/Stop Slider"]:::ui
        ConnectBtn["Connect! Button"]:::ui
        TransmitBtn["Transmit Button"]:::ui
        OverlayCanvas["Overlay Canvas"]:::ui
    end

    subgraph "Application Logic Layer"
        direction TB
        Tracker["tracker.js"]:::logic
        Distance["distance.js"]:::logic
        OverlayMod["overlay.js"]:::logic
        ConnectMod["connect.js"]:::logic
        SendDist["senddist.js"]:::logic
        ConfigMod["config.js"]:::logic
    end

    subgraph "Browser APIs"
        direction TB
        GeoAPI["navigator.geolocation API"]:::api
        BLEAPI["navigator.bluetooth API"]:::api
    end

    subgraph "External Device"
        direction TB
        Monocle["Monocle BLE Device"]:::device
    end

    %% User interactions and data flow
    Slider -->|"startTracking()"| Tracker
    Tracker -->|"watchPosition()"| GeoAPI
    GeoAPI -->|"positionUpdate"| Tracker
    Tracker -->|"positionUpdate event"| Distance
    Distance -->|"distanceComputed"| OverlayMod
    OverlayMod -->|"renderSegment"| OverlayCanvas

    ConnectBtn -->|"requestDevice()"| ConnectMod
    ConnectMod -->|"gatt.connect()"| BLEAPI
    TransmitBtn -->|"send()"| SendDist
    SendDist -->|"writeValue()"| BLEAPI
    SendDist -->|"uses UUIDs"| ConfigMod
    ConfigMod -.-> ConnectMod
    BLEAPI -->|"data"| Monocle

    %% Click events
    click I "https://github.com/ironmanfpv/fitness-odometer-for-monocle/blob/main//index.html"
    click Slider "https://github.com/ironmanfpv/fitness-odometer-for-monocle/blob/main/css/button.css"
    click OverlayCanvas "https://github.com/ironmanfpv/fitness-odometer-for-monocle/blob/main/css/overlay.css"
    click ConnectBtn "https://github.com/ironmanfpv/fitness-odometer-for-monocle/blob/main/css/tracker.css"
    click Tracker "https://github.com/ironmanfpv/fitness-odometer-for-monocle/blob/main/js/tracker.js"
    click Distance "https://github.com/ironmanfpv/fitness-odometer-for-monocle/blob/main/js/distance.js"
    click OverlayMod "https://github.com/ironmanfpv/fitness-odometer-for-monocle/blob/main/js/overlay.js"
    click ConnectMod "https://github.com/ironmanfpv/fitness-odometer-for-monocle/blob/main/js/connect.js"
    click SendDist "https://github.com/ironmanfpv/fitness-odometer-for-monocle/blob/main/js/senddist.js"
    click ConfigMod "https://github.com/ironmanfpv/fitness-odometer-for-monocle/blob/main/js/config.js"

    %% Styles
    classDef ui fill:#ADD8E6,stroke:#000,stroke-width:1px
    classDef logic fill:#FFFF99,stroke:#000,stroke-width:1px
    classDef api fill:#90EE90,stroke:#000,stroke-width:1px
    classDef device fill:#D3D3D3,stroke:#000,stroke-width:1px
```
