Metadata-Version: 2.4
Name: ioc-base
Version: 0.0.21
Summary: Base classes for building python based EPICS IOCs using CAproto
Author: KMC3-XPP-Beamline
Author-email: Matthias Rössle <matthias.roessle@helmholtz-berlin.de>
Keywords: epics,ioc,scpi,visa,caproto
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Interface Engine/Protocol Translator
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE.md
Requires-Dist: caproto>=1.1.0
Requires-Dist: pyvisa>=1.13.0
Requires-Dist: pyvisa-py>=0.7.0
Requires-Dist: pyyaml>=6.0
Requires-Dist: numpy>=1.24.0
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == "dev"
Requires-Dist: pytest-asyncio>=0.21; extra == "dev"
Requires-Dist: pytest-cov>=4.0; extra == "dev"
Provides-Extra: trinamic
Requires-Dist: pytrinamic; extra == "trinamic"
Provides-Extra: scpi
Requires-Dist: pyvisa>=1.13.0; extra == "scpi"
Requires-Dist: pyvisa-py>=0.7.0; extra == "scpi"
Provides-Extra: ui
Requires-Dist: pyepics; extra == "ui"
Requires-Dist: streamlit; extra == "ui"
Requires-Dist: rich; extra == "ui"
Dynamic: license-file

