pyosd-audio 0.2.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.
Files changed (39) hide show
  1. pyosd_audio-0.2.0/.gitignore +38 -0
  2. pyosd_audio-0.2.0/LICENSE +21 -0
  3. pyosd_audio-0.2.0/PKG-INFO +124 -0
  4. pyosd_audio-0.2.0/README.md +102 -0
  5. pyosd_audio-0.2.0/docs/api.md +41 -0
  6. pyosd_audio-0.2.0/docs/index.md +62 -0
  7. pyosd_audio-0.2.0/docs/protocol.md +126 -0
  8. pyosd_audio-0.2.0/docs/usage.md +158 -0
  9. pyosd_audio-0.2.0/pyproject.toml +70 -0
  10. pyosd_audio-0.2.0/src/pyosd/__init__.py +80 -0
  11. pyosd_audio-0.2.0/src/pyosd/devices/__init__.py +5 -0
  12. pyosd_audio-0.2.0/src/pyosd/devices/pam1270/__init__.py +50 -0
  13. pyosd_audio-0.2.0/src/pyosd/devices/pam1270/client.py +529 -0
  14. pyosd_audio-0.2.0/src/pyosd/devices/pam1270/codec.py +247 -0
  15. pyosd_audio-0.2.0/src/pyosd/devices/pam1270/service.py +947 -0
  16. pyosd_audio-0.2.0/src/pyosd/devices/pam1270/simulator.py +214 -0
  17. pyosd_audio-0.2.0/src/pyosd/devices/pam1270/tables.py +31 -0
  18. pyosd_audio-0.2.0/src/pyosd/domain/__init__.py +18 -0
  19. pyosd_audio-0.2.0/src/pyosd/domain/enums.py +31 -0
  20. pyosd_audio-0.2.0/src/pyosd/domain/state.py +46 -0
  21. pyosd_audio-0.2.0/src/pyosd/domain/values.py +160 -0
  22. pyosd_audio-0.2.0/src/pyosd/exceptions.py +65 -0
  23. pyosd_audio-0.2.0/src/pyosd/protocol/__init__.py +5 -0
  24. pyosd_audio-0.2.0/src/pyosd/protocol/framing.py +51 -0
  25. pyosd_audio-0.2.0/src/pyosd/py.typed +0 -0
  26. pyosd_audio-0.2.0/src/pyosd/transport/__init__.py +8 -0
  27. pyosd_audio-0.2.0/src/pyosd/transport/base.py +46 -0
  28. pyosd_audio-0.2.0/src/pyosd/transport/fake.py +61 -0
  29. pyosd_audio-0.2.0/src/pyosd/transport/serial.py +81 -0
  30. pyosd_audio-0.2.0/src/pyosd/transport/tcp.py +61 -0
  31. pyosd_audio-0.2.0/tests/conftest.py +20 -0
  32. pyosd_audio-0.2.0/tests/test_client.py +204 -0
  33. pyosd_audio-0.2.0/tests/test_codec.py +58 -0
  34. pyosd_audio-0.2.0/tests/test_framing.py +24 -0
  35. pyosd_audio-0.2.0/tests/test_hardware.py +189 -0
  36. pyosd_audio-0.2.0/tests/test_hardware_service.py +103 -0
  37. pyosd_audio-0.2.0/tests/test_service.py +542 -0
  38. pyosd_audio-0.2.0/tests/test_transport.py +63 -0
  39. pyosd_audio-0.2.0/tests/test_values.py +60 -0
