scietex.dac-service 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- scietex_dac_service-0.1.0/LICENSE +21 -0
- scietex_dac_service-0.1.0/PKG-INFO +198 -0
- scietex_dac_service-0.1.0/README.md +168 -0
- scietex_dac_service-0.1.0/pyproject.toml +54 -0
- scietex_dac_service-0.1.0/setup.cfg +4 -0
- scietex_dac_service-0.1.0/src/scietex/dac_service/__init__.py +79 -0
- scietex_dac_service-0.1.0/src/scietex/dac_service/config.py +161 -0
- scietex_dac_service-0.1.0/src/scietex/dac_service/dac_worker.py +172 -0
- scietex_dac_service-0.1.0/src/scietex/dac_service/devices.py +363 -0
- scietex_dac_service-0.1.0/src/scietex/dac_service/py.typed +0 -0
- scietex_dac_service-0.1.0/src/scietex/dac_service/run_worker.py +76 -0
- scietex_dac_service-0.1.0/src/scietex/dac_service/task_handlers/__init__.py +59 -0
- scietex_dac_service-0.1.0/src/scietex/dac_service/task_handlers/_request.py +141 -0
- scietex_dac_service-0.1.0/src/scietex/dac_service/task_handlers/configure.py +227 -0
- scietex_dac_service-0.1.0/src/scietex/dac_service/task_handlers/list.py +155 -0
- scietex_dac_service-0.1.0/src/scietex/dac_service/task_handlers/read.py +100 -0
- scietex_dac_service-0.1.0/src/scietex/dac_service/task_handlers/status.py +171 -0
- scietex_dac_service-0.1.0/src/scietex/dac_service/task_handlers/write.py +120 -0
- scietex_dac_service-0.1.0/src/scietex/dac_service/telemetry.py +139 -0
- scietex_dac_service-0.1.0/src/scietex/dac_service/version.py +3 -0
- scietex_dac_service-0.1.0/src/scietex.dac_service.egg-info/PKG-INFO +198 -0
- scietex_dac_service-0.1.0/src/scietex.dac_service.egg-info/SOURCES.txt +30 -0
- scietex_dac_service-0.1.0/src/scietex.dac_service.egg-info/dependency_links.txt +1 -0
- scietex_dac_service-0.1.0/src/scietex.dac_service.egg-info/entry_points.txt +2 -0
- scietex_dac_service-0.1.0/src/scietex.dac_service.egg-info/requires.txt +20 -0
- scietex_dac_service-0.1.0/src/scietex.dac_service.egg-info/top_level.txt +1 -0
- scietex_dac_service-0.1.0/tests/test_config.py +177 -0
- scietex_dac_service-0.1.0/tests/test_devices.py +381 -0
- scietex_dac_service-0.1.0/tests/test_run_worker.py +74 -0
- scietex_dac_service-0.1.0/tests/test_task_handlers.py +658 -0
- scietex_dac_service-0.1.0/tests/test_telemetry.py +181 -0
- scietex_dac_service-0.1.0/tests/test_worker.py +215 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Anton Bondarenko
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: scietex.dac_service
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Scietex microservice daemon for SPI DAC devices
|
|
5
|
+
Author-email: Anton Bondarenko <bond.anton@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Bug Tracker, https://github.com/bond-anton/scietex.dac_service/issues
|
|
8
|
+
Project-URL: Homepage, https://github.com/bond-anton/scietex.dac_service
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Requires-Python: >=3.10
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: scietex.service[valkey]~=6.2.0
|
|
15
|
+
Requires-Dist: scietex.hal.dac<1.0.0,>=0.3.0
|
|
16
|
+
Requires-Dist: msgspec>=0.18.6
|
|
17
|
+
Provides-Extra: dev
|
|
18
|
+
Requires-Dist: tox>=4.45.0; extra == "dev"
|
|
19
|
+
Provides-Extra: lint
|
|
20
|
+
Requires-Dist: ruff; extra == "lint"
|
|
21
|
+
Requires-Dist: ty; extra == "lint"
|
|
22
|
+
Provides-Extra: test
|
|
23
|
+
Requires-Dist: pytest; extra == "test"
|
|
24
|
+
Requires-Dist: pytest-asyncio; extra == "test"
|
|
25
|
+
Provides-Extra: spi
|
|
26
|
+
Requires-Dist: scietex.hal.dac[spi]<1.0.0,>=0.3.0; extra == "spi"
|
|
27
|
+
Provides-Extra: gpiozero
|
|
28
|
+
Requires-Dist: scietex.hal.dac[gpiozero]<1.0.0,>=0.3.0; extra == "gpiozero"
|
|
29
|
+
Dynamic: license-file
|
|
30
|
+
|
|
31
|
+
# scietex.dac_service
|
|
32
|
+
|
|
33
|
+
**scietex.dac_service** is a [`scietex.service`](https://github.com/bond-anton/scietex.service)
|
|
34
|
+
worker connected to Valkey that owns one or more SPI DAC devices through
|
|
35
|
+
[`scietex.hal.dac`](https://github.com/bond-anton/scietex.hal.dac) (23 supported
|
|
36
|
+
parts: voltage converters, current-loop transmitters, and digital
|
|
37
|
+
potentiometers). It opens the configured devices at startup, exposes writes,
|
|
38
|
+
reads, and configuration as `dac:*` tasks, and publishes the last-written state
|
|
39
|
+
as telemetry without touching the bus. A device that fails to open does not stop
|
|
40
|
+
the worker: it is recorded, reported as unavailable, and the remaining devices
|
|
41
|
+
keep working. Configuration can also be pushed at runtime through the
|
|
42
|
+
framework's remote-config channel.
|
|
43
|
+
|
|
44
|
+
**Python ≥ 3.10** · **License: MIT**
|
|
45
|
+
|
|
46
|
+
## Documentation
|
|
47
|
+
|
|
48
|
+
- [Overview](docs/index.md) — what the service does and how it fits together
|
|
49
|
+
- [Configuration](docs/configuration.md) — the `dac.yml` schema and precedence
|
|
50
|
+
- [Deployment](docs/deployment.md) — container, SPI device access, Quadlet
|
|
51
|
+
- [Remote configuration](docs/remote-config.md) — the declarative `dac` section
|
|
52
|
+
|
|
53
|
+
## Installation
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
pip install "scietex.dac_service[spi]" # spidev backend for /dev/spidev*
|
|
57
|
+
pip install "scietex.dac_service[gpiozero]" # plain-GPIO backend
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
The core package pulls in `scietex.hal.dac`, `scietex.service[valkey]`, and
|
|
61
|
+
`msgspec`. The backend extras are optional: a core install can still build a
|
|
62
|
+
device from a caller-supplied transport, but an operator normally installs the
|
|
63
|
+
extra that matches the board's wiring.
|
|
64
|
+
|
|
65
|
+
## Quick start
|
|
66
|
+
|
|
67
|
+
### 1. Write a configuration file
|
|
68
|
+
|
|
69
|
+
The service reads `dac/dac.yml` under its config directory. Create it by hand,
|
|
70
|
+
or let the service generate a default on first run. The file is the bootstrap
|
|
71
|
+
layer of a four-layer merge: constructor defaults < `dac.yml` < the framework's
|
|
72
|
+
`config.yml` snapshot < the remote `dac` section.
|
|
73
|
+
|
|
74
|
+
### 2. Run the service
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
start-dac-service --conf-dir /etc/scietex
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
The config directory resolves automatically when `--conf-dir` is omitted. The
|
|
81
|
+
service runs in the foreground until `SIGINT` or `SIGTERM`.
|
|
82
|
+
|
|
83
|
+
### 3. Write a channel
|
|
84
|
+
|
|
85
|
+
Submit a `dac:write` task to the worker with payload
|
|
86
|
+
`{"device_id": "dac0", "channel": 0, "output": 1.5}` — or use `code` for a raw
|
|
87
|
+
value. Exactly one of `code` and `output` must be set. The result carries the
|
|
88
|
+
code the channel holds and, for a write-only part, the scaled output.
|
|
89
|
+
|
|
90
|
+
## Configuration at a glance
|
|
91
|
+
|
|
92
|
+
Devices are keyed by a **string** id, which names the device in task payloads
|
|
93
|
+
and telemetry:
|
|
94
|
+
|
|
95
|
+
```yaml
|
|
96
|
+
devices:
|
|
97
|
+
dac0:
|
|
98
|
+
type: MCP4822
|
|
99
|
+
port: 0
|
|
100
|
+
device: 0
|
|
101
|
+
reference: 2.048
|
|
102
|
+
dac1:
|
|
103
|
+
type: AD420
|
|
104
|
+
range: 4-20mA
|
|
105
|
+
latch_pin: 27
|
|
106
|
+
dac2:
|
|
107
|
+
type: AD5420
|
|
108
|
+
port: 0
|
|
109
|
+
device: 1
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
`MCP4822` is a dual 12-bit voltage converter with a fixed 2.048 V full scale.
|
|
113
|
+
`AD420` is a single 16-bit current transmitter: its `range` only declares the
|
|
114
|
+
RANGE SELECT strap, and `latch_pin` is the GPIO line that latches each frame.
|
|
115
|
+
`AD5420` is a 16-bit current source that needs `dac:configure` to select a range
|
|
116
|
+
and enable its output before a write reaches the loop.
|
|
117
|
+
|
|
118
|
+
Per-device fields: `type` (required), `port`/`device` for the SPI wiring, the
|
|
119
|
+
gpiozero pins (`clock_pin`/`mosi_pin`/`miso_pin`/`select_pin`), `reference`,
|
|
120
|
+
`label`, `log_level`, and the part-specific `range`, `load_pin`, `latch_pin`,
|
|
121
|
+
`r_ab`, `grounded`. Unknown fields are rejected. `port` is the SPI bus number
|
|
122
|
+
and `device` is the chip-select.
|
|
123
|
+
|
|
124
|
+
## Supported parts
|
|
125
|
+
|
|
126
|
+
| Parts | Channels | Resolution | Readable | Required extra |
|
|
127
|
+
|---|---|---|---|---|
|
|
128
|
+
| MCP4231, MCP4241 | 2 | 7 | yes | — |
|
|
129
|
+
| MCP4251, MCP4261 | 2 | 8 | yes | — |
|
|
130
|
+
| MCP4802, MCP4812, MCP4822 | 2 | 8 / 10 / 12 | no | — |
|
|
131
|
+
| MCP4902, MCP4912, MCP4922 | 2 | 8 / 10 / 12 | no | — |
|
|
132
|
+
| DAC8531, DAC8551 | 1 | 16 | no | — |
|
|
133
|
+
| DAC8552 | 2 | 16 | no | — |
|
|
134
|
+
| DAC121S101 | 1 | 12 | no | — |
|
|
135
|
+
| AD5302, AD5312, AD5322 | 2 | 8 / 10 / 12 | no | — |
|
|
136
|
+
| DAC8512 | 1 | 12 | no | `load_pin` |
|
|
137
|
+
| AD7390, AD7391 | 1 | 12 / 10 | no | `load_pin` |
|
|
138
|
+
| AD5410, AD5420 | 1 | 12 / 16 | yes | — |
|
|
139
|
+
| AD420 | 1 | 16 | no | `range`, `latch_pin` |
|
|
140
|
+
|
|
141
|
+
"Readable" means the part can report its registers back, so `dac:read` and
|
|
142
|
+
register-level `dac:status` work. A write-only part still accepts `dac:write`
|
|
143
|
+
and reports the cached code. The potentiometers additionally take `r_ab` and
|
|
144
|
+
`grounded`; the current-loop parts take `range`.
|
|
145
|
+
|
|
146
|
+
## Tasks
|
|
147
|
+
|
|
148
|
+
| Task | Lane | Request | Result |
|
|
149
|
+
|---|---|---|---|
|
|
150
|
+
| `dac:list` | control | optional `device_id` | `devices[]` with `device_id`, `type`, `available`, `channels`, `resolution`, `max_code`, `unit`, `reference`, `readable`, `error` |
|
|
151
|
+
| `dac:status` | control | `device_id` | `device_id`, `type`, `available`, `error`, `readable`, `channels`, `resolution`, `max_code`, `unit`, `reference`, `last_written[]`, `status`, `control` |
|
|
152
|
+
| `dac:configure` | control | `device_id`, optional `range`, `slew`, `sr_clock`, `sr_step`, `rext`, `dcen`, `output` | `device_id`, `range`, `slew`, `sr_clock`, `sr_step`, `rext`, `dcen`, `output_enabled` |
|
|
153
|
+
| `dac:read` | data | `device_id`, optional `channel` | `device_id`, `channel`, `code`, `unit`, `reference`, `max_code` |
|
|
154
|
+
| `dac:write` | data | `device_id`, optional `channel`, exactly one of `code`/`output` | `device_id`, `channel`, `code`, `output`, `unit`, `max_code` |
|
|
155
|
+
|
|
156
|
+
`dac:configure` is only supported by parts that expose a control register; a
|
|
157
|
+
part without one answers `DAC_CONFIGURE_UNSUPPORTED`. A configured device that
|
|
158
|
+
failed to open answers `DEVICE_UNAVAILABLE` (retryable) from `dac:status`,
|
|
159
|
+
`dac:read`, and `dac:write`, while `dac:list` reports it as
|
|
160
|
+
`available: false` instead of failing the listing.
|
|
161
|
+
|
|
162
|
+
## Telemetry
|
|
163
|
+
|
|
164
|
+
The worker publishes to `scietex:DacService:<instance_id>:telemetry` every 2 s
|
|
165
|
+
with a 6 s TTL. The snapshot lists every configured device with its open state,
|
|
166
|
+
part type, and the channel state left by the last successful write. It is built
|
|
167
|
+
from the registry's last-written cache and live channel objects only: a
|
|
168
|
+
telemetry tick never touches the bus, so it cannot disturb an in-flight write,
|
|
169
|
+
and a write-only part still reports state. A device that was never written
|
|
170
|
+
reports no channels rather than fabricated zeros.
|
|
171
|
+
|
|
172
|
+
## Container and quadlet
|
|
173
|
+
|
|
174
|
+
```bash
|
|
175
|
+
podman build --build-arg VERSION=0.1.0 -t scietex-dac-service .
|
|
176
|
+
podman run --rm --device /dev/spidev0.0 -v ./config:/config scietex-dac-service
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
The container only dials Valkey, so it publishes no port and needs no
|
|
180
|
+
capabilities. `/dev/spidevX.Y` must be passed with `--device`; without it the
|
|
181
|
+
worker still starts and reports the device unavailable.
|
|
182
|
+
|
|
183
|
+
For a persistent deployment, install `deploy/dac-service.container` as
|
|
184
|
+
`~/.config/containers/systemd/dac-service.container` and the SPI drop-in as
|
|
185
|
+
`~/.config/containers/systemd/dac-service.container.d/50-spi-devices.conf`,
|
|
186
|
+
then `systemctl --user daemon-reload && systemctl --user start dac-service`.
|
|
187
|
+
The Quadlet bind-mounts `~/.config/scietex`, so `dac/dac.yml` stays
|
|
188
|
+
hand-editable on the host.
|
|
189
|
+
|
|
190
|
+
## Development
|
|
191
|
+
|
|
192
|
+
Dependencies are managed with **uv**; checks and tests run through **tox**:
|
|
193
|
+
|
|
194
|
+
```bash
|
|
195
|
+
uv sync --all-extras
|
|
196
|
+
uv run tox # format, lint, type, py314
|
|
197
|
+
uv run tox -e py314 # tests only
|
|
198
|
+
```
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# scietex.dac_service
|
|
2
|
+
|
|
3
|
+
**scietex.dac_service** is a [`scietex.service`](https://github.com/bond-anton/scietex.service)
|
|
4
|
+
worker connected to Valkey that owns one or more SPI DAC devices through
|
|
5
|
+
[`scietex.hal.dac`](https://github.com/bond-anton/scietex.hal.dac) (23 supported
|
|
6
|
+
parts: voltage converters, current-loop transmitters, and digital
|
|
7
|
+
potentiometers). It opens the configured devices at startup, exposes writes,
|
|
8
|
+
reads, and configuration as `dac:*` tasks, and publishes the last-written state
|
|
9
|
+
as telemetry without touching the bus. A device that fails to open does not stop
|
|
10
|
+
the worker: it is recorded, reported as unavailable, and the remaining devices
|
|
11
|
+
keep working. Configuration can also be pushed at runtime through the
|
|
12
|
+
framework's remote-config channel.
|
|
13
|
+
|
|
14
|
+
**Python ≥ 3.10** · **License: MIT**
|
|
15
|
+
|
|
16
|
+
## Documentation
|
|
17
|
+
|
|
18
|
+
- [Overview](docs/index.md) — what the service does and how it fits together
|
|
19
|
+
- [Configuration](docs/configuration.md) — the `dac.yml` schema and precedence
|
|
20
|
+
- [Deployment](docs/deployment.md) — container, SPI device access, Quadlet
|
|
21
|
+
- [Remote configuration](docs/remote-config.md) — the declarative `dac` section
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pip install "scietex.dac_service[spi]" # spidev backend for /dev/spidev*
|
|
27
|
+
pip install "scietex.dac_service[gpiozero]" # plain-GPIO backend
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
The core package pulls in `scietex.hal.dac`, `scietex.service[valkey]`, and
|
|
31
|
+
`msgspec`. The backend extras are optional: a core install can still build a
|
|
32
|
+
device from a caller-supplied transport, but an operator normally installs the
|
|
33
|
+
extra that matches the board's wiring.
|
|
34
|
+
|
|
35
|
+
## Quick start
|
|
36
|
+
|
|
37
|
+
### 1. Write a configuration file
|
|
38
|
+
|
|
39
|
+
The service reads `dac/dac.yml` under its config directory. Create it by hand,
|
|
40
|
+
or let the service generate a default on first run. The file is the bootstrap
|
|
41
|
+
layer of a four-layer merge: constructor defaults < `dac.yml` < the framework's
|
|
42
|
+
`config.yml` snapshot < the remote `dac` section.
|
|
43
|
+
|
|
44
|
+
### 2. Run the service
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
start-dac-service --conf-dir /etc/scietex
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
The config directory resolves automatically when `--conf-dir` is omitted. The
|
|
51
|
+
service runs in the foreground until `SIGINT` or `SIGTERM`.
|
|
52
|
+
|
|
53
|
+
### 3. Write a channel
|
|
54
|
+
|
|
55
|
+
Submit a `dac:write` task to the worker with payload
|
|
56
|
+
`{"device_id": "dac0", "channel": 0, "output": 1.5}` — or use `code` for a raw
|
|
57
|
+
value. Exactly one of `code` and `output` must be set. The result carries the
|
|
58
|
+
code the channel holds and, for a write-only part, the scaled output.
|
|
59
|
+
|
|
60
|
+
## Configuration at a glance
|
|
61
|
+
|
|
62
|
+
Devices are keyed by a **string** id, which names the device in task payloads
|
|
63
|
+
and telemetry:
|
|
64
|
+
|
|
65
|
+
```yaml
|
|
66
|
+
devices:
|
|
67
|
+
dac0:
|
|
68
|
+
type: MCP4822
|
|
69
|
+
port: 0
|
|
70
|
+
device: 0
|
|
71
|
+
reference: 2.048
|
|
72
|
+
dac1:
|
|
73
|
+
type: AD420
|
|
74
|
+
range: 4-20mA
|
|
75
|
+
latch_pin: 27
|
|
76
|
+
dac2:
|
|
77
|
+
type: AD5420
|
|
78
|
+
port: 0
|
|
79
|
+
device: 1
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
`MCP4822` is a dual 12-bit voltage converter with a fixed 2.048 V full scale.
|
|
83
|
+
`AD420` is a single 16-bit current transmitter: its `range` only declares the
|
|
84
|
+
RANGE SELECT strap, and `latch_pin` is the GPIO line that latches each frame.
|
|
85
|
+
`AD5420` is a 16-bit current source that needs `dac:configure` to select a range
|
|
86
|
+
and enable its output before a write reaches the loop.
|
|
87
|
+
|
|
88
|
+
Per-device fields: `type` (required), `port`/`device` for the SPI wiring, the
|
|
89
|
+
gpiozero pins (`clock_pin`/`mosi_pin`/`miso_pin`/`select_pin`), `reference`,
|
|
90
|
+
`label`, `log_level`, and the part-specific `range`, `load_pin`, `latch_pin`,
|
|
91
|
+
`r_ab`, `grounded`. Unknown fields are rejected. `port` is the SPI bus number
|
|
92
|
+
and `device` is the chip-select.
|
|
93
|
+
|
|
94
|
+
## Supported parts
|
|
95
|
+
|
|
96
|
+
| Parts | Channels | Resolution | Readable | Required extra |
|
|
97
|
+
|---|---|---|---|---|
|
|
98
|
+
| MCP4231, MCP4241 | 2 | 7 | yes | — |
|
|
99
|
+
| MCP4251, MCP4261 | 2 | 8 | yes | — |
|
|
100
|
+
| MCP4802, MCP4812, MCP4822 | 2 | 8 / 10 / 12 | no | — |
|
|
101
|
+
| MCP4902, MCP4912, MCP4922 | 2 | 8 / 10 / 12 | no | — |
|
|
102
|
+
| DAC8531, DAC8551 | 1 | 16 | no | — |
|
|
103
|
+
| DAC8552 | 2 | 16 | no | — |
|
|
104
|
+
| DAC121S101 | 1 | 12 | no | — |
|
|
105
|
+
| AD5302, AD5312, AD5322 | 2 | 8 / 10 / 12 | no | — |
|
|
106
|
+
| DAC8512 | 1 | 12 | no | `load_pin` |
|
|
107
|
+
| AD7390, AD7391 | 1 | 12 / 10 | no | `load_pin` |
|
|
108
|
+
| AD5410, AD5420 | 1 | 12 / 16 | yes | — |
|
|
109
|
+
| AD420 | 1 | 16 | no | `range`, `latch_pin` |
|
|
110
|
+
|
|
111
|
+
"Readable" means the part can report its registers back, so `dac:read` and
|
|
112
|
+
register-level `dac:status` work. A write-only part still accepts `dac:write`
|
|
113
|
+
and reports the cached code. The potentiometers additionally take `r_ab` and
|
|
114
|
+
`grounded`; the current-loop parts take `range`.
|
|
115
|
+
|
|
116
|
+
## Tasks
|
|
117
|
+
|
|
118
|
+
| Task | Lane | Request | Result |
|
|
119
|
+
|---|---|---|---|
|
|
120
|
+
| `dac:list` | control | optional `device_id` | `devices[]` with `device_id`, `type`, `available`, `channels`, `resolution`, `max_code`, `unit`, `reference`, `readable`, `error` |
|
|
121
|
+
| `dac:status` | control | `device_id` | `device_id`, `type`, `available`, `error`, `readable`, `channels`, `resolution`, `max_code`, `unit`, `reference`, `last_written[]`, `status`, `control` |
|
|
122
|
+
| `dac:configure` | control | `device_id`, optional `range`, `slew`, `sr_clock`, `sr_step`, `rext`, `dcen`, `output` | `device_id`, `range`, `slew`, `sr_clock`, `sr_step`, `rext`, `dcen`, `output_enabled` |
|
|
123
|
+
| `dac:read` | data | `device_id`, optional `channel` | `device_id`, `channel`, `code`, `unit`, `reference`, `max_code` |
|
|
124
|
+
| `dac:write` | data | `device_id`, optional `channel`, exactly one of `code`/`output` | `device_id`, `channel`, `code`, `output`, `unit`, `max_code` |
|
|
125
|
+
|
|
126
|
+
`dac:configure` is only supported by parts that expose a control register; a
|
|
127
|
+
part without one answers `DAC_CONFIGURE_UNSUPPORTED`. A configured device that
|
|
128
|
+
failed to open answers `DEVICE_UNAVAILABLE` (retryable) from `dac:status`,
|
|
129
|
+
`dac:read`, and `dac:write`, while `dac:list` reports it as
|
|
130
|
+
`available: false` instead of failing the listing.
|
|
131
|
+
|
|
132
|
+
## Telemetry
|
|
133
|
+
|
|
134
|
+
The worker publishes to `scietex:DacService:<instance_id>:telemetry` every 2 s
|
|
135
|
+
with a 6 s TTL. The snapshot lists every configured device with its open state,
|
|
136
|
+
part type, and the channel state left by the last successful write. It is built
|
|
137
|
+
from the registry's last-written cache and live channel objects only: a
|
|
138
|
+
telemetry tick never touches the bus, so it cannot disturb an in-flight write,
|
|
139
|
+
and a write-only part still reports state. A device that was never written
|
|
140
|
+
reports no channels rather than fabricated zeros.
|
|
141
|
+
|
|
142
|
+
## Container and quadlet
|
|
143
|
+
|
|
144
|
+
```bash
|
|
145
|
+
podman build --build-arg VERSION=0.1.0 -t scietex-dac-service .
|
|
146
|
+
podman run --rm --device /dev/spidev0.0 -v ./config:/config scietex-dac-service
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
The container only dials Valkey, so it publishes no port and needs no
|
|
150
|
+
capabilities. `/dev/spidevX.Y` must be passed with `--device`; without it the
|
|
151
|
+
worker still starts and reports the device unavailable.
|
|
152
|
+
|
|
153
|
+
For a persistent deployment, install `deploy/dac-service.container` as
|
|
154
|
+
`~/.config/containers/systemd/dac-service.container` and the SPI drop-in as
|
|
155
|
+
`~/.config/containers/systemd/dac-service.container.d/50-spi-devices.conf`,
|
|
156
|
+
then `systemctl --user daemon-reload && systemctl --user start dac-service`.
|
|
157
|
+
The Quadlet bind-mounts `~/.config/scietex`, so `dac/dac.yml` stays
|
|
158
|
+
hand-editable on the host.
|
|
159
|
+
|
|
160
|
+
## Development
|
|
161
|
+
|
|
162
|
+
Dependencies are managed with **uv**; checks and tests run through **tox**:
|
|
163
|
+
|
|
164
|
+
```bash
|
|
165
|
+
uv sync --all-extras
|
|
166
|
+
uv run tox # format, lint, type, py314
|
|
167
|
+
uv run tox -e py314 # tests only
|
|
168
|
+
```
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "scietex.dac_service"
|
|
3
|
+
description = "Scietex microservice daemon for SPI DAC devices"
|
|
4
|
+
readme = "README.md"
|
|
5
|
+
requires-python = ">=3.10"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICEN[CS]E*"]
|
|
8
|
+
authors = [{ name = "Anton Bondarenko", email = "bond.anton@gmail.com" }]
|
|
9
|
+
classifiers = [
|
|
10
|
+
"Operating System :: OS Independent",
|
|
11
|
+
"Programming Language :: Python :: 3",
|
|
12
|
+
]
|
|
13
|
+
dependencies = [
|
|
14
|
+
"scietex.service[valkey]~=6.2.0",
|
|
15
|
+
"scietex.hal.dac>=0.3.0,<1.0.0",
|
|
16
|
+
"msgspec>=0.18.6",
|
|
17
|
+
]
|
|
18
|
+
dynamic = ["version"]
|
|
19
|
+
|
|
20
|
+
[project.urls]
|
|
21
|
+
"Bug Tracker" = "https://github.com/bond-anton/scietex.dac_service/issues"
|
|
22
|
+
"Homepage" = "https://github.com/bond-anton/scietex.dac_service"
|
|
23
|
+
|
|
24
|
+
[project.scripts]
|
|
25
|
+
start-dac-service = "scietex.dac_service.run_worker:main"
|
|
26
|
+
|
|
27
|
+
[project.optional-dependencies]
|
|
28
|
+
dev = ["tox>=4.45.0"]
|
|
29
|
+
lint = ["ruff", "ty"]
|
|
30
|
+
test = ["pytest", "pytest-asyncio"]
|
|
31
|
+
# SPI backends are optional: a core install can still build DAC objects from a
|
|
32
|
+
# caller-supplied transport. An operator installs the extra for the board's
|
|
33
|
+
# wiring — spidev for /dev/spidev*, gpiozero for plain GPIO pins.
|
|
34
|
+
spi = ["scietex.hal.dac[spi]>=0.3.0,<1.0.0"]
|
|
35
|
+
gpiozero = ["scietex.hal.dac[gpiozero]>=0.3.0,<1.0.0"]
|
|
36
|
+
|
|
37
|
+
[build-system]
|
|
38
|
+
requires = ["setuptools>=61.0"]
|
|
39
|
+
build-backend = "setuptools.build_meta"
|
|
40
|
+
|
|
41
|
+
[tool.pytest.ini_options]
|
|
42
|
+
pythonpath = ["src"]
|
|
43
|
+
testpaths = ["tests"]
|
|
44
|
+
asyncio_mode = "auto"
|
|
45
|
+
|
|
46
|
+
[tool.setuptools.dynamic]
|
|
47
|
+
version = { attr = "scietex.dac_service.version.__version__" }
|
|
48
|
+
|
|
49
|
+
[tool.setuptools.package-data]
|
|
50
|
+
"scietex.dac_service" = ["py.typed"]
|
|
51
|
+
|
|
52
|
+
[tool.setuptools.packages.find]
|
|
53
|
+
where = ["src"]
|
|
54
|
+
namespaces = true
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"""DAC Service."""
|
|
2
|
+
|
|
3
|
+
from .config import (
|
|
4
|
+
DAC_SECTION,
|
|
5
|
+
DAC_SETTINGS_DEFAULTS,
|
|
6
|
+
DacDeviceSettings,
|
|
7
|
+
DacServiceSettings,
|
|
8
|
+
read_dac_config,
|
|
9
|
+
)
|
|
10
|
+
from .dac_worker import DacWorker
|
|
11
|
+
from .devices import (
|
|
12
|
+
ChannelNotFound,
|
|
13
|
+
DacDeviceRegistry,
|
|
14
|
+
DeviceConfigError,
|
|
15
|
+
DeviceNotFound,
|
|
16
|
+
DeviceOpenError,
|
|
17
|
+
DeviceUnavailable,
|
|
18
|
+
InvalidParameter,
|
|
19
|
+
UnknownDeviceType,
|
|
20
|
+
)
|
|
21
|
+
from .task_handlers import (
|
|
22
|
+
DacChannelState,
|
|
23
|
+
DacConfigureHandler,
|
|
24
|
+
DacConfigureRequest,
|
|
25
|
+
DacConfigureResponse,
|
|
26
|
+
DacDeviceInfo,
|
|
27
|
+
DacListHandler,
|
|
28
|
+
DacListRequest,
|
|
29
|
+
DacListResponse,
|
|
30
|
+
DacReadHandler,
|
|
31
|
+
DacReadRequest,
|
|
32
|
+
DacReadResponse,
|
|
33
|
+
DacStatusHandler,
|
|
34
|
+
DacStatusRequest,
|
|
35
|
+
DacStatusResponse,
|
|
36
|
+
DacWriteHandler,
|
|
37
|
+
DacWriteRequest,
|
|
38
|
+
DacWriteResponse,
|
|
39
|
+
)
|
|
40
|
+
from .telemetry import DacChannelTelemetry, DacDeviceTelemetry, DacServiceTelemetry
|
|
41
|
+
from .version import __version__
|
|
42
|
+
|
|
43
|
+
__all__ = [
|
|
44
|
+
"__version__",
|
|
45
|
+
"DacWorker",
|
|
46
|
+
"DAC_SECTION",
|
|
47
|
+
"DAC_SETTINGS_DEFAULTS",
|
|
48
|
+
"DacDeviceSettings",
|
|
49
|
+
"DacServiceSettings",
|
|
50
|
+
"DacChannelTelemetry",
|
|
51
|
+
"DacDeviceTelemetry",
|
|
52
|
+
"DacServiceTelemetry",
|
|
53
|
+
"DacDeviceRegistry",
|
|
54
|
+
"ChannelNotFound",
|
|
55
|
+
"DeviceConfigError",
|
|
56
|
+
"DeviceNotFound",
|
|
57
|
+
"DeviceOpenError",
|
|
58
|
+
"DeviceUnavailable",
|
|
59
|
+
"InvalidParameter",
|
|
60
|
+
"UnknownDeviceType",
|
|
61
|
+
"DacChannelState",
|
|
62
|
+
"DacConfigureHandler",
|
|
63
|
+
"DacConfigureRequest",
|
|
64
|
+
"DacConfigureResponse",
|
|
65
|
+
"DacDeviceInfo",
|
|
66
|
+
"DacListHandler",
|
|
67
|
+
"DacListRequest",
|
|
68
|
+
"DacListResponse",
|
|
69
|
+
"DacReadHandler",
|
|
70
|
+
"DacReadRequest",
|
|
71
|
+
"DacReadResponse",
|
|
72
|
+
"DacStatusHandler",
|
|
73
|
+
"DacStatusRequest",
|
|
74
|
+
"DacStatusResponse",
|
|
75
|
+
"DacWriteHandler",
|
|
76
|
+
"DacWriteRequest",
|
|
77
|
+
"DacWriteResponse",
|
|
78
|
+
"read_dac_config",
|
|
79
|
+
]
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
"""Configuration models and YAML loader for the DAC service.
|
|
2
|
+
|
|
3
|
+
`DacServiceSettings` is the concrete constructor (L0) base and the validation
|
|
4
|
+
target of the framework's four-layer config merge:
|
|
5
|
+
|
|
6
|
+
* L0 — `DAC_SETTINGS_DEFAULTS`, the builtins of a default struct instance.
|
|
7
|
+
* L1 — the service-owned ``dac/dac.yml`` bootstrap patch (`read_dac_config`).
|
|
8
|
+
* L2 — the framework's ``config.yml`` snapshot from the last successful apply.
|
|
9
|
+
* L3 — the remote ``dac`` section of a desired-state envelope.
|
|
10
|
+
|
|
11
|
+
The layers are patch dicts merged field by field with the three-state rule: a
|
|
12
|
+
key absent from every layer inherits the value below, a key present with
|
|
13
|
+
``null`` clears it (so the L0 value resurfaces), and a key present with a value
|
|
14
|
+
sets it. The struct is kept deliberately thin: backend selection, pin routing,
|
|
15
|
+
and part geometry are validated by the HAL constructors in
|
|
16
|
+
`devices.build_dac`, so the YAML schema only constrains types and unknown
|
|
17
|
+
fields.
|
|
18
|
+
|
|
19
|
+
Per-channel sequences are deliberately out of scope for this file: ``reference``
|
|
20
|
+
is one scalar for the whole device (the HAL accepts a per-channel sequence, but
|
|
21
|
+
the config schema does not), and per-channel ``gain``, ``power_down``,
|
|
22
|
+
``channels``, and ``buffered`` are not exposed at all. Those knobs live on the
|
|
23
|
+
task layer, where a channel index already exists.
|
|
24
|
+
|
|
25
|
+
Device entries are keyed by a **string** device id (``devices: {dac0: {...}}``),
|
|
26
|
+
not an integer. An id names a device in task payloads and telemetry, and a YAML
|
|
27
|
+
mapping key is a string anyway, so an ``int`` key would need silent coercion on
|
|
28
|
+
every hop. `dict[str, DacDeviceSettings]` decodes string keys verbatim and
|
|
29
|
+
round-trips through `msgspec.to_builtins` unchanged.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
from pathlib import Path
|
|
33
|
+
|
|
34
|
+
import msgspec
|
|
35
|
+
|
|
36
|
+
#: Remote-config section name the settings are registered under.
|
|
37
|
+
DAC_SECTION: str = "dac"
|
|
38
|
+
|
|
39
|
+
#: Subdirectory under the shared config dir that namespaces this service's
|
|
40
|
+
#: files. The framework's ``config.yml`` snapshot and the service-owned
|
|
41
|
+
#: ``dac.yml`` both live here, so services sharing one config dir (the
|
|
42
|
+
#: framework resolves a single dir for all scietex services) cannot collide.
|
|
43
|
+
DAC_CONFIG_SUBDIR: str = "dac"
|
|
44
|
+
|
|
45
|
+
#: Filename of the service-owned L1 bootstrap patch in the config subdirectory.
|
|
46
|
+
DAC_CONFIG_FILE: str = "dac.yml"
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class DacDeviceSettings(msgspec.Struct, frozen=True, forbid_unknown_fields=True):
|
|
50
|
+
"""One SPI DAC device, keyed by its id in `DacServiceSettings.devices`.
|
|
51
|
+
|
|
52
|
+
``type`` selects the HAL class through `devices.DAC_PARTS`. Every remaining
|
|
53
|
+
field is optional and forwarded to that class only when set, so the backend
|
|
54
|
+
keywords (``port``/``device`` SPI bus and chip-select) and the shared
|
|
55
|
+
keywords (``reference``, ``label``, ``log_level``) can be mixed as the part
|
|
56
|
+
and its transport require. ``None`` means "not configured": the constructor
|
|
57
|
+
default applies.
|
|
58
|
+
|
|
59
|
+
The trailing fields are part-specific: ``range``/``latch_pin`` for the
|
|
60
|
+
current-output and latched parts, ``load_pin`` for the load-strobed voltage
|
|
61
|
+
parts, and ``r_ab``/``grounded`` for the digital potentiometers. A part that
|
|
62
|
+
does not accept one of them rejects it at build time; the schema stays
|
|
63
|
+
uniform instead of growing 23 variants.
|
|
64
|
+
|
|
65
|
+
``strobe`` is deliberately absent: the HAL takes a driver object there,
|
|
66
|
+
while a config entry can only carry YAML scalars. Parts that need a load
|
|
67
|
+
line take ``load_pin`` (or ``latch_pin`` for AD420) as an integer GPIO pin
|
|
68
|
+
instead.
|
|
69
|
+
"""
|
|
70
|
+
|
|
71
|
+
type: str
|
|
72
|
+
port: int | None = None
|
|
73
|
+
device: int | None = None
|
|
74
|
+
clock_pin: int | str | None = None
|
|
75
|
+
mosi_pin: int | str | None = None
|
|
76
|
+
miso_pin: int | str | None = None
|
|
77
|
+
select_pin: int | str | None = None
|
|
78
|
+
reference: float | None = None
|
|
79
|
+
label: str | None = None
|
|
80
|
+
log_level: str | None = None
|
|
81
|
+
# part-specific parameters
|
|
82
|
+
range: str | None = None
|
|
83
|
+
load_pin: int | str | None = None
|
|
84
|
+
latch_pin: int | str | None = None
|
|
85
|
+
r_ab: float | None = None
|
|
86
|
+
grounded: str | None = None
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
class DacServiceSettings(msgspec.Struct, frozen=True, forbid_unknown_fields=True):
|
|
90
|
+
"""Top-level DAC service settings."""
|
|
91
|
+
|
|
92
|
+
devices: dict[str, DacDeviceSettings] = msgspec.field(default_factory=dict)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
#: Concrete L0 base for the framework's layered merge. A field absent from every
|
|
96
|
+
#: layer, or explicitly cleared with ``null``, resolves to the value here.
|
|
97
|
+
DAC_SETTINGS_DEFAULTS: DacServiceSettings = DacServiceSettings()
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def read_dac_config(conf_dir: Path | None, *, create_default: bool = True) -> dict[str, object]:
|
|
101
|
+
"""Read the L1 bootstrap patch from ``dac/dac.yml`` under the config directory.
|
|
102
|
+
|
|
103
|
+
The service's files are namespaced in a ``dac/`` subdirectory so they do
|
|
104
|
+
not collide with other services sharing the framework's single config dir.
|
|
105
|
+
The file (and, when missing, its directory) is only created when
|
|
106
|
+
``create_default=True`` (the bootstrap path). A ``None`` or non-directory
|
|
107
|
+
``conf_dir``, a missing file/directory with ``create_default=False``, or an
|
|
108
|
+
unparseable file each raise `RuntimeError`. An existing-but-invalid file is
|
|
109
|
+
left untouched regardless of ``create_default``.
|
|
110
|
+
|
|
111
|
+
The return value is the raw ``dac.yml`` mapping — the L1 patch the framework
|
|
112
|
+
merges onto the L0 base and validates through `DacServiceSettings`. When the
|
|
113
|
+
file is just created, the builtins of the default struct are returned so the
|
|
114
|
+
patch is equivalent to an empty override.
|
|
115
|
+
|
|
116
|
+
Args:
|
|
117
|
+
conf_dir: Path to the configuration directory.
|
|
118
|
+
create_default: Whether to create the directory and write a default
|
|
119
|
+
``dac.yml`` when missing. Default ``True``.
|
|
120
|
+
|
|
121
|
+
Returns:
|
|
122
|
+
The parsed ``dac.yml`` mapping as the L1 patch, or the builtins of the
|
|
123
|
+
default `DacServiceSettings` when the file was just created.
|
|
124
|
+
|
|
125
|
+
Raises:
|
|
126
|
+
RuntimeError: If ``conf_dir`` is ``None`` or not a directory, the file
|
|
127
|
+
is missing with ``create_default=False``, or the file cannot be parsed.
|
|
128
|
+
"""
|
|
129
|
+
if not isinstance(conf_dir, Path):
|
|
130
|
+
raise RuntimeError("Configuration dir was not set!")
|
|
131
|
+
service_dir = conf_dir / DAC_CONFIG_SUBDIR
|
|
132
|
+
if not service_dir.exists():
|
|
133
|
+
if create_default:
|
|
134
|
+
try:
|
|
135
|
+
service_dir.mkdir(parents=True, exist_ok=True)
|
|
136
|
+
except Exception as exc:
|
|
137
|
+
raise RuntimeError(f"Failed to create configuration directory {service_dir}!") from exc
|
|
138
|
+
else:
|
|
139
|
+
raise RuntimeError(
|
|
140
|
+
f"Configuration directory {service_dir} does not exist and create_default=False (no default generated)."
|
|
141
|
+
)
|
|
142
|
+
elif not service_dir.is_dir():
|
|
143
|
+
raise RuntimeError(f"Provided configuration directory path {service_dir} is not a directory!")
|
|
144
|
+
dac_yml = service_dir.joinpath(DAC_CONFIG_FILE)
|
|
145
|
+
if not dac_yml.exists():
|
|
146
|
+
if create_default:
|
|
147
|
+
settings = DacServiceSettings()
|
|
148
|
+
with open(dac_yml, "wb") as f:
|
|
149
|
+
f.write(msgspec.yaml.encode(settings))
|
|
150
|
+
return msgspec.to_builtins(settings)
|
|
151
|
+
raise RuntimeError(
|
|
152
|
+
f"DAC configuration file {dac_yml} does not exist and create_default=False "
|
|
153
|
+
"(pass create_default=True to generate defaults)."
|
|
154
|
+
)
|
|
155
|
+
try:
|
|
156
|
+
with open(dac_yml, "rb") as f:
|
|
157
|
+
return msgspec.yaml.decode(f.read(), type=dict)
|
|
158
|
+
except Exception as exc:
|
|
159
|
+
raise RuntimeError(
|
|
160
|
+
f"Failed to parse DAC configuration file {dac_yml}. Fix the file or remove it to regenerate defaults."
|
|
161
|
+
) from exc
|