Base library for building python based EPICS IOCs using [CaProto](https://github.com/caproto/caproto/tree/main).
 
## Features 

- **AbstractBaseIOC**: Generic base class for all device IOCs
- **ScpiBaseIOC**: For SCPI/VISA-based devices (e.g. LS336, PM100, etc.)
- **DirectAccessIOC**: For direct hardware access devices (PiXtend, GPIO, etc.)
- **TrinamicBaseIOC**: For Trinamic TMCM modules (3351, 6110)
- **ReadOnly / WriteRead**: Reusable PV groups with YAML configuration
- **Channel Generator**: E.g. `RELAY{0..3}` → RELAY0, RELAY1, RELAY2, RELAY3 → 4 PVs
- **Composite Actions** – Set multiple PVs atomically from YAML
- **CALC Operations** – Define a PV to perfom mathematical calculations (`SUM`, `SUB`, ...) of PVs
- **Status Monitoring**: Status PVs, event-driven logging, status box
- **Simulation** – Simulate devices for testing (`--sim`)
- **Automatic Semantic Versioning**: `auto_release.py` with changelog generation ([details](./docs/versioning.md))
- **UI**: Monitoring of IOC PVs

## Architecture

### IOC Classes
```
AbstractBaseIOC(PVGroup)
├── ScpiBaseIOC          # SCPI/VISA devices (LS336, PM100)
└── DirectAccessIOC      # Direct hardware access (no SCPI)
    └── TrinamicBaseIOC  # Trinamic controllers
```

### Important modules of `ioc-base`
```
src/ioc_base/
├── base/
│   ├── ioc_core.py          # AbstractBaseIOC (Main Loop)
│   ├── pv_groups.py         # ReadOnly, WriteRead, _map_dtype
│   ├── scpi_ioc.py          # ScpiBaseIOC
│   ├── direct_ioc.py        # DirectAccessIOC
│   ├── trinamic_ioc.py      # TrinamicBaseIOC (USB), _read_hardware(), write_hardware()
│   ...
├── config/
│   └── config.py            # IocConfig, expand_channels, expand_composite_actions
├── modules/
│   ├── tmcm3351.py          # TMCM3351KMC3
│   └── tmcm6110.py          # TMCM6110KMC3
├── ui/
│   └── base_ui.py           # UIs 
└── app/
    └── application.py       # Generic application class
```

### Workflow (Short)

```mermaid
flowchart LR
    A["PV-Configuration<br>(YAML)"] -->|"expand_channels()"| B["PV-Groups<br>(RO/RW)"]
    B -->|"_read_hardware()/<br>write_hardware()"| C["Hardware<br>(VISA/USB/etc.)"]
```

## Installation

```bash
# Base (for all IOCs)
pip install ioc-base

# With Trinamic support
pip install ioc-base[trinamic]

# With SCPI/VISA support
pip install ioc-base[scpi]

# With UI support
pip install ioc-base[ui]

# For pytest, e.g. in pipelines
pip install ioc-base[dev]


```

## Environment Variables

| Variable | Description |
|---|---|
| `<iocname>_LOGGING` | Log level (info, debug, warning, error) |
| `<iocname>_ADV_LOGGING` | Log level (debug, info) |
| `PV_CFG_FILE` | Path to PV configuration |
| `<iocname>_EPICS_PREFIX` | EPICS prefix |

## PV Configuration (YAML)

```yaml
read_only_pvs:
  "CH1:TEMP_RBV":
    "doc": "Channel 1 temperature"
    "cmd_rbv": "KRDG? A"
    "dtype": "float"

read_write_pvs:
  "CH1:SETPOINT":
    "doc": "Channel 1 setpoint"
    "cmd_rbv": "SETP? 1"
    "cmd_set": "SETP 1,"
    "dtype": "float"

  "CH{2..3}:HEATER":
    "doc": "Read/Set heater for channel {index}"
    "cmd_rbv": "SETP? {index}"
    "cmd_set": "SETP {index},"
    "dtype": "float"

  "TLIM{1..4}":
    "doc": "Read/Set temperature limit for sensor {index} [K]"
    "cmd_rbv": "TLIMIT? {index|A,B,C,D}"
    "cmd_set": "TLIMIT {index|A,B,C,D},"
    "dtype": "str"

  "MOTOR{1..2}:VTAR":
    "doc": "Motor {index} target velocity"
    "cmd_rbv": "axis:{index|0,1}:TargetVelocity"
    "cmd_set": "axis:{index|0,1}:TargetVelocity"
    "dtype": "int"
    
composite_actions:
  "HEATER":
    "enum_strings": ["OFF", "ON"]
    "actions":
      "ON": 
        "CH1:SETPOINT": "3.3" 
        "RELAY1": "ON"
```

- For details s. [examples](./docs/examples.md).

## CALC Operations

```yaml
read_only_pvs:
  "SUM_RBV":
    "cmd_rbv": "CALC:SUM|PV1|PV2"
    "dtype": "int"
```

Supported: `SUM`, `SUB`, `AVG`, `MULT`, `DIV`, `MIN`, `MAX`


## Example Usage

```python
# 1. Select Subclass
class MyDeviceIOC(ScpiBaseIOC):    # SCPI-Devices
class MyDeviceIOC(TrinamicBaseIOC): # Trinamic-Controller

# 2. Module (e.g. for Trinamic)
_module_class = MyDeviceModule
_axis_count = 3

# 3. PV-Configuration (YAML)
read_only_pvs:
  "CH1:TEMP_RBV":
    "cmd_rbv": "MEAS:TEMP? 1"

# 4. IOC 
def create_ioc(prefix, dev, pv_group_a, pv_group_b, iocname, **opts):
    ioc = MyDeviceIOC(...)
    # Register PV-Groups
    return ioc

# 5. Application (Entry Point)
def main():
    from ioc_base.config.ioc_config import IocConfig, parse_args
    from ioc_base.app.application import Application
    from ioc_base.base.scpi_device_sim import connect_simulated_device
    
    config = IocConfig()
    args = parse_args()
    
    app = Application(
        config=config,
        device_factory=lambda: None,  # _init_device creates device
        ioc_factory=create_ioc,
        simulation=args.sim,
        device_factory_sim=connect_simulated_device,
    )
    asyncio.run(app.async_run())

# 5. Run
PV_CFG_FILE=my_pvs.yaml IOCNAME=MYDEVICE my-ioc
PV_CFG_FILE=my_pvs.yaml IOCNAME=MYDEVICE my-ioc --sim
```

For more details see IOCs: tempctrl-ioc, abox-ioc, ...
