# App Class Documentation

## Overview
The App class is the main controller for an injection molding system, managing the plunger movement, speed control, and safety features through a VFD (Variable Frequency Drive).

## Class Diagram

```mermaid
classDiagram
    class App {
        +DirectionSwitch* dirSwitch
        +VFD* vfd
        +OperationModeSwitch* opModeSwitch
        +MotorLoad* mLoad
        +ProximitySensor* plungerHome
        -short plungeState
        -short _state
        -short _error
        +setup() short
        +loop() short
        +plunge(short value) short
        +home(short value) short
        +stop(short value) short
        +loopManual() ushort
        +setPlungeState(short) short
    }

    class Addon {
        <<abstract>>
        +setup()
        +loop()
        +debug()
    }

    class DirectionSwitch {
        +loop() uchar
        +clear()
    }

    class VFD {
        +fwd(bool)
        +rev(bool)
        +stop()
    }

    class MotorLoad {
        +plunging() uchar
        +idle() uchar
        +jammed() uchar
        +highLoad() uchar
    }

    App --|> Addon
    App o-- DirectionSwitch
    App o-- VFD
    App o-- MotorLoad
```

## Sequence Diagram - Normal Operation

```mermaid
sequenceDiagram
    participant U as User
    participant A as App
    participant D as DirectionSwitch
    participant V as VFD
    participant M as MotorLoad
    participant P as ProximitySensor

    U->>A: Start System
    A->>A: setup()
    A->>D: clear()
    loop Main Control Loop
        A->>D: loop()
        A->>M: check load status
        alt Direction Forward
            A->>V: fwd(true)
        else Direction Reverse
            A->>V: rev(true)
        else Stop
            A->>V: stop()
        end
        A->>P: check position
        alt Overload Detected
            M->>A: jammed()
            A->>V: stop()
        end
    end
```

## State Diagram

```mermaid
stateDiagram-v2
    [*] --> IDLE
    IDLE --> PLUNGING: Forward Command
    IDLE --> HOMING: Reverse Command
    PLUNGING --> JAMMED: Motor Overload
    HOMING --> JAMMED: Motor Overload
    JAMMED --> IDLE: Reset/Clear
    PLUNGING --> IDLE: Stop Command
    HOMING --> IDLE: Stop Command
    PLUNGING --> AUTO: Auto Mode
    HOMING --> AUTO: Auto Mode
    AUTO --> IDLE: Emergency Stop
```

## Key Components

1. **DirectionSwitch**
   - Manages plunger direction control
   - Provides forward/reverse/stop signals

2. **VFD (Variable Frequency Drive)**
   - Controls motor speed and direction
   - Handles soft start/stop
   - Provides overload protection

3. **MotorLoad**
   - Monitors motor current/load
   - Detects jamming conditions
   - Provides feedback for adaptive control

4. **ProximitySensor**
   - Detects plunger home position
   - Provides safety limits

## Operation Modes

### Manual Mode
- Direct control through direction switch
- Immediate response to user input
- Full speed control

### Auto Mode
- Automated cycle execution
- Load-adaptive speed control
- Safety monitoring and emergency stop

## Error Handling

1. **Overload Protection**
   - Monitors motor load continuously
   - Stops operation if load exceeds limits
   - Requires manual reset after jam

2. **Position Limits**
   - Prevents over-travel
   - Ensures safe homing operation
   - Monitors end positions

## Implementation Notes

- Uses Arduino framework
- Time-based state management
- Non-blocking operation design
- Interrupt-driven emergency stops
- Configurable through feature flags