apt-thorlabs 0.0.2__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.
- apt_thorlabs-0.0.2/.github/workflows/publish.yml +29 -0
- apt_thorlabs-0.0.2/.github/workflows/test.yml +92 -0
- apt_thorlabs-0.0.2/.gitignore +207 -0
- apt_thorlabs-0.0.2/LICENSE +21 -0
- apt_thorlabs-0.0.2/PKG-INFO +14 -0
- apt_thorlabs-0.0.2/README.md +2 -0
- apt_thorlabs-0.0.2/apps/thorlabs_apt_device.py +55 -0
- apt_thorlabs-0.0.2/pyproject.toml +117 -0
- apt_thorlabs-0.0.2/tests/test_device.py +134 -0
- apt_thorlabs-0.0.2/tests/test_packet.py +147 -0
- apt_thorlabs-0.0.2/thorlabs_apt/__init__.py +3 -0
- apt_thorlabs-0.0.2/thorlabs_apt/device.py +105 -0
- apt_thorlabs-0.0.2/thorlabs_apt/kbd101.py +18 -0
- apt_thorlabs-0.0.2/thorlabs_apt/kpa101.py +277 -0
- apt_thorlabs-0.0.2/thorlabs_apt/motor_controller.py +165 -0
- apt_thorlabs-0.0.2/thorlabs_apt/packet.py +203 -0
- apt_thorlabs-0.0.2/tox.ini +32 -0
- apt_thorlabs-0.0.2/uv.lock +343 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
name: Publish Thorlabs APT
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [published]
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
publish:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
environment: pypi
|
|
11
|
+
|
|
12
|
+
permissions:
|
|
13
|
+
contents: read
|
|
14
|
+
id-token: write
|
|
15
|
+
|
|
16
|
+
steps:
|
|
17
|
+
|
|
18
|
+
- uses: actions/checkout@v6
|
|
19
|
+
with:
|
|
20
|
+
persist-credentials: false
|
|
21
|
+
|
|
22
|
+
- name: Install uv
|
|
23
|
+
uses: astral-sh/setup-uv@v6
|
|
24
|
+
|
|
25
|
+
- name: Build package
|
|
26
|
+
run: uv build
|
|
27
|
+
|
|
28
|
+
- name: Publish to PyPI
|
|
29
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- main
|
|
7
|
+
pull_request:
|
|
8
|
+
|
|
9
|
+
env:
|
|
10
|
+
UV_USE_ACTIVE_ENV: 1
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
lint:
|
|
14
|
+
name: Linting
|
|
15
|
+
runs-on: ubuntu-latest
|
|
16
|
+
|
|
17
|
+
steps:
|
|
18
|
+
- uses: actions/checkout@v4
|
|
19
|
+
|
|
20
|
+
- uses: actions/setup-python@v5
|
|
21
|
+
with:
|
|
22
|
+
python-version: "3.12"
|
|
23
|
+
|
|
24
|
+
- name: Install uv
|
|
25
|
+
uses: yezz123/setup-uv@v4
|
|
26
|
+
with:
|
|
27
|
+
uv-version: 0.12.5
|
|
28
|
+
|
|
29
|
+
- name: Install tox
|
|
30
|
+
run: pip install tox
|
|
31
|
+
|
|
32
|
+
- name: Run lint
|
|
33
|
+
run: tox -e lint
|
|
34
|
+
|
|
35
|
+
type:
|
|
36
|
+
name: Type check
|
|
37
|
+
runs-on: ubuntu-latest
|
|
38
|
+
needs: lint
|
|
39
|
+
|
|
40
|
+
steps:
|
|
41
|
+
- uses: actions/checkout@v4
|
|
42
|
+
|
|
43
|
+
- uses: actions/setup-python@v5
|
|
44
|
+
with:
|
|
45
|
+
python-version: "3.12"
|
|
46
|
+
|
|
47
|
+
- name: Install uv
|
|
48
|
+
uses: yezz123/setup-uv@v4
|
|
49
|
+
with:
|
|
50
|
+
uv-version: 0.12.5
|
|
51
|
+
|
|
52
|
+
- name: Install tox
|
|
53
|
+
run: pip install tox
|
|
54
|
+
|
|
55
|
+
- name: Run type check
|
|
56
|
+
run: tox -e type
|
|
57
|
+
|
|
58
|
+
test:
|
|
59
|
+
name: Tests
|
|
60
|
+
needs: type
|
|
61
|
+
|
|
62
|
+
strategy:
|
|
63
|
+
fail-fast: false
|
|
64
|
+
matrix:
|
|
65
|
+
os:
|
|
66
|
+
- ubuntu-latest
|
|
67
|
+
- macos-latest
|
|
68
|
+
- windows-latest
|
|
69
|
+
python-version:
|
|
70
|
+
- "3.12"
|
|
71
|
+
- "3.13"
|
|
72
|
+
- "3.14"
|
|
73
|
+
|
|
74
|
+
runs-on: ${{ matrix.os }}
|
|
75
|
+
steps:
|
|
76
|
+
- uses: actions/checkout@v4
|
|
77
|
+
|
|
78
|
+
- uses: actions/setup-python@v5
|
|
79
|
+
with:
|
|
80
|
+
python-version: ${{ matrix.python-version }}
|
|
81
|
+
|
|
82
|
+
- name: Install uv
|
|
83
|
+
uses: yezz123/setup-uv@v4
|
|
84
|
+
with:
|
|
85
|
+
uv-version: 0.12.5
|
|
86
|
+
|
|
87
|
+
- name: Install tox
|
|
88
|
+
run: pip install tox
|
|
89
|
+
|
|
90
|
+
- name: Run tests
|
|
91
|
+
run: tox -e test
|
|
92
|
+
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
#uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
#poetry.lock
|
|
109
|
+
#poetry.toml
|
|
110
|
+
|
|
111
|
+
# pdm
|
|
112
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
113
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
114
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
115
|
+
#pdm.lock
|
|
116
|
+
#pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# pixi
|
|
121
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
122
|
+
#pixi.lock
|
|
123
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
124
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
125
|
+
.pixi
|
|
126
|
+
|
|
127
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
128
|
+
__pypackages__/
|
|
129
|
+
|
|
130
|
+
# Celery stuff
|
|
131
|
+
celerybeat-schedule
|
|
132
|
+
celerybeat.pid
|
|
133
|
+
|
|
134
|
+
# SageMath parsed files
|
|
135
|
+
*.sage.py
|
|
136
|
+
|
|
137
|
+
# Environments
|
|
138
|
+
.env
|
|
139
|
+
.envrc
|
|
140
|
+
.venv
|
|
141
|
+
env/
|
|
142
|
+
venv/
|
|
143
|
+
ENV/
|
|
144
|
+
env.bak/
|
|
145
|
+
venv.bak/
|
|
146
|
+
|
|
147
|
+
# Spyder project settings
|
|
148
|
+
.spyderproject
|
|
149
|
+
.spyproject
|
|
150
|
+
|
|
151
|
+
# Rope project settings
|
|
152
|
+
.ropeproject
|
|
153
|
+
|
|
154
|
+
# mkdocs documentation
|
|
155
|
+
/site
|
|
156
|
+
|
|
157
|
+
# mypy
|
|
158
|
+
.mypy_cache/
|
|
159
|
+
.dmypy.json
|
|
160
|
+
dmypy.json
|
|
161
|
+
|
|
162
|
+
# Pyre type checker
|
|
163
|
+
.pyre/
|
|
164
|
+
|
|
165
|
+
# pytype static type analyzer
|
|
166
|
+
.pytype/
|
|
167
|
+
|
|
168
|
+
# Cython debug symbols
|
|
169
|
+
cython_debug/
|
|
170
|
+
|
|
171
|
+
# PyCharm
|
|
172
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
173
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
174
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
175
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
176
|
+
#.idea/
|
|
177
|
+
|
|
178
|
+
# Abstra
|
|
179
|
+
# Abstra is an AI-powered process automation framework.
|
|
180
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
181
|
+
# Learn more at https://abstra.io/docs
|
|
182
|
+
.abstra/
|
|
183
|
+
|
|
184
|
+
# Visual Studio Code
|
|
185
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
186
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
187
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
188
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
189
|
+
# .vscode/
|
|
190
|
+
|
|
191
|
+
# Ruff stuff:
|
|
192
|
+
.ruff_cache/
|
|
193
|
+
|
|
194
|
+
# PyPI configuration file
|
|
195
|
+
.pypirc
|
|
196
|
+
|
|
197
|
+
# Cursor
|
|
198
|
+
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
|
|
199
|
+
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
|
200
|
+
# refer to https://docs.cursor.com/context/ignore-files
|
|
201
|
+
.cursorignore
|
|
202
|
+
.cursorindexingignore
|
|
203
|
+
|
|
204
|
+
# Marimo
|
|
205
|
+
marimo/_static/
|
|
206
|
+
marimo/_lsp/
|
|
207
|
+
__marimo__/
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 davidbakker123
|
|
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,14 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: apt-thorlabs
|
|
3
|
+
Version: 0.0.2
|
|
4
|
+
Summary: Python implementation of the Thorlabs APT communication protocol.
|
|
5
|
+
Author-email: David Bakker <david.bakker@tno.nl>
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Keywords: control,device
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Requires-Python: >=3.12
|
|
10
|
+
Requires-Dist: pyserial>=3.5
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
|
|
13
|
+
# thorlabs-apt
|
|
14
|
+
Python implementation of the Thorlabs APT communication protocol
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from thorlabs_apt.kpa101 import KPA101, OperationMode
|
|
4
|
+
|
|
5
|
+
MGMSG_MOD_IDENTIFY = 0x0223
|
|
6
|
+
|
|
7
|
+
MGMSG_HW_REQ_INFO = 0x0005
|
|
8
|
+
MGMSG_HW_GET_INFO = 0x0006
|
|
9
|
+
|
|
10
|
+
# K-Cube Position Aligner
|
|
11
|
+
|
|
12
|
+
if __name__ == "__main__":
|
|
13
|
+
from thorlabs_apt.device import list_devices
|
|
14
|
+
|
|
15
|
+
print(list_devices())
|
|
16
|
+
# dev = KBD101(serial_number="28251738")
|
|
17
|
+
# print(dev._hw_info)
|
|
18
|
+
#
|
|
19
|
+
kpa = KPA101(serial_number="69252608A")
|
|
20
|
+
print(kpa._hw_info)
|
|
21
|
+
|
|
22
|
+
# dev.enable_channel(1)
|
|
23
|
+
# print(dev.get_velocity_params())
|
|
24
|
+
# d = dev.get_stage_params()
|
|
25
|
+
|
|
26
|
+
# print(dev.get_homing_params())
|
|
27
|
+
# dev.home()
|
|
28
|
+
# print(dev.position())
|
|
29
|
+
# dev.move_relative(1.0)
|
|
30
|
+
|
|
31
|
+
# print(dev.position())
|
|
32
|
+
# dev.move_relative(1.0)
|
|
33
|
+
# print(dev.position())
|
|
34
|
+
|
|
35
|
+
# dev.move_absolute(10.0)
|
|
36
|
+
|
|
37
|
+
# print(dev.position())
|
|
38
|
+
|
|
39
|
+
# kpa = KPA101(serial_number="69252738")
|
|
40
|
+
|
|
41
|
+
print(kpa.position_demand_params)
|
|
42
|
+
|
|
43
|
+
kpa.operation_mode = OperationMode.OPEN_LOOP
|
|
44
|
+
print(kpa.operation_mode)
|
|
45
|
+
print(kpa.status_bits)
|
|
46
|
+
print(kpa.display_settings)
|
|
47
|
+
# print(kpa.position)
|
|
48
|
+
# kpa.position = (1.0, 1.0)
|
|
49
|
+
# print(kpa.position)
|
|
50
|
+
print(kpa.readings)
|
|
51
|
+
print(kpa.loop_params_2)
|
|
52
|
+
print(kpa.trig_io_config)
|
|
53
|
+
# while True:
|
|
54
|
+
# print(kpa.readings)
|
|
55
|
+
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "apt-thorlabs"
|
|
3
|
+
version = "0.0.2"
|
|
4
|
+
description = "Python implementation of the Thorlabs APT communication protocol."
|
|
5
|
+
authors = [
|
|
6
|
+
{ name = "David Bakker", email = "david.bakker@tno.nl" },
|
|
7
|
+
]
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
requires-python = ">=3.12"
|
|
10
|
+
keywords = ["device", "control"]
|
|
11
|
+
dependencies = [
|
|
12
|
+
"pyserial>=3.5",
|
|
13
|
+
]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
[dependency-groups]
|
|
19
|
+
dev = [
|
|
20
|
+
"coverage>=7.6.0",
|
|
21
|
+
"ty>=0.0.69",
|
|
22
|
+
"tox>=4.52.0",
|
|
23
|
+
"pytest>=8.2.2",
|
|
24
|
+
"ruff>=0.5,<0.13"
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
[tool.ruff]
|
|
28
|
+
line-length = 120
|
|
29
|
+
target-version = "py312"
|
|
30
|
+
exclude = ["apps"]
|
|
31
|
+
|
|
32
|
+
[tool.ruff.lint]
|
|
33
|
+
preview = true
|
|
34
|
+
select = [
|
|
35
|
+
"A", # flake8-builtins
|
|
36
|
+
"ANN", # flake8-annotations
|
|
37
|
+
"ASYNC", # flake8-async
|
|
38
|
+
"B", # flake8-bugbear
|
|
39
|
+
"BLE", # flake8-blind-except
|
|
40
|
+
"COM", #flake8-commas
|
|
41
|
+
"C4", #flake8-comprehensions
|
|
42
|
+
"C90", # mccabe
|
|
43
|
+
#"D", # pydocstyle
|
|
44
|
+
"DTZ", # flake8-datetimez
|
|
45
|
+
"E", # pycodestyle (error)
|
|
46
|
+
"ERA", # eradicate
|
|
47
|
+
"EM", # flake8-errmsg
|
|
48
|
+
"EXE", # flake8-executable
|
|
49
|
+
"F", # Pyflakes
|
|
50
|
+
"FA", # flake8-future-annotations
|
|
51
|
+
"FBT", # flake8-boolean-trap
|
|
52
|
+
"FIX", # flake8-fixme
|
|
53
|
+
"FLY", # flynt
|
|
54
|
+
"G", # flake8-logging-format
|
|
55
|
+
"I", # isort
|
|
56
|
+
"ISC", # flake8-implicit-str-concat
|
|
57
|
+
"ICN", # flake8-import-conventions
|
|
58
|
+
"LOG", # flake8-logging
|
|
59
|
+
"N", # pep8-naming
|
|
60
|
+
"NPY", # NumPy-specific rules
|
|
61
|
+
"PD", # pandas-vet
|
|
62
|
+
"PERF", # Perflint
|
|
63
|
+
"PIE", # flake8-pie
|
|
64
|
+
"PT", # flake8-pytest-style
|
|
65
|
+
"PTH", # flake8-use-pathlib
|
|
66
|
+
"FURB", # refurb
|
|
67
|
+
"RET", # flake8-return
|
|
68
|
+
"RUF", # Ruff-specific rules
|
|
69
|
+
"RUF022",
|
|
70
|
+
"S", # flake8-bandit
|
|
71
|
+
"SIM", # flake8-simplify
|
|
72
|
+
"SLF", # flake8-self
|
|
73
|
+
"SLOT", # flake8-slots
|
|
74
|
+
"TCH", # flake8-type-checking
|
|
75
|
+
"TRY", # tryceratops
|
|
76
|
+
"T20", # flake8-print
|
|
77
|
+
"T100", # flake8-debugger
|
|
78
|
+
"UP", # pyupgrade
|
|
79
|
+
"W", # pycodestyle (warning)
|
|
80
|
+
"Q", # flake8-quotes
|
|
81
|
+
"YTT", # flake8-2020
|
|
82
|
+
]
|
|
83
|
+
ignore = [
|
|
84
|
+
"A005", # module shadowing a Python builtin module
|
|
85
|
+
"ANN401", # typing.Any can be useful (for example in __eq__)
|
|
86
|
+
"B027", # Ignore the rule for empty methods in abstract base classes
|
|
87
|
+
"COM812", # Conflicts with ruff format
|
|
88
|
+
"ISC001", # Possible conflicts with ruff format
|
|
89
|
+
"W605", # deprecated in Python 3.6
|
|
90
|
+
"A001",
|
|
91
|
+
"A002",
|
|
92
|
+
"N801",
|
|
93
|
+
"FBT001",
|
|
94
|
+
]
|
|
95
|
+
isort.combine-as-imports = true
|
|
96
|
+
pydocstyle.convention = "google"
|
|
97
|
+
|
|
98
|
+
[tool.ruff.lint.extend-per-file-ignores]
|
|
99
|
+
"*.py" = [
|
|
100
|
+
"S101", # asserts allowed everywhere
|
|
101
|
+
]
|
|
102
|
+
"tests/**/*.py" = [
|
|
103
|
+
"FBT", # Don't care about booleans as positional arguments in tests, e.g. via @pytest.mark.parametrize()
|
|
104
|
+
"D", # No doc requirements in tests
|
|
105
|
+
"SLF001", # Testing private functions is OK
|
|
106
|
+
]
|
|
107
|
+
|
|
108
|
+
[build-system]
|
|
109
|
+
requires = ["hatchling"]
|
|
110
|
+
build-backend = "hatchling.build"
|
|
111
|
+
|
|
112
|
+
[tool.hatch.metadata]
|
|
113
|
+
allow-direct-references = true
|
|
114
|
+
|
|
115
|
+
[tool.hatch.build.targets.wheel]
|
|
116
|
+
packages = ["thorlabs_apt"]
|
|
117
|
+
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
from types import SimpleNamespace
|
|
2
|
+
from unittest.mock import MagicMock
|
|
3
|
+
|
|
4
|
+
import pytest
|
|
5
|
+
import serial
|
|
6
|
+
|
|
7
|
+
from thorlabs_apt.device import APTDevice, find_device, list_devices
|
|
8
|
+
from thorlabs_apt.packet import (
|
|
9
|
+
MGMSG_HW_GET_INFO,
|
|
10
|
+
APTPacket,
|
|
11
|
+
HardwareInfo,
|
|
12
|
+
get_identify_packet,
|
|
13
|
+
request_hardware_info_packet,
|
|
14
|
+
set_channel_enable_state_packet,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@pytest.fixture
|
|
19
|
+
def ports(monkeypatch: pytest.MonkeyPatch) -> list[SimpleNamespace]:
|
|
20
|
+
available_ports = [
|
|
21
|
+
SimpleNamespace(
|
|
22
|
+
device="COM3",
|
|
23
|
+
serial_number="12345678",
|
|
24
|
+
manufacturer="Thorlabs",
|
|
25
|
+
product="KDC101",
|
|
26
|
+
vid=0x1313,
|
|
27
|
+
pid=0x2019,
|
|
28
|
+
),
|
|
29
|
+
SimpleNamespace(
|
|
30
|
+
device="COM4",
|
|
31
|
+
serial_number="87654321",
|
|
32
|
+
manufacturer=None,
|
|
33
|
+
product=None,
|
|
34
|
+
vid=None,
|
|
35
|
+
pid=None,
|
|
36
|
+
),
|
|
37
|
+
]
|
|
38
|
+
monkeypatch.setattr("thorlabs_apt.device.serial.tools.list_ports.comports", lambda: available_ports)
|
|
39
|
+
return available_ports
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class TestDeviceDiscovery:
|
|
43
|
+
def test_find_device_returns_port_for_matching_serial_number(self, ports: list[SimpleNamespace]) -> None:
|
|
44
|
+
assert find_device("87654321") == "COM4"
|
|
45
|
+
|
|
46
|
+
def test_find_device_returns_none_when_serial_number_is_not_found(self, ports: list[SimpleNamespace]) -> None:
|
|
47
|
+
assert find_device("missing") is None
|
|
48
|
+
|
|
49
|
+
def test_list_devices_formats_all_enumerated_ports(self, ports: list[SimpleNamespace]) -> None:
|
|
50
|
+
assert list_devices() == (
|
|
51
|
+
"device=COM3, manufacturer=Thorlabs, product=KDC101, vid=0x1313, pid=0x2019,serial_number=12345678\n"
|
|
52
|
+
"device=COM4, manufacturer=None, product=None, vid=None, pid=None,serial_number=87654321"
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def data_packet(message_id: int, data: bytes) -> bytes:
|
|
57
|
+
packet = APTPacket(message_id=message_id, destination=0x50, source=0x01)
|
|
58
|
+
packet.data_packet_length = len(data)
|
|
59
|
+
return packet.tobytes()
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
@pytest.fixture
|
|
63
|
+
def serial_port(monkeypatch: pytest.MonkeyPatch) -> MagicMock:
|
|
64
|
+
hardware_info = HardwareInfo(serial_number=12345678)
|
|
65
|
+
port = MagicMock()
|
|
66
|
+
port.read.side_effect = [data_packet(MGMSG_HW_GET_INFO, hardware_info.tobytes()), hardware_info.tobytes()]
|
|
67
|
+
port.write.side_effect = lambda data: len(data)
|
|
68
|
+
serial_constructor = MagicMock(return_value=port)
|
|
69
|
+
port.serial_constructor = serial_constructor
|
|
70
|
+
monkeypatch.setattr("thorlabs_apt.device.serial.Serial", serial_constructor)
|
|
71
|
+
return port
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
@pytest.fixture
|
|
75
|
+
def device(serial_port: MagicMock) -> APTDevice:
|
|
76
|
+
return APTDevice("COM3")
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
class TestAPTDevice:
|
|
80
|
+
def test_initialization_configures_port_and_reads_hardware_info(
|
|
81
|
+
self, device: APTDevice, serial_port: MagicMock
|
|
82
|
+
) -> None:
|
|
83
|
+
assert device.serial_number == 12345678
|
|
84
|
+
serial_port.serial_constructor.assert_called_once_with(
|
|
85
|
+
port="COM3",
|
|
86
|
+
baudrate=115200,
|
|
87
|
+
bytesize=serial.EIGHTBITS,
|
|
88
|
+
parity=serial.PARITY_NONE,
|
|
89
|
+
stopbits=serial.STOPBITS_ONE,
|
|
90
|
+
rtscts=True,
|
|
91
|
+
timeout=1.0,
|
|
92
|
+
)
|
|
93
|
+
serial_port.reset_input_buffer.assert_called_once()
|
|
94
|
+
serial_port.reset_output_buffer.assert_called_once()
|
|
95
|
+
serial_port.write.assert_called_once_with(request_hardware_info_packet().tobytes())
|
|
96
|
+
serial_port.flush.assert_called_once_with()
|
|
97
|
+
device._hw_info.serial_number = 12345678
|
|
98
|
+
|
|
99
|
+
def test_request_writes_packet_and_returns_data_payload(self, device: APTDevice, serial_port: MagicMock) -> None:
|
|
100
|
+
response_data = b"response"
|
|
101
|
+
serial_port.read.side_effect = [data_packet(0x1234, response_data), response_data]
|
|
102
|
+
packet = APTPacket(message_id=0x4321, destination=0x50, source=0x01)
|
|
103
|
+
|
|
104
|
+
assert device.request(packet) == response_data
|
|
105
|
+
serial_port.write.assert_called_with(packet.tobytes())
|
|
106
|
+
|
|
107
|
+
def test_set_marks_packet_as_data_packet_and_appends_payload(
|
|
108
|
+
self, device: APTDevice, serial_port: MagicMock
|
|
109
|
+
) -> None:
|
|
110
|
+
packet = APTPacket(message_id=0x4321, destination=0x50, source=0x01)
|
|
111
|
+
|
|
112
|
+
device.set(packet, b"data")
|
|
113
|
+
|
|
114
|
+
assert serial_port.write.call_args.args == (b"!C\x04\x00\xd0\x01data",)
|
|
115
|
+
|
|
116
|
+
@pytest.mark.parametrize(
|
|
117
|
+
("method_name", "expected_packet"),
|
|
118
|
+
[
|
|
119
|
+
("identify", get_identify_packet(channel=2)),
|
|
120
|
+
("enable_channel", set_channel_enable_state_packet(channel=2, enabled=True)),
|
|
121
|
+
("disable_channel", set_channel_enable_state_packet(channel=2, enabled=False)),
|
|
122
|
+
],
|
|
123
|
+
)
|
|
124
|
+
def test_channel_commands_write_expected_packets(
|
|
125
|
+
self, device: APTDevice, serial_port: MagicMock, method_name: str, expected_packet: APTPacket
|
|
126
|
+
) -> None:
|
|
127
|
+
getattr(device, method_name)(2)
|
|
128
|
+
|
|
129
|
+
serial_port.write.assert_called_with(expected_packet.tobytes())
|
|
130
|
+
|
|
131
|
+
def test_get_channel_enable_state_reads_response_flag(self, device: APTDevice, serial_port: MagicMock) -> None:
|
|
132
|
+
serial_port.read.side_effect = [APTPacket(message_id=0x0212, param2=0x01).tobytes()]
|
|
133
|
+
|
|
134
|
+
assert device.get_channel_enable_state(2) is True
|