# CODESYS Source Tree (Redesign / PLC_App)

Structured folder and source files for the home automation redesign: lighting (fb_light, fb_room) and water boiler (fb_boiler), with Option C relay feedback.

## Folder Structure

```
codesys/src/
├── DUT/                    # Data Unit Types
│   ├── struct_room_cmds.typ
│   ├── struct_room_outs.typ
│   ├── struct_boiler_cmd.typ
│   └── struct_boiler_status.typ
├── GVL/                    # Global Variable Lists
│   ├── GVL_IO.gvl          # EtherCAT_RelayFeedback, EtherCAT_Outputs (EL2809)
│   └── GVL_NVL_placeholders.gvl  # NVL_In, NVL_Out, DI_Emergency_Stop (for Node-RED)
├── NVL/                    # Network Variables (Node-RED)
│   ├── README.md           # NVL_Out / NVL_In config, CODESYS + Node-RED setup
│   └── nodered-payload.md  # Binary payload layout + parse/build examples
├── POUs/                   # Program Organization Units
│   ├── fb_light.st         # Single light FB
│   ├── fb_room.st          # Room (6 lights) FB
│   ├── fb_boiler.st        # Boiler FB
│   └── PLC_App.st          # Main program
└── README.md
```

## Contents

| Item | Description |
|------|-------------|
| **DUT** | `struct_room_cmds`, `struct_room_outs`, `struct_boiler_cmd`, `struct_boiler_status` |
| **GVL_IO** | `EtherCAT_RelayFeedback` (Option C), `EtherCAT_Outputs` (Ch0..Ch15 for EL2809) |
| **GVL_NVL_placeholders** | `NVL_In`, `NVL_Out` (struct stubs), `DI_Emergency_Stop`; replace with real NVL config if needed |
| **fb_light** | One light: HA ON/OFF + Zigbee toggle, relay_status in → light_output/light_status out |
| **fb_room** | Six fb_light instances + ha_all_on / ha_all_off overwrite |
| **fb_boiler** | ON/OFF with max-on-time and emergency stop |
| **PLC_App** | Calls all rooms and boiler; copies room outputs to EtherCAT_RelayFeedback and EtherCAT_Outputs |

## How to Use in CODESYS

1. **Create or open a CODESYS project** (e.g. Control for Raspberry Pi).
2. **Add DUTs**: Create new DUTs under the device or application and paste the content of each `.typ` file (or use **Add Object → DUT** and paste).
3. **Add GVLs**: Create a GVL (e.g. `GVL_IO`), paste `GVL_IO.gvl`. Create/configure NVL_In and NVL_Out for your UDP/network variables and ensure their structure matches the design (struct_room_cmds per room, struct_boiler_cmd; struct_room_outs per room, struct_boiler_status).
4. **Add POUs**: Create POUs (Function Block / Program), set language to Structured Text, and paste the corresponding `.st` file. Order: add `fb_light`, then `fb_room`, then `fb_boiler`, then `PLC_App`.
5. **Task**: Call `PLC_App` from your main task (e.g. EtherCAT_Task or MainTask).
6. **I/O**: Link `EtherCAT_Outputs.Ch0`..`Ch15` to the EL2809 output process image (or build one WORD from Ch0..Ch15 and link that to the EL2809).

## Network variables (Node-RED)

- **NVL_Out** (PLC → Node-RED): light states + boiler status, sent via UDP.
- **NVL_In** (Node-RED → PLC): light commands + boiler commands, received via UDP.

See **NVL/README.md** for CODESYS NVL Sender/Receiver setup and **NVL/nodered-payload.md** for binary payload layout and Node-RED parse/build examples.

## Dependencies

- **Standard library** (R_TRIG, F_TRIG, TON, TIME_TO_DINT, DINT_TO_INT, etc.).
- **NVL_In / NVL_Out**: Use **GVL_NVL_placeholders.gvl** or bind your NVL Sender/Receiver to the same struct layout (see NVL/README.md).
- **DI_Emergency_Stop**: Optional BOOL; set to FALSE or link to a physical input.

## Design Reference

See **docs/codesys/plc-algorithm-design.md** for full algorithm description, I/O mapping, and Option C relay feedback.
