Metadata-Version: 2.5
Name: plesty-pcd-m02
Version: 0.1.0
Summary: Plesty device for the Luna (General Photonics) PCD-M02 polarization controller, driven through its digital input by a board running StandardFirmata.
Author-email: Yunshuang Yuan <yunshuang.yuan@fkp.uni-hannover.de>
Maintainer-email: Plesty Development Team <plesty.dev@example.com>
License-Expression: LGPL-3.0-or-later
License-File: LICENSE
License-File: LICENSES/LGPL-3.0-or-later.txt
Requires-Python: >=3.12
Requires-Dist: plesty-arduino-firmata>=0.1.0
Requires-Dist: plesty-lib>=0.5.2
Provides-Extra: gui
Requires-Dist: plesty-lib[gui]>=0.5.2; extra == 'gui'
Description-Content-Type: text/markdown

# Plesty PCD-M02

A [PLESTY](https://plesty.net) device module for the **Luna (General
Photonics) PCD-M02** polarization controller: four piezo fiber squeezers
driven at 0–140 V.

The PCD-M02 has no computer interface of its own. Its digital input — a
17-line TTL bus — is wired to an Arduino running StandardFirmata, and this
module drives that bus through
[`plesty-arduino-firmata`](https://gitlab.com/plesty/hub/devices/arduino/plesty-arduino-firmata).
You talk to channels and volts; the module turns them into pin writes.

## Hardware setup

1. On the PCD-M02, remove jumper **JP1000** to enable digital control, and
   disconnect the analog input (or hold it at 0 V) — both inputs active at once
   can drive the board into saturation (manual GP-UM-PCD-M02-14, p. 9).
2. Upload **StandardFirmata** to the Arduino (Arduino IDE: File → Examples →
   Firmata → StandardFirmata).
3. Wire the PCD-M02's 20-pin digital connector **JDD** to the Arduino. The
   module's default wiring is the lab's Arduino Mega:

   | JDD pin | Line | Mega pin |
   |---|---|---|
   | 1 | RW | 22 |
   | 2 | /CS | 23 |
   | 3 | /RESET | 24 |
   | 4 | A0 | 29 |
   | 5 | A1 | 28 |
   | 6–17 | DB0–DB11 (DB0 least significant) | 32–43 |
   | 18, 19 | DGND | GND |

   A board wired differently passes its own `Wiring` (see below).

## Installation

```bash
git clone https://gitlab.com/plesty/hub/devices/luna/plesty-pcd-m02.git
cd plesty-pcd-m02
uv sync
```

## Usage

```python
from plesty.pcd_m02 import Device

with Device(port="auto") as pc:  # or "COM5", "/dev/ttyACM0"
    pc.set_voltage(channel=1, voltage=70.0)  # channels 1–4, 0–140 V
    pc.set_voltages(voltages=[10.0, 20.0, 30.0, 40.0])
    print(pc.get_voltages())  # {'voltages': [9.98, 20.0, 30.02, 40.0]}
    pc.step_voltage(2, -5.0)  # stops at 0 V / 140 V
```

What to know:

- **Connecting resets the outputs to 0 V.** The bus cannot be read back, so
  the module starts from the one state it can be sure of. To restore a saved
  polarization state, call `set_voltages` after connecting.
- **Voltages are reported, not measured.** `get_voltage` answers the value
  last set in this session, rounded to the 12-bit word (steps of 34 mV).
- **Disconnecting leaves the outputs where they are.** The controller holds its
  voltages while the port is closed; the next connect resets them.

A different wiring:

```python
from plesty.pcd_m02 import Device, Wiring

wiring = Wiring(rw=2, cs=3, reset=4, a0=5, a1=6, data=tuple(range(7, 19)))
with Device(port="COM5", wiring=wiring) as pc:
    ...
```

`port="mock"` runs against a simulated Arduino with a simulated PCD-M02 on it;
`pc.simulator.outputs` reads its four output voltages.

## Run as TCP server

Copy `.env.example` to `.env` and set `DEVICE_ADDRESS`, then:

```bash
uv run python -m plesty.pcd_m02 --tcp-port 5555
```

## Open the control panel

```bash
uv sync --extra gui
uv run python -m plesty.pcd_m02 --gui
```