@@ -0,0 +1,38 @@
1
+ # Vendor manual (copyrighted, scanned) and anything rendered from it
2
+ manuals/
3
+
4
+ # Python
5
+ __pycache__/
6
+ *.py[cod]
7
+ .venv/
8
+ dist/
9
+ build/
10
+ *.egg-info/
11
+
12
+ # Tooling
13
+ .cache/
14
+ .coverage
15
+ .coverage.*
16
+ coverage.xml
17
+ htmlcov/
18
+ # Reports the CI commands write when run locally
19
+ report.xml
20
+ code-quality.json
21
+ .pytest_cache/
22
+ .mypy_cache/
23
+ .ruff_cache/
24
+
25
+ # Docs
26
+ site/
27
+ public/
28
+
29
+ # Local secrets (publish tokens live in .devcontainer/.env)
30
+ .env
31
+ .env.*
32
+ !.env.example
33
+ .devcontainer/.env
34
+
35
+ # Editors and OS
36
+ .idea/
37
+ *.swp
38
+ .DS_Store
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 mvheimburg
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,124 @@
1
+ Metadata-Version: 2.5
2
+ Name: pyosd-audio
3
+ Version: 0.2.0
4
+ Summary: Python library for controlling OSD Audio devices, starting with the PAM1270 multi-zone amplifier
5
+ Project-URL: Homepage, https://gitlab.com/libs6952135/pyosd
6
+ Project-URL: Documentation, https://libs6952135.gitlab.io/pyosd/
7
+ Project-URL: Repository, https://gitlab.com/libs6952135/pyosd.git
8
+ Author-email: mvheimburg <heimburg@gmail.com>
9
+ License-Expression: MIT
10
+ License-File: LICENSE
11
+ Keywords: amplifier,home-automation,multi-zone,osd,osd-audio,pam1270,rs232
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3 :: Only
16
+ Classifier: Topic :: Home Automation
17
+ Classifier: Topic :: System :: Hardware
18
+ Classifier: Typing :: Typed
19
+ Requires-Python: >=3.10
20
+ Requires-Dist: pyserial>=3.5
21
+ Description-Content-Type: text/markdown
22
+
23
+ # pyosd
24
+
25
+ [![docs](https://img.shields.io/badge/docs-gitlab%20pages-blue)](https://libs6952135.gitlab.io/pyosd/)
26
+
27
+ Control OSD Audio devices from Python. The first supported device is the
28
+ **PAM1270** — a 12-channel / 6-zone commercial multi-zone amplifier controlled
29
+ over RS232, directly or through an IP-to-serial gateway.
30
+
31
+ > **Unofficial.** pyosd is an independent project, not affiliated with or endorsed by
32
+ > OSD Audio. "OSD Audio" and "PAM1270" are used only to name the hardware it talks to.
33
+
34
+ ```python
35
+ from pyosd import PAM1270, InputSource, Volume
36
+
37
+ with PAM1270.from_serial("/dev/ttyUSB0") as amp:
38
+ amp.zones[3].power = True
39
+ amp.zones[3].input_source = InputSource.LINE
40
+ amp.zones[3].volume = Volume.from_db(-25.0)
41
+
42
+ print(amp.zones[3].state)
43
+ ```
44
+
45
+ ## Install
46
+
47
+ The package is not on PyPI yet — releases go to this project's private GitLab
48
+ package registry. Authenticate with a deploy token (username = the deploy token
49
+ username) or a personal access token (username = the token's *name*):
50
+
51
+ ```bash
52
+ uv add pyosd-audio --index "https://<user>:<token>@gitlab.com/api/v4/projects/libs6952135%2Fpyosd/packages/pypi/simple"
53
+ ```
54
+
55
+ Or pin it in the consuming project's `pyproject.toml` and keep the credentials
56
+ in the environment:
57
+
58
+ ```toml
59
+ [[tool.uv.index]]
60
+ name = "pyosd"
61
+ url = "https://gitlab.com/api/v4/projects/libs6952135%2Fpyosd/packages/pypi/simple"
62
+ ```
63
+
64
+ ```bash
65
+ export UV_INDEX_PYOSD_USERNAME=<user> UV_INDEX_PYOSD_PASSWORD=<token>
66
+ uv add pyosd-audio
67
+ ```
68
+
69
+ From another project's CI, the built-in job token works: username
70
+ `gitlab-ci-token`, password `$CI_JOB_TOKEN`.
71
+
72
+ Fully type annotated and `py.typed`; no stubs needed.
73
+
74
+ ## Why it is shaped this way
75
+
76
+ - **A domain layer** (`pyosd.domain`) that knows about zones, volumes, tone and
77
+ balance — validated value objects that own the non-linear step-to-decibel
78
+ conversions, with no I/O and no device specifics.
79
+ - **A device client** (`pyosd.devices.pam1270`) that presents that model as an
80
+ ergonomic wrapper, encodes the wire protocol in pure functions, and keeps a
81
+ cache fed by the amplifier's unsolicited status broadcasts.
82
+ - **Transports** (`pyosd.transport`) for serial, TCP, and an in-memory fake.
83
+
84
+ Because the amplifier reports changes made by the IR remote and voltage
85
+ triggers, the client demultiplexes broadcasts from query answers rather than
86
+ assuming the next line answers the last write.
87
+
88
+ ## Testing without hardware
89
+
90
+ `PAM1270Simulator` implements the protocol as observed on real hardware, so the whole stack runs
91
+ in-process:
92
+
93
+ ```python
94
+ from pyosd import PAM1270, FakeTransport, PAM1270Simulator
95
+
96
+ amp = PAM1270(FakeTransport(PAM1270Simulator())).open()
97
+ amp.zones[1].volume = 25
98
+ assert amp.zones[1].volume.step == 25
99
+ ```
100
+
101
+ ## Development
102
+
103
+ ```bash
104
+ uv sync
105
+ uv run pytest
106
+ uv run ruff check . && uv run ruff format .
107
+ uv run ty check
108
+ uv run mkdocs serve
109
+ ```
110
+
111
+ ## Releasing
112
+
113
+ Bump `version` in `pyproject.toml` and merge to `main`. CI tags `v<version>` for you, publishes the
114
+ build to this project's GitLab package registry, and cuts a GitLab release. Publishing to
115
+ TestPyPI/PyPI is wired up but stays manual until the package goes public.
116
+
117
+ ## Documentation
118
+
119
+ <https://libs6952135.gitlab.io/pyosd/> — usage guide, the full PAM1270 RS232
120
+ protocol reference, and the API reference.
121
+
122
+ ## License
123
+
124
+ MIT — see [LICENSE](LICENSE).
@@ -0,0 +1,102 @@
1
+ # pyosd
2
+
3
+ [![docs](https://img.shields.io/badge/docs-gitlab%20pages-blue)](https://libs6952135.gitlab.io/pyosd/)
4
+
5
+ Control OSD Audio devices from Python. The first supported device is the
6
+ **PAM1270** — a 12-channel / 6-zone commercial multi-zone amplifier controlled
7
+ over RS232, directly or through an IP-to-serial gateway.
8
+
9
+ > **Unofficial.** pyosd is an independent project, not affiliated with or endorsed by
10
+ > OSD Audio. "OSD Audio" and "PAM1270" are used only to name the hardware it talks to.
11
+
12
+ ```python
13
+ from pyosd import PAM1270, InputSource, Volume
14
+
15
+ with PAM1270.from_serial("/dev/ttyUSB0") as amp:
16
+ amp.zones[3].power = True
17
+ amp.zones[3].input_source = InputSource.LINE
18
+ amp.zones[3].volume = Volume.from_db(-25.0)
19
+
20
+ print(amp.zones[3].state)
21
+ ```
22
+
23
+ ## Install
24
+
25
+ The package is not on PyPI yet — releases go to this project's private GitLab
26
+ package registry. Authenticate with a deploy token (username = the deploy token
27
+ username) or a personal access token (username = the token's *name*):
28
+
29
+ ```bash
30
+ uv add pyosd-audio --index "https://<user>:<token>@gitlab.com/api/v4/projects/libs6952135%2Fpyosd/packages/pypi/simple"
31
+ ```
32
+
33
+ Or pin it in the consuming project's `pyproject.toml` and keep the credentials
34
+ in the environment:
35
+
36
+ ```toml
37
+ [[tool.uv.index]]
38
+ name = "pyosd"
39
+ url = "https://gitlab.com/api/v4/projects/libs6952135%2Fpyosd/packages/pypi/simple"
40
+ ```
41
+
42
+ ```bash
43
+ export UV_INDEX_PYOSD_USERNAME=<user> UV_INDEX_PYOSD_PASSWORD=<token>
44
+ uv add pyosd-audio
45
+ ```
46
+
47
+ From another project's CI, the built-in job token works: username
48
+ `gitlab-ci-token`, password `$CI_JOB_TOKEN`.
49
+
50
+ Fully type annotated and `py.typed`; no stubs needed.
51
+
52
+ ## Why it is shaped this way
53
+
54
+ - **A domain layer** (`pyosd.domain`) that knows about zones, volumes, tone and
55
+ balance — validated value objects that own the non-linear step-to-decibel
56
+ conversions, with no I/O and no device specifics.
57
+ - **A device client** (`pyosd.devices.pam1270`) that presents that model as an
58
+ ergonomic wrapper, encodes the wire protocol in pure functions, and keeps a
59
+ cache fed by the amplifier's unsolicited status broadcasts.
60
+ - **Transports** (`pyosd.transport`) for serial, TCP, and an in-memory fake.
61
+
62
+ Because the amplifier reports changes made by the IR remote and voltage
63
+ triggers, the client demultiplexes broadcasts from query answers rather than
64
+ assuming the next line answers the last write.
65
+
66
+ ## Testing without hardware
67
+
68
+ `PAM1270Simulator` implements the protocol as observed on real hardware, so the whole stack runs
69
+ in-process:
70
+
71
+ ```python
72
+ from pyosd import PAM1270, FakeTransport, PAM1270Simulator
73
+
74
+ amp = PAM1270(FakeTransport(PAM1270Simulator())).open()
75
+ amp.zones[1].volume = 25
76
+ assert amp.zones[1].volume.step == 25
77
+ ```
78
+
79
+ ## Development
80
+
81
+ ```bash
82
+ uv sync
83
+ uv run pytest
84
+ uv run ruff check . && uv run ruff format .
85
+ uv run ty check
86
+ uv run mkdocs serve
87
+ ```
88
+
89
+ ## Releasing
90
+
91
+ Bump `version` in `pyproject.toml` and merge to `main`. CI tags `v<version>` for you, publishes the
92
+ build to this project's GitLab package registry, and cuts a GitLab release. Publishing to
93
+ TestPyPI/PyPI is wired up but stays manual until the package goes public.
94
+
95
+ ## Documentation
96
+
97
+ <https://libs6952135.gitlab.io/pyosd/> — usage guide, the full PAM1270 RS232
98
+ protocol reference, and the API reference.
99
+
100
+ ## License
101
+
102
+ MIT — see [LICENSE](LICENSE).
@@ -0,0 +1,41 @@
1
+ # API reference
2
+
3
+ ## Devices
4
+
5
+ ::: pyosd.devices.pam1270.client
6
+ options:
7
+ members: [PAM1270, Zone, Channel, UpdateSource]
8
+
9
+ ::: pyosd.devices.pam1270.service
10
+
11
+ ::: pyosd.devices.pam1270.simulator
12
+ options:
13
+ members: [PAM1270Simulator, SimulatedZone]
14
+
15
+ ## Domain
16
+
17
+ ::: pyosd.domain.values
18
+
19
+ ::: pyosd.domain.state
20
+
21
+ ::: pyosd.domain.enums
22
+
23
+ ## Protocol
24
+
25
+ ::: pyosd.devices.pam1270.codec
26
+
27
+ ::: pyosd.protocol.framing
28
+
29
+ ## Transport
30
+
31
+ ::: pyosd.transport.base
32
+
33
+ ::: pyosd.transport.serial
34
+
35
+ ::: pyosd.transport.tcp
36
+
37
+ ::: pyosd.transport.fake
38
+
39
+ ## Exceptions
40
+
41
+ ::: pyosd.exceptions
@@ -0,0 +1,62 @@
1
+ # pyosd
2
+
3
+ Control OSD Audio devices from Python. The first supported device is the
4
+ **PAM1270**, a 12-channel / 6-zone commercial amplifier controlled over RS232 —
5
+ directly, or through an IP-to-serial gateway.
6
+
7
+ !!! note "Unofficial"
8
+ pyosd is an independent project, not affiliated with or endorsed by OSD
9
+ Audio. "OSD Audio" and "PAM1270" are used only to name the hardware it
10
+ talks to.
11
+
12
+ ```python
13
+ from pyosd import PAM1270, InputSource, Volume
14
+
15
+ with PAM1270.from_serial("/dev/ttyUSB0") as amp:
16
+ amp.zones[3].power = True
17
+ amp.zones[3].input_source = InputSource.LINE
18
+ amp.zones[3].volume = Volume.from_db(-25.0)
19
+ ```
20
+
21
+ ## How the library is put together
22
+
23
+ The package is split into two layers, and the split is deliberate:
24
+
25
+ | Layer | Package | Responsibility |
26
+ | --- | --- | --- |
27
+ | Domain | `pyosd.domain` | Zones, volumes, tone, balance — validated value objects and the dB conversions. No I/O, no device specifics. |
28
+ | Device client | `pyosd.devices.<device>` | The ergonomic, stateful wrapper for one device, plus its pure wire-format codec. |
29
+ | Transport | `pyosd.transport` | Bytes over serial, TCP, or an in-memory fake. |
30
+
31
+ Anything you can do against real hardware, you can do against
32
+ [`PAM1270Simulator`](usage.md#testing-without-hardware) in a test.
33
+
34
+ ## Install
35
+
36
+ The package is not on PyPI yet — releases go to this project's private GitLab
37
+ package registry. Authenticate with a deploy token (username = the deploy token
38
+ username) or a personal access token (username = the token's *name*):
39
+
40
+ ```bash
41
+ uv add pyosd-audio --index "https://<user>:<token>@gitlab.com/api/v4/projects/libs6952135%2Fpyosd/packages/pypi/simple"
42
+ ```
43
+
44
+ Or pin it in the consuming project's `pyproject.toml` and keep the credentials
45
+ in the environment:
46
+
47
+ ```toml
48
+ [[tool.uv.index]]
49
+ name = "pyosd"
50
+ url = "https://gitlab.com/api/v4/projects/libs6952135%2Fpyosd/packages/pypi/simple"
51
+ ```
52
+
53
+ ```bash
54
+ export UV_INDEX_PYOSD_USERNAME=<user> UV_INDEX_PYOSD_PASSWORD=<token>
55
+ uv add pyosd-audio
56
+ ```
57
+
58
+ From another project's CI, the built-in job token works: username
59
+ `gitlab-ci-token`, password `$CI_JOB_TOKEN`.
60
+
61
+ The package ships type information (`py.typed`), so type checkers see the full
62
+ API without stubs.
@@ -0,0 +1,126 @@
1
+ # PAM1270 RS232 protocol
2
+
3
+ Transcribed from the PAM1270 V2 manual and corrected where real hardware
4
+ disagrees with it — see [Observed on hardware](#observed-on-hardware).
5
+ `pyosd.devices.pam1270.codec` implements all of it; this page is the reference
6
+ the code was written against.
7
+
8
+ ## Link settings
9
+
10
+ 9600 baud, 8 data bits, no parity, 1 stop bit, no flow control.
11
+
12
+ ## Frame shape
13
+
14
+ | Prefix | Meaning |
15
+ | --- | --- |
16
+ | `!` | command |
17
+ | `?` | query, and the response to one |
18
+ | `#` | unsolicited zone status |
19
+ | `+` | terminates every line |
20
+
21
+ Placeholders: `{z#}` zone `1..6` · `{c#}` channel `1..12` · `{0/1}` off/on ·
22
+ `{v#}` volume `0..38` · `{bt#}` bass/treble `0..14` · `{b#}` balance `0..63`.
23
+
24
+ ## Commands
25
+
26
+ | Function | Command | Notes |
27
+ | --- | --- | --- |
28
+ | Zone power / toggle | `!{z#}PR{0/1}+` · `!{z#}PT+` | `!2PR1+` on, `!1PR0+` off |
29
+ | Zone volume / up / down | `!{z#}VO{v#}+` · `!{z#}VI+` · `!{z#}VD+` | |
30
+ | Zone input / toggle | `!{z#}IS{0/1}+` · `!{z#}IT+` | `0` = bus, `1` = line |
31
+ | Channel input / toggle | `!{c#}CS{0/1}+` · `!{c#}CT+` | `0` = bus, `1` = line |
32
+ | Mute / toggle | `!{z#}MU{0/1}+` · `!{z#}MT+` | `1` = muted |
33
+ | Treble / up / down | `!{z#}TR{bt#}+` · `!{z#}TI+` · `!{z#}TD+` | |
34
+ | Bass / up / down | `!{z#}BS{bt#}+` · `!{z#}BI+` · `!{z#}BD+` | |
35
+ | Balance / step L / step R | `!{z#}BA{b#}+` · `!{z#}BL+` · `!{z#}BR+` | |
36
+ | Activity auto update | `!ZA{0/1}+` | global, not per zone |
37
+ | Max volume | `!{z#}MX+` | latches the zone's current volume as its ceiling |
38
+
39
+ The manual says nothing about replies, but the device acknowledges zone
40
+ commands by sending the resulting value as a query response: `!1VI+` →
41
+ `?1VO22+`. `ZA`, `CS` and `CT` are never acknowledged; `MX` is untested, so the
42
+ client sends it without waiting.
43
+
44
+ ## Queries
45
+
46
+ `?{z#}VO+` · `?{c#}CH+` · `?{z#}MU+` · `?{z#}PR+` · `?{z#}IS+` · `?{z#}TR+` ·
47
+ `?{z#}BS+` · `?{z#}BA+` · `?ZA+` · `?{z#}ZS+`
48
+
49
+ Each answers by echoing the query with the value filled in, e.g. `?4VO12+` —
50
+ except `?{c#}CH+`, which the device never answers (see below).
51
+
52
+ !!! note "A contradiction in the manual"
53
+ The manual's query examples print `?4VO49+` for a volume of 49 and reuse the
54
+ same example for the channel-volume query, which contradicts both the
55
+ documented `0..38` range and the `CH` response shape. The codec follows the
56
+ range and the grammar, and ignores frames it cannot parse rather than
57
+ failing the link.
58
+
59
+ ## Zone status
60
+
61
+ Every status line starts with `#`:
62
+
63
+ ```
64
+ #{z#}ZS VO{v#} PO{0/1} MU{0/1} IS{0/1}+ e.g. #6ZS VO8 PO1 MU0 IS0+
65
+ ```
66
+
67
+ With `!ZA1+` enabled the device sends this whenever a zone changes — about a
68
+ second after activity in that zone stops — so IR-remote and voltage-trigger
69
+ changes show up asynchronously. `?{z#}ZS+` requests the same line on demand.
70
+ RS232 commands do not trigger it; their acknowledgement takes its place.
71
+
72
+ ## Level tables
73
+
74
+ **Volume** `0..38` → attenuation. The step size widens twice, so this is a
75
+ table, not a formula:
76
+
77
+ | Steps | dB per step | Range |
78
+ | --- | --- | --- |
79
+ | 38 → 18 | −1.25 | 0.00 → −25.00 |
80
+ | 18 → 7 | −2.50 | −25.00 → −52.50 |
81
+ | 7 → 0 | −3.75 | −52.50 → −78.75 |
82
+
83
+ **Bass / treble** `0..14`: `7` is flat, ±2 dB per step, so `14` = +14 dB and
84
+ `0` = −14 dB.
85
+
86
+ **Balance** `0..63`: `31` and `32` are centre, `0` mutes the right channel and
87
+ `63` mutes the left; every step attenuates the opposite channel by 1.25 dB.
88
+
89
+ Tone-flat and balance-centre are reachable **only** over RS232 — the IR remote
90
+ has no command for either.
91
+
92
+ ## What the protocol cannot tell you
93
+
94
+ - Each zone has a Remote Bypass switch that disables IR *and* RS232 control for
95
+ that zone. Commands to a bypassed zone get no acknowledgement, which the
96
+ client reports as `CommandNotAcknowledged`, but nothing says *why*.
97
+ - Zone hardware configuration — 8 Ω/4 Ω versus 70 V/100 V, stereo versus
98
+ bridged, limiter on or off — is set by physical switches with no read-back.
99
+
100
+ So even an acknowledged command can have no audible effect — and with no
101
+ speakers connected, nothing will be heard at all.
102
+
103
+ ## Observed on hardware
104
+
105
+ Verified against a PAM1270 V2 over a USB-RS232 (FTDI) cable with byte-level
106
+ traces. `tests/test_hardware.py` pins the command and query behaviour (all but
107
+ the last two rows, which need someone at the DIP switches) and runs against a
108
+ real amplifier with `PYOSD_PAM1270_PORT=/dev/ttyUSB0 uv run pytest -m hardware`.
109
+
110
+ | Behaviour | What the client does about it |
111
+ | --- | --- |
112
+ | Every zone command tried (all but `MX`) is acknowledged with the resulting value, e.g. `!1VI+` → `?1VO22+`. | Waits for the acknowledgement and raises `CommandNotAcknowledged` when none comes. |
113
+ | A line sent while the device is still transmitting can make it drop the `+` from its reply (`?1VO20?1VO20+`), losing both frames. | Waiting for each acknowledgement keeps the link paced. |
114
+ | A powered-off zone ignores every command except `PR`/`PT`, silently. | Surfaces as `CommandNotAcknowledged`. |
115
+ | `PR` to the state the zone is already in gets no reply at all; `PT` always replies. | The power setter reads the state first and sends nothing if it already matches. |
116
+ | An input change (`IS`/`IT`) within about 2.1 s of the previous accepted one is silently dropped. | Spaces input commands to the same zone at least `INPUT_LOCKOUT` (2.5 s) apart. |
117
+ | Power off mutes the zone; power on leaves it muted. | Nothing — unmute explicitly after switching a zone on. |
118
+ | `?6BS+` is answered labelled as zone 1 (`?1BS7+`); the value is zone 6's bass. Every other query on every zone is labelled correctly. | Relabels that answer to zone 6 (`MISLABELLED_ANSWERS` in `tables.py`), so zone 1's cache is not corrupted. |
119
+ | `?{c#}CH+` gets no reply for any channel, in any form tried. | `Channel.volume` raises `NotImplementedError`; read the zone's volume. |
120
+ | Remote Bypass is read live — flipping a switch makes the device report the zone's power (`?1PR0+`). A bypassed zone still answers queries but ignores commands, and runs powered on and unmuted. | Commands to it raise `CommandNotAcknowledged`. |
121
+ | A zone under RS232 control (bypass OFF) comes up powered off and muted at boot. At boot the device also reports each zone's power unprompted as `?{z#}PR{0/1}+`. | Those lines are folded into the cache like any other value. |
122
+
123
+ Untested so far: whether the input lockout spans zones (only one zone was under
124
+ RS232 control), whether `MX` replies, what `CS` and `CT` do beyond not replying, and whether
125
+ the `!6BS{bt#}+` acknowledgement is mislabelled like the query (zone 6 was bypassed); the client
126
+ accepts either label.
@@ -0,0 +1,158 @@
1
+ # Usage
2
+
3
+ ## Connecting
4
+
5
+ ```python
6
+ from pyosd import PAM1270
7
+
8
+ amp = PAM1270.from_serial("/dev/ttyUSB0") # 9600 8N1, per the manual
9
+ amp = PAM1270.from_tcp("192.168.1.50", 4999) # IP-to-serial gateway
10
+ ```
11
+
12
+ Both accept `timeout=` (seconds to wait for a query answer) and work as context
13
+ managers, which open and close the link:
14
+
15
+ ```python
16
+ with PAM1270.from_serial("/dev/ttyUSB0") as amp:
17
+ ...
18
+ ```
19
+
20
+ ## Zones and channels
21
+
22
+ Zones are addressed `1..6`, channels `1..12`. Reading a property queries the
23
+ device and blocks until it answers; assigning one sends a command.
24
+
25
+ ```python
26
+ zone = amp.zones[2]
27
+
28
+ zone.power = True
29
+ zone.volume = 30 # a step, or a Volume
30
+ zone.mute = False
31
+ zone.input_source = InputSource.LINE
32
+
33
+ zone.volume_up()
34
+ zone.toggle_mute()
35
+ ```
36
+
37
+ Two resets are worth knowing about, because the IR remote cannot do them at all:
38
+
39
+ ```python
40
+ zone.flatten_tone() # bass and treble exactly flat
41
+ zone.center_balance() # balance exactly centred
42
+ ```
43
+
44
+ `zone.lock_max_volume()` latches the zone's *current* volume as its ceiling —
45
+ set the volume first, then lock it.
46
+
47
+ ## Values and decibels
48
+
49
+ Every quantity is an integer step with a non-linear dB mapping, so convert
50
+ through the value objects rather than by arithmetic:
51
+
52
+ ```python
53
+ from pyosd import Balance, Tone, Volume
54
+
55
+ Volume(38).db # 0.0
56
+ Volume(7).db # -52.5
57
+ Volume.from_db(-25.0) # Volume(step=18)
58
+ Tone.flat().db # 0.0
59
+ Balance(0).right_db # None -- the right channel is muted
60
+ ```
61
+
62
+ ## Reacting to changes made elsewhere
63
+
64
+ The amplifier can broadcast a zone's state whenever anything changes it,
65
+ including the IR remote and voltage triggers. Enable it once, then poll:
66
+
67
+ ```python
68
+ amp.set_auto_update(True)
69
+ amp.subscribe(lambda state: print(state.zone, state.volume, state.power))
70
+
71
+ while True:
72
+ amp.poll(timeout=0.5) # dispatches broadcasts, updates the cache
73
+ ```
74
+
75
+ `zone.state` is the cached view and never touches the wire; `zone.refresh()`
76
+ forces a full status query. Every command waits for the device's
77
+ acknowledgement, which updates the cache and reaches subscribers, so
78
+ `zone.state` is current as soon as an assignment returns. Changes made
79
+ elsewhere arrive as broadcasts about a second *after* activity in a zone stops.
80
+
81
+ ## When a command is ignored
82
+
83
+ Assigning a property raises `CommandNotAcknowledged` (a `ResponseTimeout`) if
84
+ the device does not confirm it. The usual causes are a powered-off zone, which
85
+ accepts only power commands, and a zone whose Remote Bypass switch is ON.
86
+ Switching a zone on leaves it muted:
87
+
88
+ ```python
89
+ zone = amp.zones[1]
90
+ zone.power = True
91
+ zone.mute = False # power on does not unmute
92
+ ```
93
+
94
+ Input changes to one zone are spaced 2.5 s apart automatically, because the
95
+ device drops a second change that follows the first too closely.
96
+
97
+ ## Sharing one amplifier: `AmpService`
98
+
99
+ `PAM1270` is single-threaded and owns the port. When several parts of an application need the
100
+ amp, run an `AmpService` instead. It owns the connection on one worker thread, polls between
101
+ requests, reconnects with backoff, and never replays a write:
102
+
103
+ ```python
104
+ from pyosd import AmpService, SafetyPolicy, ZoneUpdate
105
+
106
+ policy = SafetyPolicy(max_volume=30, zone_max_volume={1: 26}) # caps are your choice
107
+ port = "/dev/serial/by-id/usb-FTDI_USB_HS_SERIAL_CONVERTER_FTFNVHL4-if00-port0"
108
+
109
+ with AmpService.for_serial(port, policy) as service:
110
+ service.wait_until_connected(timeout=10)
111
+ service.update_zone(1, ZoneUpdate(power=True, volume_step=20, mute=False))
112
+ service.nudge_volume(1, -2)
113
+ print(service.snapshot().zones[1].state)
114
+
115
+ with service.subscribe() as events:
116
+ for event in events: # connection, zone, command-failure and limit events
117
+ print(event.kind, event.zone, event.source)
118
+ ```
119
+
120
+ An update is validated in full before anything is sent. It is applied power first and mute last,
121
+ and it stops at the first failed command with `UpdateFailed`, whose `failure` lists what was
122
+ confirmed, what failed (`applied`, `rejected` or `unknown` after a read-back), and what was never
123
+ attempted. Every method is thread-safe; async applications call it from a worker thread.
124
+
125
+ ## Testing without hardware
126
+
127
+ `PAM1270Simulator` implements the protocol as observed on real hardware and plugs into
128
+ `FakeTransport`, so the whole stack runs in-process:
129
+
130
+ ```python
131
+ from pyosd import PAM1270, FakeTransport, PAM1270Simulator
132
+
133
+ simulator = PAM1270Simulator()
134
+ amp = PAM1270(FakeTransport(simulator)).open()
135
+
136
+ amp.zones[1].volume = 25
137
+ assert amp.zones[1].volume.step == 25
138
+ ```
139
+
140
+ To simulate an unsolicited change — someone pressing the IR remote — mutate the
141
+ simulator and feed the resulting line to the transport:
142
+
143
+ ```python
144
+ simulator.zones[6].volume = 14
145
+ transport.feed(simulator.status_line(6).encode())
146
+ amp.poll()
147
+ ```
148
+
149
+ ## Testing against a real amplifier
150
+
151
+ `tests/test_hardware.py` is skipped unless `PYOSD_PAM1270_PORT` names the serial
152
+ port. It drives one zone (`PYOSD_PAM1270_ZONE`, default `1`, whose Remote Bypass
153
+ switch must be OFF) and restores it afterwards; it never sends `MX` or `ZA`.
154
+
155
+ ```bash
156
+ PYOSD_PAM1270_PORT=/dev/ttyUSB0 uv run pytest -m hardware -v
157
+ uv run python scripts/pam_diag.py serial /dev/ttyUSB0 # read-only health check
158
+ ```