# ftservo/ftservo_arduino architecture

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

- Interactive diagram: https://gitdiagram.com/ftservo/ftservo_arduino
- Repository: https://github.com/ftservo/ftservo_arduino

## Overview

##### Project Type and Purpose
- Project type: Embedded‐library for Arduino/ESP32.
- Purpose: Provide a uniform C++ API to command three series of FEETECH BUS servos (HLSCL, SCSCL, SMS_STS) via a serial bus.
- Key features: layered architecture (hardware interface, communication, application), example sketches illustrating usage.

##### File Structure
- Top‐level:
– examples/ → Arduino sketches grouped by servo series (HLSCL, SCSCL, SMS_STS).
– src/ → core library modules.
– library.properties, README.md, LICENSE.
- src/ layers:
– Hardware Interface: SCSerial.{h,cpp}
– Communication Layer: SCS.{h,cpp}
– Instruction Definitions: INST.h
– Application Layer: HLSCL.{h,cpp}, SCSCL.{h,cpp}, SMS_STS.{h,cpp}

##### README Insights
- It explicitly calls out three layers:
– Hardware interface (serial drivers)
– Communication (packet formation/parsing)
– Application (series‐specific high‐level APIs)
- Examples demonstrate end‐to‐end usage: sketch → library → servo.

##### Main Components
a. External:
– Arduino IDE/Sketch (user code)
– FEETECH BUS Servo devices on the physical serial bus
– Underlying MCU hardware (Arduino board or ESP32)
b. Library Layers (inside MCU):
1. Application Layer
- HLSCL API module
- SCSCL API module
- SMS_STS API module
2. Instruction Definitions
- INST.h (common command codes, memory tables)
3. Communication Layer
- SCS module: packet builder/parser
4. Hardware Interface Layer
- SCSerial: low‐level UART control, TX/RX

##### Relationships and Data Flow
- Sketch calls functions in the Application Layer (e.g., SCSCL.writePos(...)).
- Application Layer consults INST definitions for opcode, parameters.
- Data passed to Communication Layer (SCS) to frame packets.
- Framed packet passed to SCSerial to send via UART pins.
- Physical serial bus carries bytes to servo devices.
- Servo replies flow back: UART → SCSerial → SCS → Application → sketch callback.

##### Architectural Patterns & Principles
- Layered architecture (clear dependency direction: App → INST → Comm → HW).
- Separation of concerns: hardware details isolated from high‐level API.
- Extensibility: new servo series can add another Application module that reuses INST + Comm + HW layers.

##### Technologies & Frameworks
- C++17 (Arduino core)
- Arduino IDE / Arduino Library Manager
- UART serial protocol over FEETECH BUS

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Components
    Sketch["Arduino Sketch"]:::external
    Servo["FEETECH BUS Servo Device(s)"]:::external

    %% MCU Boundary
    subgraph "MCU Boundary"
        direction TB

        %% Application Layer
        subgraph "Application Layer"
            direction TB
            HLSCL["HLSCL API Module\n(src/HLSCL.h/.cpp)"]:::app
            SCSCL["SCSCL API Module\n(src/SCSCL.h/.cpp)"]:::app
            SMSSTS["SMS_STS API Module\n(src/SMS_STS.h/.cpp)"]:::app
        end

        %% Instruction Definitions
        INST["INST Definitions\n(src/INST.h)"]:::instr

        %% Communication Layer
        SCS["Packet Builder/Parser\n(src/SCS.h/.cpp)"]:::comm

        %% Hardware Interface Layer
        SCSerial["UART Driver\n(src/SCSerial.h/.cpp)"]:::hw
    end

    %% Physical Bus
    Bus["Physical Serial Bus\n(UART TX/RX)"]:::bus

    %% Relationships
    Sketch -->|"function calls"| HLSCL
    Sketch -->|"function calls"| SCSCL
    Sketch -->|"function calls"| SMSSTS

    HLSCL -->|"consult opcode"| INST
    SCSCL -->|"consult opcode"| INST
    SMSSTS -->|"consult opcode"| INST

    HLSCL -->|"packet data"| SCS
    SCSCL -->|"packet data"| SCS
    SMSSTS -->|"packet data"| SCS

    SCS -->|"UART TX data"| SCSerial
    SCSerial -->|"TX bytes"| Bus
    Bus -->|"bytes to servo"| Servo

    Servo -->|"RX bytes"| Bus
    Bus -->|"UART RX data"| SCSerial
    SCSerial -->|"parsed packet"| SCS
    SCS -->|"callback data"| HLSCL
    SCS -->|"callback data"| SCSCL
    SCS -->|"callback data"| SMSSTS
    HLSCL -->|"sketch callback"| Sketch
    SCSCL -->|"sketch callback"| Sketch
    SMSSTS -->|"sketch callback"| Sketch

    %% Example Sketches
    subgraph "Example Sketches"
        HLSCL_Example["HLSCL Series Examples"]:::example
        SCSCL_Example["SCSCL Series Examples"]:::example
        SMSSTS_Example["SMS_STS Series Examples"]:::example
    end

    HLSCL_Example -->|uses| HLSCL
    SCSCL_Example -->|uses| SCSCL
    SMSSTS_Example -->|uses| SMSSTS

    %% Library Metadata
    Metadata["Library Metadata\n(library.properties,\nREADME.md, LICENSE)"]:::meta

    %% Click Events
    click HLSCL "https://github.com/ftservo/ftservo_arduino/blob/main/src/HLSCL.h"
    click HLSCL "https://github.com/ftservo/ftservo_arduino/blob/main/src/HLSCL.cpp"
    click SCSCL "https://github.com/ftservo/ftservo_arduino/blob/main/src/SCSCL.h"
    click SCSCL "https://github.com/ftservo/ftservo_arduino/blob/main/src/SCSCL.cpp"
    click SMSSTS "https://github.com/ftservo/ftservo_arduino/blob/main/src/SMS_STS.h"
    click SMSSTS "https://github.com/ftservo/ftservo_arduino/blob/main/src/SMS_STS.cpp"
    click INST "https://github.com/ftservo/ftservo_arduino/blob/main/src/INST.h"
    click SCS "https://github.com/ftservo/ftservo_arduino/blob/main/src/SCS.h"
    click SCS "https://github.com/ftservo/ftservo_arduino/blob/main/src/SCS.cpp"
    click SCSerial "https://github.com/ftservo/ftservo_arduino/blob/main/src/SCSerial.h"
    click SCSerial "https://github.com/ftservo/ftservo_arduino/blob/main/src/SCSerial.cpp"
    click HLSCL_Example "https://github.com/ftservo/ftservo_arduino/blob/main/examples/HLSCL/**/*.ino"
    click SCSCL_Example "https://github.com/ftservo/ftservo_arduino/blob/main/examples/SCSCL/**/*.ino"
    click SMSSTS_Example "https://github.com/ftservo/ftservo_arduino/blob/main/examples/SMS_STS/**/*.ino"
    click Metadata "https://github.com/ftservo/ftservo_arduino/blob/main/library.properties"
    click Metadata "https://github.com/ftservo/ftservo_arduino/blob/main/README.md"
    click Metadata "https://github.com/ftservo/ftservo_arduino/tree/main/LICENSE"

    %% Styles
    classDef external fill:#D5C9F3,stroke:#4B0082,color:#000
    classDef app fill:#C8E1FF,stroke:#00529B,color:#000
    classDef instr fill:#E0E0E0,stroke:#666666,color:#000
    classDef comm fill:#D4F5D4,stroke:#2E7D32,color:#000
    classDef hw fill:#FFE0B2,stroke:#EF6C00,color:#000
    classDef bus fill:#FFF3E0,stroke:#FF8F00,color:#000
    classDef example fill:#F0F4C3,stroke:#827717,color:#000
    classDef meta fill:#F5F5F5,stroke:#9E9E9E,color:#000
```
